blob: 5d02cc06d1a76a165d2234886ba3941d3045c591 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -03002 * thinkpad_acpi.c - ThinkPad ACPI Extras
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 *
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005 * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
Henrique de Moraes Holschuh1c762ca2009-04-04 04:25:42 +00006 * Copyright (C) 2006-2009 Henrique de Moraes Holschuh <hmh@hmh.eng.br>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
Henrique de Moraes Holschuha62bc912007-03-23 17:33:58 -030020 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301, USA.
Borislav Deianov78f81cc2005-08-17 00:00:00 -040022 */
23
Henrique de Moraes Holschuh5d2eb142009-12-15 21:51:13 -020024#define TPACPI_VERSION "0.24"
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -020025#define TPACPI_SYSFS_VERSION 0x020700
Borislav Deianov78f81cc2005-08-17 00:00:00 -040026
27/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 * Changelog:
Henrique de Moraes Holschuh4b45cc02008-01-08 13:02:46 -020029 * 2007-10-20 changelog trimmed down
30 *
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -030031 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
32 * drivers/misc.
Henrique de Moraes Holschuhf9ff43a2006-11-25 16:37:38 -020033 *
34 * 2006-11-22 0.13 new maintainer
35 * changelog now lives in git commit history, and will
36 * not be updated further in-file.
Henrique de Moraes Holschuh837ca6d2007-03-23 17:33:54 -030037 *
Borislav Deianov78f81cc2005-08-17 00:00:00 -040038 * 2005-03-17 0.11 support for 600e, 770x
39 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
Henrique de Moraes Holschuh4b45cc02008-01-08 13:02:46 -020040 *
41 * 2005-01-16 0.9 use MODULE_VERSION
Borislav Deianov78f81cc2005-08-17 00:00:00 -040042 * thanks to Henrik Brix Andersen <brix@gentoo.org>
43 * fix parameter passing on module loading
44 * thanks to Rusty Russell <rusty@rustcorp.com.au>
45 * thanks to Jim Radford <radford@blackbean.org>
46 * 2004-11-08 0.8 fix init error case, don't return from a macro
47 * thanks to Chris Wright <chrisw@osdl.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 */
49
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020050#include <linux/kernel.h>
51#include <linux/module.h>
52#include <linux/init.h>
53#include <linux/types.h>
54#include <linux/string.h>
55#include <linux/list.h>
56#include <linux/mutex.h>
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +000057#include <linux/sched.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020058#include <linux/kthread.h>
59#include <linux/freezer.h>
60#include <linux/delay.h>
61
62#include <linux/nvram.h>
63#include <linux/proc_fs.h>
Alexey Dobriyan887965e2009-12-15 21:51:12 -020064#include <linux/seq_file.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020065#include <linux/sysfs.h>
66#include <linux/backlight.h>
67#include <linux/fb.h>
68#include <linux/platform_device.h>
69#include <linux/hwmon.h>
70#include <linux/hwmon-sysfs.h>
71#include <linux/input.h>
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -030072#include <linux/leds.h>
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -030073#include <linux/rfkill.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020074#include <asm/uaccess.h>
75
76#include <linux/dmi.h>
77#include <linux/jiffies.h>
78#include <linux/workqueue.h>
79
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -020080#include <sound/core.h>
81#include <sound/control.h>
82#include <sound/initval.h>
83
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020084#include <acpi/acpi_drivers.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020085
86#include <linux/pci_ids.h>
87
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020088
89/* ThinkPad CMOS commands */
90#define TP_CMOS_VOLUME_DOWN 0
91#define TP_CMOS_VOLUME_UP 1
92#define TP_CMOS_VOLUME_MUTE 2
93#define TP_CMOS_BRIGHTNESS_UP 4
94#define TP_CMOS_BRIGHTNESS_DOWN 5
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -030095#define TP_CMOS_THINKLIGHT_ON 12
96#define TP_CMOS_THINKLIGHT_OFF 13
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020097
98/* NVRAM Addresses */
99enum tp_nvram_addr {
100 TP_NVRAM_ADDR_HK2 = 0x57,
101 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
102 TP_NVRAM_ADDR_VIDEO = 0x59,
103 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
104 TP_NVRAM_ADDR_MIXER = 0x60,
105};
106
107/* NVRAM bit masks */
108enum {
109 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
110 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
111 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
112 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
113 TP_NVRAM_MASK_THINKLIGHT = 0x10,
114 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
115 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
116 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
117 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
118 TP_NVRAM_MASK_MUTE = 0x40,
119 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
120 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
121 TP_NVRAM_POS_LEVEL_VOLUME = 0,
122};
123
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200124/* ACPI HIDs */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200125#define TPACPI_ACPI_HKEY_HID "IBM0068"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200126
127/* Input IDs */
128#define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
129#define TPACPI_HKEY_INPUT_VERSION 0x4101
130
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -0200131/* ACPI \WGSV commands */
132enum {
133 TP_ACPI_WGSV_GET_STATE = 0x01, /* Get state information */
134 TP_ACPI_WGSV_PWR_ON_ON_RESUME = 0x02, /* Resume WWAN powered on */
135 TP_ACPI_WGSV_PWR_OFF_ON_RESUME = 0x03, /* Resume WWAN powered off */
136 TP_ACPI_WGSV_SAVE_STATE = 0x04, /* Save state for S4/S5 */
137};
138
139/* TP_ACPI_WGSV_GET_STATE bits */
140enum {
141 TP_ACPI_WGSV_STATE_WWANEXIST = 0x0001, /* WWAN hw available */
142 TP_ACPI_WGSV_STATE_WWANPWR = 0x0002, /* WWAN radio enabled */
143 TP_ACPI_WGSV_STATE_WWANPWRRES = 0x0004, /* WWAN state at resume */
144 TP_ACPI_WGSV_STATE_WWANBIOSOFF = 0x0008, /* WWAN disabled in BIOS */
145 TP_ACPI_WGSV_STATE_BLTHEXIST = 0x0001, /* BLTH hw available */
146 TP_ACPI_WGSV_STATE_BLTHPWR = 0x0002, /* BLTH radio enabled */
147 TP_ACPI_WGSV_STATE_BLTHPWRRES = 0x0004, /* BLTH state at resume */
148 TP_ACPI_WGSV_STATE_BLTHBIOSOFF = 0x0008, /* BLTH disabled in BIOS */
149 TP_ACPI_WGSV_STATE_UWBEXIST = 0x0010, /* UWB hw available */
150 TP_ACPI_WGSV_STATE_UWBPWR = 0x0020, /* UWB radio enabled */
151};
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200152
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -0300153/* HKEY events */
154enum tpacpi_hkey_event_t {
155 /* Hotkey-related */
156 TP_HKEY_EV_HOTKEY_BASE = 0x1001, /* first hotkey (FN+F1) */
157 TP_HKEY_EV_BRGHT_UP = 0x1010, /* Brightness up */
158 TP_HKEY_EV_BRGHT_DOWN = 0x1011, /* Brightness down */
159 TP_HKEY_EV_VOL_UP = 0x1015, /* Volume up or unmute */
160 TP_HKEY_EV_VOL_DOWN = 0x1016, /* Volume down or unmute */
161 TP_HKEY_EV_VOL_MUTE = 0x1017, /* Mixer output mute */
162
163 /* Reasons for waking up from S3/S4 */
164 TP_HKEY_EV_WKUP_S3_UNDOCK = 0x2304, /* undock requested, S3 */
165 TP_HKEY_EV_WKUP_S4_UNDOCK = 0x2404, /* undock requested, S4 */
166 TP_HKEY_EV_WKUP_S3_BAYEJ = 0x2305, /* bay ejection req, S3 */
167 TP_HKEY_EV_WKUP_S4_BAYEJ = 0x2405, /* bay ejection req, S4 */
168 TP_HKEY_EV_WKUP_S3_BATLOW = 0x2313, /* battery empty, S3 */
169 TP_HKEY_EV_WKUP_S4_BATLOW = 0x2413, /* battery empty, S4 */
170
171 /* Auto-sleep after eject request */
172 TP_HKEY_EV_BAYEJ_ACK = 0x3003, /* bay ejection complete */
173 TP_HKEY_EV_UNDOCK_ACK = 0x4003, /* undock complete */
174
175 /* Misc bay events */
176 TP_HKEY_EV_OPTDRV_EJ = 0x3006, /* opt. drive tray ejected */
177
178 /* User-interface events */
179 TP_HKEY_EV_LID_CLOSE = 0x5001, /* laptop lid closed */
180 TP_HKEY_EV_LID_OPEN = 0x5002, /* laptop lid opened */
181 TP_HKEY_EV_TABLET_TABLET = 0x5009, /* tablet swivel up */
182 TP_HKEY_EV_TABLET_NOTEBOOK = 0x500a, /* tablet swivel down */
183 TP_HKEY_EV_PEN_INSERTED = 0x500b, /* tablet pen inserted */
184 TP_HKEY_EV_PEN_REMOVED = 0x500c, /* tablet pen removed */
185 TP_HKEY_EV_BRGHT_CHANGED = 0x5010, /* backlight control event */
186
187 /* Thermal events */
188 TP_HKEY_EV_ALARM_BAT_HOT = 0x6011, /* battery too hot */
189 TP_HKEY_EV_ALARM_BAT_XHOT = 0x6012, /* battery critically hot */
190 TP_HKEY_EV_ALARM_SENSOR_HOT = 0x6021, /* sensor too hot */
191 TP_HKEY_EV_ALARM_SENSOR_XHOT = 0x6022, /* sensor critically hot */
192 TP_HKEY_EV_THM_TABLE_CHANGED = 0x6030, /* thermal table changed */
193
194 /* Misc */
195 TP_HKEY_EV_RFKILL_CHANGED = 0x7000, /* rfkill switch changed */
196};
197
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200198/****************************************************************************
199 * Main driver
200 */
201
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200202#define TPACPI_NAME "thinkpad"
203#define TPACPI_DESC "ThinkPad ACPI Extras"
204#define TPACPI_FILE TPACPI_NAME "_acpi"
205#define TPACPI_URL "http://ibm-acpi.sf.net/"
206#define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200207
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200208#define TPACPI_PROC_DIR "ibm"
209#define TPACPI_ACPI_EVENT_PREFIX "ibm"
210#define TPACPI_DRVR_NAME TPACPI_FILE
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300211#define TPACPI_DRVR_SHORTNAME "tpacpi"
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200212#define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200213
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300214#define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -0300215#define TPACPI_WORKQUEUE_NAME "ktpacpid"
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300216
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200217#define TPACPI_MAX_ACPI_ARGS 3
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200218
Henrique de Moraes Holschuh7ff8d622009-04-04 04:25:46 +0000219/* printk headers */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200220#define TPACPI_LOG TPACPI_FILE ": "
Henrique de Moraes Holschuh7ff8d622009-04-04 04:25:46 +0000221#define TPACPI_EMERG KERN_EMERG TPACPI_LOG
222#define TPACPI_ALERT KERN_ALERT TPACPI_LOG
223#define TPACPI_CRIT KERN_CRIT TPACPI_LOG
224#define TPACPI_ERR KERN_ERR TPACPI_LOG
225#define TPACPI_WARN KERN_WARNING TPACPI_LOG
226#define TPACPI_NOTICE KERN_NOTICE TPACPI_LOG
227#define TPACPI_INFO KERN_INFO TPACPI_LOG
228#define TPACPI_DEBUG KERN_DEBUG TPACPI_LOG
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200229
Henrique de Moraes Holschuh7ff8d622009-04-04 04:25:46 +0000230/* Debugging printk groups */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200231#define TPACPI_DBG_ALL 0xffff
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +0000232#define TPACPI_DBG_DISCLOSETASK 0x8000
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200233#define TPACPI_DBG_INIT 0x0001
234#define TPACPI_DBG_EXIT 0x0002
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +0000235#define TPACPI_DBG_RFKILL 0x0004
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +0000236#define TPACPI_DBG_HKEY 0x0008
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +0000237#define TPACPI_DBG_FAN 0x0010
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +0000238#define TPACPI_DBG_BRGHT 0x0020
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -0200239#define TPACPI_DBG_MIXER 0x0040
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200240
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200241#define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
242#define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
243#define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200244
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200245
246/****************************************************************************
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200247 * Driver-wide structs and misc. variables
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200248 */
249
250struct ibm_struct;
251
252struct tp_acpi_drv_struct {
253 const struct acpi_device_id *hid;
254 struct acpi_driver *driver;
255
256 void (*notify) (struct ibm_struct *, u32);
257 acpi_handle *handle;
258 u32 type;
259 struct acpi_device *device;
260};
261
262struct ibm_struct {
263 char *name;
264
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200265 int (*read) (struct seq_file *);
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200266 int (*write) (char *);
267 void (*exit) (void);
268 void (*resume) (void);
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200269 void (*suspend) (pm_message_t state);
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200270 void (*shutdown) (void);
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200271
272 struct list_head all_drivers;
273
274 struct tp_acpi_drv_struct *acpi;
275
276 struct {
277 u8 acpi_driver_registered:1;
278 u8 acpi_notify_installed:1;
279 u8 proc_created:1;
280 u8 init_called:1;
281 u8 experimental:1;
282 } flags;
283};
284
285struct ibm_init_struct {
286 char param[32];
287
288 int (*init) (struct ibm_init_struct *);
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -0300289 mode_t base_procfs_mode;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200290 struct ibm_struct *data;
291};
292
293static struct {
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200294 u32 bluetooth:1;
295 u32 hotkey:1;
296 u32 hotkey_mask:1;
297 u32 hotkey_wlsw:1;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -0200298 u32 hotkey_tablet:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200299 u32 light:1;
300 u32 light_status:1;
301 u32 bright_16levels:1;
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -0300302 u32 bright_acpimode:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200303 u32 wan:1;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -0200304 u32 uwb:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200305 u32 fan_ctrl_status_undef:1;
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -0300306 u32 second_fan:1;
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -0300307 u32 beep_needs_two_args:1;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -0200308 u32 mixer_no_level_control:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200309 u32 input_device_registered:1;
310 u32 platform_drv_registered:1;
311 u32 platform_drv_attrs_registered:1;
312 u32 sensors_pdrv_registered:1;
313 u32 sensors_pdrv_attrs_registered:1;
314 u32 sensors_pdev_attrs_registered:1;
315 u32 hotkey_poll_active:1;
316} tp_features;
317
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -0300318static struct {
319 u16 hotkey_mask_ff:1;
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -0200320 u16 volume_ctrl_forbidden:1;
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -0300321} tp_warned;
322
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200323struct thinkpad_id_data {
324 unsigned int vendor; /* ThinkPad vendor:
325 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
326
327 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
328 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
329
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -0300330 u16 bios_model; /* 1Y = 0x5931, 0 = unknown */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200331 u16 ec_model;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -0300332 u16 bios_release; /* 1ZETK1WW = 0x314b, 0 = unknown */
333 u16 ec_release;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200334
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -0300335 char *model_str; /* ThinkPad T43 */
336 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200337};
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200338static struct thinkpad_id_data thinkpad_id;
339
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300340static enum {
341 TPACPI_LIFE_INIT = 0,
342 TPACPI_LIFE_RUNNING,
343 TPACPI_LIFE_EXITING,
344} tpacpi_lifecycle;
345
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200346static int experimental;
347static u32 dbg_level;
348
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -0300349static struct workqueue_struct *tpacpi_wq;
350
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +0000351enum led_status_t {
352 TPACPI_LED_OFF = 0,
353 TPACPI_LED_ON,
354 TPACPI_LED_BLINK,
355};
356
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -0300357/* Special LED class that can defer work */
358struct tpacpi_led_classdev {
359 struct led_classdev led_classdev;
360 struct work_struct work;
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +0000361 enum led_status_t new_state;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -0300362 unsigned int led;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -0300363};
364
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200365#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
366static int dbg_wlswemul;
367static int tpacpi_wlsw_emulstate;
368static int dbg_bluetoothemul;
369static int tpacpi_bluetooth_emulstate;
370static int dbg_wwanemul;
371static int tpacpi_wwan_emulstate;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -0200372static int dbg_uwbemul;
373static int tpacpi_uwb_emulstate;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200374#endif
375
376
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000377/*************************************************************************
378 * Debugging helpers
379 */
380
381#define dbg_printk(a_dbg_level, format, arg...) \
382 do { if (dbg_level & (a_dbg_level)) \
383 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
384 } while (0)
385
386#ifdef CONFIG_THINKPAD_ACPI_DEBUG
387#define vdbg_printk dbg_printk
388static const char *str_supported(int is_supported);
389#else
390#define vdbg_printk(a_dbg_level, format, arg...) \
391 do { } while (0)
392#endif
393
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +0000394static void tpacpi_log_usertask(const char * const what)
395{
396 printk(TPACPI_DEBUG "%s: access by process with PID %d\n",
397 what, task_tgid_vnr(current));
398}
399
400#define tpacpi_disclose_usertask(what, format, arg...) \
401 do { \
402 if (unlikely( \
403 (dbg_level & TPACPI_DBG_DISCLOSETASK) && \
404 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
405 printk(TPACPI_DEBUG "%s: PID %d: " format, \
406 what, task_tgid_vnr(current), ## arg); \
407 } \
408 } while (0)
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000409
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -0300410/*
411 * Quirk handling helpers
412 *
413 * ThinkPad IDs and versions seen in the field so far
414 * are two-characters from the set [0-9A-Z], i.e. base 36.
415 *
416 * We use values well outside that range as specials.
417 */
418
419#define TPACPI_MATCH_ANY 0xffffU
420#define TPACPI_MATCH_UNKNOWN 0U
421
422/* TPID('1', 'Y') == 0x5931 */
423#define TPID(__c1, __c2) (((__c2) << 8) | (__c1))
424
425#define TPACPI_Q_IBM(__id1, __id2, __quirk) \
426 { .vendor = PCI_VENDOR_ID_IBM, \
427 .bios = TPID(__id1, __id2), \
428 .ec = TPACPI_MATCH_ANY, \
429 .quirks = (__quirk) }
430
431#define TPACPI_Q_LNV(__id1, __id2, __quirk) \
432 { .vendor = PCI_VENDOR_ID_LENOVO, \
433 .bios = TPID(__id1, __id2), \
434 .ec = TPACPI_MATCH_ANY, \
435 .quirks = (__quirk) }
436
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -0200437#define TPACPI_QEC_LNV(__id1, __id2, __quirk) \
438 { .vendor = PCI_VENDOR_ID_LENOVO, \
439 .bios = TPACPI_MATCH_ANY, \
440 .ec = TPID(__id1, __id2), \
441 .quirks = (__quirk) }
442
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -0300443struct tpacpi_quirk {
444 unsigned int vendor;
445 u16 bios;
446 u16 ec;
447 unsigned long quirks;
448};
449
450/**
451 * tpacpi_check_quirks() - search BIOS/EC version on a list
452 * @qlist: array of &struct tpacpi_quirk
453 * @qlist_size: number of elements in @qlist
454 *
455 * Iterates over a quirks list until one is found that matches the
456 * ThinkPad's vendor, BIOS and EC model.
457 *
458 * Returns 0 if nothing matches, otherwise returns the quirks field of
459 * the matching &struct tpacpi_quirk entry.
460 *
461 * The match criteria is: vendor, ec and bios much match.
462 */
463static unsigned long __init tpacpi_check_quirks(
464 const struct tpacpi_quirk *qlist,
465 unsigned int qlist_size)
466{
467 while (qlist_size) {
468 if ((qlist->vendor == thinkpad_id.vendor ||
469 qlist->vendor == TPACPI_MATCH_ANY) &&
470 (qlist->bios == thinkpad_id.bios_model ||
471 qlist->bios == TPACPI_MATCH_ANY) &&
472 (qlist->ec == thinkpad_id.ec_model ||
473 qlist->ec == TPACPI_MATCH_ANY))
474 return qlist->quirks;
475
476 qlist_size--;
477 qlist++;
478 }
479 return 0;
480}
481
482
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300483/****************************************************************************
484 ****************************************************************************
485 *
486 * ACPI Helpers and device model
487 *
488 ****************************************************************************
489 ****************************************************************************/
490
491/*************************************************************************
492 * ACPI basic handles
493 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -0300495static acpi_handle root_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200497#define TPACPI_HANDLE(object, parent, paths...) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 static acpi_handle object##_handle; \
499 static acpi_handle *object##_parent = &parent##_handle; \
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400500 static char *object##_path; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 static char *object##_paths[] = { paths }
502
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200503TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400504 "\\_SB.PCI.ISA.EC", /* 570 */
505 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
506 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
507 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
508 "\\_SB.PCI0.ICH3.EC0", /* R31 */
509 "\\_SB.PCI0.LPC.EC", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300510 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200512TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
513TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200515TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
516 /* T4x, X31, X40 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400517 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
518 "\\CMS", /* R40, R40e */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300519 ); /* all others */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400520
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200521TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400522 "^HKEY", /* R30, R31 */
523 "HKEY", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300524 ); /* 570 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400525
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -0200526TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
527 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
528 "\\_SB.PCI0.VID0", /* 770e */
529 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
530 "\\_SB.PCI0.AGP.VID", /* all others */
531 ); /* R30, R31 */
532
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400533
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300534/*************************************************************************
535 * ACPI helpers
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -0200536 */
537
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538static int acpi_evalf(acpi_handle handle,
539 void *res, char *method, char *fmt, ...)
540{
541 char *fmt0 = fmt;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400542 struct acpi_object_list params;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200543 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400544 struct acpi_buffer result, *resultp;
545 union acpi_object out_obj;
546 acpi_status status;
547 va_list ap;
548 char res_type;
549 int success;
550 int quiet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
552 if (!*fmt) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200553 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 return 0;
555 }
556
557 if (*fmt == 'q') {
558 quiet = 1;
559 fmt++;
560 } else
561 quiet = 0;
562
563 res_type = *(fmt++);
564
565 params.count = 0;
566 params.pointer = &in_objs[0];
567
568 va_start(ap, fmt);
569 while (*fmt) {
570 char c = *(fmt++);
571 switch (c) {
572 case 'd': /* int */
573 in_objs[params.count].integer.value = va_arg(ap, int);
574 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
575 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400576 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 default:
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200578 printk(TPACPI_ERR "acpi_evalf() called "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 "with invalid format character '%c'\n", c);
580 return 0;
581 }
582 }
583 va_end(ap);
584
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400585 if (res_type != 'v') {
586 result.length = sizeof(out_obj);
587 result.pointer = &out_obj;
588 resultp = &result;
589 } else
590 resultp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400592 status = acpi_evaluate_object(handle, method, &params, resultp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
594 switch (res_type) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400595 case 'd': /* int */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 if (res)
597 *(int *)res = out_obj.integer.value;
598 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
599 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400600 case 'v': /* void */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 success = status == AE_OK;
602 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400603 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 default:
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200605 printk(TPACPI_ERR "acpi_evalf() called "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 "with invalid format character '%c'\n", res_type);
607 return 0;
608 }
609
610 if (!success && !quiet)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200611 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 method, fmt0, status);
613
614 return success;
615}
616
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200617static int acpi_ec_read(int i, u8 *p)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300618{
619 int v;
620
621 if (ecrd_handle) {
622 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
623 return 0;
624 *p = v;
625 } else {
626 if (ec_read(i, p) < 0)
627 return 0;
628 }
629
630 return 1;
631}
632
633static int acpi_ec_write(int i, u8 v)
634{
635 if (ecwr_handle) {
636 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
637 return 0;
638 } else {
639 if (ec_write(i, v) < 0)
640 return 0;
641 }
642
643 return 1;
644}
645
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -0300646static int issue_thinkpad_cmos_command(int cmos_cmd)
647{
648 if (!cmos_handle)
649 return -ENXIO;
650
651 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
652 return -EIO;
653
654 return 0;
655}
656
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300657/*************************************************************************
658 * ACPI device model
659 */
660
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200661#define TPACPI_ACPIHANDLE_INIT(object) \
662 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200663 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
664
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300665static void drv_acpi_handle_init(char *name,
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300666 acpi_handle *handle, acpi_handle parent,
667 char **paths, int num_paths, char **path)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300668{
669 int i;
670 acpi_status status;
671
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300672 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
673 name);
674
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300675 for (i = 0; i < num_paths; i++) {
676 status = acpi_get_handle(parent, paths[i], handle);
677 if (ACPI_SUCCESS(status)) {
678 *path = paths[i];
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300679 dbg_printk(TPACPI_DBG_INIT,
680 "Found ACPI handle %s for %s\n",
681 *path, name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300682 return;
683 }
684 }
685
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300686 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
687 name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300688 *handle = NULL;
689}
690
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300691static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300692{
693 struct ibm_struct *ibm = data;
694
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300695 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
696 return;
697
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300698 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300699 return;
700
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300701 ibm->acpi->notify(ibm, event);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300702}
703
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300704static int __init setup_acpi_notify(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300705{
706 acpi_status status;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300707 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300708
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300709 BUG_ON(!ibm->acpi);
710
711 if (!*ibm->acpi->handle)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300712 return 0;
713
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300714 vdbg_printk(TPACPI_DBG_INIT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300715 "setting up ACPI notify for %s\n", ibm->name);
716
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300717 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
718 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200719 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300720 ibm->name, rc);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300721 return -ENODEV;
722 }
723
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700724 ibm->acpi->device->driver_data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300725 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200726 TPACPI_ACPI_EVENT_PREFIX,
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -0300727 ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300728
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300729 status = acpi_install_notify_handler(*ibm->acpi->handle,
730 ibm->acpi->type, dispatch_acpi_notify, ibm);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300731 if (ACPI_FAILURE(status)) {
732 if (status == AE_ALREADY_EXISTS) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200733 printk(TPACPI_NOTICE
734 "another device driver is already "
735 "handling %s events\n", ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300736 } else {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200737 printk(TPACPI_ERR
738 "acpi_install_notify_handler(%s) failed: %d\n",
739 ibm->name, status);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300740 }
741 return -ENODEV;
742 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300743 ibm->flags.acpi_notify_installed = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300744 return 0;
745}
746
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300747static int __init tpacpi_device_add(struct acpi_device *device)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300748{
749 return 0;
750}
751
Henrique de Moraes Holschuh67001212007-04-21 11:08:25 -0300752static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300753{
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300754 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300755
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300756 dbg_printk(TPACPI_DBG_INIT,
757 "registering %s as an ACPI driver\n", ibm->name);
758
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300759 BUG_ON(!ibm->acpi);
760
761 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
762 if (!ibm->acpi->driver) {
Mariusz Kozlowski4f778b92008-10-18 14:23:53 -0300763 printk(TPACPI_ERR
764 "failed to allocate memory for ibm->acpi->driver\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300765 return -ENOMEM;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300766 }
767
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200768 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300769 ibm->acpi->driver->ids = ibm->acpi->hid;
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200770
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300771 ibm->acpi->driver->ops.add = &tpacpi_device_add;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300772
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300773 rc = acpi_bus_register_driver(ibm->acpi->driver);
774 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200775 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200776 ibm->name, rc);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300777 kfree(ibm->acpi->driver);
778 ibm->acpi->driver = NULL;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300779 } else if (!rc)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300780 ibm->flags.acpi_driver_registered = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300781
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300782 return rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300783}
784
785
786/****************************************************************************
787 ****************************************************************************
788 *
789 * Procfs Helpers
790 *
791 ****************************************************************************
792 ****************************************************************************/
793
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200794static int dispatch_proc_show(struct seq_file *m, void *v)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300795{
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200796 struct ibm_struct *ibm = m->private;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300797
798 if (!ibm || !ibm->read)
799 return -EINVAL;
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200800 return ibm->read(m);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300801}
802
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200803static int dispatch_proc_open(struct inode *inode, struct file *file)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300804{
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200805 return single_open(file, dispatch_proc_show, PDE(inode)->data);
806}
807
808static ssize_t dispatch_proc_write(struct file *file,
809 const char __user *userbuf,
810 size_t count, loff_t *pos)
811{
812 struct ibm_struct *ibm = PDE(file->f_path.dentry->d_inode)->data;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300813 char *kernbuf;
814 int ret;
815
816 if (!ibm || !ibm->write)
817 return -EINVAL;
Michael Buesch5b05d462009-08-01 12:04:19 -0300818 if (count > PAGE_SIZE - 2)
819 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300820
821 kernbuf = kmalloc(count + 2, GFP_KERNEL);
822 if (!kernbuf)
823 return -ENOMEM;
824
825 if (copy_from_user(kernbuf, userbuf, count)) {
826 kfree(kernbuf);
827 return -EFAULT;
828 }
829
830 kernbuf[count] = 0;
831 strcat(kernbuf, ",");
832 ret = ibm->write(kernbuf);
833 if (ret == 0)
834 ret = count;
835
836 kfree(kernbuf);
837
838 return ret;
839}
840
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200841static const struct file_operations dispatch_proc_fops = {
842 .owner = THIS_MODULE,
843 .open = dispatch_proc_open,
844 .read = seq_read,
845 .llseek = seq_lseek,
846 .release = single_release,
847 .write = dispatch_proc_write,
848};
849
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850static char *next_cmd(char **cmds)
851{
852 char *start = *cmds;
853 char *end;
854
855 while ((end = strchr(start, ',')) && end == start)
856 start = end + 1;
857
858 if (!end)
859 return NULL;
860
861 *end = 0;
862 *cmds = end + 1;
863 return start;
864}
865
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300866
867/****************************************************************************
868 ****************************************************************************
869 *
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300870 * Device model: input, hwmon and platform
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300871 *
872 ****************************************************************************
873 ****************************************************************************/
874
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -0300875static struct platform_device *tpacpi_pdev;
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300876static struct platform_device *tpacpi_sensors_pdev;
Tony Jones1beeffe2007-08-20 13:46:20 -0700877static struct device *tpacpi_hwmon;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300878static struct input_dev *tpacpi_inputdev;
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -0300879static struct mutex tpacpi_inputdev_send_mutex;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200880static LIST_HEAD(tpacpi_all_drivers);
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300881
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200882static int tpacpi_suspend_handler(struct platform_device *pdev,
883 pm_message_t state)
884{
885 struct ibm_struct *ibm, *itmp;
886
887 list_for_each_entry_safe(ibm, itmp,
888 &tpacpi_all_drivers,
889 all_drivers) {
890 if (ibm->suspend)
891 (ibm->suspend)(state);
892 }
893
894 return 0;
895}
896
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300897static int tpacpi_resume_handler(struct platform_device *pdev)
898{
899 struct ibm_struct *ibm, *itmp;
900
901 list_for_each_entry_safe(ibm, itmp,
902 &tpacpi_all_drivers,
903 all_drivers) {
904 if (ibm->resume)
905 (ibm->resume)();
906 }
907
908 return 0;
909}
910
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200911static void tpacpi_shutdown_handler(struct platform_device *pdev)
912{
913 struct ibm_struct *ibm, *itmp;
914
915 list_for_each_entry_safe(ibm, itmp,
916 &tpacpi_all_drivers,
917 all_drivers) {
918 if (ibm->shutdown)
919 (ibm->shutdown)();
920 }
921}
922
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300923static struct platform_driver tpacpi_pdriver = {
924 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200925 .name = TPACPI_DRVR_NAME,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300926 .owner = THIS_MODULE,
927 },
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200928 .suspend = tpacpi_suspend_handler,
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300929 .resume = tpacpi_resume_handler,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200930 .shutdown = tpacpi_shutdown_handler,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300931};
932
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300933static struct platform_driver tpacpi_hwmon_pdriver = {
934 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200935 .name = TPACPI_HWMON_DRVR_NAME,
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300936 .owner = THIS_MODULE,
937 },
938};
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300939
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -0300940/*************************************************************************
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300941 * sysfs support helpers
942 */
943
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200944struct attribute_set {
945 unsigned int members, max_members;
946 struct attribute_group group;
947};
948
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300949struct attribute_set_obj {
950 struct attribute_set s;
951 struct attribute *a;
952} __attribute__((packed));
953
954static struct attribute_set *create_attr_set(unsigned int max_members,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200955 const char *name)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300956{
957 struct attribute_set_obj *sobj;
958
959 if (max_members == 0)
960 return NULL;
961
962 /* Allocates space for implicit NULL at the end too */
963 sobj = kzalloc(sizeof(struct attribute_set_obj) +
964 max_members * sizeof(struct attribute *),
965 GFP_KERNEL);
966 if (!sobj)
967 return NULL;
968 sobj->s.max_members = max_members;
969 sobj->s.group.attrs = &sobj->a;
970 sobj->s.group.name = name;
971
972 return &sobj->s;
973}
974
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200975#define destroy_attr_set(_set) \
976 kfree(_set);
977
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300978/* not multi-threaded safe, use it in a single thread per set */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200979static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300980{
981 if (!s || !attr)
982 return -EINVAL;
983
984 if (s->members >= s->max_members)
985 return -ENOMEM;
986
987 s->group.attrs[s->members] = attr;
988 s->members++;
989
990 return 0;
991}
992
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200993static int add_many_to_attr_set(struct attribute_set *s,
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300994 struct attribute **attr,
995 unsigned int count)
996{
997 int i, res;
998
999 for (i = 0; i < count; i++) {
1000 res = add_to_attr_set(s, attr[i]);
1001 if (res)
1002 return res;
1003 }
1004
1005 return 0;
1006}
1007
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001008static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001009{
1010 sysfs_remove_group(kobj, &s->group);
1011 destroy_attr_set(s);
1012}
1013
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001014#define register_attr_set_with_sysfs(_attr_set, _kobj) \
1015 sysfs_create_group(_kobj, &_attr_set->group)
1016
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001017static int parse_strtoul(const char *buf,
1018 unsigned long max, unsigned long *value)
1019{
1020 char *endp;
1021
André Goddard Rosae7d28602009-12-14 18:01:06 -08001022 *value = simple_strtoul(skip_spaces(buf), &endp, 0);
1023 endp = skip_spaces(endp);
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001024 if (*endp || *value > max)
1025 return -EINVAL;
1026
1027 return 0;
1028}
1029
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03001030static void tpacpi_disable_brightness_delay(void)
1031{
1032 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
1033 printk(TPACPI_NOTICE
1034 "ACPI backlight control delay disabled\n");
1035}
1036
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03001037static int __init tpacpi_query_bcl_levels(acpi_handle handle)
1038{
1039 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1040 union acpi_object *obj;
1041 int rc;
1042
1043 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
1044 obj = (union acpi_object *)buffer.pointer;
1045 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
1046 printk(TPACPI_ERR "Unknown _BCL data, "
1047 "please report this to %s\n", TPACPI_MAIL);
1048 rc = 0;
1049 } else {
1050 rc = obj->package.count;
1051 }
1052 } else {
1053 return 0;
1054 }
1055
1056 kfree(buffer.pointer);
1057 return rc;
1058}
1059
1060static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
1061 u32 lvl, void *context, void **rv)
1062{
1063 char name[ACPI_PATH_SEGMENT_LENGTH];
1064 struct acpi_buffer buffer = { sizeof(name), &name };
1065
1066 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
1067 !strncmp("_BCL", name, sizeof(name) - 1)) {
1068 BUG_ON(!rv || !*rv);
1069 **(int **)rv = tpacpi_query_bcl_levels(handle);
1070 return AE_CTRL_TERMINATE;
1071 } else {
1072 return AE_OK;
1073 }
1074}
1075
1076/*
1077 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
1078 */
1079static int __init tpacpi_check_std_acpi_brightness_support(void)
1080{
1081 int status;
1082 int bcl_levels = 0;
1083 void *bcl_ptr = &bcl_levels;
1084
1085 if (!vid_handle) {
1086 TPACPI_ACPIHANDLE_INIT(vid);
1087 }
1088 if (!vid_handle)
1089 return 0;
1090
1091 /*
1092 * Search for a _BCL method, and execute it. This is safe on all
1093 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
1094 * BIOS in ACPI backlight control mode. We do NOT have to care
1095 * about calling the _BCL method in an enabled video device, any
1096 * will do for our purposes.
1097 */
1098
1099 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
Lin Ming22635762009-11-13 10:06:08 +08001100 tpacpi_acpi_walk_find_bcl, NULL, NULL,
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03001101 &bcl_ptr);
1102
1103 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
1104 tp_features.bright_acpimode = 1;
1105 return (bcl_levels - 2);
1106 }
1107
1108 return 0;
1109}
1110
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00001111static void printk_deprecated_attribute(const char * const what,
1112 const char * const details)
1113{
1114 tpacpi_log_usertask("deprecated sysfs attribute");
1115 printk(TPACPI_WARN "WARNING: sysfs attribute %s is deprecated and "
1116 "will be removed. %s\n",
1117 what, details);
1118}
1119
Johannes Berg19d337d2009-06-02 13:01:37 +02001120/*************************************************************************
1121 * rfkill and radio control support helpers
1122 */
1123
1124/*
1125 * ThinkPad-ACPI firmware handling model:
1126 *
1127 * WLSW (master wireless switch) is event-driven, and is common to all
1128 * firmware-controlled radios. It cannot be controlled, just monitored,
1129 * as expected. It overrides all radio state in firmware
1130 *
1131 * The kernel, a masked-off hotkey, and WLSW can change the radio state
1132 * (TODO: verify how WLSW interacts with the returned radio state).
1133 *
1134 * The only time there are shadow radio state changes, is when
1135 * masked-off hotkeys are used.
1136 */
1137
1138/*
1139 * Internal driver API for radio state:
1140 *
1141 * int: < 0 = error, otherwise enum tpacpi_rfkill_state
1142 * bool: true means radio blocked (off)
1143 */
1144enum tpacpi_rfkill_state {
1145 TPACPI_RFK_RADIO_OFF = 0,
1146 TPACPI_RFK_RADIO_ON
1147};
1148
1149/* rfkill switches */
1150enum tpacpi_rfk_id {
1151 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
1152 TPACPI_RFK_WWAN_SW_ID,
1153 TPACPI_RFK_UWB_SW_ID,
1154 TPACPI_RFK_SW_MAX
1155};
1156
1157static const char *tpacpi_rfkill_names[] = {
1158 [TPACPI_RFK_BLUETOOTH_SW_ID] = "bluetooth",
1159 [TPACPI_RFK_WWAN_SW_ID] = "wwan",
1160 [TPACPI_RFK_UWB_SW_ID] = "uwb",
1161 [TPACPI_RFK_SW_MAX] = NULL
1162};
1163
1164/* ThinkPad-ACPI rfkill subdriver */
1165struct tpacpi_rfk {
1166 struct rfkill *rfkill;
1167 enum tpacpi_rfk_id id;
1168 const struct tpacpi_rfk_ops *ops;
1169};
1170
1171struct tpacpi_rfk_ops {
1172 /* firmware interface */
1173 int (*get_status)(void);
1174 int (*set_status)(const enum tpacpi_rfkill_state);
1175};
1176
1177static struct tpacpi_rfk *tpacpi_rfkill_switches[TPACPI_RFK_SW_MAX];
1178
1179/* Query FW and update rfkill sw state for a given rfkill switch */
1180static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk *tp_rfk)
1181{
1182 int status;
1183
1184 if (!tp_rfk)
1185 return -ENODEV;
1186
1187 status = (tp_rfk->ops->get_status)();
1188 if (status < 0)
1189 return status;
1190
1191 rfkill_set_sw_state(tp_rfk->rfkill,
1192 (status == TPACPI_RFK_RADIO_OFF));
1193
1194 return status;
1195}
1196
1197/* Query FW and update rfkill sw state for all rfkill switches */
1198static void tpacpi_rfk_update_swstate_all(void)
1199{
1200 unsigned int i;
1201
1202 for (i = 0; i < TPACPI_RFK_SW_MAX; i++)
1203 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[i]);
1204}
1205
1206/*
1207 * Sync the HW-blocking state of all rfkill switches,
1208 * do notice it causes the rfkill core to schedule uevents
1209 */
1210static void tpacpi_rfk_update_hwblock_state(bool blocked)
1211{
1212 unsigned int i;
1213 struct tpacpi_rfk *tp_rfk;
1214
1215 for (i = 0; i < TPACPI_RFK_SW_MAX; i++) {
1216 tp_rfk = tpacpi_rfkill_switches[i];
1217 if (tp_rfk) {
1218 if (rfkill_set_hw_state(tp_rfk->rfkill,
1219 blocked)) {
1220 /* ignore -- we track sw block */
1221 }
1222 }
1223 }
1224}
1225
1226/* Call to get the WLSW state from the firmware */
1227static int hotkey_get_wlsw(void);
1228
1229/* Call to query WLSW state and update all rfkill switches */
1230static bool tpacpi_rfk_check_hwblock_state(void)
1231{
1232 int res = hotkey_get_wlsw();
1233 int hw_blocked;
1234
1235 /* When unknown or unsupported, we have to assume it is unblocked */
1236 if (res < 0)
1237 return false;
1238
1239 hw_blocked = (res == TPACPI_RFK_RADIO_OFF);
1240 tpacpi_rfk_update_hwblock_state(hw_blocked);
1241
1242 return hw_blocked;
1243}
1244
1245static int tpacpi_rfk_hook_set_block(void *data, bool blocked)
1246{
1247 struct tpacpi_rfk *tp_rfk = data;
1248 int res;
1249
1250 dbg_printk(TPACPI_DBG_RFKILL,
1251 "request to change radio state to %s\n",
1252 blocked ? "blocked" : "unblocked");
1253
1254 /* try to set radio state */
1255 res = (tp_rfk->ops->set_status)(blocked ?
1256 TPACPI_RFK_RADIO_OFF : TPACPI_RFK_RADIO_ON);
1257
1258 /* and update the rfkill core with whatever the FW really did */
1259 tpacpi_rfk_update_swstate(tp_rfk);
1260
1261 return (res < 0) ? res : 0;
1262}
1263
1264static const struct rfkill_ops tpacpi_rfk_rfkill_ops = {
1265 .set_block = tpacpi_rfk_hook_set_block,
1266};
1267
1268static int __init tpacpi_new_rfkill(const enum tpacpi_rfk_id id,
1269 const struct tpacpi_rfk_ops *tp_rfkops,
1270 const enum rfkill_type rfktype,
1271 const char *name,
1272 const bool set_default)
1273{
1274 struct tpacpi_rfk *atp_rfk;
1275 int res;
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001276 bool sw_state = false;
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001277 bool hw_state;
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001278 int sw_status;
Johannes Berg19d337d2009-06-02 13:01:37 +02001279
1280 BUG_ON(id >= TPACPI_RFK_SW_MAX || tpacpi_rfkill_switches[id]);
1281
Johannes Berg19d337d2009-06-02 13:01:37 +02001282 atp_rfk = kzalloc(sizeof(struct tpacpi_rfk), GFP_KERNEL);
1283 if (atp_rfk)
1284 atp_rfk->rfkill = rfkill_alloc(name,
1285 &tpacpi_pdev->dev,
1286 rfktype,
1287 &tpacpi_rfk_rfkill_ops,
1288 atp_rfk);
1289 if (!atp_rfk || !atp_rfk->rfkill) {
1290 printk(TPACPI_ERR
1291 "failed to allocate memory for rfkill class\n");
1292 kfree(atp_rfk);
1293 return -ENOMEM;
1294 }
1295
1296 atp_rfk->id = id;
1297 atp_rfk->ops = tp_rfkops;
1298
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001299 sw_status = (tp_rfkops->get_status)();
1300 if (sw_status < 0) {
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001301 printk(TPACPI_ERR
1302 "failed to read initial state for %s, error %d\n",
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001303 name, sw_status);
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001304 } else {
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001305 sw_state = (sw_status == TPACPI_RFK_RADIO_OFF);
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001306 if (set_default) {
1307 /* try to keep the initial state, since we ask the
1308 * firmware to preserve it across S5 in NVRAM */
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001309 rfkill_init_sw_state(atp_rfk->rfkill, sw_state);
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001310 }
1311 }
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001312 hw_state = tpacpi_rfk_check_hwblock_state();
1313 rfkill_set_hw_state(atp_rfk->rfkill, hw_state);
Johannes Berg19d337d2009-06-02 13:01:37 +02001314
1315 res = rfkill_register(atp_rfk->rfkill);
1316 if (res < 0) {
1317 printk(TPACPI_ERR
1318 "failed to register %s rfkill switch: %d\n",
1319 name, res);
1320 rfkill_destroy(atp_rfk->rfkill);
1321 kfree(atp_rfk);
1322 return res;
1323 }
1324
1325 tpacpi_rfkill_switches[id] = atp_rfk;
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001326
1327 printk(TPACPI_INFO "rfkill switch %s: radio is %sblocked\n",
1328 name, (sw_state || hw_state) ? "" : "un");
Johannes Berg19d337d2009-06-02 13:01:37 +02001329 return 0;
1330}
1331
1332static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id)
1333{
1334 struct tpacpi_rfk *tp_rfk;
1335
1336 BUG_ON(id >= TPACPI_RFK_SW_MAX);
1337
1338 tp_rfk = tpacpi_rfkill_switches[id];
1339 if (tp_rfk) {
1340 rfkill_unregister(tp_rfk->rfkill);
Corentin Chary5f0dadb2009-09-14 12:43:52 +02001341 rfkill_destroy(tp_rfk->rfkill);
Johannes Berg19d337d2009-06-02 13:01:37 +02001342 tpacpi_rfkill_switches[id] = NULL;
1343 kfree(tp_rfk);
1344 }
1345}
1346
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00001347static void printk_deprecated_rfkill_attribute(const char * const what)
1348{
1349 printk_deprecated_attribute(what,
1350 "Please switch to generic rfkill before year 2010");
1351}
1352
Johannes Berg19d337d2009-06-02 13:01:37 +02001353/* sysfs <radio> enable ------------------------------------------------ */
1354static ssize_t tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id,
1355 struct device_attribute *attr,
1356 char *buf)
1357{
1358 int status;
1359
1360 printk_deprecated_rfkill_attribute(attr->attr.name);
1361
1362 /* This is in the ABI... */
1363 if (tpacpi_rfk_check_hwblock_state()) {
1364 status = TPACPI_RFK_RADIO_OFF;
1365 } else {
1366 status = tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1367 if (status < 0)
1368 return status;
1369 }
1370
1371 return snprintf(buf, PAGE_SIZE, "%d\n",
1372 (status == TPACPI_RFK_RADIO_ON) ? 1 : 0);
1373}
1374
1375static ssize_t tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id,
1376 struct device_attribute *attr,
1377 const char *buf, size_t count)
1378{
1379 unsigned long t;
1380 int res;
1381
1382 printk_deprecated_rfkill_attribute(attr->attr.name);
1383
1384 if (parse_strtoul(buf, 1, &t))
1385 return -EINVAL;
1386
1387 tpacpi_disclose_usertask(attr->attr.name, "set to %ld\n", t);
1388
1389 /* This is in the ABI... */
1390 if (tpacpi_rfk_check_hwblock_state() && !!t)
1391 return -EPERM;
1392
1393 res = tpacpi_rfkill_switches[id]->ops->set_status((!!t) ?
1394 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF);
1395 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1396
1397 return (res < 0) ? res : count;
1398}
1399
1400/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001401static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id, struct seq_file *m)
Johannes Berg19d337d2009-06-02 13:01:37 +02001402{
Johannes Berg19d337d2009-06-02 13:01:37 +02001403 if (id >= TPACPI_RFK_SW_MAX)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001404 seq_printf(m, "status:\t\tnot supported\n");
Johannes Berg19d337d2009-06-02 13:01:37 +02001405 else {
1406 int status;
1407
1408 /* This is in the ABI... */
1409 if (tpacpi_rfk_check_hwblock_state()) {
1410 status = TPACPI_RFK_RADIO_OFF;
1411 } else {
1412 status = tpacpi_rfk_update_swstate(
1413 tpacpi_rfkill_switches[id]);
1414 if (status < 0)
1415 return status;
1416 }
1417
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001418 seq_printf(m, "status:\t\t%s\n",
Johannes Berg19d337d2009-06-02 13:01:37 +02001419 (status == TPACPI_RFK_RADIO_ON) ?
1420 "enabled" : "disabled");
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001421 seq_printf(m, "commands:\tenable, disable\n");
Johannes Berg19d337d2009-06-02 13:01:37 +02001422 }
1423
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001424 return 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02001425}
1426
1427static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id, char *buf)
1428{
1429 char *cmd;
1430 int status = -1;
1431 int res = 0;
1432
1433 if (id >= TPACPI_RFK_SW_MAX)
1434 return -ENODEV;
1435
1436 while ((cmd = next_cmd(&buf))) {
1437 if (strlencmp(cmd, "enable") == 0)
1438 status = TPACPI_RFK_RADIO_ON;
1439 else if (strlencmp(cmd, "disable") == 0)
1440 status = TPACPI_RFK_RADIO_OFF;
1441 else
1442 return -EINVAL;
1443 }
1444
1445 if (status != -1) {
1446 tpacpi_disclose_usertask("procfs", "attempt to %s %s\n",
1447 (status == TPACPI_RFK_RADIO_ON) ?
1448 "enable" : "disable",
1449 tpacpi_rfkill_names[id]);
1450 res = (tpacpi_rfkill_switches[id]->ops->set_status)(status);
1451 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1452 }
1453
1454 return res;
1455}
1456
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001457/*************************************************************************
1458 * thinkpad-acpi driver attributes
1459 */
1460
1461/* interface_version --------------------------------------------------- */
1462static ssize_t tpacpi_driver_interface_version_show(
1463 struct device_driver *drv,
1464 char *buf)
1465{
1466 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1467}
1468
1469static DRIVER_ATTR(interface_version, S_IRUGO,
1470 tpacpi_driver_interface_version_show, NULL);
1471
1472/* debug_level --------------------------------------------------------- */
1473static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1474 char *buf)
1475{
1476 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1477}
1478
1479static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1480 const char *buf, size_t count)
1481{
1482 unsigned long t;
1483
1484 if (parse_strtoul(buf, 0xffff, &t))
1485 return -EINVAL;
1486
1487 dbg_level = t;
1488
1489 return count;
1490}
1491
1492static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1493 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1494
1495/* version ------------------------------------------------------------- */
1496static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1497 char *buf)
1498{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001499 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1500 TPACPI_DESC, TPACPI_VERSION);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001501}
1502
1503static DRIVER_ATTR(version, S_IRUGO,
1504 tpacpi_driver_version_show, NULL);
1505
1506/* --------------------------------------------------------------------- */
1507
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001508#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1509
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001510/* wlsw_emulstate ------------------------------------------------------ */
1511static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1512 char *buf)
1513{
1514 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1515}
1516
1517static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1518 const char *buf, size_t count)
1519{
1520 unsigned long t;
1521
1522 if (parse_strtoul(buf, 1, &t))
1523 return -EINVAL;
1524
Johannes Berg19d337d2009-06-02 13:01:37 +02001525 if (tpacpi_wlsw_emulstate != !!t) {
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001526 tpacpi_wlsw_emulstate = !!t;
Johannes Berg19d337d2009-06-02 13:01:37 +02001527 tpacpi_rfk_update_hwblock_state(!t); /* negative logic */
1528 }
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001529
1530 return count;
1531}
1532
1533static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1534 tpacpi_driver_wlsw_emulstate_show,
1535 tpacpi_driver_wlsw_emulstate_store);
1536
1537/* bluetooth_emulstate ------------------------------------------------- */
1538static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1539 struct device_driver *drv,
1540 char *buf)
1541{
1542 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1543}
1544
1545static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1546 struct device_driver *drv,
1547 const char *buf, size_t count)
1548{
1549 unsigned long t;
1550
1551 if (parse_strtoul(buf, 1, &t))
1552 return -EINVAL;
1553
1554 tpacpi_bluetooth_emulstate = !!t;
1555
1556 return count;
1557}
1558
1559static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1560 tpacpi_driver_bluetooth_emulstate_show,
1561 tpacpi_driver_bluetooth_emulstate_store);
1562
1563/* wwan_emulstate ------------------------------------------------- */
1564static ssize_t tpacpi_driver_wwan_emulstate_show(
1565 struct device_driver *drv,
1566 char *buf)
1567{
1568 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1569}
1570
1571static ssize_t tpacpi_driver_wwan_emulstate_store(
1572 struct device_driver *drv,
1573 const char *buf, size_t count)
1574{
1575 unsigned long t;
1576
1577 if (parse_strtoul(buf, 1, &t))
1578 return -EINVAL;
1579
1580 tpacpi_wwan_emulstate = !!t;
1581
1582 return count;
1583}
1584
1585static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1586 tpacpi_driver_wwan_emulstate_show,
1587 tpacpi_driver_wwan_emulstate_store);
1588
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001589/* uwb_emulstate ------------------------------------------------- */
1590static ssize_t tpacpi_driver_uwb_emulstate_show(
1591 struct device_driver *drv,
1592 char *buf)
1593{
1594 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1595}
1596
1597static ssize_t tpacpi_driver_uwb_emulstate_store(
1598 struct device_driver *drv,
1599 const char *buf, size_t count)
1600{
1601 unsigned long t;
1602
1603 if (parse_strtoul(buf, 1, &t))
1604 return -EINVAL;
1605
1606 tpacpi_uwb_emulstate = !!t;
1607
1608 return count;
1609}
1610
1611static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
1612 tpacpi_driver_uwb_emulstate_show,
1613 tpacpi_driver_uwb_emulstate_store);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001614#endif
1615
1616/* --------------------------------------------------------------------- */
1617
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001618static struct driver_attribute *tpacpi_driver_attributes[] = {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001619 &driver_attr_debug_level, &driver_attr_version,
1620 &driver_attr_interface_version,
1621};
1622
1623static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1624{
1625 int i, res;
1626
1627 i = 0;
1628 res = 0;
1629 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1630 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1631 i++;
1632 }
1633
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001634#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1635 if (!res && dbg_wlswemul)
1636 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1637 if (!res && dbg_bluetoothemul)
1638 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1639 if (!res && dbg_wwanemul)
1640 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001641 if (!res && dbg_uwbemul)
1642 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001643#endif
1644
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001645 return res;
1646}
1647
1648static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1649{
1650 int i;
1651
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001652 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001653 driver_remove_file(drv, tpacpi_driver_attributes[i]);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001654
1655#ifdef THINKPAD_ACPI_DEBUGFACILITIES
1656 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1657 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1658 driver_remove_file(drv, &driver_attr_wwan_emulstate);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001659 driver_remove_file(drv, &driver_attr_uwb_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001660#endif
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001661}
1662
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001663/*************************************************************************
1664 * Firmware Data
1665 */
1666
1667/*
1668 * Table of recommended minimum BIOS versions
1669 *
1670 * Reasons for listing:
1671 * 1. Stable BIOS, listed because the unknown ammount of
1672 * bugs and bad ACPI behaviour on older versions
1673 *
1674 * 2. BIOS or EC fw with known bugs that trigger on Linux
1675 *
1676 * 3. BIOS with known reduced functionality in older versions
1677 *
1678 * We recommend the latest BIOS and EC version.
1679 * We only support the latest BIOS and EC fw version as a rule.
1680 *
1681 * Sources: IBM ThinkPad Public Web Documents (update changelogs),
1682 * Information from users in ThinkWiki
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03001683 *
1684 * WARNING: we use this table also to detect that the machine is
1685 * a ThinkPad in some cases, so don't remove entries lightly.
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001686 */
1687
1688#define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1689 { .vendor = (__v), \
1690 .bios = TPID(__id1, __id2), \
1691 .ec = TPACPI_MATCH_ANY, \
1692 .quirks = TPACPI_MATCH_ANY << 16 \
1693 | (__bv1) << 8 | (__bv2) }
1694
1695#define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001696 __eid, __ev1, __ev2) \
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001697 { .vendor = (__v), \
1698 .bios = TPID(__bid1, __bid2), \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001699 .ec = __eid, \
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001700 .quirks = (__ev1) << 24 | (__ev2) << 16 \
1701 | (__bv1) << 8 | (__bv2) }
1702
1703#define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1704 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1705
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001706/* Outdated IBM BIOSes often lack the EC id string */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001707#define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1708 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001709 __bv1, __bv2, TPID(__id1, __id2), \
1710 __ev1, __ev2), \
1711 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1712 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1713 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001714
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001715/* Outdated IBM BIOSes often lack the EC id string */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001716#define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1717 __eid1, __eid2, __ev1, __ev2) \
1718 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001719 __bv1, __bv2, TPID(__eid1, __eid2), \
1720 __ev1, __ev2), \
1721 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1722 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1723 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001724
1725#define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1726 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1727
1728#define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1729 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001730 __bv1, __bv2, TPID(__id1, __id2), \
1731 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001732
1733#define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1734 __eid1, __eid2, __ev1, __ev2) \
1735 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001736 __bv1, __bv2, TPID(__eid1, __eid2), \
1737 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001738
1739static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
1740 /* Numeric models ------------------ */
1741 /* FW MODEL BIOS VERS */
1742 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1743 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1744 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1745 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1746 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1747 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1748 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1749 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1750 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1751
1752 /* A-series ------------------------- */
1753 /* FW MODEL BIOS VERS EC VERS */
1754 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1755 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1756 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1757 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1758 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1759 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1760 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1761 TPV_QI0('1', '3', '2', '0'), /* A22m */
1762 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1763 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1764 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1765
1766 /* G-series ------------------------- */
1767 /* FW MODEL BIOS VERS */
1768 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1769 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1770
1771 /* R-series, T-series --------------- */
1772 /* FW MODEL BIOS VERS EC VERS */
1773 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1774 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1775 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1776 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1777 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1778 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1779 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1780 T40/p, T41/p, T42/p (1) */
1781 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1782 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1783 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1784 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1785
1786 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1787 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1788 TPV_QI0('1', '6', '3', '2'), /* T22 */
1789 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1790 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1791 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1792
1793 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1794 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
Henrique de Moraes Holschuh90765c62009-12-09 01:36:23 +00001795 TPV_QL1('7', 'E', 'D', '0', '1', '5'), /* R60e, R60i */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001796
1797 /* BIOS FW BIOS VERS EC FW EC VERS */
1798 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1799 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1800
1801 /* X-series ------------------------- */
1802 /* FW MODEL BIOS VERS EC VERS */
1803 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1804 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1805 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1806 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1807 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1808 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1809 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1810
Henrique de Moraes Holschuh90765c62009-12-09 01:36:23 +00001811 TPV_QL1('7', 'B', 'D', '7', '4', '0'), /* X60/s */
1812 TPV_QL1('7', 'J', '3', '0', '1', '3'), /* X60t */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001813
1814 /* (0) - older versions lack DMI EC fw string and functionality */
1815 /* (1) - older versions known to lack functionality */
1816};
1817
1818#undef TPV_QL1
1819#undef TPV_QL0
1820#undef TPV_QI2
1821#undef TPV_QI1
1822#undef TPV_QI0
1823#undef TPV_Q_X
1824#undef TPV_Q
1825
1826static void __init tpacpi_check_outdated_fw(void)
1827{
1828 unsigned long fwvers;
1829 u16 ec_version, bios_version;
1830
1831 fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
1832 ARRAY_SIZE(tpacpi_bios_version_qtable));
1833
1834 if (!fwvers)
1835 return;
1836
1837 bios_version = fwvers & 0xffffU;
1838 ec_version = (fwvers >> 16) & 0xffffU;
1839
1840 /* note that unknown versions are set to 0x0000 and we use that */
1841 if ((bios_version > thinkpad_id.bios_release) ||
1842 (ec_version > thinkpad_id.ec_release &&
1843 ec_version != TPACPI_MATCH_ANY)) {
1844 /*
1845 * The changelogs would let us track down the exact
1846 * reason, but it is just too much of a pain to track
1847 * it. We only list BIOSes that are either really
1848 * broken, or really stable to begin with, so it is
1849 * best if the user upgrades the firmware anyway.
1850 */
1851 printk(TPACPI_WARN
1852 "WARNING: Outdated ThinkPad BIOS/EC firmware\n");
1853 printk(TPACPI_WARN
1854 "WARNING: This firmware may be missing critical bug "
1855 "fixes and/or important features\n");
1856 }
1857}
1858
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03001859static bool __init tpacpi_is_fw_known(void)
1860{
1861 return tpacpi_check_quirks(tpacpi_bios_version_qtable,
1862 ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
1863}
1864
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03001865/****************************************************************************
1866 ****************************************************************************
1867 *
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001868 * Subdrivers
1869 *
1870 ****************************************************************************
1871 ****************************************************************************/
1872
1873/*************************************************************************
Henrique de Moraes Holschuh142cfc92007-04-21 11:08:26 -03001874 * thinkpad-acpi init subdriver
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001875 */
1876
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001877static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001879 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
1880 printk(TPACPI_INFO "%s\n", TPACPI_URL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001882 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03001883 (thinkpad_id.bios_version_str) ?
1884 thinkpad_id.bios_version_str : "unknown",
1885 (thinkpad_id.ec_version_str) ?
1886 thinkpad_id.ec_version_str : "unknown");
1887
1888 if (thinkpad_id.vendor && thinkpad_id.model_str)
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03001889 printk(TPACPI_INFO "%s %s, model %s\n",
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03001890 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
1891 "IBM" : ((thinkpad_id.vendor ==
1892 PCI_VENDOR_ID_LENOVO) ?
1893 "Lenovo" : "Unknown vendor"),
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03001894 thinkpad_id.model_str,
1895 (thinkpad_id.nummodel_str) ?
1896 thinkpad_id.nummodel_str : "unknown");
Henrique de Moraes Holschuh3945ac32007-02-06 19:13:44 -02001897
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001898 tpacpi_check_outdated_fw();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 return 0;
1900}
1901
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001902static int thinkpad_acpi_driver_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903{
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001904 seq_printf(m, "driver:\t\t%s\n", TPACPI_DESC);
1905 seq_printf(m, "version:\t%s\n", TPACPI_VERSION);
1906 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907}
1908
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001909static struct ibm_struct thinkpad_acpi_driver_data = {
1910 .name = "driver",
1911 .read = thinkpad_acpi_driver_read,
1912};
1913
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001914/*************************************************************************
1915 * Hotkey subdriver
1916 */
1917
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001918/*
1919 * ThinkPad firmware event model
1920 *
1921 * The ThinkPad firmware has two main event interfaces: normal ACPI
1922 * notifications (which follow the ACPI standard), and a private event
1923 * interface.
1924 *
1925 * The private event interface also issues events for the hotkeys. As
1926 * the driver gained features, the event handling code ended up being
1927 * built around the hotkey subdriver. This will need to be refactored
1928 * to a more formal event API eventually.
1929 *
1930 * Some "hotkeys" are actually supposed to be used as event reports,
1931 * such as "brightness has changed", "volume has changed", depending on
1932 * the ThinkPad model and how the firmware is operating.
1933 *
1934 * Unlike other classes, hotkey-class events have mask/unmask control on
1935 * non-ancient firmware. However, how it behaves changes a lot with the
1936 * firmware model and version.
1937 */
1938
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001939enum { /* hot key scan codes (derived from ACPI DSDT) */
1940 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1941 TP_ACPI_HOTKEYSCAN_FNF2,
1942 TP_ACPI_HOTKEYSCAN_FNF3,
1943 TP_ACPI_HOTKEYSCAN_FNF4,
1944 TP_ACPI_HOTKEYSCAN_FNF5,
1945 TP_ACPI_HOTKEYSCAN_FNF6,
1946 TP_ACPI_HOTKEYSCAN_FNF7,
1947 TP_ACPI_HOTKEYSCAN_FNF8,
1948 TP_ACPI_HOTKEYSCAN_FNF9,
1949 TP_ACPI_HOTKEYSCAN_FNF10,
1950 TP_ACPI_HOTKEYSCAN_FNF11,
1951 TP_ACPI_HOTKEYSCAN_FNF12,
1952 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1953 TP_ACPI_HOTKEYSCAN_FNINSERT,
1954 TP_ACPI_HOTKEYSCAN_FNDELETE,
1955 TP_ACPI_HOTKEYSCAN_FNHOME,
1956 TP_ACPI_HOTKEYSCAN_FNEND,
1957 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1958 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1959 TP_ACPI_HOTKEYSCAN_FNSPACE,
1960 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1961 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1962 TP_ACPI_HOTKEYSCAN_MUTE,
1963 TP_ACPI_HOTKEYSCAN_THINKPAD,
1964};
1965
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001966enum { /* Keys/events available through NVRAM polling */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001967 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1968 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1969};
1970
1971enum { /* Positions of some of the keys in hotkey masks */
1972 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1973 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1974 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1975 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1976 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1977 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1978 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1979 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1980 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1981 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1982 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1983};
1984
1985enum { /* NVRAM to ACPI HKEY group map */
1986 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1987 TP_ACPI_HKEY_ZOOM_MASK |
1988 TP_ACPI_HKEY_DISPSWTCH_MASK |
1989 TP_ACPI_HKEY_HIBERNATE_MASK,
1990 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1991 TP_ACPI_HKEY_BRGHTDWN_MASK,
1992 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1993 TP_ACPI_HKEY_VOLDWN_MASK |
1994 TP_ACPI_HKEY_MUTE_MASK,
1995};
1996
1997#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1998struct tp_nvram_state {
1999 u16 thinkpad_toggle:1;
2000 u16 zoom_toggle:1;
2001 u16 display_toggle:1;
2002 u16 thinklight_toggle:1;
2003 u16 hibernate_toggle:1;
2004 u16 displayexp_toggle:1;
2005 u16 display_state:1;
2006 u16 brightness_toggle:1;
2007 u16 volume_toggle:1;
2008 u16 mute:1;
2009
2010 u8 brightness_level;
2011 u8 volume_level;
2012};
2013
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002014/* kthread for the hotkey poller */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002015static struct task_struct *tpacpi_hotkey_task;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002016
2017/* Acquired while the poller kthread is running, use to sync start/stop */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002018static struct mutex hotkey_thread_mutex;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002019
2020/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002021 * Acquire mutex to write poller control variables as an
2022 * atomic block.
2023 *
2024 * Increment hotkey_config_change when changing them if you
2025 * want the kthread to forget old state.
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002026 *
2027 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
2028 */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002029static struct mutex hotkey_thread_data_mutex;
2030static unsigned int hotkey_config_change;
2031
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002032/*
2033 * hotkey poller control variables
2034 *
2035 * Must be atomic or readers will also need to acquire mutex
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002036 *
2037 * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
2038 * should be used only when the changes need to be taken as
2039 * a block, OR when one needs to force the kthread to forget
2040 * old state.
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002041 */
2042static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
2043static unsigned int hotkey_poll_freq = 10; /* Hz */
2044
2045#define HOTKEY_CONFIG_CRITICAL_START \
2046 do { \
2047 mutex_lock(&hotkey_thread_data_mutex); \
2048 hotkey_config_change++; \
2049 } while (0);
2050#define HOTKEY_CONFIG_CRITICAL_END \
2051 mutex_unlock(&hotkey_thread_data_mutex);
2052
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002053#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2054
2055#define hotkey_source_mask 0U
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002056#define HOTKEY_CONFIG_CRITICAL_START
2057#define HOTKEY_CONFIG_CRITICAL_END
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002058
2059#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2060
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002061static struct mutex hotkey_mutex;
2062
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002063static enum { /* Reasons for waking up */
2064 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
2065 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
2066 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
2067} hotkey_wakeup_reason;
2068
2069static int hotkey_autosleep_ack;
2070
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002071static u32 hotkey_orig_mask; /* events the BIOS had enabled */
2072static u32 hotkey_all_mask; /* all events supported in fw */
2073static u32 hotkey_reserved_mask; /* events better left disabled */
2074static u32 hotkey_driver_mask; /* events needed by the driver */
2075static u32 hotkey_user_mask; /* events visible to userspace */
2076static u32 hotkey_acpi_mask; /* events enabled in firmware */
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002077
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02002078static unsigned int hotkey_report_mode;
2079
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002080static u16 *hotkey_keycode_map;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002081
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -03002082static struct attribute_set *hotkey_dev_attributes;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002083
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002084static void tpacpi_driver_event(const unsigned int hkey_event);
2085static void hotkey_driver_event(const unsigned int scancode);
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002086static void hotkey_poll_setup(const bool may_warn);
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002087
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002088/* HKEY.MHKG() return bits */
2089#define TP_HOTKEY_TABLET_MASK (1 << 3)
2090
Johannes Berg19d337d2009-06-02 13:01:37 +02002091static int hotkey_get_wlsw(void)
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002092{
Johannes Berg19d337d2009-06-02 13:01:37 +02002093 int status;
2094
2095 if (!tp_features.hotkey_wlsw)
2096 return -ENODEV;
2097
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02002098#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
Johannes Berg19d337d2009-06-02 13:01:37 +02002099 if (dbg_wlswemul)
2100 return (tpacpi_wlsw_emulstate) ?
2101 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02002102#endif
Johannes Berg19d337d2009-06-02 13:01:37 +02002103
2104 if (!acpi_evalf(hkey_handle, &status, "WLSW", "d"))
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002105 return -EIO;
Johannes Berg19d337d2009-06-02 13:01:37 +02002106
2107 return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002108}
2109
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002110static int hotkey_get_tablet_mode(int *status)
2111{
2112 int s;
2113
2114 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
2115 return -EIO;
2116
Henrique de Moraes Holschuhcee47f52008-03-04 14:29:21 -08002117 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
2118 return 0;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002119}
2120
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002121/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002122 * Reads current event mask from firmware, and updates
2123 * hotkey_acpi_mask accordingly. Also resets any bits
2124 * from hotkey_user_mask that are unavailable to be
2125 * delivered (shadow requirement of the userspace ABI).
2126 *
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002127 * Call with hotkey_mutex held
2128 */
2129static int hotkey_mask_get(void)
2130{
2131 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002132 u32 m = 0;
2133
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002134 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002135 return -EIO;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002136
2137 hotkey_acpi_mask = m;
2138 } else {
2139 /* no mask support doesn't mean no event support... */
2140 hotkey_acpi_mask = hotkey_all_mask;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002141 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002142
2143 /* sync userspace-visible mask */
2144 hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002145
2146 return 0;
2147}
2148
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002149void static hotkey_mask_warn_incomplete_mask(void)
2150{
2151 /* log only what the user can fix... */
2152 const u32 wantedmask = hotkey_driver_mask &
2153 ~(hotkey_acpi_mask | hotkey_source_mask) &
2154 (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
2155
2156 if (wantedmask)
2157 printk(TPACPI_NOTICE
2158 "required events 0x%08x not enabled!\n",
2159 wantedmask);
2160}
2161
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002162/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002163 * Set the firmware mask when supported
2164 *
2165 * Also calls hotkey_mask_get to update hotkey_acpi_mask.
2166 *
2167 * NOTE: does not set bits in hotkey_user_mask, but may reset them.
2168 *
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002169 * Call with hotkey_mutex held
2170 */
2171static int hotkey_mask_set(u32 mask)
2172{
2173 int i;
2174 int rc = 0;
2175
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002176 const u32 fwmask = mask & ~hotkey_source_mask;
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -03002177
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002178 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002179 for (i = 0; i < 32; i++) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002180 if (!acpi_evalf(hkey_handle,
2181 NULL, "MHKM", "vdd", i + 1,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002182 !!(mask & (1 << i)))) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002183 rc = -EIO;
2184 break;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002185 }
2186 }
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002187 }
2188
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002189 /*
2190 * We *must* make an inconditional call to hotkey_mask_get to
2191 * refresh hotkey_acpi_mask and update hotkey_user_mask
2192 *
2193 * Take the opportunity to also log when we cannot _enable_
2194 * a given event.
2195 */
2196 if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
2197 printk(TPACPI_NOTICE
2198 "asked for hotkey mask 0x%08x, but "
2199 "firmware forced it to 0x%08x\n",
2200 fwmask, hotkey_acpi_mask);
2201 }
2202
Henrique de Moraes Holschuh6b30eb72009-12-09 01:36:25 +00002203 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING)
2204 hotkey_mask_warn_incomplete_mask();
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002205
2206 return rc;
2207}
2208
2209/*
2210 * Sets hotkey_user_mask and tries to set the firmware mask
2211 *
2212 * Call with hotkey_mutex held
2213 */
2214static int hotkey_user_mask_set(const u32 mask)
2215{
2216 int rc;
2217
2218 /* Give people a chance to notice they are doing something that
2219 * is bound to go boom on their users sooner or later */
2220 if (!tp_warned.hotkey_mask_ff &&
2221 (mask == 0xffff || mask == 0xffffff ||
2222 mask == 0xffffffff)) {
2223 tp_warned.hotkey_mask_ff = 1;
2224 printk(TPACPI_NOTICE
2225 "setting the hotkey mask to 0x%08x is likely "
2226 "not the best way to go about it\n", mask);
2227 printk(TPACPI_NOTICE
2228 "please consider using the driver defaults, "
2229 "and refer to up-to-date thinkpad-acpi "
2230 "documentation\n");
2231 }
2232
2233 /* Try to enable what the user asked for, plus whatever we need.
2234 * this syncs everything but won't enable bits in hotkey_user_mask */
2235 rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
2236
2237 /* Enable the available bits in hotkey_user_mask */
2238 hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
2239
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002240 return rc;
2241}
2242
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002243/*
2244 * Sets the driver hotkey mask.
2245 *
2246 * Can be called even if the hotkey subdriver is inactive
2247 */
2248static int tpacpi_hotkey_driver_mask_set(const u32 mask)
2249{
2250 int rc;
2251
2252 /* Do the right thing if hotkey_init has not been called yet */
2253 if (!tp_features.hotkey) {
2254 hotkey_driver_mask = mask;
2255 return 0;
2256 }
2257
2258 mutex_lock(&hotkey_mutex);
2259
2260 HOTKEY_CONFIG_CRITICAL_START
2261 hotkey_driver_mask = mask;
Henrique de Moraes Holschuhb684a362009-09-26 21:42:49 -03002262#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002263 hotkey_source_mask |= (mask & ~hotkey_all_mask);
Henrique de Moraes Holschuhb684a362009-09-26 21:42:49 -03002264#endif
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002265 HOTKEY_CONFIG_CRITICAL_END
2266
2267 rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) &
2268 ~hotkey_source_mask);
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002269 hotkey_poll_setup(true);
2270
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002271 mutex_unlock(&hotkey_mutex);
2272
2273 return rc;
2274}
2275
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002276static int hotkey_status_get(int *status)
2277{
2278 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
2279 return -EIO;
2280
2281 return 0;
2282}
2283
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002284static int hotkey_status_set(bool enable)
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002285{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002286 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002287 return -EIO;
2288
2289 return 0;
2290}
2291
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002292static void tpacpi_input_send_tabletsw(void)
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002293{
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002294 int state;
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002295
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002296 if (tp_features.hotkey_tablet &&
2297 !hotkey_get_tablet_mode(&state)) {
2298 mutex_lock(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002299
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002300 input_report_switch(tpacpi_inputdev,
2301 SW_TABLET_MODE, !!state);
2302 input_sync(tpacpi_inputdev);
2303
2304 mutex_unlock(&tpacpi_inputdev_send_mutex);
2305 }
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002306}
2307
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002308/* Do NOT call without validating scancode first */
2309static void tpacpi_input_send_key(const unsigned int scancode)
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002310{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002311 const unsigned int keycode = hotkey_keycode_map[scancode];
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002312
2313 if (keycode != KEY_RESERVED) {
2314 mutex_lock(&tpacpi_inputdev_send_mutex);
2315
2316 input_report_key(tpacpi_inputdev, keycode, 1);
2317 if (keycode == KEY_UNKNOWN)
2318 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
2319 scancode);
2320 input_sync(tpacpi_inputdev);
2321
2322 input_report_key(tpacpi_inputdev, keycode, 0);
2323 if (keycode == KEY_UNKNOWN)
2324 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
2325 scancode);
2326 input_sync(tpacpi_inputdev);
2327
2328 mutex_unlock(&tpacpi_inputdev_send_mutex);
2329 }
2330}
2331
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002332/* Do NOT call without validating scancode first */
2333static void tpacpi_input_send_key_masked(const unsigned int scancode)
2334{
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002335 hotkey_driver_event(scancode);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002336 if (hotkey_user_mask & (1 << scancode))
2337 tpacpi_input_send_key(scancode);
2338}
2339
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002340#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2341static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
2342
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002343/* Do NOT call without validating scancode first */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002344static void tpacpi_hotkey_send_key(unsigned int scancode)
2345{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002346 tpacpi_input_send_key_masked(scancode);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002347 if (hotkey_report_mode < 2) {
2348 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03002349 0x80, TP_HKEY_EV_HOTKEY_BASE + scancode);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002350 }
2351}
2352
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002353static void hotkey_read_nvram(struct tp_nvram_state *n, const u32 m)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002354{
2355 u8 d;
2356
2357 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
2358 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
2359 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
2360 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
2361 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
2362 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
2363 }
2364 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
2365 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
2366 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
2367 }
2368 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
2369 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
2370 n->displayexp_toggle =
2371 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
2372 }
2373 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
2374 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
2375 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
2376 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
2377 n->brightness_toggle =
2378 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
2379 }
2380 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
2381 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
2382 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
2383 >> TP_NVRAM_POS_LEVEL_VOLUME;
2384 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2385 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2386 }
2387}
2388
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002389static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
2390 struct tp_nvram_state *newn,
2391 const u32 event_mask)
2392{
2393
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002394#define TPACPI_COMPARE_KEY(__scancode, __member) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002395 do { \
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002396 if ((event_mask & (1 << __scancode)) && \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002397 oldn->__member != newn->__member) \
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002398 tpacpi_hotkey_send_key(__scancode); \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002399 } while (0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002400
2401#define TPACPI_MAY_SEND_KEY(__scancode) \
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002402 do { \
2403 if (event_mask & (1 << __scancode)) \
2404 tpacpi_hotkey_send_key(__scancode); \
2405 } while (0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002406
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002407 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2408 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2409 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2410 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2411
2412 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2413
2414 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2415
2416 /* handle volume */
2417 if (oldn->volume_toggle != newn->volume_toggle) {
2418 if (oldn->mute != newn->mute) {
2419 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2420 }
2421 if (oldn->volume_level > newn->volume_level) {
2422 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2423 } else if (oldn->volume_level < newn->volume_level) {
2424 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2425 } else if (oldn->mute == newn->mute) {
2426 /* repeated key presses that didn't change state */
2427 if (newn->mute) {
2428 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2429 } else if (newn->volume_level != 0) {
2430 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2431 } else {
2432 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2433 }
2434 }
2435 }
2436
2437 /* handle brightness */
2438 if (oldn->brightness_toggle != newn->brightness_toggle) {
2439 if (oldn->brightness_level < newn->brightness_level) {
2440 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2441 } else if (oldn->brightness_level > newn->brightness_level) {
2442 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2443 } else {
2444 /* repeated key presses that didn't change state */
2445 if (newn->brightness_level != 0) {
2446 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2447 } else {
2448 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2449 }
2450 }
2451 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002452
2453#undef TPACPI_COMPARE_KEY
2454#undef TPACPI_MAY_SEND_KEY
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002455}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002456
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002457/*
2458 * Polling driver
2459 *
2460 * We track all events in hotkey_source_mask all the time, since
2461 * most of them are edge-based. We only issue those requested by
2462 * hotkey_user_mask or hotkey_driver_mask, though.
2463 */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002464static int hotkey_kthread(void *data)
2465{
2466 struct tp_nvram_state s[2];
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002467 u32 poll_mask, event_mask;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002468 unsigned int si, so;
2469 unsigned long t;
2470 unsigned int change_detector, must_reset;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002471 unsigned int poll_freq;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002472
2473 mutex_lock(&hotkey_thread_mutex);
2474
2475 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2476 goto exit;
2477
2478 set_freezable();
2479
2480 so = 0;
2481 si = 1;
2482 t = 0;
2483
2484 /* Initial state for compares */
2485 mutex_lock(&hotkey_thread_data_mutex);
2486 change_detector = hotkey_config_change;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002487 poll_mask = hotkey_source_mask;
2488 event_mask = hotkey_source_mask &
2489 (hotkey_driver_mask | hotkey_user_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002490 poll_freq = hotkey_poll_freq;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002491 mutex_unlock(&hotkey_thread_data_mutex);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002492 hotkey_read_nvram(&s[so], poll_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002493
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002494 while (!kthread_should_stop()) {
2495 if (t == 0) {
2496 if (likely(poll_freq))
2497 t = 1000/poll_freq;
2498 else
2499 t = 100; /* should never happen... */
2500 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002501 t = msleep_interruptible(t);
2502 if (unlikely(kthread_should_stop()))
2503 break;
2504 must_reset = try_to_freeze();
2505 if (t > 0 && !must_reset)
2506 continue;
2507
2508 mutex_lock(&hotkey_thread_data_mutex);
2509 if (must_reset || hotkey_config_change != change_detector) {
2510 /* forget old state on thaw or config change */
2511 si = so;
2512 t = 0;
2513 change_detector = hotkey_config_change;
2514 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002515 poll_mask = hotkey_source_mask;
2516 event_mask = hotkey_source_mask &
2517 (hotkey_driver_mask | hotkey_user_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002518 poll_freq = hotkey_poll_freq;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002519 mutex_unlock(&hotkey_thread_data_mutex);
2520
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002521 if (likely(poll_mask)) {
2522 hotkey_read_nvram(&s[si], poll_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002523 if (likely(si != so)) {
2524 hotkey_compare_and_issue_event(&s[so], &s[si],
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002525 event_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002526 }
2527 }
2528
2529 so = si;
2530 si ^= 1;
2531 }
2532
2533exit:
2534 mutex_unlock(&hotkey_thread_mutex);
2535 return 0;
2536}
2537
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002538/* call with hotkey_mutex held */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002539static void hotkey_poll_stop_sync(void)
2540{
2541 if (tpacpi_hotkey_task) {
2542 if (frozen(tpacpi_hotkey_task) ||
2543 freezing(tpacpi_hotkey_task))
2544 thaw_process(tpacpi_hotkey_task);
2545
2546 kthread_stop(tpacpi_hotkey_task);
2547 tpacpi_hotkey_task = NULL;
2548 mutex_lock(&hotkey_thread_mutex);
2549 /* at this point, the thread did exit */
2550 mutex_unlock(&hotkey_thread_mutex);
2551 }
2552}
2553
2554/* call with hotkey_mutex held */
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002555static void hotkey_poll_setup(const bool may_warn)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002556{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002557 const u32 poll_driver_mask = hotkey_driver_mask & hotkey_source_mask;
2558 const u32 poll_user_mask = hotkey_user_mask & hotkey_source_mask;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002559
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002560 if (hotkey_poll_freq > 0 &&
2561 (poll_driver_mask ||
2562 (poll_user_mask && tpacpi_inputdev->users > 0))) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002563 if (!tpacpi_hotkey_task) {
2564 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03002565 NULL, TPACPI_NVRAM_KTHREAD_NAME);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002566 if (IS_ERR(tpacpi_hotkey_task)) {
2567 tpacpi_hotkey_task = NULL;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002568 printk(TPACPI_ERR
2569 "could not create kernel thread "
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002570 "for hotkey polling\n");
2571 }
2572 }
2573 } else {
2574 hotkey_poll_stop_sync();
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002575 if (may_warn && (poll_driver_mask || poll_user_mask) &&
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002576 hotkey_poll_freq == 0) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002577 printk(TPACPI_NOTICE
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002578 "hot keys 0x%08x and/or events 0x%08x "
2579 "require polling, which is currently "
2580 "disabled\n",
2581 poll_user_mask, poll_driver_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002582 }
2583 }
2584}
2585
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002586static void hotkey_poll_setup_safe(const bool may_warn)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002587{
2588 mutex_lock(&hotkey_mutex);
2589 hotkey_poll_setup(may_warn);
2590 mutex_unlock(&hotkey_mutex);
2591}
2592
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002593/* call with hotkey_mutex held */
2594static void hotkey_poll_set_freq(unsigned int freq)
2595{
2596 if (!freq)
2597 hotkey_poll_stop_sync();
2598
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002599 hotkey_poll_freq = freq;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002600}
2601
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02002602#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2603
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002604static void hotkey_poll_setup(const bool __unused)
2605{
2606}
2607
2608static void hotkey_poll_setup_safe(const bool __unused)
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02002609{
2610}
2611
2612#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2613
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002614static int hotkey_inputdev_open(struct input_dev *dev)
2615{
2616 switch (tpacpi_lifecycle) {
2617 case TPACPI_LIFE_INIT:
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002618 case TPACPI_LIFE_RUNNING:
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002619 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002620 return 0;
Henrique de Moraes Holschuhb589ea42010-02-25 21:28:58 -03002621 case TPACPI_LIFE_EXITING:
2622 return -EBUSY;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002623 }
2624
2625 /* Should only happen if tpacpi_lifecycle is corrupt */
2626 BUG();
2627 return -EBUSY;
2628}
2629
2630static void hotkey_inputdev_close(struct input_dev *dev)
2631{
2632 /* disable hotkey polling when possible */
Henrique de Moraes Holschuhb589ea42010-02-25 21:28:58 -03002633 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING &&
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002634 !(hotkey_source_mask & hotkey_driver_mask))
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002635 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002636}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002637
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002638/* sysfs hotkey enable ------------------------------------------------- */
2639static ssize_t hotkey_enable_show(struct device *dev,
2640 struct device_attribute *attr,
2641 char *buf)
2642{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002643 int res, status;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002644
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002645 printk_deprecated_attribute("hotkey_enable",
2646 "Hotkey reporting is always enabled");
2647
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002648 res = hotkey_status_get(&status);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002649 if (res)
2650 return res;
2651
2652 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2653}
2654
2655static ssize_t hotkey_enable_store(struct device *dev,
2656 struct device_attribute *attr,
2657 const char *buf, size_t count)
2658{
2659 unsigned long t;
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002660
2661 printk_deprecated_attribute("hotkey_enable",
2662 "Hotkeys can be disabled through hotkey_mask");
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002663
2664 if (parse_strtoul(buf, 1, &t))
2665 return -EINVAL;
2666
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002667 if (t == 0)
2668 return -EPERM;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002669
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002670 return count;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002671}
2672
2673static struct device_attribute dev_attr_hotkey_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002674 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002675 hotkey_enable_show, hotkey_enable_store);
2676
2677/* sysfs hotkey mask --------------------------------------------------- */
2678static ssize_t hotkey_mask_show(struct device *dev,
2679 struct device_attribute *attr,
2680 char *buf)
2681{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002682 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_user_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002683}
2684
2685static ssize_t hotkey_mask_store(struct device *dev,
2686 struct device_attribute *attr,
2687 const char *buf, size_t count)
2688{
2689 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002690 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002691
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002692 if (parse_strtoul(buf, 0xffffffffUL, &t))
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002693 return -EINVAL;
2694
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002695 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002696 return -ERESTARTSYS;
2697
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002698 res = hotkey_user_mask_set(t);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002699
2700#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002701 hotkey_poll_setup(true);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002702#endif
2703
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002704 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002705
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002706 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2707
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002708 return (res) ? res : count;
2709}
2710
2711static struct device_attribute dev_attr_hotkey_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002712 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002713 hotkey_mask_show, hotkey_mask_store);
2714
2715/* sysfs hotkey bios_enabled ------------------------------------------- */
2716static ssize_t hotkey_bios_enabled_show(struct device *dev,
2717 struct device_attribute *attr,
2718 char *buf)
2719{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002720 return sprintf(buf, "0\n");
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002721}
2722
2723static struct device_attribute dev_attr_hotkey_bios_enabled =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002724 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002725
2726/* sysfs hotkey bios_mask ---------------------------------------------- */
2727static ssize_t hotkey_bios_mask_show(struct device *dev,
2728 struct device_attribute *attr,
2729 char *buf)
2730{
Henrique de Moraes Holschuh06777be2009-09-12 15:22:15 -03002731 printk_deprecated_attribute("hotkey_bios_mask",
2732 "This attribute is useless.");
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002733 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002734}
2735
2736static struct device_attribute dev_attr_hotkey_bios_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002737 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002738
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002739/* sysfs hotkey all_mask ----------------------------------------------- */
2740static ssize_t hotkey_all_mask_show(struct device *dev,
2741 struct device_attribute *attr,
2742 char *buf)
2743{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002744 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2745 hotkey_all_mask | hotkey_source_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002746}
2747
2748static struct device_attribute dev_attr_hotkey_all_mask =
2749 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
2750
2751/* sysfs hotkey recommended_mask --------------------------------------- */
2752static ssize_t hotkey_recommended_mask_show(struct device *dev,
2753 struct device_attribute *attr,
2754 char *buf)
2755{
2756 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002757 (hotkey_all_mask | hotkey_source_mask)
2758 & ~hotkey_reserved_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002759}
2760
2761static struct device_attribute dev_attr_hotkey_recommended_mask =
2762 __ATTR(hotkey_recommended_mask, S_IRUGO,
2763 hotkey_recommended_mask_show, NULL);
2764
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002765#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2766
2767/* sysfs hotkey hotkey_source_mask ------------------------------------- */
2768static ssize_t hotkey_source_mask_show(struct device *dev,
2769 struct device_attribute *attr,
2770 char *buf)
2771{
2772 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2773}
2774
2775static ssize_t hotkey_source_mask_store(struct device *dev,
2776 struct device_attribute *attr,
2777 const char *buf, size_t count)
2778{
2779 unsigned long t;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002780 u32 r_ev;
2781 int rc;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002782
2783 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2784 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2785 return -EINVAL;
2786
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002787 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002788 return -ERESTARTSYS;
2789
2790 HOTKEY_CONFIG_CRITICAL_START
2791 hotkey_source_mask = t;
2792 HOTKEY_CONFIG_CRITICAL_END
2793
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002794 rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
2795 ~hotkey_source_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002796 hotkey_poll_setup(true);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002797
2798 /* check if events needed by the driver got disabled */
2799 r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
2800 & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002801
2802 mutex_unlock(&hotkey_mutex);
2803
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002804 if (rc < 0)
2805 printk(TPACPI_ERR "hotkey_source_mask: failed to update the"
2806 "firmware event mask!\n");
2807
2808 if (r_ev)
2809 printk(TPACPI_NOTICE "hotkey_source_mask: "
2810 "some important events were disabled: "
2811 "0x%04x\n", r_ev);
2812
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002813 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2814
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002815 return (rc < 0) ? rc : count;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002816}
2817
2818static struct device_attribute dev_attr_hotkey_source_mask =
2819 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2820 hotkey_source_mask_show, hotkey_source_mask_store);
2821
2822/* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2823static ssize_t hotkey_poll_freq_show(struct device *dev,
2824 struct device_attribute *attr,
2825 char *buf)
2826{
2827 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2828}
2829
2830static ssize_t hotkey_poll_freq_store(struct device *dev,
2831 struct device_attribute *attr,
2832 const char *buf, size_t count)
2833{
2834 unsigned long t;
2835
2836 if (parse_strtoul(buf, 25, &t))
2837 return -EINVAL;
2838
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002839 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002840 return -ERESTARTSYS;
2841
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002842 hotkey_poll_set_freq(t);
2843 hotkey_poll_setup(true);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002844
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002845 mutex_unlock(&hotkey_mutex);
2846
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002847 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2848
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002849 return count;
2850}
2851
2852static struct device_attribute dev_attr_hotkey_poll_freq =
2853 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2854 hotkey_poll_freq_show, hotkey_poll_freq_store);
2855
2856#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2857
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002858/* sysfs hotkey radio_sw (pollable) ------------------------------------ */
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002859static ssize_t hotkey_radio_sw_show(struct device *dev,
2860 struct device_attribute *attr,
2861 char *buf)
2862{
Johannes Berg19d337d2009-06-02 13:01:37 +02002863 int res;
2864 res = hotkey_get_wlsw();
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002865 if (res < 0)
2866 return res;
2867
Johannes Berg19d337d2009-06-02 13:01:37 +02002868 /* Opportunistic update */
2869 tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
2870
2871 return snprintf(buf, PAGE_SIZE, "%d\n",
2872 (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002873}
2874
2875static struct device_attribute dev_attr_hotkey_radio_sw =
2876 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2877
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002878static void hotkey_radio_sw_notify_change(void)
2879{
2880 if (tp_features.hotkey_wlsw)
2881 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2882 "hotkey_radio_sw");
2883}
2884
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002885/* sysfs hotkey tablet mode (pollable) --------------------------------- */
2886static ssize_t hotkey_tablet_mode_show(struct device *dev,
2887 struct device_attribute *attr,
2888 char *buf)
2889{
2890 int res, s;
2891 res = hotkey_get_tablet_mode(&s);
2892 if (res < 0)
2893 return res;
2894
2895 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2896}
2897
2898static struct device_attribute dev_attr_hotkey_tablet_mode =
2899 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2900
2901static void hotkey_tablet_mode_notify_change(void)
2902{
2903 if (tp_features.hotkey_tablet)
2904 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2905 "hotkey_tablet_mode");
2906}
2907
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002908/* sysfs hotkey report_mode -------------------------------------------- */
2909static ssize_t hotkey_report_mode_show(struct device *dev,
2910 struct device_attribute *attr,
2911 char *buf)
2912{
2913 return snprintf(buf, PAGE_SIZE, "%d\n",
2914 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2915}
2916
2917static struct device_attribute dev_attr_hotkey_report_mode =
2918 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2919
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002920/* sysfs wakeup reason (pollable) -------------------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002921static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2922 struct device_attribute *attr,
2923 char *buf)
2924{
2925 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2926}
2927
2928static struct device_attribute dev_attr_hotkey_wakeup_reason =
2929 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2930
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02002931static void hotkey_wakeup_reason_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002932{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002933 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2934 "wakeup_reason");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002935}
2936
2937/* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002938static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2939 struct device_attribute *attr,
2940 char *buf)
2941{
2942 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2943}
2944
2945static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2946 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2947 hotkey_wakeup_hotunplug_complete_show, NULL);
2948
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02002949static void hotkey_wakeup_hotunplug_complete_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002950{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002951 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2952 "wakeup_hotunplug_complete");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002953}
2954
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002955/* --------------------------------------------------------------------- */
2956
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002957static struct attribute *hotkey_attributes[] __initdata = {
2958 &dev_attr_hotkey_enable.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002959 &dev_attr_hotkey_bios_enabled.attr,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002960 &dev_attr_hotkey_bios_mask.attr,
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002961 &dev_attr_hotkey_report_mode.attr,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002962 &dev_attr_hotkey_wakeup_reason.attr,
2963 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002964 &dev_attr_hotkey_mask.attr,
2965 &dev_attr_hotkey_all_mask.attr,
2966 &dev_attr_hotkey_recommended_mask.attr,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002967#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002968 &dev_attr_hotkey_source_mask.attr,
2969 &dev_attr_hotkey_poll_freq.attr,
2970#endif
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002971};
2972
Johannes Berg19d337d2009-06-02 13:01:37 +02002973/*
2974 * Sync both the hw and sw blocking state of all switches
2975 */
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002976static void tpacpi_send_radiosw_update(void)
2977{
2978 int wlsw;
2979
Johannes Berg19d337d2009-06-02 13:01:37 +02002980 /*
2981 * We must sync all rfkill controllers *before* issuing any
2982 * rfkill input events, or we will race the rfkill core input
2983 * handler.
2984 *
2985 * tpacpi_inputdev_send_mutex works as a syncronization point
2986 * for the above.
2987 *
2988 * We optimize to avoid numerous calls to hotkey_get_wlsw.
2989 */
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002990
Johannes Berg19d337d2009-06-02 13:01:37 +02002991 wlsw = hotkey_get_wlsw();
2992
2993 /* Sync hw blocking state first if it is hw-blocked */
2994 if (wlsw == TPACPI_RFK_RADIO_OFF)
2995 tpacpi_rfk_update_hwblock_state(true);
2996
2997 /* Sync sw blocking state */
2998 tpacpi_rfk_update_swstate_all();
2999
3000 /* Sync hw blocking state last if it is hw-unblocked */
3001 if (wlsw == TPACPI_RFK_RADIO_ON)
3002 tpacpi_rfk_update_hwblock_state(false);
3003
3004 /* Issue rfkill input event for WLSW switch */
3005 if (!(wlsw < 0)) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003006 mutex_lock(&tpacpi_inputdev_send_mutex);
3007
3008 input_report_switch(tpacpi_inputdev,
Johannes Berg19d337d2009-06-02 13:01:37 +02003009 SW_RFKILL_ALL, (wlsw > 0));
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003010 input_sync(tpacpi_inputdev);
3011
3012 mutex_unlock(&tpacpi_inputdev_send_mutex);
3013 }
Johannes Berg19d337d2009-06-02 13:01:37 +02003014
3015 /*
3016 * this can be unconditional, as we will poll state again
3017 * if userspace uses the notify to read data
3018 */
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003019 hotkey_radio_sw_notify_change();
3020}
3021
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003022static void hotkey_exit(void)
3023{
3024#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003025 mutex_lock(&hotkey_mutex);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003026 hotkey_poll_stop_sync();
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003027 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003028#endif
3029
3030 if (hotkey_dev_attributes)
3031 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3032
3033 kfree(hotkey_keycode_map);
3034
Henrique de Moraes Holschuh4be73002009-09-20 14:09:23 -03003035 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003036 "restoring original HKEY status and mask\n");
3037 /* yes, there is a bitwise or below, we want the
3038 * functions to be called even if one of them fail */
3039 if (((tp_features.hotkey_mask &&
3040 hotkey_mask_set(hotkey_orig_mask)) |
3041 hotkey_status_set(false)) != 0)
Henrique de Moraes Holschuh4be73002009-09-20 14:09:23 -03003042 printk(TPACPI_ERR
3043 "failed to restore hot key mask "
3044 "to BIOS defaults\n");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003045}
3046
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003047static void __init hotkey_unmap(const unsigned int scancode)
3048{
3049 if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
3050 clear_bit(hotkey_keycode_map[scancode],
3051 tpacpi_inputdev->keybit);
3052 hotkey_keycode_map[scancode] = KEY_RESERVED;
3053 }
3054}
3055
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003056/*
3057 * HKEY quirks:
3058 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
3059 */
3060
3061#define TPACPI_HK_Q_INIMASK 0x0001
3062
3063static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
3064 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
3065 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
3066 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
3067 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
3068 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
3069 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
3070 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
3071 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
3072 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
3073 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
3074 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
3075 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
3076 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
3077 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
3078 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
3079 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
3080 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
3081 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
3082 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
3083};
3084
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003085static int __init hotkey_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003086{
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003087 /* Requirements for changing the default keymaps:
3088 *
3089 * 1. Many of the keys are mapped to KEY_RESERVED for very
3090 * good reasons. Do not change them unless you have deep
3091 * knowledge on the IBM and Lenovo ThinkPad firmware for
3092 * the various ThinkPad models. The driver behaves
3093 * differently for KEY_RESERVED: such keys have their
3094 * hot key mask *unset* in mask_recommended, and also
3095 * in the initial hot key mask programmed into the
3096 * firmware at driver load time, which means the firm-
3097 * ware may react very differently if you change them to
3098 * something else;
3099 *
3100 * 2. You must be subscribed to the linux-thinkpad and
3101 * ibm-acpi-devel mailing lists, and you should read the
3102 * list archives since 2007 if you want to change the
3103 * keymaps. This requirement exists so that you will
3104 * know the past history of problems with the thinkpad-
3105 * acpi driver keymaps, and also that you will be
3106 * listening to any bug reports;
3107 *
3108 * 3. Do not send thinkpad-acpi specific patches directly to
3109 * for merging, *ever*. Send them to the linux-acpi
3110 * mailinglist for comments. Merging is to be done only
3111 * through acpi-test and the ACPI maintainer.
3112 *
3113 * If the above is too much to ask, don't change the keymap.
3114 * Ask the thinkpad-acpi maintainer to do it, instead.
3115 */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003116 static u16 ibm_keycode_map[] __initdata = {
3117 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3118 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
3119 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3120 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003121
3122 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003123 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3124 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3125 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003126
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003127 /* brightness: firmware always reacts to them */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003128 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003129 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003130
3131 /* Thinklight: firmware always react to it */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003132 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003133
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003134 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3135 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003136
3137 /* Volume: firmware always react to it and reprograms
3138 * the built-in *extra* mixer. Never map it to control
3139 * another mixer by default. */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003140 KEY_RESERVED, /* 0x14: VOLUME UP */
3141 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3142 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003143
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003144 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003145
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003146 /* (assignments unknown, please report if found) */
3147 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3148 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3149 };
3150 static u16 lenovo_keycode_map[] __initdata = {
3151 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3152 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
3153 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3154 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003155
3156 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003157 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3158 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3159 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003160
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003161 /* These should be enabled --only-- when ACPI video
3162 * is disabled (i.e. in "vendor" mode), and are handled
3163 * in a special way by the init code */
3164 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
3165 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003166
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003167 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003168
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003169 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3170 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003171
3172 /* Volume: z60/z61, T60 (BIOS version?): firmware always
3173 * react to it and reprograms the built-in *extra* mixer.
3174 * Never map it to control another mixer by default.
3175 *
3176 * T60?, T61, R60?, R61: firmware and EC tries to send
3177 * these over the regular keyboard, so these are no-ops,
3178 * but there are still weird bugs re. MUTE, so do not
3179 * change unless you get test reports from all Lenovo
3180 * models. May cause the BIOS to interfere with the
3181 * HDA mixer.
3182 */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003183 KEY_RESERVED, /* 0x14: VOLUME UP */
3184 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3185 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003186
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003187 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003188
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003189 /* (assignments unknown, please report if found) */
3190 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3191 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3192 };
3193
3194#define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
3195#define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
3196#define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
3197
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003198 int res, i;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003199 int status;
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03003200 int hkeyv;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003201 bool radiosw_state = false;
3202 bool tabletsw_state = false;
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03003203
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003204 unsigned long quirks;
3205
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003206 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3207 "initializing hotkey subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003208
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003209 BUG_ON(!tpacpi_inputdev);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003210 BUG_ON(tpacpi_inputdev->open != NULL ||
3211 tpacpi_inputdev->close != NULL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003212
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003213 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003214 mutex_init(&hotkey_mutex);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003215
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003216#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3217 mutex_init(&hotkey_thread_mutex);
3218 mutex_init(&hotkey_thread_data_mutex);
3219#endif
3220
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003221 /* hotkey not supported on 570 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003222 tp_features.hotkey = hkey_handle != NULL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003223
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003224 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3225 "hotkeys are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003226 str_supported(tp_features.hotkey));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003227
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003228 if (!tp_features.hotkey)
3229 return 1;
3230
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003231 quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
3232 ARRAY_SIZE(tpacpi_hotkey_qtable));
3233
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03003234 tpacpi_disable_brightness_delay();
3235
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003236 /* MUST have enough space for all attributes to be added to
3237 * hotkey_dev_attributes */
3238 hotkey_dev_attributes = create_attr_set(
3239 ARRAY_SIZE(hotkey_attributes) + 2,
3240 NULL);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003241 if (!hotkey_dev_attributes)
3242 return -ENOMEM;
3243 res = add_many_to_attr_set(hotkey_dev_attributes,
3244 hotkey_attributes,
3245 ARRAY_SIZE(hotkey_attributes));
3246 if (res)
3247 goto err_exit;
3248
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003249 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003250 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
3251 for HKEY interface version 0x100 */
3252 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
3253 if ((hkeyv >> 8) != 1) {
3254 printk(TPACPI_ERR "unknown version of the "
3255 "HKEY interface: 0x%x\n", hkeyv);
3256 printk(TPACPI_ERR "please report this to %s\n",
3257 TPACPI_MAIL);
3258 } else {
3259 /*
3260 * MHKV 0x100 in A31, R40, R40e,
3261 * T4x, X31, and later
3262 */
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003263 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3264 "firmware HKEY interface version: 0x%x\n",
3265 hkeyv);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003266
3267 /* Paranoia check AND init hotkey_all_mask */
3268 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3269 "MHKA", "qd")) {
3270 printk(TPACPI_ERR
3271 "missing MHKA handler, "
3272 "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;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003309 printk(TPACPI_INFO
3310 "radio switch emulation enabled\n");
3311 } else
3312#endif
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003313 /* Not all thinkpads have a hardware radio switch */
3314 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
3315 tp_features.hotkey_wlsw = 1;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003316 radiosw_state = !!status;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003317 printk(TPACPI_INFO
3318 "radio switch found; radios are %s\n",
3319 enabled(status, 0));
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003320 }
3321 if (tp_features.hotkey_wlsw)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003322 res = add_to_attr_set(hotkey_dev_attributes,
3323 &dev_attr_hotkey_radio_sw.attr);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003324
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003325 /* For X41t, X60t, X61t Tablets... */
3326 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
3327 tp_features.hotkey_tablet = 1;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003328 tabletsw_state = !!(status & TP_HOTKEY_TABLET_MASK);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003329 printk(TPACPI_INFO
3330 "possible tablet mode switch found; "
3331 "ThinkPad in %s mode\n",
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003332 (tabletsw_state) ? "tablet" : "laptop");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003333 res = add_to_attr_set(hotkey_dev_attributes,
3334 &dev_attr_hotkey_tablet_mode.attr);
3335 }
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02003336
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003337 if (!res)
3338 res = register_attr_set_with_sysfs(
3339 hotkey_dev_attributes,
3340 &tpacpi_pdev->dev.kobj);
3341 if (res)
3342 goto err_exit;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003343
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003344 /* Set up key map */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003345
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003346 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
3347 GFP_KERNEL);
3348 if (!hotkey_keycode_map) {
3349 printk(TPACPI_ERR
3350 "failed to allocate memory for key map\n");
3351 res = -ENOMEM;
3352 goto err_exit;
3353 }
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003354
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003355 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003356 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003357 "using Lenovo default hot key map\n");
3358 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
3359 TPACPI_HOTKEY_MAP_SIZE);
3360 } else {
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003361 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003362 "using IBM default hot key map\n");
3363 memcpy(hotkey_keycode_map, &ibm_keycode_map,
3364 TPACPI_HOTKEY_MAP_SIZE);
3365 }
3366
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003367 input_set_capability(tpacpi_inputdev, EV_MSC, MSC_SCAN);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003368 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
3369 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3370 tpacpi_inputdev->keycode = hotkey_keycode_map;
3371 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3372 if (hotkey_keycode_map[i] != KEY_RESERVED) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003373 input_set_capability(tpacpi_inputdev, EV_KEY,
3374 hotkey_keycode_map[i]);
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003375 } else {
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003376 if (i < sizeof(hotkey_reserved_mask)*8)
3377 hotkey_reserved_mask |= 1 << i;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003378 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003379 }
3380
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003381 if (tp_features.hotkey_wlsw) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003382 input_set_capability(tpacpi_inputdev, EV_SW, SW_RFKILL_ALL);
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003383 input_report_switch(tpacpi_inputdev,
3384 SW_RFKILL_ALL, radiosw_state);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003385 }
3386 if (tp_features.hotkey_tablet) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003387 input_set_capability(tpacpi_inputdev, EV_SW, SW_TABLET_MODE);
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003388 input_report_switch(tpacpi_inputdev,
3389 SW_TABLET_MODE, tabletsw_state);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003390 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003391
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003392 /* Do not issue duplicate brightness change events to
3393 * userspace */
3394 if (!tp_features.bright_acpimode)
3395 /* update bright_acpimode... */
3396 tpacpi_check_std_acpi_brightness_support();
3397
Henrique de Moraes Holschuh8bf3d4c2009-05-30 13:25:09 -03003398 if (tp_features.bright_acpimode && acpi_video_backlight_support()) {
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003399 printk(TPACPI_INFO
3400 "This ThinkPad has standard ACPI backlight "
3401 "brightness control, supported by the ACPI "
3402 "video driver\n");
3403 printk(TPACPI_NOTICE
3404 "Disabling thinkpad-acpi brightness events "
3405 "by default...\n");
3406
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003407 /* Disable brightness up/down on Lenovo thinkpads when
3408 * ACPI is handling them, otherwise it is plain impossible
3409 * for userspace to do something even remotely sane */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003410 hotkey_reserved_mask |=
3411 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3412 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003413 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
3414 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003415 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003416
Henrique de Moraes Holschuh230d8cf252009-09-12 15:22:17 -03003417#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003418 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3419 & ~hotkey_all_mask
3420 & ~hotkey_reserved_mask;
Henrique de Moraes Holschuh230d8cf252009-09-12 15:22:17 -03003421
3422 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3423 "hotkey source mask 0x%08x, polling freq %u\n",
3424 hotkey_source_mask, hotkey_poll_freq);
3425#endif
3426
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003427 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3428 "enabling firmware HKEY event interface...\n");
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003429 res = hotkey_status_set(true);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003430 if (res) {
3431 hotkey_exit();
3432 return res;
3433 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003434 res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
3435 | hotkey_driver_mask)
3436 & ~hotkey_source_mask);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003437 if (res < 0 && res != -ENXIO) {
3438 hotkey_exit();
3439 return res;
3440 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003441 hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
3442 & ~hotkey_reserved_mask;
3443 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3444 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3445 hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003446
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003447 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3448 "legacy ibm/hotkey event reporting over procfs %s\n",
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003449 (hotkey_report_mode < 2) ?
3450 "enabled" : "disabled");
3451
3452 tpacpi_inputdev->open = &hotkey_inputdev_open;
3453 tpacpi_inputdev->close = &hotkey_inputdev_close;
3454
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003455 hotkey_poll_setup_safe(true);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003456
3457 return 0;
3458
3459err_exit:
3460 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3461 hotkey_dev_attributes = NULL;
3462
3463 return (res < 0)? res : 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003464}
3465
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003466static bool hotkey_notify_hotkey(const u32 hkey,
3467 bool *send_acpi_ev,
3468 bool *ignore_acpi_ev)
3469{
3470 /* 0x1000-0x1FFF: key presses */
3471 unsigned int scancode = hkey & 0xfff;
3472 *send_acpi_ev = true;
3473 *ignore_acpi_ev = false;
3474
3475 if (scancode > 0 && scancode < 0x21) {
3476 scancode--;
3477 if (!(hotkey_source_mask & (1 << scancode))) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003478 tpacpi_input_send_key_masked(scancode);
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003479 *send_acpi_ev = false;
3480 } else {
3481 *ignore_acpi_ev = true;
3482 }
3483 return true;
3484 }
3485 return false;
3486}
3487
3488static bool hotkey_notify_wakeup(const u32 hkey,
3489 bool *send_acpi_ev,
3490 bool *ignore_acpi_ev)
3491{
3492 /* 0x2000-0x2FFF: Wakeup reason */
3493 *send_acpi_ev = true;
3494 *ignore_acpi_ev = false;
3495
3496 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003497 case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */
3498 case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003499 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3500 *ignore_acpi_ev = true;
3501 break;
3502
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003503 case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */
3504 case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003505 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3506 *ignore_acpi_ev = true;
3507 break;
3508
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003509 case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */
3510 case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003511 printk(TPACPI_ALERT
3512 "EMERGENCY WAKEUP: battery almost empty\n");
3513 /* how to auto-heal: */
3514 /* 2313: woke up from S3, go to S4/S5 */
3515 /* 2413: woke up from S4, go to S5 */
3516 break;
3517
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003518 default:
3519 return false;
3520 }
3521
3522 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
3523 printk(TPACPI_INFO
3524 "woke up due to a hot-unplug "
3525 "request...\n");
3526 hotkey_wakeup_reason_notify_change();
3527 }
3528 return true;
3529}
3530
3531static bool hotkey_notify_usrevent(const u32 hkey,
3532 bool *send_acpi_ev,
3533 bool *ignore_acpi_ev)
3534{
3535 /* 0x5000-0x5FFF: human interface helpers */
3536 *send_acpi_ev = true;
3537 *ignore_acpi_ev = false;
3538
3539 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003540 case TP_HKEY_EV_PEN_INSERTED: /* X61t: tablet pen inserted into bay */
3541 case TP_HKEY_EV_PEN_REMOVED: /* X61t: tablet pen removed from bay */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003542 return true;
3543
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003544 case TP_HKEY_EV_TABLET_TABLET: /* X41t-X61t: tablet mode */
3545 case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003546 tpacpi_input_send_tabletsw();
3547 hotkey_tablet_mode_notify_change();
3548 *send_acpi_ev = false;
3549 return true;
3550
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003551 case TP_HKEY_EV_LID_CLOSE: /* Lid closed */
3552 case TP_HKEY_EV_LID_OPEN: /* Lid opened */
3553 case TP_HKEY_EV_BRGHT_CHANGED: /* brightness changed */
Henrique de Moraes Holschuh176dd982009-09-20 14:09:24 -03003554 /* do not propagate these events */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003555 *ignore_acpi_ev = true;
3556 return true;
3557
3558 default:
3559 return false;
3560 }
3561}
3562
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003563static void thermal_dump_all_sensors(void);
3564
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003565static bool hotkey_notify_thermal(const u32 hkey,
3566 bool *send_acpi_ev,
3567 bool *ignore_acpi_ev)
3568{
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003569 bool known = true;
3570
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003571 /* 0x6000-0x6FFF: thermal alarms */
3572 *send_acpi_ev = true;
3573 *ignore_acpi_ev = false;
3574
3575 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003576 case TP_HKEY_EV_THM_TABLE_CHANGED:
Henrique de Moraes Holschuh54926ce2009-01-11 03:01:09 -02003577 printk(TPACPI_INFO
3578 "EC reports that Thermal Table has changed\n");
3579 /* recommended action: do nothing, we don't have
3580 * Lenovo ATM information */
3581 return true;
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003582 case TP_HKEY_EV_ALARM_BAT_HOT:
3583 printk(TPACPI_CRIT
3584 "THERMAL ALARM: battery is too hot!\n");
3585 /* recommended action: warn user through gui */
3586 break;
3587 case TP_HKEY_EV_ALARM_BAT_XHOT:
3588 printk(TPACPI_ALERT
3589 "THERMAL EMERGENCY: battery is extremely hot!\n");
3590 /* recommended action: immediate sleep/hibernate */
3591 break;
3592 case TP_HKEY_EV_ALARM_SENSOR_HOT:
3593 printk(TPACPI_CRIT
3594 "THERMAL ALARM: "
3595 "a sensor reports something is too hot!\n");
3596 /* recommended action: warn user through gui, that */
3597 /* some internal component is too hot */
3598 break;
3599 case TP_HKEY_EV_ALARM_SENSOR_XHOT:
3600 printk(TPACPI_ALERT
3601 "THERMAL EMERGENCY: "
3602 "a sensor reports something is extremely hot!\n");
3603 /* recommended action: immediate sleep/hibernate */
3604 break;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003605 default:
3606 printk(TPACPI_ALERT
3607 "THERMAL ALERT: unknown thermal alarm received\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003608 known = false;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003609 }
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003610
3611 thermal_dump_all_sensors();
3612
3613 return known;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003614}
3615
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003616static void hotkey_notify(struct ibm_struct *ibm, u32 event)
3617{
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003618 u32 hkey;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003619 bool send_acpi_ev;
3620 bool ignore_acpi_ev;
3621 bool known_ev;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003622
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003623 if (event != 0x80) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003624 printk(TPACPI_ERR
3625 "unknown HKEY notification event %d\n", event);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003626 /* forward it to userspace, maybe it knows how to handle it */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003627 acpi_bus_generate_netlink_event(
3628 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02003629 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003630 event, 0);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003631 return;
3632 }
3633
3634 while (1) {
3635 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003636 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003637 return;
3638 }
3639
3640 if (hkey == 0) {
3641 /* queue empty */
3642 return;
3643 }
3644
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003645 send_acpi_ev = true;
3646 ignore_acpi_ev = false;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003647
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003648 switch (hkey >> 12) {
3649 case 1:
3650 /* 0x1000-0x1FFF: key presses */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003651 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
3652 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003653 break;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003654 case 2:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003655 /* 0x2000-0x2FFF: Wakeup reason */
3656 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
3657 &ignore_acpi_ev);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003658 break;
3659 case 3:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003660 /* 0x3000-0x3FFF: bay-related wakeups */
Henrique de Moraes Holschuhbf8b29c2010-02-25 21:28:56 -03003661 switch (hkey) {
3662 case TP_HKEY_EV_BAYEJ_ACK:
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003663 hotkey_autosleep_ack = 1;
3664 printk(TPACPI_INFO
3665 "bay ejected\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003666 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003667 known_ev = true;
Henrique de Moraes Holschuhbf8b29c2010-02-25 21:28:56 -03003668 break;
3669 case TP_HKEY_EV_OPTDRV_EJ:
3670 /* FIXME: kick libata if SATA link offline */
3671 known_ev = true;
3672 break;
3673 default:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003674 known_ev = false;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003675 }
3676 break;
3677 case 4:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003678 /* 0x4000-0x4FFF: dock-related wakeups */
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003679 if (hkey == TP_HKEY_EV_UNDOCK_ACK) {
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003680 hotkey_autosleep_ack = 1;
3681 printk(TPACPI_INFO
3682 "undocked\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003683 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003684 known_ev = true;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003685 } else {
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003686 known_ev = false;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003687 }
3688 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003689 case 5:
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02003690 /* 0x5000-0x5FFF: human interface helpers */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003691 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
3692 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003693 break;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003694 case 6:
3695 /* 0x6000-0x6FFF: thermal alarms */
3696 known_ev = hotkey_notify_thermal(hkey, &send_acpi_ev,
3697 &ignore_acpi_ev);
3698 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003699 case 7:
3700 /* 0x7000-0x7FFF: misc */
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003701 if (tp_features.hotkey_wlsw &&
3702 hkey == TP_HKEY_EV_RFKILL_CHANGED) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003703 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02003704 send_acpi_ev = 0;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003705 known_ev = true;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003706 break;
3707 }
3708 /* fallthrough to default */
3709 default:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003710 known_ev = false;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02003711 }
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003712 if (!known_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003713 printk(TPACPI_NOTICE
3714 "unhandled HKEY event 0x%04x\n", hkey);
Henrique de Moraes Holschuhcb429352009-01-11 03:01:07 -02003715 printk(TPACPI_NOTICE
3716 "please report the conditions when this "
3717 "event happened to %s\n", TPACPI_MAIL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003718 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003719
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003720 /* Legacy events */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003721 if (!ignore_acpi_ev &&
3722 (send_acpi_ev || hotkey_report_mode < 2)) {
3723 acpi_bus_generate_proc_event(ibm->acpi->device,
3724 event, hkey);
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03003725 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003726
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003727 /* netlink events */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03003728 if (!ignore_acpi_ev && send_acpi_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003729 acpi_bus_generate_netlink_event(
3730 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02003731 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003732 event, hkey);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003733 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003734 }
3735}
3736
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003737static void hotkey_suspend(pm_message_t state)
3738{
3739 /* Do these on suspend, we get the events on early resume! */
3740 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
3741 hotkey_autosleep_ack = 0;
3742}
3743
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03003744static void hotkey_resume(void)
3745{
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03003746 tpacpi_disable_brightness_delay();
3747
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003748 if (hotkey_status_set(true) < 0 ||
3749 hotkey_mask_set(hotkey_acpi_mask) < 0)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003750 printk(TPACPI_ERR
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003751 "error while attempting to reset the event "
3752 "firmware interface\n");
3753
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003754 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02003755 hotkey_tablet_mode_notify_change();
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003756 hotkey_wakeup_reason_notify_change();
3757 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003758 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03003759}
3760
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003761/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003762static int hotkey_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03003764 int res, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003766 if (!tp_features.hotkey) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003767 seq_printf(m, "status:\t\tnot supported\n");
3768 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003769 }
3770
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02003771 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02003772 return -ERESTARTSYS;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02003773 res = hotkey_status_get(&status);
3774 if (!res)
3775 res = hotkey_mask_get();
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003776 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03003777 if (res)
3778 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003780 seq_printf(m, "status:\t\t%s\n", enabled(status, 0));
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003781 if (hotkey_all_mask) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003782 seq_printf(m, "mask:\t\t0x%08x\n", hotkey_user_mask);
3783 seq_printf(m, "commands:\tenable, disable, reset, <mask>\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784 } else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003785 seq_printf(m, "mask:\t\tnot supported\n");
3786 seq_printf(m, "commands:\tenable, disable, reset\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787 }
3788
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003789 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790}
3791
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003792static void hotkey_enabledisable_warn(bool enable)
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003793{
3794 tpacpi_log_usertask("procfs hotkey enable/disable");
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003795 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
3796 TPACPI_WARN
3797 "hotkey enable/disable functionality has been "
3798 "removed from the driver. Hotkeys are always "
3799 "enabled\n"))
3800 printk(TPACPI_ERR
3801 "Please remove the hotkey=enable module "
3802 "parameter, it is deprecated. Hotkeys are always "
3803 "enabled\n");
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003804}
3805
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003806static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003808 int res;
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03003809 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003812 if (!tp_features.hotkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813 return -ENODEV;
3814
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02003815 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02003816 return -ERESTARTSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003817
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003818 mask = hotkey_user_mask;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003819
3820 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821 while ((cmd = next_cmd(&buf))) {
3822 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003823 hotkey_enabledisable_warn(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003825 hotkey_enabledisable_warn(0);
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003826 res = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827 } else if (strlencmp(cmd, "reset") == 0) {
Henrique de Moraes Holschuh20c9aa42009-09-12 15:22:16 -03003828 mask = (hotkey_all_mask | hotkey_source_mask)
3829 & ~hotkey_reserved_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
3831 /* mask set */
3832 } else if (sscanf(cmd, "%x", &mask) == 1) {
3833 /* mask set */
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003834 } else {
3835 res = -EINVAL;
3836 goto errexit;
3837 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838 }
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003839
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003840 if (!res) {
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003841 tpacpi_disclose_usertask("procfs hotkey",
3842 "set mask to 0x%08x\n", mask);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003843 res = hotkey_user_mask_set(mask);
3844 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003846errexit:
3847 mutex_unlock(&hotkey_mutex);
3848 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003849}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003851static const struct acpi_device_id ibm_htk_device_ids[] = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003852 {TPACPI_ACPI_HKEY_HID, 0},
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003853 {"", 0},
3854};
3855
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003856static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003857 .hid = ibm_htk_device_ids,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003858 .notify = hotkey_notify,
3859 .handle = &hkey_handle,
3860 .type = ACPI_DEVICE_NOTIFY,
3861};
3862
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003863static struct ibm_struct hotkey_driver_data = {
3864 .name = "hotkey",
3865 .read = hotkey_read,
3866 .write = hotkey_write,
3867 .exit = hotkey_exit,
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03003868 .resume = hotkey_resume,
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003869 .suspend = hotkey_suspend,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003870 .acpi = &ibm_hotkey_acpidriver,
3871};
3872
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003873/*************************************************************************
3874 * Bluetooth subdriver
3875 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003877enum {
3878 /* ACPI GBDC/SBDC bits */
3879 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
3880 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003881 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03003882 0 = disable, 1 = enable */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003883};
3884
3885enum {
3886 /* ACPI \BLTH commands */
3887 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
3888 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
3889 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
3890 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
3891 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003892};
3893
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003894#define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
3895
Johannes Berg19d337d2009-06-02 13:01:37 +02003896static int bluetooth_get_status(void)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003897{
3898 int status;
3899
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003900#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3901 if (dbg_bluetoothemul)
3902 return (tpacpi_bluetooth_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02003903 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003904#endif
3905
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003906 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
3907 return -EIO;
3908
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003909 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02003910 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003911}
3912
Johannes Berg19d337d2009-06-02 13:01:37 +02003913static int bluetooth_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003914{
3915 int status;
3916
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003917 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02003918 "will attempt to %s bluetooth\n",
3919 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003920
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003921#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3922 if (dbg_bluetoothemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02003923 tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003924 return 0;
3925 }
3926#endif
3927
Johannes Berg19d337d2009-06-02 13:01:37 +02003928 if (state == TPACPI_RFK_RADIO_ON)
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03003929 status = TP_ACPI_BLUETOOTH_RADIOSSW
3930 | TP_ACPI_BLUETOOTH_RESUMECTRL;
3931 else
3932 status = 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02003933
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003934 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
3935 return -EIO;
3936
3937 return 0;
3938}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003939
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003940/* sysfs bluetooth enable ---------------------------------------------- */
3941static ssize_t bluetooth_enable_show(struct device *dev,
3942 struct device_attribute *attr,
3943 char *buf)
3944{
Johannes Berg19d337d2009-06-02 13:01:37 +02003945 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID,
3946 attr, buf);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003947}
3948
3949static ssize_t bluetooth_enable_store(struct device *dev,
3950 struct device_attribute *attr,
3951 const char *buf, size_t count)
3952{
Johannes Berg19d337d2009-06-02 13:01:37 +02003953 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID,
3954 attr, buf, count);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003955}
3956
3957static struct device_attribute dev_attr_bluetooth_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03003958 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003959 bluetooth_enable_show, bluetooth_enable_store);
3960
3961/* --------------------------------------------------------------------- */
3962
3963static struct attribute *bluetooth_attributes[] = {
3964 &dev_attr_bluetooth_enable.attr,
3965 NULL
3966};
3967
3968static const struct attribute_group bluetooth_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003969 .attrs = bluetooth_attributes,
3970};
3971
Johannes Berg19d337d2009-06-02 13:01:37 +02003972static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = {
3973 .get_status = bluetooth_get_status,
3974 .set_status = bluetooth_set_status,
3975};
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003976
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003977static void bluetooth_shutdown(void)
3978{
3979 /* Order firmware to save current state to NVRAM */
3980 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3981 TP_ACPI_BLTH_SAVE_STATE))
3982 printk(TPACPI_NOTICE
3983 "failed to save bluetooth state to NVRAM\n");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003984 else
3985 vdbg_printk(TPACPI_DBG_RFKILL,
3986 "bluestooth state saved to NVRAM\n");
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003987}
3988
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003989static void bluetooth_exit(void)
3990{
3991 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3992 &bluetooth_attr_group);
Johannes Berg19d337d2009-06-02 13:01:37 +02003993
3994 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
3995
3996 bluetooth_shutdown();
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003997}
3998
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003999static int __init bluetooth_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004001 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004002 int status = 0;
4003
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004004 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4005 "initializing bluetooth subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004006
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004007 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004008
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004009 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4010 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004011 tp_features.bluetooth = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004012 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004014 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4015 "bluetooth is %s, status 0x%02x\n",
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004016 str_supported(tp_features.bluetooth),
4017 status);
4018
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004019#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4020 if (dbg_bluetoothemul) {
4021 tp_features.bluetooth = 1;
4022 printk(TPACPI_INFO
4023 "bluetooth switch emulation enabled\n");
4024 } else
4025#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004026 if (tp_features.bluetooth &&
4027 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
4028 /* no bluetooth hardware present in system */
4029 tp_features.bluetooth = 0;
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004030 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004031 "bluetooth hardware not installed\n");
4032 }
4033
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004034 if (!tp_features.bluetooth)
4035 return 1;
4036
Johannes Berg19d337d2009-06-02 13:01:37 +02004037 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
4038 &bluetooth_tprfk_ops,
4039 RFKILL_TYPE_BLUETOOTH,
4040 TPACPI_RFK_BLUETOOTH_SW_NAME,
4041 true);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004042 if (res)
4043 return res;
4044
Johannes Berg19d337d2009-06-02 13:01:37 +02004045 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4046 &bluetooth_attr_group);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004047 if (res) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004048 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004049 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004050 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004051
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004052 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053}
4054
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004055/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004056static int bluetooth_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057{
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004058 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059}
4060
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004061static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062{
Johannes Berg19d337d2009-06-02 13:01:37 +02004063 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064}
4065
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004066static struct ibm_struct bluetooth_driver_data = {
4067 .name = "bluetooth",
4068 .read = bluetooth_read,
4069 .write = bluetooth_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004070 .exit = bluetooth_exit,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004071 .shutdown = bluetooth_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004072};
4073
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004074/*************************************************************************
4075 * Wan subdriver
4076 */
4077
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004078enum {
4079 /* ACPI GWAN/SWAN bits */
4080 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
4081 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02004082 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03004083 0 = disable, 1 = enable */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004084};
4085
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004086#define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
4087
Johannes Berg19d337d2009-06-02 13:01:37 +02004088static int wan_get_status(void)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004089{
4090 int status;
4091
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004092#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4093 if (dbg_wwanemul)
4094 return (tpacpi_wwan_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004095 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004096#endif
4097
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004098 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
4099 return -EIO;
4100
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004101 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004102 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004103}
4104
Johannes Berg19d337d2009-06-02 13:01:37 +02004105static int wan_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004106{
4107 int status;
4108
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004109 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02004110 "will attempt to %s wwan\n",
4111 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004112
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004113#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4114 if (dbg_wwanemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004115 tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004116 return 0;
4117 }
4118#endif
4119
Johannes Berg19d337d2009-06-02 13:01:37 +02004120 if (state == TPACPI_RFK_RADIO_ON)
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03004121 status = TP_ACPI_WANCARD_RADIOSSW
4122 | TP_ACPI_WANCARD_RESUMECTRL;
4123 else
4124 status = 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02004125
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004126 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
4127 return -EIO;
4128
4129 return 0;
4130}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004131
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004132/* sysfs wan enable ---------------------------------------------------- */
4133static ssize_t wan_enable_show(struct device *dev,
4134 struct device_attribute *attr,
4135 char *buf)
4136{
Johannes Berg19d337d2009-06-02 13:01:37 +02004137 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
4138 attr, buf);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004139}
4140
4141static ssize_t wan_enable_store(struct device *dev,
4142 struct device_attribute *attr,
4143 const char *buf, size_t count)
4144{
Johannes Berg19d337d2009-06-02 13:01:37 +02004145 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
4146 attr, buf, count);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004147}
4148
4149static struct device_attribute dev_attr_wan_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03004150 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004151 wan_enable_show, wan_enable_store);
4152
4153/* --------------------------------------------------------------------- */
4154
4155static struct attribute *wan_attributes[] = {
4156 &dev_attr_wan_enable.attr,
4157 NULL
4158};
4159
4160static const struct attribute_group wan_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004161 .attrs = wan_attributes,
4162};
4163
Johannes Berg19d337d2009-06-02 13:01:37 +02004164static const struct tpacpi_rfk_ops wan_tprfk_ops = {
4165 .get_status = wan_get_status,
4166 .set_status = wan_set_status,
4167};
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004168
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004169static void wan_shutdown(void)
4170{
4171 /* Order firmware to save current state to NVRAM */
4172 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
4173 TP_ACPI_WGSV_SAVE_STATE))
4174 printk(TPACPI_NOTICE
4175 "failed to save WWAN state to NVRAM\n");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004176 else
4177 vdbg_printk(TPACPI_DBG_RFKILL,
4178 "WWAN state saved to NVRAM\n");
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004179}
4180
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004181static void wan_exit(void)
4182{
4183 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4184 &wan_attr_group);
Johannes Berg19d337d2009-06-02 13:01:37 +02004185
4186 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4187
4188 wan_shutdown();
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004189}
4190
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004191static int __init wan_init(struct ibm_init_struct *iibm)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004192{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004193 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004194 int status = 0;
4195
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004196 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4197 "initializing wan subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004198
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004199 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004200
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004201 tp_features.wan = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004202 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004203
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004204 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4205 "wan is %s, status 0x%02x\n",
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004206 str_supported(tp_features.wan),
4207 status);
4208
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004209#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4210 if (dbg_wwanemul) {
4211 tp_features.wan = 1;
4212 printk(TPACPI_INFO
4213 "wwan switch emulation enabled\n");
4214 } else
4215#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004216 if (tp_features.wan &&
4217 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
4218 /* no wan hardware present in system */
4219 tp_features.wan = 0;
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004220 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004221 "wan hardware not installed\n");
4222 }
4223
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004224 if (!tp_features.wan)
4225 return 1;
4226
Johannes Berg19d337d2009-06-02 13:01:37 +02004227 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
4228 &wan_tprfk_ops,
4229 RFKILL_TYPE_WWAN,
4230 TPACPI_RFK_WWAN_SW_NAME,
4231 true);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004232 if (res)
4233 return res;
4234
Johannes Berg19d337d2009-06-02 13:01:37 +02004235 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4236 &wan_attr_group);
4237
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004238 if (res) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004239 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004240 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004241 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004242
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004243 return 0;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004244}
4245
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004246/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004247static int wan_read(struct seq_file *m)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004248{
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004249 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, m);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004250}
4251
4252static int wan_write(char *buf)
4253{
Johannes Berg19d337d2009-06-02 13:01:37 +02004254 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004255}
4256
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004257static struct ibm_struct wan_driver_data = {
4258 .name = "wan",
4259 .read = wan_read,
4260 .write = wan_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004261 .exit = wan_exit,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004262 .shutdown = wan_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004263};
4264
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004265/*************************************************************************
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004266 * UWB subdriver
4267 */
4268
4269enum {
4270 /* ACPI GUWB/SUWB bits */
4271 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4272 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4273};
4274
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004275#define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4276
Johannes Berg19d337d2009-06-02 13:01:37 +02004277static int uwb_get_status(void)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004278{
4279 int status;
4280
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004281#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4282 if (dbg_uwbemul)
4283 return (tpacpi_uwb_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004284 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004285#endif
4286
4287 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4288 return -EIO;
4289
4290 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004291 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004292}
4293
Johannes Berg19d337d2009-06-02 13:01:37 +02004294static int uwb_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004295{
4296 int status;
4297
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004298 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02004299 "will attempt to %s UWB\n",
4300 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004301
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004302#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4303 if (dbg_uwbemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004304 tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004305 return 0;
4306 }
4307#endif
4308
Johannes Berg19d337d2009-06-02 13:01:37 +02004309 if (state == TPACPI_RFK_RADIO_ON)
4310 status = TP_ACPI_UWB_RADIOSSW;
4311 else
4312 status = 0;
4313
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004314 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4315 return -EIO;
4316
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004317 return 0;
4318}
4319
4320/* --------------------------------------------------------------------- */
4321
Johannes Berg19d337d2009-06-02 13:01:37 +02004322static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
4323 .get_status = uwb_get_status,
4324 .set_status = uwb_set_status,
4325};
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004326
4327static void uwb_exit(void)
4328{
Johannes Berg19d337d2009-06-02 13:01:37 +02004329 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004330}
4331
4332static int __init uwb_init(struct ibm_init_struct *iibm)
4333{
4334 int res;
4335 int status = 0;
4336
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004337 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4338 "initializing uwb subdriver\n");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004339
4340 TPACPI_ACPIHANDLE_INIT(hkey);
4341
4342 tp_features.uwb = hkey_handle &&
4343 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4344
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004345 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4346 "uwb is %s, status 0x%02x\n",
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004347 str_supported(tp_features.uwb),
4348 status);
4349
4350#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4351 if (dbg_uwbemul) {
4352 tp_features.uwb = 1;
4353 printk(TPACPI_INFO
4354 "uwb switch emulation enabled\n");
4355 } else
4356#endif
4357 if (tp_features.uwb &&
4358 !(status & TP_ACPI_UWB_HWPRESENT)) {
4359 /* no uwb hardware present in system */
4360 tp_features.uwb = 0;
4361 dbg_printk(TPACPI_DBG_INIT,
4362 "uwb hardware not installed\n");
4363 }
4364
4365 if (!tp_features.uwb)
4366 return 1;
4367
4368 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
Johannes Berg19d337d2009-06-02 13:01:37 +02004369 &uwb_tprfk_ops,
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004370 RFKILL_TYPE_UWB,
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004371 TPACPI_RFK_UWB_SW_NAME,
Johannes Berg19d337d2009-06-02 13:01:37 +02004372 false);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004373 return res;
4374}
4375
4376static struct ibm_struct uwb_driver_data = {
4377 .name = "uwb",
4378 .exit = uwb_exit,
4379 .flags.experimental = 1,
4380};
4381
4382/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004383 * Video subdriver
4384 */
4385
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02004386#ifdef CONFIG_THINKPAD_ACPI_VIDEO
4387
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004388enum video_access_mode {
4389 TPACPI_VIDEO_NONE = 0,
4390 TPACPI_VIDEO_570, /* 570 */
4391 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4392 TPACPI_VIDEO_NEW, /* all others */
4393};
4394
4395enum { /* video status flags, based on VIDEO_570 */
4396 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4397 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4398 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4399};
4400
4401enum { /* TPACPI_VIDEO_570 constants */
4402 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4403 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4404 * video_status_flags */
4405 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4406 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4407};
4408
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02004409static enum video_access_mode video_supported;
4410static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004411
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004412static int video_autosw_get(void);
4413static int video_autosw_set(int enable);
4414
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004415TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004416
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004417static int __init video_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004419 int ivga;
4420
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004421 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4422
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004423 TPACPI_ACPIHANDLE_INIT(vid);
4424 TPACPI_ACPIHANDLE_INIT(vid2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004425
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004426 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4427 /* G41, assume IVGA doesn't change */
4428 vid_handle = vid2_handle;
4429
4430 if (!vid_handle)
4431 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004432 video_supported = TPACPI_VIDEO_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004433 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
4434 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004435 video_supported = TPACPI_VIDEO_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004436 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
4437 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004438 video_supported = TPACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004439 else
4440 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004441 video_supported = TPACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004443 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
4444 str_supported(video_supported != TPACPI_VIDEO_NONE),
4445 video_supported);
4446
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004447 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448}
4449
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004450static void video_exit(void)
4451{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004452 dbg_printk(TPACPI_DBG_EXIT,
4453 "restoring original video autoswitch mode\n");
4454 if (video_autosw_set(video_orig_autosw))
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004455 printk(TPACPI_ERR "error while trying to restore original "
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004456 "video autoswitch mode\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004457}
4458
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004459static int video_outputsw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460{
4461 int status = 0;
4462 int i;
4463
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004464 switch (video_supported) {
4465 case TPACPI_VIDEO_570:
4466 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
4467 TP_ACPI_VIDEO_570_PHSCMD))
4468 return -EIO;
4469 status = i & TP_ACPI_VIDEO_570_PHSMASK;
4470 break;
4471 case TPACPI_VIDEO_770:
4472 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4473 return -EIO;
4474 if (i)
4475 status |= TP_ACPI_VIDEO_S_LCD;
4476 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4477 return -EIO;
4478 if (i)
4479 status |= TP_ACPI_VIDEO_S_CRT;
4480 break;
4481 case TPACPI_VIDEO_NEW:
4482 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4483 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4484 return -EIO;
4485 if (i)
4486 status |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004488 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4489 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4490 return -EIO;
4491 if (i)
4492 status |= TP_ACPI_VIDEO_S_LCD;
4493 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4494 return -EIO;
4495 if (i)
4496 status |= TP_ACPI_VIDEO_S_DVI;
4497 break;
4498 default:
4499 return -ENOSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004500 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501
4502 return status;
4503}
4504
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004505static int video_outputsw_set(int status)
4506{
4507 int autosw;
4508 int res = 0;
4509
4510 switch (video_supported) {
4511 case TPACPI_VIDEO_570:
4512 res = acpi_evalf(NULL, NULL,
4513 "\\_SB.PHS2", "vdd",
4514 TP_ACPI_VIDEO_570_PHS2CMD,
4515 status | TP_ACPI_VIDEO_570_PHS2SET);
4516 break;
4517 case TPACPI_VIDEO_770:
4518 autosw = video_autosw_get();
4519 if (autosw < 0)
4520 return autosw;
4521
4522 res = video_autosw_set(1);
4523 if (res)
4524 return res;
4525 res = acpi_evalf(vid_handle, NULL,
4526 "ASWT", "vdd", status * 0x100, 0);
4527 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004528 printk(TPACPI_ERR
4529 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004530 return -EIO;
4531 }
4532 break;
4533 case TPACPI_VIDEO_NEW:
4534 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004535 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004536 break;
4537 default:
4538 return -ENOSYS;
4539 }
4540
4541 return (res)? 0 : -EIO;
4542}
4543
4544static int video_autosw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004545{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004546 int autosw = 0;
4547
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004548 switch (video_supported) {
4549 case TPACPI_VIDEO_570:
4550 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
4551 return -EIO;
4552 break;
4553 case TPACPI_VIDEO_770:
4554 case TPACPI_VIDEO_NEW:
4555 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4556 return -EIO;
4557 break;
4558 default:
4559 return -ENOSYS;
4560 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004561
4562 return autosw & 1;
4563}
4564
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004565static int video_autosw_set(int enable)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004566{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004567 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004568 return -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004569 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004570}
4571
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004572static int video_outputsw_cycle(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004573{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004574 int autosw = video_autosw_get();
4575 int res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004576
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004577 if (autosw < 0)
4578 return autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004579
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004580 switch (video_supported) {
4581 case TPACPI_VIDEO_570:
4582 res = video_autosw_set(1);
4583 if (res)
4584 return res;
4585 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4586 break;
4587 case TPACPI_VIDEO_770:
4588 case TPACPI_VIDEO_NEW:
4589 res = video_autosw_set(1);
4590 if (res)
4591 return res;
4592 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4593 break;
4594 default:
4595 return -ENOSYS;
4596 }
4597 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004598 printk(TPACPI_ERR
4599 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004600 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004601 }
4602
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004603 return (res)? 0 : -EIO;
4604}
4605
4606static int video_expand_toggle(void)
4607{
4608 switch (video_supported) {
4609 case TPACPI_VIDEO_570:
4610 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
4611 0 : -EIO;
4612 case TPACPI_VIDEO_770:
4613 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
4614 0 : -EIO;
4615 case TPACPI_VIDEO_NEW:
4616 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
4617 0 : -EIO;
4618 default:
4619 return -ENOSYS;
4620 }
4621 /* not reached */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004622}
4623
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004624static int video_read(struct seq_file *m)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004625{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004626 int status, autosw;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004627
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004628 if (video_supported == TPACPI_VIDEO_NONE) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004629 seq_printf(m, "status:\t\tnot supported\n");
4630 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004631 }
4632
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03004633 /* Even reads can crash X.org, so... */
4634 if (!capable(CAP_SYS_ADMIN))
4635 return -EPERM;
4636
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004637 status = video_outputsw_get();
4638 if (status < 0)
4639 return status;
4640
4641 autosw = video_autosw_get();
4642 if (autosw < 0)
4643 return autosw;
4644
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004645 seq_printf(m, "status:\t\tsupported\n");
4646 seq_printf(m, "lcd:\t\t%s\n", enabled(status, 0));
4647 seq_printf(m, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004648 if (video_supported == TPACPI_VIDEO_NEW)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004649 seq_printf(m, "dvi:\t\t%s\n", enabled(status, 3));
4650 seq_printf(m, "auto:\t\t%s\n", enabled(autosw, 0));
4651 seq_printf(m, "commands:\tlcd_enable, lcd_disable\n");
4652 seq_printf(m, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004653 if (video_supported == TPACPI_VIDEO_NEW)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004654 seq_printf(m, "commands:\tdvi_enable, dvi_disable\n");
4655 seq_printf(m, "commands:\tauto_enable, auto_disable\n");
4656 seq_printf(m, "commands:\tvideo_switch, expand_toggle\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004657
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004658 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004659}
4660
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004661static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662{
4663 char *cmd;
4664 int enable, disable, status;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004665 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004667 if (video_supported == TPACPI_VIDEO_NONE)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004668 return -ENODEV;
4669
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03004670 /* Even reads can crash X.org, let alone writes... */
4671 if (!capable(CAP_SYS_ADMIN))
4672 return -EPERM;
4673
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004674 enable = 0;
4675 disable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676
4677 while ((cmd = next_cmd(&buf))) {
4678 if (strlencmp(cmd, "lcd_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004679 enable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680 } else if (strlencmp(cmd, "lcd_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004681 disable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682 } else if (strlencmp(cmd, "crt_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004683 enable |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684 } else if (strlencmp(cmd, "crt_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004685 disable |= TP_ACPI_VIDEO_S_CRT;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004686 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004687 strlencmp(cmd, "dvi_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004688 enable |= TP_ACPI_VIDEO_S_DVI;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004689 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004690 strlencmp(cmd, "dvi_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004691 disable |= TP_ACPI_VIDEO_S_DVI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004692 } else if (strlencmp(cmd, "auto_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004693 res = video_autosw_set(1);
4694 if (res)
4695 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696 } else if (strlencmp(cmd, "auto_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004697 res = video_autosw_set(0);
4698 if (res)
4699 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700 } else if (strlencmp(cmd, "video_switch") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004701 res = video_outputsw_cycle();
4702 if (res)
4703 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004704 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004705 res = video_expand_toggle();
4706 if (res)
4707 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708 } else
4709 return -EINVAL;
4710 }
4711
4712 if (enable || disable) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004713 status = video_outputsw_get();
4714 if (status < 0)
4715 return status;
4716 res = video_outputsw_set((status & ~disable) | enable);
4717 if (res)
4718 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719 }
4720
4721 return 0;
4722}
4723
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004724static struct ibm_struct video_driver_data = {
4725 .name = "video",
4726 .read = video_read,
4727 .write = video_write,
4728 .exit = video_exit,
4729};
4730
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02004731#endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4732
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004733/*************************************************************************
4734 * Light (thinklight) subdriver
4735 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004737TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4738TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004739
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004740static int light_get_status(void)
4741{
4742 int status = 0;
4743
4744 if (tp_features.light_status) {
4745 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4746 return -EIO;
4747 return (!!status);
4748 }
4749
4750 return -ENXIO;
4751}
4752
4753static int light_set_status(int status)
4754{
4755 int rc;
4756
4757 if (tp_features.light) {
4758 if (cmos_handle) {
4759 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4760 (status)?
4761 TP_CMOS_THINKLIGHT_ON :
4762 TP_CMOS_THINKLIGHT_OFF);
4763 } else {
4764 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4765 (status)? 1 : 0);
4766 }
4767 return (rc)? 0 : -EIO;
4768 }
4769
4770 return -ENXIO;
4771}
4772
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004773static void light_set_status_worker(struct work_struct *work)
4774{
4775 struct tpacpi_led_classdev *data =
4776 container_of(work, struct tpacpi_led_classdev, work);
4777
4778 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00004779 light_set_status((data->new_state != TPACPI_LED_OFF));
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004780}
4781
4782static void light_sysfs_set(struct led_classdev *led_cdev,
4783 enum led_brightness brightness)
4784{
4785 struct tpacpi_led_classdev *data =
4786 container_of(led_cdev,
4787 struct tpacpi_led_classdev,
4788 led_classdev);
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00004789 data->new_state = (brightness != LED_OFF) ?
4790 TPACPI_LED_ON : TPACPI_LED_OFF;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004791 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004792}
4793
4794static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4795{
4796 return (light_get_status() == 1)? LED_FULL : LED_OFF;
4797}
4798
4799static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4800 .led_classdev = {
4801 .name = "tpacpi::thinklight",
4802 .brightness_set = &light_sysfs_set,
4803 .brightness_get = &light_sysfs_get,
4804 }
4805};
4806
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004807static int __init light_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004808{
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004809 int rc;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004810
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004811 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4812
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004813 TPACPI_ACPIHANDLE_INIT(ledb);
4814 TPACPI_ACPIHANDLE_INIT(lght);
4815 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004816 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004817
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004818 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004819 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004820
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004821 if (tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004822 /* light status not supported on
4823 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004824 tp_features.light_status =
4825 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004827 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4828 str_supported(tp_features.light),
4829 str_supported(tp_features.light_status));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004830
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004831 if (!tp_features.light)
4832 return 1;
4833
4834 rc = led_classdev_register(&tpacpi_pdev->dev,
4835 &tpacpi_led_thinklight.led_classdev);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004836
4837 if (rc < 0) {
4838 tp_features.light = 0;
4839 tp_features.light_status = 0;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004840 } else {
4841 rc = 0;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004842 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004843
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004844 return rc;
4845}
4846
4847static void light_exit(void)
4848{
4849 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
4850 if (work_pending(&tpacpi_led_thinklight.work))
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004851 flush_workqueue(tpacpi_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852}
4853
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004854static int light_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004855{
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004856 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004857
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004858 if (!tp_features.light) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004859 seq_printf(m, "status:\t\tnot supported\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004860 } else if (!tp_features.light_status) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004861 seq_printf(m, "status:\t\tunknown\n");
4862 seq_printf(m, "commands:\ton, off\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004863 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004864 status = light_get_status();
4865 if (status < 0)
4866 return status;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004867 seq_printf(m, "status:\t\t%s\n", onoff(status, 0));
4868 seq_printf(m, "commands:\ton, off\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004869 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004870
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004871 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872}
4873
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004874static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004875{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004877 int newstatus = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004878
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004879 if (!tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004880 return -ENODEV;
4881
Linus Torvalds1da177e2005-04-16 15:20:36 -07004882 while ((cmd = next_cmd(&buf))) {
4883 if (strlencmp(cmd, "on") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004884 newstatus = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004885 } else if (strlencmp(cmd, "off") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004886 newstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004887 } else
4888 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004889 }
4890
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004891 return light_set_status(newstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004892}
4893
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004894static struct ibm_struct light_driver_data = {
4895 .name = "light",
4896 .read = light_read,
4897 .write = light_write,
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004898 .exit = light_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004899};
4900
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004901/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004902 * CMOS subdriver
4903 */
4904
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004905/* sysfs cmos_command -------------------------------------------------- */
4906static ssize_t cmos_command_store(struct device *dev,
4907 struct device_attribute *attr,
4908 const char *buf, size_t count)
4909{
4910 unsigned long cmos_cmd;
4911 int res;
4912
4913 if (parse_strtoul(buf, 21, &cmos_cmd))
4914 return -EINVAL;
4915
4916 res = issue_thinkpad_cmos_command(cmos_cmd);
4917 return (res)? res : count;
4918}
4919
4920static struct device_attribute dev_attr_cmos_command =
4921 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4922
4923/* --------------------------------------------------------------------- */
4924
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004925static int __init cmos_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004926{
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004927 int res;
4928
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004929 vdbg_printk(TPACPI_DBG_INIT,
4930 "initializing cmos commands subdriver\n");
4931
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004932 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004933
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004934 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4935 str_supported(cmos_handle != NULL));
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004936
4937 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4938 if (res)
4939 return res;
4940
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004941 return (cmos_handle)? 0 : 1;
4942}
4943
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004944static void cmos_exit(void)
4945{
4946 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4947}
4948
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004949static int cmos_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004950{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004951 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4952 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004953 if (!cmos_handle)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004954 seq_printf(m, "status:\t\tnot supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955 else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004956 seq_printf(m, "status:\t\tsupported\n");
4957 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004958 }
4959
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004960 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961}
4962
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004963static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004964{
4965 char *cmd;
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004966 int cmos_cmd, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967
4968 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004969 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
4970 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004971 /* cmos_cmd set */
4972 } else
4973 return -EINVAL;
4974
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004975 res = issue_thinkpad_cmos_command(cmos_cmd);
4976 if (res)
4977 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004978 }
4979
4980 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004981}
4982
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004983static struct ibm_struct cmos_driver_data = {
4984 .name = "cmos",
4985 .read = cmos_read,
4986 .write = cmos_write,
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004987 .exit = cmos_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004988};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004989
4990/*************************************************************************
4991 * LED subdriver
4992 */
4993
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004994enum led_access_mode {
4995 TPACPI_LED_NONE = 0,
4996 TPACPI_LED_570, /* 570 */
4997 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4998 TPACPI_LED_NEW, /* all others */
4999};
5000
5001enum { /* For TPACPI_LED_OLD */
5002 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
5003 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
5004 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
5005};
5006
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02005007static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005008
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005009TPACPI_HANDLE(led, ec, "SLED", /* 570 */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005010 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
5011 /* T20-22, X20-21 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005012 "LED", /* all others */
5013 ); /* R30, R31 */
5014
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005015#define TPACPI_LED_NUMLEDS 16
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005016static struct tpacpi_led_classdev *tpacpi_leds;
5017static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
Harvey Harrisone3aa51f2008-05-29 17:51:57 -07005018static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005019 /* there's a limit of 19 chars + NULL before 2.6.26 */
5020 "tpacpi::power",
5021 "tpacpi:orange:batt",
5022 "tpacpi:green:batt",
5023 "tpacpi::dock_active",
5024 "tpacpi::bay_active",
5025 "tpacpi::dock_batt",
5026 "tpacpi::unknown_led",
5027 "tpacpi::standby",
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005028 "tpacpi::dock_status1",
5029 "tpacpi::dock_status2",
5030 "tpacpi::unknown_led2",
5031 "tpacpi::unknown_led3",
5032 "tpacpi::thinkvantage",
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005033};
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005034#define TPACPI_SAFE_LEDS 0x1081U
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005035
5036static inline bool tpacpi_is_led_restricted(const unsigned int led)
5037{
5038#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5039 return false;
5040#else
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005041 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005042#endif
5043}
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005044
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005045static int led_get_status(const unsigned int led)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005046{
5047 int status;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005048 enum led_status_t led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005049
5050 switch (led_supported) {
5051 case TPACPI_LED_570:
5052 if (!acpi_evalf(ec_handle,
5053 &status, "GLED", "dd", 1 << led))
5054 return -EIO;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005055 led_s = (status == 0)?
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005056 TPACPI_LED_OFF :
5057 ((status == 1)?
5058 TPACPI_LED_ON :
5059 TPACPI_LED_BLINK);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005060 tpacpi_led_state_cache[led] = led_s;
5061 return led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005062 default:
5063 return -ENXIO;
5064 }
5065
5066 /* not reached */
5067}
5068
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005069static int led_set_status(const unsigned int led,
5070 const enum led_status_t ledstatus)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005071{
5072 /* off, on, blink. Index is led_status_t */
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005073 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
5074 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005075
5076 int rc = 0;
5077
5078 switch (led_supported) {
5079 case TPACPI_LED_570:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005080 /* 570 */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005081 if (unlikely(led > 7))
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005082 return -EINVAL;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005083 if (unlikely(tpacpi_is_led_restricted(led)))
5084 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005085 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5086 (1 << led), led_sled_arg1[ledstatus]))
5087 rc = -EIO;
5088 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005089 case TPACPI_LED_OLD:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005090 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005091 if (unlikely(led > 7))
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005092 return -EINVAL;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005093 if (unlikely(tpacpi_is_led_restricted(led)))
5094 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005095 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
5096 if (rc >= 0)
5097 rc = ec_write(TPACPI_LED_EC_HLBL,
5098 (ledstatus == TPACPI_LED_BLINK) << led);
5099 if (rc >= 0)
5100 rc = ec_write(TPACPI_LED_EC_HLCL,
5101 (ledstatus != TPACPI_LED_OFF) << led);
5102 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005103 case TPACPI_LED_NEW:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005104 /* all others */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005105 if (unlikely(led >= TPACPI_LED_NUMLEDS))
5106 return -EINVAL;
5107 if (unlikely(tpacpi_is_led_restricted(led)))
5108 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005109 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5110 led, led_led_arg1[ledstatus]))
5111 rc = -EIO;
5112 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005113 default:
5114 rc = -ENXIO;
5115 }
5116
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005117 if (!rc)
5118 tpacpi_led_state_cache[led] = ledstatus;
5119
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005120 return rc;
5121}
5122
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005123static void led_set_status_worker(struct work_struct *work)
5124{
5125 struct tpacpi_led_classdev *data =
5126 container_of(work, struct tpacpi_led_classdev, work);
5127
5128 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005129 led_set_status(data->led, data->new_state);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005130}
5131
5132static void led_sysfs_set(struct led_classdev *led_cdev,
5133 enum led_brightness brightness)
5134{
5135 struct tpacpi_led_classdev *data = container_of(led_cdev,
5136 struct tpacpi_led_classdev, led_classdev);
5137
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005138 if (brightness == LED_OFF)
5139 data->new_state = TPACPI_LED_OFF;
5140 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
5141 data->new_state = TPACPI_LED_ON;
5142 else
5143 data->new_state = TPACPI_LED_BLINK;
5144
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03005145 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005146}
5147
5148static int led_sysfs_blink_set(struct led_classdev *led_cdev,
5149 unsigned long *delay_on, unsigned long *delay_off)
5150{
5151 struct tpacpi_led_classdev *data = container_of(led_cdev,
5152 struct tpacpi_led_classdev, led_classdev);
5153
5154 /* Can we choose the flash rate? */
5155 if (*delay_on == 0 && *delay_off == 0) {
5156 /* yes. set them to the hardware blink rate (1 Hz) */
5157 *delay_on = 500; /* ms */
5158 *delay_off = 500; /* ms */
5159 } else if ((*delay_on != 500) || (*delay_off != 500))
5160 return -EINVAL;
5161
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005162 data->new_state = TPACPI_LED_BLINK;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03005163 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005164
5165 return 0;
5166}
5167
5168static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
5169{
5170 int rc;
5171
5172 struct tpacpi_led_classdev *data = container_of(led_cdev,
5173 struct tpacpi_led_classdev, led_classdev);
5174
5175 rc = led_get_status(data->led);
5176
5177 if (rc == TPACPI_LED_OFF || rc < 0)
5178 rc = LED_OFF; /* no error handling in led class :( */
5179 else
5180 rc = LED_FULL;
5181
5182 return rc;
5183}
5184
5185static void led_exit(void)
5186{
5187 unsigned int i;
5188
5189 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5190 if (tpacpi_leds[i].led_classdev.name)
5191 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
5192 }
5193
5194 kfree(tpacpi_leds);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005195}
5196
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005197static int __init tpacpi_init_led(unsigned int led)
5198{
5199 int rc;
5200
5201 tpacpi_leds[led].led = led;
5202
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005203 /* LEDs with no name don't get registered */
5204 if (!tpacpi_led_names[led])
5205 return 0;
5206
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005207 tpacpi_leds[led].led_classdev.brightness_set = &led_sysfs_set;
5208 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5209 if (led_supported == TPACPI_LED_570)
5210 tpacpi_leds[led].led_classdev.brightness_get =
5211 &led_sysfs_get;
5212
5213 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5214
5215 INIT_WORK(&tpacpi_leds[led].work, led_set_status_worker);
5216
5217 rc = led_classdev_register(&tpacpi_pdev->dev,
5218 &tpacpi_leds[led].led_classdev);
5219 if (rc < 0)
5220 tpacpi_leds[led].led_classdev.name = NULL;
5221
5222 return rc;
5223}
5224
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005225static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
5226 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5227 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5228 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5229
5230 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5231 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5232 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5233 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5234 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5235 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5236 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5237 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5238
5239 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5240 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5241 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5242 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5243 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5244
5245 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5246 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5247 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5248 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5249
5250 /* (1) - may have excess leds enabled on MSB */
5251
5252 /* Defaults (order matters, keep last, don't reorder!) */
5253 { /* Lenovo */
5254 .vendor = PCI_VENDOR_ID_LENOVO,
5255 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5256 .quirks = 0x1fffU,
5257 },
5258 { /* IBM ThinkPads with no EC version string */
5259 .vendor = PCI_VENDOR_ID_IBM,
5260 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
5261 .quirks = 0x00ffU,
5262 },
5263 { /* IBM ThinkPads with EC version string */
5264 .vendor = PCI_VENDOR_ID_IBM,
5265 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5266 .quirks = 0x00bfU,
5267 },
5268};
5269
5270#undef TPACPI_LEDQ_IBM
5271#undef TPACPI_LEDQ_LNV
5272
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005273static int __init led_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005274{
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005275 unsigned int i;
5276 int rc;
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005277 unsigned long useful_leds;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005278
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005279 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
5280
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005281 TPACPI_ACPIHANDLE_INIT(led);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005282
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005283 if (!led_handle)
5284 /* led not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005285 led_supported = TPACPI_LED_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005286 else if (strlencmp(led_path, "SLED") == 0)
5287 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005288 led_supported = TPACPI_LED_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005289 else if (strlencmp(led_path, "SYSL") == 0)
5290 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005291 led_supported = TPACPI_LED_OLD;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005292 else
5293 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005294 led_supported = TPACPI_LED_NEW;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005295
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005296 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
5297 str_supported(led_supported), led_supported);
5298
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005299 if (led_supported == TPACPI_LED_NONE)
5300 return 1;
5301
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005302 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
5303 GFP_KERNEL);
5304 if (!tpacpi_leds) {
5305 printk(TPACPI_ERR "Out of memory for LED data\n");
5306 return -ENOMEM;
5307 }
5308
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005309 useful_leds = tpacpi_check_quirks(led_useful_qtable,
5310 ARRAY_SIZE(led_useful_qtable));
5311
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005312 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005313 if (!tpacpi_is_led_restricted(i) &&
5314 test_bit(i, &useful_leds)) {
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005315 rc = tpacpi_init_led(i);
5316 if (rc < 0) {
5317 led_exit();
5318 return rc;
5319 }
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005320 }
5321 }
5322
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005323#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005324 printk(TPACPI_NOTICE
5325 "warning: userspace override of important "
5326 "firmware LEDs is enabled\n");
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005327#endif
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005328 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005329}
5330
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005331#define str_led_status(s) \
5332 ((s) == TPACPI_LED_OFF ? "off" : \
5333 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005334
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005335static int led_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005336{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005337 if (!led_supported) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005338 seq_printf(m, "status:\t\tnot supported\n");
5339 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005340 }
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005341 seq_printf(m, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005342
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005343 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005344 /* 570 */
5345 int i, status;
5346 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005347 status = led_get_status(i);
5348 if (status < 0)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005349 return -EIO;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005350 seq_printf(m, "%d:\t\t%s\n",
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005351 i, str_led_status(status));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005352 }
5353 }
5354
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005355 seq_printf(m, "commands:\t"
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005356 "<led> on, <led> off, <led> blink (<led> is 0-15)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005357
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005358 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005359}
5360
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005361static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005362{
5363 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005364 int led, rc;
5365 enum led_status_t s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005366
5367 if (!led_supported)
5368 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005369
5370 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005371 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 15)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005372 return -EINVAL;
5373
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005374 if (strstr(cmd, "off")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005375 s = TPACPI_LED_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005376 } else if (strstr(cmd, "on")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005377 s = TPACPI_LED_ON;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005378 } else if (strstr(cmd, "blink")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005379 s = TPACPI_LED_BLINK;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005380 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005381 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005382 }
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005383
5384 rc = led_set_status(led, s);
5385 if (rc < 0)
5386 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005387 }
5388
5389 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005390}
5391
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005392static struct ibm_struct led_driver_data = {
5393 .name = "led",
5394 .read = led_read,
5395 .write = led_write,
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005396 .exit = led_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005397};
5398
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005399/*************************************************************************
5400 * Beep subdriver
5401 */
5402
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005403TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005404
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005405#define TPACPI_BEEP_Q1 0x0001
5406
5407static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
5408 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
5409 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
5410};
5411
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005412static int __init beep_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005413{
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005414 unsigned long quirks;
5415
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005416 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
5417
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005418 TPACPI_ACPIHANDLE_INIT(beep);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005419
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005420 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
5421 str_supported(beep_handle != NULL));
5422
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005423 quirks = tpacpi_check_quirks(beep_quirk_table,
5424 ARRAY_SIZE(beep_quirk_table));
5425
5426 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
5427
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005428 return (beep_handle)? 0 : 1;
5429}
5430
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005431static int beep_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005432{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005433 if (!beep_handle)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005434 seq_printf(m, "status:\t\tnot supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005435 else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005436 seq_printf(m, "status:\t\tsupported\n");
5437 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-17)\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005438 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005439
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005440 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005441}
5442
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005443static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005444{
5445 char *cmd;
5446 int beep_cmd;
5447
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005448 if (!beep_handle)
5449 return -ENODEV;
5450
Linus Torvalds1da177e2005-04-16 15:20:36 -07005451 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005452 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
5453 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005454 /* beep_cmd set */
5455 } else
5456 return -EINVAL;
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005457 if (tp_features.beep_needs_two_args) {
5458 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
5459 beep_cmd, 0))
5460 return -EIO;
5461 } else {
5462 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
5463 beep_cmd))
5464 return -EIO;
5465 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005466 }
5467
5468 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005469}
5470
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005471static struct ibm_struct beep_driver_data = {
5472 .name = "beep",
5473 .read = beep_read,
5474 .write = beep_write,
5475};
5476
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005477/*************************************************************************
5478 * Thermal subdriver
5479 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005480
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005481enum thermal_access_mode {
5482 TPACPI_THERMAL_NONE = 0, /* No thermal support */
5483 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
5484 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
5485 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
5486 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
5487};
5488
5489enum { /* TPACPI_THERMAL_TPEC_* */
5490 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
5491 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
5492 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005493
5494 TPACPI_THERMAL_SENSOR_NA = -128000, /* Sensor not available */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005495};
5496
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005497
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005498#define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5499struct ibm_thermal_sensors_struct {
5500 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
5501};
5502
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005503static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005504
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005505/* idx is zero-based */
5506static int thermal_get_sensor(int idx, s32 *value)
5507{
5508 int t;
5509 s8 tmp;
5510 char tmpi[5];
5511
5512 t = TP_EC_THERMAL_TMP0;
5513
5514 switch (thermal_read_mode) {
5515#if TPACPI_MAX_THERMAL_SENSORS >= 16
5516 case TPACPI_THERMAL_TPEC_16:
5517 if (idx >= 8 && idx <= 15) {
5518 t = TP_EC_THERMAL_TMP8;
5519 idx -= 8;
5520 }
5521 /* fallthrough */
5522#endif
5523 case TPACPI_THERMAL_TPEC_8:
5524 if (idx <= 7) {
5525 if (!acpi_ec_read(t + idx, &tmp))
5526 return -EIO;
5527 *value = tmp * 1000;
5528 return 0;
5529 }
5530 break;
5531
5532 case TPACPI_THERMAL_ACPI_UPDT:
5533 if (idx <= 7) {
5534 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5535 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
5536 return -EIO;
5537 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5538 return -EIO;
5539 *value = (t - 2732) * 100;
5540 return 0;
5541 }
5542 break;
5543
5544 case TPACPI_THERMAL_ACPI_TMP07:
5545 if (idx <= 7) {
5546 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5547 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5548 return -EIO;
5549 if (t > 127 || t < -127)
5550 t = TP_EC_THERMAL_TMP_NA;
5551 *value = t * 1000;
5552 return 0;
5553 }
5554 break;
5555
5556 case TPACPI_THERMAL_NONE:
5557 default:
5558 return -ENOSYS;
5559 }
5560
5561 return -EINVAL;
5562}
5563
5564static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5565{
5566 int res, i;
5567 int n;
5568
5569 n = 8;
5570 i = 0;
5571
5572 if (!s)
5573 return -EINVAL;
5574
5575 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5576 n = 16;
5577
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005578 for (i = 0 ; i < n; i++) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005579 res = thermal_get_sensor(i, &s->temp[i]);
5580 if (res)
5581 return res;
5582 }
5583
5584 return n;
5585}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005586
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005587static void thermal_dump_all_sensors(void)
5588{
5589 int n, i;
5590 struct ibm_thermal_sensors_struct t;
5591
5592 n = thermal_get_sensors(&t);
5593 if (n <= 0)
5594 return;
5595
5596 printk(TPACPI_NOTICE
5597 "temperatures (Celsius):");
5598
5599 for (i = 0; i < n; i++) {
5600 if (t.temp[i] != TPACPI_THERMAL_SENSOR_NA)
5601 printk(KERN_CONT " %d", (int)(t.temp[i] / 1000));
5602 else
5603 printk(KERN_CONT " N/A");
5604 }
5605
5606 printk(KERN_CONT "\n");
5607}
5608
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005609/* sysfs temp##_input -------------------------------------------------- */
5610
5611static ssize_t thermal_temp_input_show(struct device *dev,
5612 struct device_attribute *attr,
5613 char *buf)
5614{
5615 struct sensor_device_attribute *sensor_attr =
5616 to_sensor_dev_attr(attr);
5617 int idx = sensor_attr->index;
5618 s32 value;
5619 int res;
5620
5621 res = thermal_get_sensor(idx, &value);
5622 if (res)
5623 return res;
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005624 if (value == TPACPI_THERMAL_SENSOR_NA)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005625 return -ENXIO;
5626
5627 return snprintf(buf, PAGE_SIZE, "%d\n", value);
5628}
5629
5630#define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005631 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5632 thermal_temp_input_show, NULL, _idxB)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005633
5634static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5635 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5636 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5637 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5638 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5639 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5640 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5641 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5642 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5643 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5644 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5645 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5646 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5647 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5648 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5649 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5650 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5651};
5652
5653#define THERMAL_ATTRS(X) \
5654 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5655
5656static struct attribute *thermal_temp_input_attr[] = {
5657 THERMAL_ATTRS(8),
5658 THERMAL_ATTRS(9),
5659 THERMAL_ATTRS(10),
5660 THERMAL_ATTRS(11),
5661 THERMAL_ATTRS(12),
5662 THERMAL_ATTRS(13),
5663 THERMAL_ATTRS(14),
5664 THERMAL_ATTRS(15),
5665 THERMAL_ATTRS(0),
5666 THERMAL_ATTRS(1),
5667 THERMAL_ATTRS(2),
5668 THERMAL_ATTRS(3),
5669 THERMAL_ATTRS(4),
5670 THERMAL_ATTRS(5),
5671 THERMAL_ATTRS(6),
5672 THERMAL_ATTRS(7),
5673 NULL
5674};
5675
5676static const struct attribute_group thermal_temp_input16_group = {
5677 .attrs = thermal_temp_input_attr
5678};
5679
5680static const struct attribute_group thermal_temp_input8_group = {
5681 .attrs = &thermal_temp_input_attr[8]
5682};
5683
5684#undef THERMAL_SENSOR_ATTR_TEMP
5685#undef THERMAL_ATTRS
5686
5687/* --------------------------------------------------------------------- */
5688
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005689static int __init thermal_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005690{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005691 u8 t, ta1, ta2;
5692 int i;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005693 int acpi_tmp7;
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005694 int res;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005695
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005696 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5697
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005698 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005699
Henrique de Moraes Holschuh3d6f99c2007-07-18 23:45:46 -03005700 if (thinkpad_id.ec_model) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005701 /*
5702 * Direct EC access mode: sensors at registers
5703 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5704 * non-implemented, thermal sensors return 0x80 when
5705 * not available
5706 */
5707
5708 ta1 = ta2 = 0;
5709 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03005710 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005711 ta1 |= t;
5712 } else {
5713 ta1 = 0;
5714 break;
5715 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03005716 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005717 ta2 |= t;
5718 } else {
5719 ta1 = 0;
5720 break;
5721 }
5722 }
5723 if (ta1 == 0) {
5724 /* This is sheer paranoia, but we handle it anyway */
5725 if (acpi_tmp7) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005726 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005727 "ThinkPad ACPI EC access misbehaving, "
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005728 "falling back to ACPI TMPx access "
5729 "mode\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005730 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005731 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005732 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005733 "ThinkPad ACPI EC access misbehaving, "
5734 "disabling thermal sensors access\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005735 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005736 }
5737 } else {
5738 thermal_read_mode =
5739 (ta2 != 0) ?
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005740 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005741 }
5742 } else if (acpi_tmp7) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005743 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
5744 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005745 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005746 } else {
5747 /* Standard ACPI TMPx access, max 8 sensors */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005748 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005749 }
5750 } else {
5751 /* temperatures not supported on 570, G4x, R30, R31, R32 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005752 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005753 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005754
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005755 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5756 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5757 thermal_read_mode);
5758
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005759 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005760 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005761 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005762 &thermal_temp_input16_group);
5763 if (res)
5764 return res;
5765 break;
5766 case TPACPI_THERMAL_TPEC_8:
5767 case TPACPI_THERMAL_ACPI_TMP07:
5768 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005769 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005770 &thermal_temp_input8_group);
5771 if (res)
5772 return res;
5773 break;
5774 case TPACPI_THERMAL_NONE:
5775 default:
5776 return 1;
5777 }
5778
5779 return 0;
5780}
5781
5782static void thermal_exit(void)
5783{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005784 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005785 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005786 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005787 &thermal_temp_input16_group);
5788 break;
5789 case TPACPI_THERMAL_TPEC_8:
5790 case TPACPI_THERMAL_ACPI_TMP07:
5791 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005792 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Roel Kluinf04d5e02010-02-02 14:37:58 -08005793 &thermal_temp_input8_group);
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005794 break;
5795 case TPACPI_THERMAL_NONE:
5796 default:
5797 break;
5798 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005799}
5800
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005801static int thermal_read(struct seq_file *m)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005802{
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005803 int n, i;
5804 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005805
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005806 n = thermal_get_sensors(&t);
5807 if (unlikely(n < 0))
5808 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005809
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005810 seq_printf(m, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005811
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005812 if (n > 0) {
5813 for (i = 0; i < (n - 1); i++)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005814 seq_printf(m, "%d ", t.temp[i] / 1000);
5815 seq_printf(m, "%d\n", t.temp[i] / 1000);
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005816 } else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005817 seq_printf(m, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005818
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005819 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005820}
5821
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005822static struct ibm_struct thermal_driver_data = {
5823 .name = "thermal",
5824 .read = thermal_read,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005825 .exit = thermal_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005826};
5827
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005828/*************************************************************************
5829 * EC Dump subdriver
5830 */
5831
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005832static u8 ecdump_regs[256];
5833
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005834static int ecdump_read(struct seq_file *m)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005835{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005836 int i, j;
5837 u8 v;
5838
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005839 seq_printf(m, "EC "
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005840 " +00 +01 +02 +03 +04 +05 +06 +07"
5841 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
5842 for (i = 0; i < 256; i += 16) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005843 seq_printf(m, "EC 0x%02x:", i);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005844 for (j = 0; j < 16; j++) {
5845 if (!acpi_ec_read(i + j, &v))
5846 break;
5847 if (v != ecdump_regs[i + j])
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005848 seq_printf(m, " *%02x", v);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005849 else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005850 seq_printf(m, " %02x", v);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005851 ecdump_regs[i + j] = v;
5852 }
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005853 seq_putc(m, '\n');
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005854 if (j != 16)
5855 break;
5856 }
5857
5858 /* These are way too dangerous to advertise openly... */
5859#if 0
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005860 seq_printf(m, "commands:\t0x<offset> 0x<value>"
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005861 " (<offset> is 00-ff, <value> is 00-ff)\n");
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005862 seq_printf(m, "commands:\t0x<offset> <value> "
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005863 " (<offset> is 00-ff, <value> is 0-255)\n");
5864#endif
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005865 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005866}
5867
5868static int ecdump_write(char *buf)
5869{
5870 char *cmd;
5871 int i, v;
5872
5873 while ((cmd = next_cmd(&buf))) {
5874 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
5875 /* i and v set */
5876 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
5877 /* i and v set */
5878 } else
5879 return -EINVAL;
5880 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
5881 if (!acpi_ec_write(i, v))
5882 return -EIO;
5883 } else
5884 return -EINVAL;
5885 }
5886
5887 return 0;
5888}
5889
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005890static struct ibm_struct ecdump_driver_data = {
5891 .name = "ecdump",
5892 .read = ecdump_read,
5893 .write = ecdump_write,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03005894 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005895};
5896
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005897/*************************************************************************
5898 * Backlight/brightness subdriver
5899 */
Holger Macht8acb0252006-10-20 14:30:28 -07005900
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005901#define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5902
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005903/*
5904 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
5905 * CMOS NVRAM byte 0x5E, bits 0-3.
5906 *
5907 * EC HBRV (0x31) has the following layout
5908 * Bit 7: unknown function
5909 * Bit 6: unknown function
5910 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5911 * Bit 4: must be set to zero to avoid problems
5912 * Bit 3-0: backlight brightness level
5913 *
5914 * brightness_get_raw returns status data in the HBRV layout
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03005915 *
5916 * WARNING: The X61 has been verified to use HBRV for something else, so
5917 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
5918 * testing on the very early *60 Lenovo models...
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005919 */
5920
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005921enum {
5922 TP_EC_BACKLIGHT = 0x31,
5923
5924 /* TP_EC_BACKLIGHT bitmasks */
5925 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5926 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5927 TP_EC_BACKLIGHT_MAPSW = 0x20,
5928};
5929
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005930enum tpacpi_brightness_access_mode {
5931 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
5932 TPACPI_BRGHT_MODE_EC, /* EC control */
5933 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
5934 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
5935 TPACPI_BRGHT_MODE_MAX
5936};
5937
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -03005938static struct backlight_device *ibm_backlight_device;
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005939
5940static enum tpacpi_brightness_access_mode brightness_mode =
5941 TPACPI_BRGHT_MODE_MAX;
5942
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005943static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5944
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005945static struct mutex brightness_mutex;
5946
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005947/* NVRAM brightness access,
5948 * call with brightness_mutex held! */
5949static unsigned int tpacpi_brightness_nvram_get(void)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005950{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005951 u8 lnvram;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005952
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005953 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5954 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5955 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
5956 lnvram &= (tp_features.bright_16levels) ? 0x0f : 0x07;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005957
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005958 return lnvram;
5959}
5960
5961static void tpacpi_brightness_checkpoint_nvram(void)
5962{
5963 u8 lec = 0;
5964 u8 b_nvram;
5965
5966 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
5967 return;
5968
5969 vdbg_printk(TPACPI_DBG_BRGHT,
5970 "trying to checkpoint backlight level to NVRAM...\n");
5971
5972 if (mutex_lock_killable(&brightness_mutex) < 0)
5973 return;
5974
5975 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5976 goto unlock;
5977 lec &= TP_EC_BACKLIGHT_LVLMSK;
5978 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
5979
5980 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5981 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
5982 /* NVRAM needs update */
5983 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
5984 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
5985 b_nvram |= lec;
5986 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
5987 dbg_printk(TPACPI_DBG_BRGHT,
5988 "updated NVRAM backlight level to %u (0x%02x)\n",
5989 (unsigned int) lec, (unsigned int) b_nvram);
5990 } else
5991 vdbg_printk(TPACPI_DBG_BRGHT,
5992 "NVRAM backlight level already is %u (0x%02x)\n",
5993 (unsigned int) lec, (unsigned int) b_nvram);
5994
5995unlock:
5996 mutex_unlock(&brightness_mutex);
5997}
5998
5999
6000/* call with brightness_mutex held! */
6001static int tpacpi_brightness_get_raw(int *status)
6002{
6003 u8 lec = 0;
6004
6005 switch (brightness_mode) {
6006 case TPACPI_BRGHT_MODE_UCMS_STEP:
6007 *status = tpacpi_brightness_nvram_get();
6008 return 0;
6009 case TPACPI_BRGHT_MODE_EC:
6010 case TPACPI_BRGHT_MODE_ECNVRAM:
6011 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
Henrique de Moraes Holschuh2d5e94d2008-04-26 01:02:20 -03006012 return -EIO;
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006013 *status = lec;
6014 return 0;
6015 default:
6016 return -ENXIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006017 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006018}
6019
6020/* call with brightness_mutex held! */
6021/* do NOT call with illegal backlight level value */
6022static int tpacpi_brightness_set_ec(unsigned int value)
6023{
6024 u8 lec = 0;
6025
6026 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6027 return -EIO;
6028
6029 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
6030 (lec & TP_EC_BACKLIGHT_CMDMSK) |
6031 (value & TP_EC_BACKLIGHT_LVLMSK))))
6032 return -EIO;
6033
6034 return 0;
6035}
6036
6037/* call with brightness_mutex held! */
6038static int tpacpi_brightness_set_ucmsstep(unsigned int value)
6039{
6040 int cmos_cmd, inc;
6041 unsigned int current_value, i;
6042
6043 current_value = tpacpi_brightness_nvram_get();
6044
6045 if (value == current_value)
6046 return 0;
6047
6048 cmos_cmd = (value > current_value) ?
6049 TP_CMOS_BRIGHTNESS_UP :
6050 TP_CMOS_BRIGHTNESS_DOWN;
6051 inc = (value > current_value) ? 1 : -1;
6052
6053 for (i = current_value; i != value; i += inc)
6054 if (issue_thinkpad_cmos_command(cmos_cmd))
6055 return -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006056
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006057 return 0;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006058}
6059
6060/* May return EINTR which can always be mapped to ERESTARTSYS */
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006061static int brightness_set(unsigned int value)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006062{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006063 int res;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006064
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006065 if (value > ((tp_features.bright_16levels)? 15 : 7) ||
6066 value < 0)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006067 return -EINVAL;
6068
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006069 vdbg_printk(TPACPI_DBG_BRGHT,
6070 "set backlight level to %d\n", value);
6071
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006072 res = mutex_lock_killable(&brightness_mutex);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006073 if (res < 0)
6074 return res;
6075
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006076 switch (brightness_mode) {
6077 case TPACPI_BRGHT_MODE_EC:
6078 case TPACPI_BRGHT_MODE_ECNVRAM:
6079 res = tpacpi_brightness_set_ec(value);
6080 break;
6081 case TPACPI_BRGHT_MODE_UCMS_STEP:
6082 res = tpacpi_brightness_set_ucmsstep(value);
6083 break;
6084 default:
6085 res = -ENXIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006086 }
6087
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006088 mutex_unlock(&brightness_mutex);
6089 return res;
6090}
6091
6092/* sysfs backlight class ----------------------------------------------- */
6093
6094static int brightness_update_status(struct backlight_device *bd)
6095{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006096 unsigned int level =
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006097 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
6098 bd->props.power == FB_BLANK_UNBLANK) ?
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006099 bd->props.brightness : 0;
6100
6101 dbg_printk(TPACPI_DBG_BRGHT,
6102 "backlight: attempt to set level to %d\n",
6103 level);
6104
6105 /* it is the backlight class's job (caller) to handle
6106 * EINTR and other errors properly */
6107 return brightness_set(level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006108}
Holger Macht8acb0252006-10-20 14:30:28 -07006109
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006110static int brightness_get(struct backlight_device *bd)
6111{
6112 int status, res;
6113
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006114 res = mutex_lock_killable(&brightness_mutex);
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006115 if (res < 0)
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006116 return 0;
6117
6118 res = tpacpi_brightness_get_raw(&status);
6119
6120 mutex_unlock(&brightness_mutex);
6121
6122 if (res < 0)
6123 return 0;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006124
6125 return status & TP_EC_BACKLIGHT_LVLMSK;
6126}
6127
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00006128static void tpacpi_brightness_notify_change(void)
6129{
6130 backlight_force_update(ibm_backlight_device,
6131 BACKLIGHT_UPDATE_HOTKEY);
6132}
6133
Richard Purdie599a52d2007-02-10 23:07:48 +00006134static struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006135 .get_brightness = brightness_get,
6136 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006137};
6138
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006139/* --------------------------------------------------------------------- */
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03006140
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006141/*
6142 * These are only useful for models that have only one possibility
6143 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6144 * these quirks.
6145 */
6146#define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6147#define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6148#define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6149
6150static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
6151 /* Models with ATI GPUs known to require ECNVRAM mode */
6152 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
6153
Henrique de Moraes Holschuh6da25bf2009-09-12 15:22:11 -03006154 /* Models with ATI GPUs that can use ECNVRAM */
Henrique de Moraes Holschuh7d1894d2010-02-25 21:28:56 -03006155 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC), /* R50,51 T40-42 */
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006156 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
Henrique de Moraes Holschuh7d1894d2010-02-25 21:28:56 -03006157 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_EC), /* R52 */
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006158 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6159
Henrique de Moraes Holschuh6da25bf2009-09-12 15:22:11 -03006160 /* Models with Intel Extreme Graphics 2 */
Henrique de Moraes Holschuh7d1894d2010-02-25 21:28:56 -03006161 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC), /* X40 */
Henrique de Moraes Holschuha9f8eac2009-12-09 01:36:21 +00006162 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6163 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006164
6165 /* Models with Intel GMA900 */
6166 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
6167 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
6168 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
6169};
6170
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006171static int __init brightness_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006172{
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 Holschuhb5972792008-04-26 01:02:17 -03006183 /*
6184 * We always attempt to detect acpi support, so as to switch
6185 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6186 * going to publish a backlight interface
6187 */
6188 b = tpacpi_check_std_acpi_brightness_support();
6189 if (b > 0) {
Thomas Renninger2dba1b52008-08-01 17:38:03 +02006190
6191 if (acpi_video_backlight_support()) {
6192 if (brightness_enable > 1) {
6193 printk(TPACPI_NOTICE
6194 "Standard ACPI backlight interface "
6195 "available, not loading native one.\n");
6196 return 1;
6197 } else if (brightness_enable == 1) {
6198 printk(TPACPI_NOTICE
6199 "Backlight control force enabled, even if standard "
6200 "ACPI backlight interface is available\n");
6201 }
6202 } else {
6203 if (brightness_enable > 1) {
6204 printk(TPACPI_NOTICE
6205 "Standard ACPI backlight interface not "
6206 "available, thinkpad_acpi native "
6207 "brightness control enabled\n");
6208 }
Henrique de Moraes Holschuhe11e2112007-10-30 17:46:22 -02006209 }
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02006210 }
6211
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03006212 if (!brightness_enable) {
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006213 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03006214 "brightness support disabled by "
6215 "module parameter\n");
6216 return 1;
6217 }
6218
6219 if (b > 16) {
6220 printk(TPACPI_ERR
6221 "Unsupported brightness interface, "
6222 "please contact %s\n", TPACPI_MAIL);
6223 return 1;
6224 }
6225 if (b == 16)
6226 tp_features.bright_16levels = 1;
6227
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006228 /*
6229 * Check for module parameter bogosity, note that we
6230 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
6231 * able to detect "unspecified"
6232 */
6233 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03006234 return -EINVAL;
6235
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006236 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
6237 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
6238 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006239 if (quirks & TPACPI_BRGHT_Q_EC)
6240 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
6241 else
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006242 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
6243
6244 dbg_printk(TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006245 "driver auto-selected brightness_mode=%d\n",
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006246 brightness_mode);
6247 }
6248
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03006249 /* Safety */
6250 if (thinkpad_id.vendor != PCI_VENDOR_ID_IBM &&
6251 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
6252 brightness_mode == TPACPI_BRGHT_MODE_EC))
6253 return -EINVAL;
6254
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006255 if (tpacpi_brightness_get_raw(&b) < 0)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03006256 return 1;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006257
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02006258 if (tp_features.bright_16levels)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006259 printk(TPACPI_INFO
6260 "detected a 16-level brightness capable ThinkPad\n");
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02006261
Henrique de Moraes Holschuh7d5a0152007-04-24 11:48:20 -03006262 ibm_backlight_device = backlight_device_register(
6263 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
6264 &ibm_backlight_data);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006265 if (IS_ERR(ibm_backlight_device)) {
Henrique de Moraes Holschuh435c47e2009-09-20 14:09:22 -03006266 int rc = PTR_ERR(ibm_backlight_device);
6267 ibm_backlight_device = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006268 printk(TPACPI_ERR "Could not register backlight device\n");
Henrique de Moraes Holschuh435c47e2009-09-20 14:09:22 -03006269 return rc;
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006270 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006271 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6272 "brightness is supported\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006273
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006274 if (quirks & TPACPI_BRGHT_Q_ASK) {
6275 printk(TPACPI_NOTICE
6276 "brightness: will use unverified default: "
6277 "brightness_mode=%d\n", brightness_mode);
6278 printk(TPACPI_NOTICE
6279 "brightness: please report to %s whether it works well "
6280 "or not on your ThinkPad\n", TPACPI_MAIL);
6281 }
6282
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02006283 ibm_backlight_device->props.max_brightness =
6284 (tp_features.bright_16levels)? 15 : 7;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006285 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006286 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00006287
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00006288 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6289 "brightness: registering brightness hotkeys "
6290 "as change notification\n");
6291 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6292 | TP_ACPI_HKEY_BRGHTUP_MASK
6293 | TP_ACPI_HKEY_BRGHTDWN_MASK);;
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006294 return 0;
6295}
6296
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006297static void brightness_suspend(pm_message_t state)
6298{
6299 tpacpi_brightness_checkpoint_nvram();
6300}
6301
6302static void brightness_shutdown(void)
6303{
6304 tpacpi_brightness_checkpoint_nvram();
6305}
6306
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006307static void brightness_exit(void)
6308{
6309 if (ibm_backlight_device) {
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006310 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006311 "calling backlight_device_unregister()\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006312 backlight_device_unregister(ibm_backlight_device);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006313 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006314
6315 tpacpi_brightness_checkpoint_nvram();
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006316}
6317
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006318static int brightness_read(struct seq_file *m)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006319{
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006320 int level;
6321
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006322 level = brightness_get(NULL);
6323 if (level < 0) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006324 seq_printf(m, "level:\t\tunreadable\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006325 } else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006326 seq_printf(m, "level:\t\t%d\n", level);
6327 seq_printf(m, "commands:\tup, down\n");
6328 seq_printf(m, "commands:\tlevel <level>"
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02006329 " (<level> is 0-%d)\n",
6330 (tp_features.bright_16levels) ? 15 : 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006331 }
6332
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006333 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006334}
6335
6336static int brightness_write(char *buf)
6337{
6338 int level;
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006339 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006340 char *cmd;
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02006341 int max_level = (tp_features.bright_16levels) ? 15 : 7;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006342
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006343 level = brightness_get(NULL);
6344 if (level < 0)
6345 return level;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006346
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006347 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006348 if (strlencmp(cmd, "up") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006349 if (level < max_level)
6350 level++;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006351 } else if (strlencmp(cmd, "down") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006352 if (level > 0)
6353 level--;
6354 } else if (sscanf(cmd, "level %d", &level) == 1 &&
6355 level >= 0 && level <= max_level) {
6356 /* new level set */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006357 } else
6358 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006359 }
6360
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006361 tpacpi_disclose_usertask("procfs brightness",
6362 "set level to %d\n", level);
6363
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006364 /*
6365 * Now we know what the final level should be, so we try to set it.
6366 * Doing it this way makes the syscall restartable in case of EINTR
6367 */
6368 rc = brightness_set(level);
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00006369 if (!rc && ibm_backlight_device)
6370 backlight_force_update(ibm_backlight_device,
6371 BACKLIGHT_UPDATE_SYSFS);
Roel Kluin80a8d122009-11-20 19:48:23 +01006372 return (rc == -EINTR)? -ERESTARTSYS : rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006373}
6374
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006375static struct ibm_struct brightness_driver_data = {
6376 .name = "brightness",
6377 .read = brightness_read,
6378 .write = brightness_write,
6379 .exit = brightness_exit,
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006380 .suspend = brightness_suspend,
6381 .shutdown = brightness_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006382};
6383
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006384/*************************************************************************
6385 * Volume subdriver
6386 */
6387
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006388/*
6389 * IBM ThinkPads have a simple volume controller with MUTE gating.
6390 * Very early Lenovo ThinkPads follow the IBM ThinkPad spec.
6391 *
6392 * Since the *61 series (and probably also the later *60 series), Lenovo
6393 * ThinkPads only implement the MUTE gate.
6394 *
6395 * EC register 0x30
6396 * Bit 6: MUTE (1 mutes sound)
6397 * Bit 3-0: Volume
6398 * Other bits should be zero as far as we know.
6399 *
6400 * This is also stored in CMOS NVRAM, byte 0x60, bit 6 (MUTE), and
6401 * bits 3-0 (volume). Other bits in NVRAM may have other functions,
6402 * such as bit 7 which is used to detect repeated presses of MUTE,
6403 * and we leave them unchanged.
6404 */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006405
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02006406#ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
6407
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006408#define TPACPI_ALSA_DRVNAME "ThinkPad EC"
6409#define TPACPI_ALSA_SHRTNAME "ThinkPad Console Audio Control"
6410#define TPACPI_ALSA_MIXERNAME TPACPI_ALSA_SHRTNAME
6411
Henrique de Moraes Holschuhead510c2009-12-26 22:52:13 -02006412static int alsa_index = ~((1 << (SNDRV_CARDS - 3)) - 1); /* last three slots */
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006413static char *alsa_id = "ThinkPadEC";
6414static int alsa_enable = SNDRV_DEFAULT_ENABLE1;
6415
6416struct tpacpi_alsa_data {
6417 struct snd_card *card;
6418 struct snd_ctl_elem_id *ctl_mute_id;
6419 struct snd_ctl_elem_id *ctl_vol_id;
6420};
6421
6422static struct snd_card *alsa_card;
6423
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006424enum {
6425 TP_EC_AUDIO = 0x30,
6426
6427 /* TP_EC_AUDIO bits */
6428 TP_EC_AUDIO_MUTESW = 6,
6429
6430 /* TP_EC_AUDIO bitmasks */
6431 TP_EC_AUDIO_LVL_MSK = 0x0F,
6432 TP_EC_AUDIO_MUTESW_MSK = (1 << TP_EC_AUDIO_MUTESW),
6433
6434 /* Maximum volume */
6435 TP_EC_VOLUME_MAX = 14,
6436};
6437
6438enum tpacpi_volume_access_mode {
6439 TPACPI_VOL_MODE_AUTO = 0, /* Not implemented yet */
6440 TPACPI_VOL_MODE_EC, /* Pure EC control */
6441 TPACPI_VOL_MODE_UCMS_STEP, /* UCMS step-based control: N/A */
6442 TPACPI_VOL_MODE_ECNVRAM, /* EC control w/ NVRAM store */
6443 TPACPI_VOL_MODE_MAX
6444};
6445
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006446enum tpacpi_volume_capabilities {
6447 TPACPI_VOL_CAP_AUTO = 0, /* Use white/blacklist */
6448 TPACPI_VOL_CAP_VOLMUTE, /* Output vol and mute */
6449 TPACPI_VOL_CAP_MUTEONLY, /* Output mute only */
6450 TPACPI_VOL_CAP_MAX
6451};
6452
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006453static enum tpacpi_volume_access_mode volume_mode =
6454 TPACPI_VOL_MODE_MAX;
6455
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006456static enum tpacpi_volume_capabilities volume_capabilities;
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006457static int volume_control_allowed;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006458
6459/*
6460 * Used to syncronize writers to TP_EC_AUDIO and
6461 * TP_NVRAM_ADDR_MIXER, as we need to do read-modify-write
6462 */
6463static struct mutex volume_mutex;
6464
6465static void tpacpi_volume_checkpoint_nvram(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006466{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006467 u8 lec = 0;
6468 u8 b_nvram;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006469 u8 ec_mask;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006470
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006471 if (volume_mode != TPACPI_VOL_MODE_ECNVRAM)
6472 return;
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006473 if (!volume_control_allowed)
6474 return;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006475
6476 vdbg_printk(TPACPI_DBG_MIXER,
6477 "trying to checkpoint mixer state to NVRAM...\n");
6478
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006479 if (tp_features.mixer_no_level_control)
6480 ec_mask = TP_EC_AUDIO_MUTESW_MSK;
6481 else
6482 ec_mask = TP_EC_AUDIO_MUTESW_MSK | TP_EC_AUDIO_LVL_MSK;
6483
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006484 if (mutex_lock_killable(&volume_mutex) < 0)
6485 return;
6486
6487 if (unlikely(!acpi_ec_read(TP_EC_AUDIO, &lec)))
6488 goto unlock;
6489 lec &= ec_mask;
6490 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
6491
6492 if (lec != (b_nvram & ec_mask)) {
6493 /* NVRAM needs update */
6494 b_nvram &= ~ec_mask;
6495 b_nvram |= lec;
6496 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_MIXER);
6497 dbg_printk(TPACPI_DBG_MIXER,
6498 "updated NVRAM mixer status to 0x%02x (0x%02x)\n",
6499 (unsigned int) lec, (unsigned int) b_nvram);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006500 } else {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006501 vdbg_printk(TPACPI_DBG_MIXER,
6502 "NVRAM mixer status already is 0x%02x (0x%02x)\n",
6503 (unsigned int) lec, (unsigned int) b_nvram);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006504 }
6505
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006506unlock:
6507 mutex_unlock(&volume_mutex);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006508}
6509
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006510static int volume_get_status_ec(u8 *status)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006511{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006512 u8 s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006513
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006514 if (!acpi_ec_read(TP_EC_AUDIO, &s))
6515 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006516
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006517 *status = s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006518
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006519 dbg_printk(TPACPI_DBG_MIXER, "status 0x%02x\n", s);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006520
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006521 return 0;
6522}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006523
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006524static int volume_get_status(u8 *status)
6525{
6526 return volume_get_status_ec(status);
6527}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006528
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006529static int volume_set_status_ec(const u8 status)
6530{
6531 if (!acpi_ec_write(TP_EC_AUDIO, status))
6532 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006533
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006534 dbg_printk(TPACPI_DBG_MIXER, "set EC mixer to 0x%02x\n", status);
6535
6536 return 0;
6537}
6538
6539static int volume_set_status(const u8 status)
6540{
6541 return volume_set_status_ec(status);
6542}
6543
6544static int volume_set_mute_ec(const bool mute)
6545{
6546 int rc;
6547 u8 s, n;
6548
6549 if (mutex_lock_killable(&volume_mutex) < 0)
6550 return -EINTR;
6551
6552 rc = volume_get_status_ec(&s);
6553 if (rc)
6554 goto unlock;
6555
6556 n = (mute) ? s | TP_EC_AUDIO_MUTESW_MSK :
6557 s & ~TP_EC_AUDIO_MUTESW_MSK;
6558
6559 if (n != s)
6560 rc = volume_set_status_ec(n);
6561
6562unlock:
6563 mutex_unlock(&volume_mutex);
6564 return rc;
6565}
6566
6567static int volume_set_mute(const bool mute)
6568{
6569 dbg_printk(TPACPI_DBG_MIXER, "trying to %smute\n",
6570 (mute) ? "" : "un");
6571 return volume_set_mute_ec(mute);
6572}
6573
6574static int volume_set_volume_ec(const u8 vol)
6575{
6576 int rc;
6577 u8 s, n;
6578
6579 if (vol > TP_EC_VOLUME_MAX)
6580 return -EINVAL;
6581
6582 if (mutex_lock_killable(&volume_mutex) < 0)
6583 return -EINTR;
6584
6585 rc = volume_get_status_ec(&s);
6586 if (rc)
6587 goto unlock;
6588
6589 n = (s & ~TP_EC_AUDIO_LVL_MSK) | vol;
6590
6591 if (n != s)
6592 rc = volume_set_status_ec(n);
6593
6594unlock:
6595 mutex_unlock(&volume_mutex);
6596 return rc;
6597}
6598
6599static int volume_set_volume(const u8 vol)
6600{
6601 dbg_printk(TPACPI_DBG_MIXER,
6602 "trying to set volume level to %hu\n", vol);
6603 return volume_set_volume_ec(vol);
6604}
6605
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006606static void volume_alsa_notify_change(void)
6607{
6608 struct tpacpi_alsa_data *d;
6609
6610 if (alsa_card && alsa_card->private_data) {
6611 d = alsa_card->private_data;
6612 if (d->ctl_mute_id)
6613 snd_ctl_notify(alsa_card,
6614 SNDRV_CTL_EVENT_MASK_VALUE,
6615 d->ctl_mute_id);
6616 if (d->ctl_vol_id)
6617 snd_ctl_notify(alsa_card,
6618 SNDRV_CTL_EVENT_MASK_VALUE,
6619 d->ctl_vol_id);
6620 }
6621}
6622
6623static int volume_alsa_vol_info(struct snd_kcontrol *kcontrol,
6624 struct snd_ctl_elem_info *uinfo)
6625{
6626 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
6627 uinfo->count = 1;
6628 uinfo->value.integer.min = 0;
6629 uinfo->value.integer.max = TP_EC_VOLUME_MAX;
6630 return 0;
6631}
6632
6633static int volume_alsa_vol_get(struct snd_kcontrol *kcontrol,
6634 struct snd_ctl_elem_value *ucontrol)
6635{
6636 u8 s;
6637 int rc;
6638
6639 rc = volume_get_status(&s);
6640 if (rc < 0)
6641 return rc;
6642
6643 ucontrol->value.integer.value[0] = s & TP_EC_AUDIO_LVL_MSK;
6644 return 0;
6645}
6646
6647static int volume_alsa_vol_put(struct snd_kcontrol *kcontrol,
6648 struct snd_ctl_elem_value *ucontrol)
6649{
6650 return volume_set_volume(ucontrol->value.integer.value[0]);
6651}
6652
6653#define volume_alsa_mute_info snd_ctl_boolean_mono_info
6654
6655static int volume_alsa_mute_get(struct snd_kcontrol *kcontrol,
6656 struct snd_ctl_elem_value *ucontrol)
6657{
6658 u8 s;
6659 int rc;
6660
6661 rc = volume_get_status(&s);
6662 if (rc < 0)
6663 return rc;
6664
6665 ucontrol->value.integer.value[0] =
6666 (s & TP_EC_AUDIO_MUTESW_MSK) ? 0 : 1;
6667 return 0;
6668}
6669
6670static int volume_alsa_mute_put(struct snd_kcontrol *kcontrol,
6671 struct snd_ctl_elem_value *ucontrol)
6672{
6673 return volume_set_mute(!ucontrol->value.integer.value[0]);
6674}
6675
6676static struct snd_kcontrol_new volume_alsa_control_vol __devinitdata = {
6677 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6678 .name = "Console Playback Volume",
6679 .index = 0,
6680 .access = SNDRV_CTL_ELEM_ACCESS_READ,
6681 .info = volume_alsa_vol_info,
6682 .get = volume_alsa_vol_get,
6683};
6684
6685static struct snd_kcontrol_new volume_alsa_control_mute __devinitdata = {
6686 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6687 .name = "Console Playback Switch",
6688 .index = 0,
6689 .access = SNDRV_CTL_ELEM_ACCESS_READ,
6690 .info = volume_alsa_mute_info,
6691 .get = volume_alsa_mute_get,
6692};
6693
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006694static void volume_suspend(pm_message_t state)
6695{
6696 tpacpi_volume_checkpoint_nvram();
6697}
6698
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006699static void volume_resume(void)
6700{
6701 volume_alsa_notify_change();
6702}
6703
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006704static void volume_shutdown(void)
6705{
6706 tpacpi_volume_checkpoint_nvram();
6707}
6708
6709static void volume_exit(void)
6710{
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006711 if (alsa_card) {
6712 snd_card_free(alsa_card);
6713 alsa_card = NULL;
6714 }
6715
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006716 tpacpi_volume_checkpoint_nvram();
6717}
6718
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006719static int __init volume_create_alsa_mixer(void)
6720{
6721 struct snd_card *card;
6722 struct tpacpi_alsa_data *data;
6723 struct snd_kcontrol *ctl_vol;
6724 struct snd_kcontrol *ctl_mute;
6725 int rc;
6726
6727 rc = snd_card_create(alsa_index, alsa_id, THIS_MODULE,
6728 sizeof(struct tpacpi_alsa_data), &card);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02006729 if (rc < 0 || !card) {
6730 printk(TPACPI_ERR
6731 "Failed to create ALSA card structures: %d\n", rc);
6732 return 1;
6733 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006734
6735 BUG_ON(!card->private_data);
6736 data = card->private_data;
6737 data->card = card;
6738
6739 strlcpy(card->driver, TPACPI_ALSA_DRVNAME,
6740 sizeof(card->driver));
6741 strlcpy(card->shortname, TPACPI_ALSA_SHRTNAME,
6742 sizeof(card->shortname));
6743 snprintf(card->mixername, sizeof(card->mixername), "ThinkPad EC %s",
6744 (thinkpad_id.ec_version_str) ?
6745 thinkpad_id.ec_version_str : "(unknown)");
6746 snprintf(card->longname, sizeof(card->longname),
6747 "%s at EC reg 0x%02x, fw %s", card->shortname, TP_EC_AUDIO,
6748 (thinkpad_id.ec_version_str) ?
6749 thinkpad_id.ec_version_str : "unknown");
6750
6751 if (volume_control_allowed) {
6752 volume_alsa_control_vol.put = volume_alsa_vol_put;
6753 volume_alsa_control_vol.access =
6754 SNDRV_CTL_ELEM_ACCESS_READWRITE;
6755
6756 volume_alsa_control_mute.put = volume_alsa_mute_put;
6757 volume_alsa_control_mute.access =
6758 SNDRV_CTL_ELEM_ACCESS_READWRITE;
6759 }
6760
6761 if (!tp_features.mixer_no_level_control) {
6762 ctl_vol = snd_ctl_new1(&volume_alsa_control_vol, NULL);
6763 rc = snd_ctl_add(card, ctl_vol);
6764 if (rc < 0) {
6765 printk(TPACPI_ERR
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02006766 "Failed to create ALSA volume control: %d\n",
6767 rc);
6768 goto err_exit;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006769 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006770 data->ctl_vol_id = &ctl_vol->id;
6771 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006772
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006773 ctl_mute = snd_ctl_new1(&volume_alsa_control_mute, NULL);
6774 rc = snd_ctl_add(card, ctl_mute);
6775 if (rc < 0) {
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02006776 printk(TPACPI_ERR "Failed to create ALSA mute control: %d\n",
6777 rc);
6778 goto err_exit;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006779 }
6780 data->ctl_mute_id = &ctl_mute->id;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006781
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006782 snd_card_set_dev(card, &tpacpi_pdev->dev);
6783 rc = snd_card_register(card);
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006784 if (rc < 0) {
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02006785 printk(TPACPI_ERR "Failed to register ALSA card: %d\n", rc);
6786 goto err_exit;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006787 }
6788
6789 alsa_card = card;
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02006790 return 0;
6791
6792err_exit:
6793 snd_card_free(card);
6794 return 1;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006795}
6796
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006797#define TPACPI_VOL_Q_MUTEONLY 0x0001 /* Mute-only control available */
6798#define TPACPI_VOL_Q_LEVEL 0x0002 /* Volume control available */
6799
6800static const struct tpacpi_quirk volume_quirk_table[] __initconst = {
6801 /* Whitelist volume level on all IBM by default */
6802 { .vendor = PCI_VENDOR_ID_IBM,
6803 .bios = TPACPI_MATCH_ANY,
6804 .ec = TPACPI_MATCH_ANY,
6805 .quirks = TPACPI_VOL_Q_LEVEL },
6806
6807 /* Lenovo models with volume control (needs confirmation) */
6808 TPACPI_QEC_LNV('7', 'C', TPACPI_VOL_Q_LEVEL), /* R60/i */
6809 TPACPI_QEC_LNV('7', 'E', TPACPI_VOL_Q_LEVEL), /* R60e/i */
6810 TPACPI_QEC_LNV('7', '9', TPACPI_VOL_Q_LEVEL), /* T60/p */
6811 TPACPI_QEC_LNV('7', 'B', TPACPI_VOL_Q_LEVEL), /* X60/s */
6812 TPACPI_QEC_LNV('7', 'J', TPACPI_VOL_Q_LEVEL), /* X60t */
6813 TPACPI_QEC_LNV('7', '7', TPACPI_VOL_Q_LEVEL), /* Z60 */
6814 TPACPI_QEC_LNV('7', 'F', TPACPI_VOL_Q_LEVEL), /* Z61 */
6815
6816 /* Whitelist mute-only on all Lenovo by default */
6817 { .vendor = PCI_VENDOR_ID_LENOVO,
6818 .bios = TPACPI_MATCH_ANY,
6819 .ec = TPACPI_MATCH_ANY,
6820 .quirks = TPACPI_VOL_Q_MUTEONLY }
6821};
6822
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006823static int __init volume_init(struct ibm_init_struct *iibm)
6824{
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006825 unsigned long quirks;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006826 int rc;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006827
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006828 vdbg_printk(TPACPI_DBG_INIT, "initializing volume subdriver\n");
6829
6830 mutex_init(&volume_mutex);
6831
6832 /*
6833 * Check for module parameter bogosity, note that we
6834 * init volume_mode to TPACPI_VOL_MODE_MAX in order to be
6835 * able to detect "unspecified"
6836 */
6837 if (volume_mode > TPACPI_VOL_MODE_MAX)
6838 return -EINVAL;
6839
6840 if (volume_mode == TPACPI_VOL_MODE_UCMS_STEP) {
6841 printk(TPACPI_ERR
6842 "UCMS step volume mode not implemented, "
6843 "please contact %s\n", TPACPI_MAIL);
6844 return 1;
6845 }
6846
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006847 if (volume_capabilities >= TPACPI_VOL_CAP_MAX)
6848 return -EINVAL;
6849
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006850 /*
6851 * The ALSA mixer is our primary interface.
6852 * When disabled, don't install the subdriver at all
6853 */
6854 if (!alsa_enable) {
6855 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6856 "ALSA mixer disabled by parameter, "
6857 "not loading volume subdriver...\n");
6858 return 1;
6859 }
6860
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006861 quirks = tpacpi_check_quirks(volume_quirk_table,
6862 ARRAY_SIZE(volume_quirk_table));
6863
6864 switch (volume_capabilities) {
6865 case TPACPI_VOL_CAP_AUTO:
6866 if (quirks & TPACPI_VOL_Q_MUTEONLY)
6867 tp_features.mixer_no_level_control = 1;
6868 else if (quirks & TPACPI_VOL_Q_LEVEL)
6869 tp_features.mixer_no_level_control = 0;
6870 else
6871 return 1; /* no mixer */
6872 break;
6873 case TPACPI_VOL_CAP_VOLMUTE:
6874 tp_features.mixer_no_level_control = 0;
6875 break;
6876 case TPACPI_VOL_CAP_MUTEONLY:
6877 tp_features.mixer_no_level_control = 1;
6878 break;
6879 default:
6880 return 1;
6881 }
6882
6883 if (volume_capabilities != TPACPI_VOL_CAP_AUTO)
6884 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6885 "using user-supplied volume_capabilities=%d\n",
6886 volume_capabilities);
6887
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006888 if (volume_mode == TPACPI_VOL_MODE_AUTO ||
6889 volume_mode == TPACPI_VOL_MODE_MAX) {
6890 volume_mode = TPACPI_VOL_MODE_ECNVRAM;
6891
6892 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6893 "driver auto-selected volume_mode=%d\n",
6894 volume_mode);
6895 } else {
6896 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6897 "using user-supplied volume_mode=%d\n",
6898 volume_mode);
6899 }
6900
6901 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006902 "mute is supported, volume control is %s\n",
6903 str_supported(!tp_features.mixer_no_level_control));
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006904
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006905 rc = volume_create_alsa_mixer();
6906 if (rc) {
6907 printk(TPACPI_ERR
6908 "Could not create the ALSA mixer interface\n");
6909 return rc;
6910 }
6911
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006912 printk(TPACPI_INFO
6913 "Console audio control enabled, mode: %s\n",
6914 (volume_control_allowed) ?
6915 "override (read/write)" :
6916 "monitor (read only)");
6917
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006918 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6919 "registering volume hotkeys as change notification\n");
6920 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6921 | TP_ACPI_HKEY_VOLUP_MASK
6922 | TP_ACPI_HKEY_VOLDWN_MASK
6923 | TP_ACPI_HKEY_MUTE_MASK);
6924
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006925 return 0;
6926}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006927
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006928static int volume_read(struct seq_file *m)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006929{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006930 u8 status;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006931
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006932 if (volume_get_status(&status) < 0) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006933 seq_printf(m, "level:\t\tunreadable\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006934 } else {
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006935 if (tp_features.mixer_no_level_control)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006936 seq_printf(m, "level:\t\tunsupported\n");
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006937 else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006938 seq_printf(m, "level:\t\t%d\n",
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006939 status & TP_EC_AUDIO_LVL_MSK);
6940
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006941 seq_printf(m, "mute:\t\t%s\n",
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006942 onoff(status, TP_EC_AUDIO_MUTESW));
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006943
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006944 if (volume_control_allowed) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006945 seq_printf(m, "commands:\tunmute, mute\n");
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006946 if (!tp_features.mixer_no_level_control) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006947 seq_printf(m,
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006948 "commands:\tup, down\n");
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006949 seq_printf(m,
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006950 "commands:\tlevel <level>"
6951 " (<level> is 0-%d)\n",
6952 TP_EC_VOLUME_MAX);
6953 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006954 }
6955 }
6956
6957 return 0;
6958}
6959
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006960static int volume_write(char *buf)
6961{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006962 u8 s;
6963 u8 new_level, new_mute;
6964 int l;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006965 char *cmd;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006966 int rc;
6967
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006968 /*
6969 * We do allow volume control at driver startup, so that the
6970 * user can set initial state through the volume=... parameter hack.
6971 */
6972 if (!volume_control_allowed && tpacpi_lifecycle != TPACPI_LIFE_INIT) {
6973 if (unlikely(!tp_warned.volume_ctrl_forbidden)) {
6974 tp_warned.volume_ctrl_forbidden = 1;
6975 printk(TPACPI_NOTICE
6976 "Console audio control in monitor mode, "
6977 "changes are not allowed.\n");
6978 printk(TPACPI_NOTICE
6979 "Use the volume_control=1 module parameter "
6980 "to enable volume control\n");
6981 }
6982 return -EPERM;
6983 }
6984
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006985 rc = volume_get_status(&s);
6986 if (rc < 0)
6987 return rc;
6988
6989 new_level = s & TP_EC_AUDIO_LVL_MSK;
6990 new_mute = s & TP_EC_AUDIO_MUTESW_MSK;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006991
6992 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006993 if (!tp_features.mixer_no_level_control) {
6994 if (strlencmp(cmd, "up") == 0) {
6995 if (new_mute)
6996 new_mute = 0;
6997 else if (new_level < TP_EC_VOLUME_MAX)
6998 new_level++;
6999 continue;
7000 } else if (strlencmp(cmd, "down") == 0) {
7001 if (new_mute)
7002 new_mute = 0;
7003 else if (new_level > 0)
7004 new_level--;
7005 continue;
7006 } else if (sscanf(cmd, "level %u", &l) == 1 &&
7007 l >= 0 && l <= TP_EC_VOLUME_MAX) {
7008 new_level = l;
7009 continue;
7010 }
7011 }
7012 if (strlencmp(cmd, "mute") == 0)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007013 new_mute = TP_EC_AUDIO_MUTESW_MSK;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007014 else if (strlencmp(cmd, "unmute") == 0)
7015 new_mute = 0;
7016 else
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007017 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007018 }
7019
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007020 if (tp_features.mixer_no_level_control) {
7021 tpacpi_disclose_usertask("procfs volume", "%smute\n",
7022 new_mute ? "" : "un");
7023 rc = volume_set_mute(!!new_mute);
7024 } else {
7025 tpacpi_disclose_usertask("procfs volume",
7026 "%smute and set level to %d\n",
7027 new_mute ? "" : "un", new_level);
7028 rc = volume_set_status(new_mute | new_level);
7029 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007030 volume_alsa_notify_change();
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007031
7032 return (rc == -EINTR) ? -ERESTARTSYS : rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007033}
7034
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007035static struct ibm_struct volume_driver_data = {
7036 .name = "volume",
7037 .read = volume_read,
7038 .write = volume_write,
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007039 .exit = volume_exit,
7040 .suspend = volume_suspend,
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007041 .resume = volume_resume,
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007042 .shutdown = volume_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007043};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007044
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02007045#else /* !CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7046
7047#define alsa_card NULL
7048
7049static void inline volume_alsa_notify_change(void)
7050{
7051}
7052
7053static int __init volume_init(struct ibm_init_struct *iibm)
7054{
7055 printk(TPACPI_INFO
7056 "volume: disabled as there is no ALSA support in this kernel\n");
7057
7058 return 1;
7059}
7060
7061static struct ibm_struct volume_driver_data = {
7062 .name = "volume",
7063};
7064
7065#endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7066
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007067/*************************************************************************
7068 * Fan subdriver
7069 */
7070
7071/*
7072 * FAN ACCESS MODES
7073 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007074 * TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007075 * ACPI GFAN method: returns fan level
7076 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007077 * see TPACPI_FAN_WR_ACPI_SFAN
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007078 * EC 0x2f (HFSP) not available if GFAN exists
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007079 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007080 * TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007081 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
7082 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007083 * EC 0x2f (HFSP) might be available *for reading*, but do not use
7084 * it for writing.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007085 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007086 * TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007087 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
7088 * Supported on almost all ThinkPads
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007089 *
7090 * Fan speed changes of any sort (including those caused by the
7091 * disengaged mode) are usually done slowly by the firmware as the
7092 * maximum ammount of fan duty cycle change per second seems to be
7093 * limited.
7094 *
7095 * Reading is not available if GFAN exists.
7096 * Writing is not available if SFAN exists.
7097 *
7098 * Bits
7099 * 7 automatic mode engaged;
7100 * (default operation mode of the ThinkPad)
7101 * fan level is ignored in this mode.
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007102 * 6 full speed mode (takes precedence over bit 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007103 * not available on all thinkpads. May disable
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007104 * the tachometer while the fan controller ramps up
7105 * the speed (which can take up to a few *minutes*).
7106 * Speeds up fan to 100% duty-cycle, which is far above
7107 * the standard RPM levels. It is not impossible that
7108 * it could cause hardware damage.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007109 * 5-3 unused in some models. Extra bits for fan level
7110 * in others, but still useless as all values above
7111 * 7 map to the same speed as level 7 in these models.
7112 * 2-0 fan level (0..7 usually)
7113 * 0x00 = stop
7114 * 0x07 = max (set when temperatures critical)
7115 * Some ThinkPads may have other levels, see
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007116 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007117 *
7118 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
7119 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
7120 * does so, its initial value is meaningless (0x07).
7121 *
7122 * For firmware bugs, refer to:
7123 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7124 *
7125 * ----
7126 *
7127 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
7128 * Main fan tachometer reading (in RPM)
7129 *
7130 * This register is present on all ThinkPads with a new-style EC, and
7131 * it is known not to be present on the A21m/e, and T22, as there is
7132 * something else in offset 0x84 according to the ACPI DSDT. Other
7133 * ThinkPads from this same time period (and earlier) probably lack the
7134 * tachometer as well.
7135 *
Nick Andrew877d0312009-01-26 11:06:57 +01007136 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007137 * was never fixed by IBM to report the EC firmware version string
7138 * probably support the tachometer (like the early X models), so
7139 * detecting it is quite hard. We need more data to know for sure.
7140 *
7141 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
7142 * might result.
7143 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007144 * FIRMWARE BUG: may go stale while the EC is switching to full speed
7145 * mode.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007146 *
7147 * For firmware bugs, refer to:
7148 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7149 *
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007150 * ----
7151 *
7152 * ThinkPad EC register 0x31 bit 0 (only on select models)
7153 *
7154 * When bit 0 of EC register 0x31 is zero, the tachometer registers
7155 * show the speed of the main fan. When bit 0 of EC register 0x31
7156 * is one, the tachometer registers show the speed of the auxiliary
7157 * fan.
7158 *
7159 * Fan control seems to affect both fans, regardless of the state
7160 * of this bit.
7161 *
7162 * So far, only the firmware for the X60/X61 non-tablet versions
7163 * seem to support this (firmware TP-7M).
7164 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007165 * TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007166 * ThinkPad X31, X40, X41. Not available in the X60.
7167 *
7168 * FANS ACPI handle: takes three arguments: low speed, medium speed,
7169 * high speed. ACPI DSDT seems to map these three speeds to levels
7170 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
7171 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
7172 *
7173 * The speeds are stored on handles
7174 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
7175 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007176 * There are three default speed sets, accessible as handles:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007177 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
7178 *
7179 * ACPI DSDT switches which set is in use depending on various
7180 * factors.
7181 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007182 * TPACPI_FAN_WR_TPEC is also available and should be used to
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007183 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
7184 * but the ACPI tables just mention level 7.
7185 */
7186
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007187enum { /* Fan control constants */
7188 fan_status_offset = 0x2f, /* EC register 0x2f */
7189 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
7190 * 0x84 must be read before 0x85 */
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007191 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
7192 bit 0 selects which fan is active */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007193
7194 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
7195 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
7196
7197 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
7198};
7199
7200enum fan_status_access_mode {
7201 TPACPI_FAN_NONE = 0, /* No fan status or control */
7202 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
7203 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
7204};
7205
7206enum fan_control_access_mode {
7207 TPACPI_FAN_WR_NONE = 0, /* No fan control */
7208 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
7209 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
7210 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
7211};
7212
7213enum fan_control_commands {
7214 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
7215 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
7216 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
7217 * and also watchdog cmd */
7218};
7219
7220static int fan_control_allowed;
7221
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007222static enum fan_status_access_mode fan_status_access_mode;
7223static enum fan_control_access_mode fan_control_access_mode;
7224static enum fan_control_commands fan_control_commands;
7225
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02007226static u8 fan_control_initial_status;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007227static u8 fan_control_desired_level;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007228static u8 fan_control_resume_level;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007229static int fan_watchdog_maxinterval;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007230
7231static struct mutex fan_mutex;
7232
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007233static void fan_watchdog_fire(struct work_struct *ignored);
Len Brown25c68a32006-12-08 04:43:41 -05007234static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007235
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007236TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
7237TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007238 "\\FSPD", /* 600e/x, 770e, 770x */
7239 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007240TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007241 "JFNS", /* 770x-JL */
7242 ); /* all others */
7243
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007244/*
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007245 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
7246 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
7247 * be in auto mode (0x80).
7248 *
7249 * This is corrected by any write to HFSP either by the driver, or
7250 * by the firmware.
7251 *
7252 * We assume 0x07 really means auto mode while this quirk is active,
7253 * as this is far more likely than the ThinkPad being in level 7,
7254 * which is only used by the firmware during thermal emergencies.
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007255 *
7256 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
7257 * TP-70 (T43, R52), which are known to be buggy.
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007258 */
7259
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007260static void fan_quirk1_setup(void)
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007261{
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007262 if (fan_control_initial_status == 0x07) {
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007263 printk(TPACPI_NOTICE
7264 "fan_init: initial fan status is unknown, "
7265 "assuming it is in auto mode\n");
7266 tp_features.fan_ctrl_status_undef = 1;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007267 }
7268}
7269
7270static void fan_quirk1_handle(u8 *fan_status)
7271{
7272 if (unlikely(tp_features.fan_ctrl_status_undef)) {
7273 if (*fan_status != fan_control_initial_status) {
7274 /* something changed the HFSP regisnter since
7275 * driver init time, so it is not undefined
7276 * anymore */
7277 tp_features.fan_ctrl_status_undef = 0;
7278 } else {
7279 /* Return most likely status. In fact, it
7280 * might be the only possible status */
7281 *fan_status = TP_EC_FAN_AUTO;
7282 }
7283 }
7284}
7285
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007286/* Select main fan on X60/X61, NOOP on others */
7287static bool fan_select_fan1(void)
7288{
7289 if (tp_features.second_fan) {
7290 u8 val;
7291
7292 if (ec_read(fan_select_offset, &val) < 0)
7293 return false;
7294 val &= 0xFEU;
7295 if (ec_write(fan_select_offset, val) < 0)
7296 return false;
7297 }
7298 return true;
7299}
7300
7301/* Select secondary fan on X60/X61 */
7302static bool fan_select_fan2(void)
7303{
7304 u8 val;
7305
7306 if (!tp_features.second_fan)
7307 return false;
7308
7309 if (ec_read(fan_select_offset, &val) < 0)
7310 return false;
7311 val |= 0x01U;
7312 if (ec_write(fan_select_offset, val) < 0)
7313 return false;
7314
7315 return true;
7316}
7317
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007318/*
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007319 * Call with fan_mutex held
7320 */
7321static void fan_update_desired_level(u8 status)
7322{
7323 if ((status &
7324 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7325 if (status > 7)
7326 fan_control_desired_level = 7;
7327 else
7328 fan_control_desired_level = status;
7329 }
7330}
7331
7332static int fan_get_status(u8 *status)
7333{
7334 u8 s;
7335
7336 /* TODO:
7337 * Add TPACPI_FAN_RD_ACPI_FANS ? */
7338
7339 switch (fan_status_access_mode) {
7340 case TPACPI_FAN_RD_ACPI_GFAN:
7341 /* 570, 600e/x, 770e, 770x */
7342
7343 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
7344 return -EIO;
7345
7346 if (likely(status))
7347 *status = s & 0x07;
7348
7349 break;
7350
7351 case TPACPI_FAN_RD_TPEC:
7352 /* all except 570, 600e/x, 770e, 770x */
7353 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
7354 return -EIO;
7355
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007356 if (likely(status)) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007357 *status = s;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007358 fan_quirk1_handle(status);
7359 }
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007360
7361 break;
7362
7363 default:
7364 return -ENXIO;
7365 }
7366
7367 return 0;
7368}
7369
7370static int fan_get_status_safe(u8 *status)
7371{
7372 int rc;
7373 u8 s;
7374
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007375 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007376 return -ERESTARTSYS;
7377 rc = fan_get_status(&s);
7378 if (!rc)
7379 fan_update_desired_level(s);
7380 mutex_unlock(&fan_mutex);
7381
7382 if (status)
7383 *status = s;
7384
7385 return rc;
7386}
7387
7388static int fan_get_speed(unsigned int *speed)
7389{
7390 u8 hi, lo;
7391
7392 switch (fan_status_access_mode) {
7393 case TPACPI_FAN_RD_TPEC:
7394 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007395 if (unlikely(!fan_select_fan1()))
7396 return -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007397 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
7398 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
7399 return -EIO;
7400
7401 if (likely(speed))
7402 *speed = (hi << 8) | lo;
7403
7404 break;
7405
7406 default:
7407 return -ENXIO;
7408 }
7409
7410 return 0;
7411}
7412
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007413static int fan2_get_speed(unsigned int *speed)
7414{
7415 u8 hi, lo;
7416 bool rc;
7417
7418 switch (fan_status_access_mode) {
7419 case TPACPI_FAN_RD_TPEC:
7420 /* all except 570, 600e/x, 770e, 770x */
7421 if (unlikely(!fan_select_fan2()))
7422 return -EIO;
7423 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
7424 !acpi_ec_read(fan_rpm_offset + 1, &hi);
7425 fan_select_fan1(); /* play it safe */
7426 if (rc)
7427 return -EIO;
7428
7429 if (likely(speed))
7430 *speed = (hi << 8) | lo;
7431
7432 break;
7433
7434 default:
7435 return -ENXIO;
7436 }
7437
7438 return 0;
7439}
7440
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007441static int fan_set_level(int level)
7442{
7443 if (!fan_control_allowed)
7444 return -EPERM;
7445
7446 switch (fan_control_access_mode) {
7447 case TPACPI_FAN_WR_ACPI_SFAN:
7448 if (level >= 0 && level <= 7) {
7449 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
7450 return -EIO;
7451 } else
7452 return -EINVAL;
7453 break;
7454
7455 case TPACPI_FAN_WR_ACPI_FANS:
7456 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007457 if (!(level & TP_EC_FAN_AUTO) &&
7458 !(level & TP_EC_FAN_FULLSPEED) &&
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007459 ((level < 0) || (level > 7)))
7460 return -EINVAL;
7461
7462 /* safety net should the EC not support AUTO
7463 * or FULLSPEED mode bits and just ignore them */
7464 if (level & TP_EC_FAN_FULLSPEED)
7465 level |= 7; /* safety min speed 7 */
Roel Kluin547266e2008-02-05 00:24:56 +01007466 else if (level & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007467 level |= 4; /* safety min speed 4 */
7468
7469 if (!acpi_ec_write(fan_status_offset, level))
7470 return -EIO;
7471 else
7472 tp_features.fan_ctrl_status_undef = 0;
7473 break;
7474
7475 default:
7476 return -ENXIO;
7477 }
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007478
7479 vdbg_printk(TPACPI_DBG_FAN,
7480 "fan control: set fan control register to 0x%02x\n", level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007481 return 0;
7482}
7483
7484static int fan_set_level_safe(int level)
7485{
7486 int rc;
7487
7488 if (!fan_control_allowed)
7489 return -EPERM;
7490
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007491 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007492 return -ERESTARTSYS;
7493
7494 if (level == TPACPI_FAN_LAST_LEVEL)
7495 level = fan_control_desired_level;
7496
7497 rc = fan_set_level(level);
7498 if (!rc)
7499 fan_update_desired_level(level);
7500
7501 mutex_unlock(&fan_mutex);
7502 return rc;
7503}
7504
7505static int fan_set_enable(void)
7506{
7507 u8 s;
7508 int rc;
7509
7510 if (!fan_control_allowed)
7511 return -EPERM;
7512
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007513 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007514 return -ERESTARTSYS;
7515
7516 switch (fan_control_access_mode) {
7517 case TPACPI_FAN_WR_ACPI_FANS:
7518 case TPACPI_FAN_WR_TPEC:
7519 rc = fan_get_status(&s);
7520 if (rc < 0)
7521 break;
7522
7523 /* Don't go out of emergency fan mode */
7524 if (s != 7) {
7525 s &= 0x07;
7526 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
7527 }
7528
7529 if (!acpi_ec_write(fan_status_offset, s))
7530 rc = -EIO;
7531 else {
7532 tp_features.fan_ctrl_status_undef = 0;
7533 rc = 0;
7534 }
7535 break;
7536
7537 case TPACPI_FAN_WR_ACPI_SFAN:
7538 rc = fan_get_status(&s);
7539 if (rc < 0)
7540 break;
7541
7542 s &= 0x07;
7543
7544 /* Set fan to at least level 4 */
7545 s |= 4;
7546
7547 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007548 rc = -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007549 else
7550 rc = 0;
7551 break;
7552
7553 default:
7554 rc = -ENXIO;
7555 }
7556
7557 mutex_unlock(&fan_mutex);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007558
7559 if (!rc)
7560 vdbg_printk(TPACPI_DBG_FAN,
7561 "fan control: set fan control register to 0x%02x\n",
7562 s);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007563 return rc;
7564}
7565
7566static int fan_set_disable(void)
7567{
7568 int rc;
7569
7570 if (!fan_control_allowed)
7571 return -EPERM;
7572
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007573 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007574 return -ERESTARTSYS;
7575
7576 rc = 0;
7577 switch (fan_control_access_mode) {
7578 case TPACPI_FAN_WR_ACPI_FANS:
7579 case TPACPI_FAN_WR_TPEC:
7580 if (!acpi_ec_write(fan_status_offset, 0x00))
7581 rc = -EIO;
7582 else {
7583 fan_control_desired_level = 0;
7584 tp_features.fan_ctrl_status_undef = 0;
7585 }
7586 break;
7587
7588 case TPACPI_FAN_WR_ACPI_SFAN:
7589 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
7590 rc = -EIO;
7591 else
7592 fan_control_desired_level = 0;
7593 break;
7594
7595 default:
7596 rc = -ENXIO;
7597 }
7598
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007599 if (!rc)
7600 vdbg_printk(TPACPI_DBG_FAN,
7601 "fan control: set fan control register to 0\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007602
7603 mutex_unlock(&fan_mutex);
7604 return rc;
7605}
7606
7607static int fan_set_speed(int speed)
7608{
7609 int rc;
7610
7611 if (!fan_control_allowed)
7612 return -EPERM;
7613
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007614 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007615 return -ERESTARTSYS;
7616
7617 rc = 0;
7618 switch (fan_control_access_mode) {
7619 case TPACPI_FAN_WR_ACPI_FANS:
7620 if (speed >= 0 && speed <= 65535) {
7621 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
7622 speed, speed, speed))
7623 rc = -EIO;
7624 } else
7625 rc = -EINVAL;
7626 break;
7627
7628 default:
7629 rc = -ENXIO;
7630 }
7631
7632 mutex_unlock(&fan_mutex);
7633 return rc;
7634}
7635
7636static void fan_watchdog_reset(void)
7637{
7638 static int fan_watchdog_active;
7639
7640 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
7641 return;
7642
7643 if (fan_watchdog_active)
7644 cancel_delayed_work(&fan_watchdog_task);
7645
7646 if (fan_watchdog_maxinterval > 0 &&
7647 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
7648 fan_watchdog_active = 1;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03007649 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007650 msecs_to_jiffies(fan_watchdog_maxinterval
7651 * 1000))) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007652 printk(TPACPI_ERR
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03007653 "failed to queue the fan watchdog, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007654 "watchdog will not trigger\n");
7655 }
7656 } else
7657 fan_watchdog_active = 0;
7658}
7659
7660static void fan_watchdog_fire(struct work_struct *ignored)
7661{
7662 int rc;
7663
7664 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
7665 return;
7666
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007667 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007668 rc = fan_set_enable();
7669 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007670 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007671 "will try again later...\n", -rc);
7672 /* reschedule for later */
7673 fan_watchdog_reset();
7674 }
7675}
7676
7677/*
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007678 * SYSFS fan layout: hwmon compatible (device)
7679 *
7680 * pwm*_enable:
7681 * 0: "disengaged" mode
7682 * 1: manual mode
7683 * 2: native EC "auto" mode (recommended, hardware default)
7684 *
7685 * pwm*: set speed in manual mode, ignored otherwise.
7686 * 0 is level 0; 255 is level 7. Intermediate points done with linear
7687 * interpolation.
7688 *
7689 * fan*_input: tachometer reading, RPM
7690 *
7691 *
7692 * SYSFS fan layout: extensions
7693 *
7694 * fan_watchdog (driver):
7695 * fan watchdog interval in seconds, 0 disables (default), max 120
7696 */
7697
7698/* sysfs fan pwm1_enable ----------------------------------------------- */
7699static ssize_t fan_pwm1_enable_show(struct device *dev,
7700 struct device_attribute *attr,
7701 char *buf)
7702{
7703 int res, mode;
7704 u8 status;
7705
7706 res = fan_get_status_safe(&status);
7707 if (res)
7708 return res;
7709
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007710 if (status & TP_EC_FAN_FULLSPEED) {
7711 mode = 0;
7712 } else if (status & TP_EC_FAN_AUTO) {
7713 mode = 2;
7714 } else
7715 mode = 1;
7716
7717 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
7718}
7719
7720static ssize_t fan_pwm1_enable_store(struct device *dev,
7721 struct device_attribute *attr,
7722 const char *buf, size_t count)
7723{
7724 unsigned long t;
7725 int res, level;
7726
7727 if (parse_strtoul(buf, 2, &t))
7728 return -EINVAL;
7729
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007730 tpacpi_disclose_usertask("hwmon pwm1_enable",
7731 "set fan mode to %lu\n", t);
7732
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007733 switch (t) {
7734 case 0:
7735 level = TP_EC_FAN_FULLSPEED;
7736 break;
7737 case 1:
7738 level = TPACPI_FAN_LAST_LEVEL;
7739 break;
7740 case 2:
7741 level = TP_EC_FAN_AUTO;
7742 break;
7743 case 3:
7744 /* reserved for software-controlled auto mode */
7745 return -ENOSYS;
7746 default:
7747 return -EINVAL;
7748 }
7749
7750 res = fan_set_level_safe(level);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03007751 if (res == -ENXIO)
7752 return -EINVAL;
7753 else if (res < 0)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007754 return res;
7755
7756 fan_watchdog_reset();
7757
7758 return count;
7759}
7760
7761static struct device_attribute dev_attr_fan_pwm1_enable =
7762 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
7763 fan_pwm1_enable_show, fan_pwm1_enable_store);
7764
7765/* sysfs fan pwm1 ------------------------------------------------------ */
7766static ssize_t fan_pwm1_show(struct device *dev,
7767 struct device_attribute *attr,
7768 char *buf)
7769{
7770 int res;
7771 u8 status;
7772
7773 res = fan_get_status_safe(&status);
7774 if (res)
7775 return res;
7776
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007777 if ((status &
7778 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
7779 status = fan_control_desired_level;
7780
7781 if (status > 7)
7782 status = 7;
7783
7784 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
7785}
7786
7787static ssize_t fan_pwm1_store(struct device *dev,
7788 struct device_attribute *attr,
7789 const char *buf, size_t count)
7790{
7791 unsigned long s;
7792 int rc;
7793 u8 status, newlevel;
7794
7795 if (parse_strtoul(buf, 255, &s))
7796 return -EINVAL;
7797
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007798 tpacpi_disclose_usertask("hwmon pwm1",
7799 "set fan speed to %lu\n", s);
7800
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007801 /* scale down from 0-255 to 0-7 */
7802 newlevel = (s >> 5) & 0x07;
7803
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007804 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02007805 return -ERESTARTSYS;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007806
7807 rc = fan_get_status(&status);
7808 if (!rc && (status &
7809 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7810 rc = fan_set_level(newlevel);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03007811 if (rc == -ENXIO)
7812 rc = -EINVAL;
7813 else if (!rc) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007814 fan_update_desired_level(newlevel);
Henrique de Moraes Holschuhca4ac2f2007-04-27 22:00:11 -03007815 fan_watchdog_reset();
7816 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007817 }
7818
7819 mutex_unlock(&fan_mutex);
7820 return (rc)? rc : count;
7821}
7822
7823static struct device_attribute dev_attr_fan_pwm1 =
7824 __ATTR(pwm1, S_IWUSR | S_IRUGO,
7825 fan_pwm1_show, fan_pwm1_store);
7826
7827/* sysfs fan fan1_input ------------------------------------------------ */
7828static ssize_t fan_fan1_input_show(struct device *dev,
7829 struct device_attribute *attr,
7830 char *buf)
7831{
7832 int res;
7833 unsigned int speed;
7834
7835 res = fan_get_speed(&speed);
7836 if (res < 0)
7837 return res;
7838
7839 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7840}
7841
7842static struct device_attribute dev_attr_fan_fan1_input =
7843 __ATTR(fan1_input, S_IRUGO,
7844 fan_fan1_input_show, NULL);
7845
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007846/* sysfs fan fan2_input ------------------------------------------------ */
7847static ssize_t fan_fan2_input_show(struct device *dev,
7848 struct device_attribute *attr,
7849 char *buf)
7850{
7851 int res;
7852 unsigned int speed;
7853
7854 res = fan2_get_speed(&speed);
7855 if (res < 0)
7856 return res;
7857
7858 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7859}
7860
7861static struct device_attribute dev_attr_fan_fan2_input =
7862 __ATTR(fan2_input, S_IRUGO,
7863 fan_fan2_input_show, NULL);
7864
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007865/* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007866static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
7867 char *buf)
7868{
7869 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
7870}
7871
7872static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
7873 const char *buf, size_t count)
7874{
7875 unsigned long t;
7876
7877 if (parse_strtoul(buf, 120, &t))
7878 return -EINVAL;
7879
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03007880 if (!fan_control_allowed)
7881 return -EPERM;
7882
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007883 fan_watchdog_maxinterval = t;
7884 fan_watchdog_reset();
7885
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007886 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
7887
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007888 return count;
7889}
7890
7891static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
7892 fan_fan_watchdog_show, fan_fan_watchdog_store);
7893
7894/* --------------------------------------------------------------------- */
7895static struct attribute *fan_attributes[] = {
7896 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
7897 &dev_attr_fan_fan1_input.attr,
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007898 NULL, /* for fan2_input */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007899 NULL
7900};
7901
7902static const struct attribute_group fan_attr_group = {
7903 .attrs = fan_attributes,
7904};
7905
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007906#define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
7907#define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007908
7909#define TPACPI_FAN_QI(__id1, __id2, __quirks) \
7910 { .vendor = PCI_VENDOR_ID_IBM, \
7911 .bios = TPACPI_MATCH_ANY, \
7912 .ec = TPID(__id1, __id2), \
7913 .quirks = __quirks }
7914
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007915#define TPACPI_FAN_QL(__id1, __id2, __quirks) \
7916 { .vendor = PCI_VENDOR_ID_LENOVO, \
7917 .bios = TPACPI_MATCH_ANY, \
7918 .ec = TPID(__id1, __id2), \
7919 .quirks = __quirks }
7920
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007921static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
7922 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
7923 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
7924 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
7925 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007926 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007927};
7928
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007929#undef TPACPI_FAN_QL
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007930#undef TPACPI_FAN_QI
7931
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007932static int __init fan_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007933{
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007934 int rc;
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007935 unsigned long quirks;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007936
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007937 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7938 "initializing fan subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007939
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03007940 mutex_init(&fan_mutex);
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007941 fan_status_access_mode = TPACPI_FAN_NONE;
7942 fan_control_access_mode = TPACPI_FAN_WR_NONE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007943 fan_control_commands = 0;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007944 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03007945 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007946 tp_features.second_fan = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007947 fan_control_desired_level = 7;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007948
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007949 TPACPI_ACPIHANDLE_INIT(fans);
7950 TPACPI_ACPIHANDLE_INIT(gfan);
7951 TPACPI_ACPIHANDLE_INIT(sfan);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007952
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007953 quirks = tpacpi_check_quirks(fan_quirk_table,
7954 ARRAY_SIZE(fan_quirk_table));
7955
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007956 if (gfan_handle) {
7957 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007958 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007959 } else {
7960 /* all other ThinkPads: note that even old-style
7961 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02007962 if (likely(acpi_ec_read(fan_status_offset,
7963 &fan_control_initial_status))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007964 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007965 if (quirks & TPACPI_FAN_Q1)
7966 fan_quirk1_setup();
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007967 if (quirks & TPACPI_FAN_2FAN) {
7968 tp_features.second_fan = 1;
7969 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7970 "secondary fan support enabled\n");
7971 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007972 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007973 printk(TPACPI_ERR
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007974 "ThinkPad ACPI EC access misbehaving, "
7975 "fan status and control unavailable\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007976 return 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007977 }
7978 }
7979
7980 if (sfan_handle) {
7981 /* 570, 770x-JL */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007982 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02007983 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007984 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007985 } else {
7986 if (!gfan_handle) {
7987 /* gfan without sfan means no fan control */
7988 /* all other models implement TP EC 0x2f control */
7989
7990 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02007991 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007992 fan_control_access_mode =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007993 TPACPI_FAN_WR_ACPI_FANS;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007994 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007995 TPACPI_FAN_CMD_SPEED |
7996 TPACPI_FAN_CMD_LEVEL |
7997 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007998 } else {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007999 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02008000 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008001 TPACPI_FAN_CMD_LEVEL |
8002 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008003 }
8004 }
8005 }
8006
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008007 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8008 "fan is %s, modes %d, %d\n",
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008009 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
8010 fan_control_access_mode != TPACPI_FAN_WR_NONE),
8011 fan_status_access_mode, fan_control_access_mode);
8012
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008013 /* fan control master switch */
8014 if (!fan_control_allowed) {
8015 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8016 fan_control_commands = 0;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008017 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008018 "fan control features disabled by parameter\n");
8019 }
8020
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008021 /* update fan_control_desired_level */
8022 if (fan_status_access_mode != TPACPI_FAN_NONE)
8023 fan_get_status_safe(NULL);
8024
8025 if (fan_status_access_mode != TPACPI_FAN_NONE ||
8026 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008027 if (tp_features.second_fan) {
8028 /* attach second fan tachometer */
8029 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
8030 &dev_attr_fan_fan2_input.attr;
8031 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008032 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008033 &fan_attr_group);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008034 if (rc < 0)
8035 return rc;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03008036
8037 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
8038 &driver_attr_fan_watchdog);
8039 if (rc < 0) {
8040 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
8041 &fan_attr_group);
8042 return rc;
8043 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008044 return 0;
8045 } else
8046 return 1;
8047}
8048
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008049static void fan_exit(void)
8050{
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008051 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008052 "cancelling any pending fan watchdog tasks\n");
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008053
8054 /* FIXME: can we really do this unconditionally? */
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008055 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008056 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
8057 &driver_attr_fan_watchdog);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008058
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008059 cancel_delayed_work(&fan_watchdog_task);
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03008060 flush_workqueue(tpacpi_wq);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008061}
8062
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008063static void fan_suspend(pm_message_t state)
8064{
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008065 int rc;
8066
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008067 if (!fan_control_allowed)
8068 return;
8069
8070 /* Store fan status in cache */
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008071 fan_control_resume_level = 0;
8072 rc = fan_get_status_safe(&fan_control_resume_level);
8073 if (rc < 0)
8074 printk(TPACPI_NOTICE
8075 "failed to read fan level for later "
8076 "restore during resume: %d\n", rc);
8077
8078 /* if it is undefined, don't attempt to restore it.
8079 * KEEP THIS LAST */
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008080 if (tp_features.fan_ctrl_status_undef)
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008081 fan_control_resume_level = 0;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008082}
8083
8084static void fan_resume(void)
8085{
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008086 u8 current_level = 7;
8087 bool do_set = false;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008088 int rc;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008089
8090 /* DSDT *always* updates status on resume */
8091 tp_features.fan_ctrl_status_undef = 0;
8092
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008093 if (!fan_control_allowed ||
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008094 !fan_control_resume_level ||
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008095 (fan_get_status_safe(&current_level) < 0))
8096 return;
8097
8098 switch (fan_control_access_mode) {
8099 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008100 /* never decrease fan level */
8101 do_set = (fan_control_resume_level > current_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008102 break;
8103 case TPACPI_FAN_WR_ACPI_FANS:
8104 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008105 /* never decrease fan level, scale is:
8106 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
8107 *
8108 * We expect the firmware to set either 7 or AUTO, but we
8109 * handle FULLSPEED out of paranoia.
8110 *
8111 * So, we can safely only restore FULLSPEED or 7, anything
8112 * else could slow the fan. Restoring AUTO is useless, at
8113 * best that's exactly what the DSDT already set (it is the
8114 * slower it uses).
8115 *
8116 * Always keep in mind that the DSDT *will* have set the
8117 * fans to what the vendor supposes is the best level. We
8118 * muck with it only to speed the fan up.
8119 */
8120 if (fan_control_resume_level != 7 &&
8121 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
8122 return;
8123 else
8124 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
8125 (current_level != fan_control_resume_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008126 break;
8127 default:
8128 return;
8129 }
8130 if (do_set) {
8131 printk(TPACPI_NOTICE
8132 "restoring fan level to 0x%02x\n",
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008133 fan_control_resume_level);
8134 rc = fan_set_level_safe(fan_control_resume_level);
8135 if (rc < 0)
8136 printk(TPACPI_NOTICE
8137 "failed to restore fan level: %d\n", rc);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008138 }
8139}
8140
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008141static int fan_read(struct seq_file *m)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008142{
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008143 int rc;
8144 u8 status;
8145 unsigned int speed = 0;
8146
8147 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008148 case TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008149 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008150 rc = fan_get_status_safe(&status);
8151 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008152 return rc;
8153
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008154 seq_printf(m, "status:\t\t%s\n"
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008155 "level:\t\t%d\n",
8156 (status != 0) ? "enabled" : "disabled", status);
8157 break;
8158
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008159 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008160 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008161 rc = fan_get_status_safe(&status);
8162 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008163 return rc;
8164
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008165 seq_printf(m, "status:\t\t%s\n",
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008166 (status != 0) ? "enabled" : "disabled");
8167
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008168 rc = fan_get_speed(&speed);
8169 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008170 return rc;
8171
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008172 seq_printf(m, "speed:\t\t%d\n", speed);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008173
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008174 if (status & TP_EC_FAN_FULLSPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008175 /* Disengaged mode takes precedence */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008176 seq_printf(m, "level:\t\tdisengaged\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008177 else if (status & TP_EC_FAN_AUTO)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008178 seq_printf(m, "level:\t\tauto\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008179 else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008180 seq_printf(m, "level:\t\t%d\n", status);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008181 break;
8182
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008183 case TPACPI_FAN_NONE:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008184 default:
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008185 seq_printf(m, "status:\t\tnot supported\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008186 }
8187
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008188 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008189 seq_printf(m, "commands:\tlevel <level>");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008190
8191 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008192 case TPACPI_FAN_WR_ACPI_SFAN:
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008193 seq_printf(m, " (<level> is 0-7)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008194 break;
8195
8196 default:
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008197 seq_printf(m, " (<level> is 0-7, "
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008198 "auto, disengaged, full-speed)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008199 break;
8200 }
8201 }
8202
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008203 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008204 seq_printf(m, "commands:\tenable, disable\n"
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008205 "commands:\twatchdog <timeout> (<timeout> "
8206 "is 0 (off), 1-120 (seconds))\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008207
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008208 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008209 seq_printf(m, "commands:\tspeed <speed>"
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008210 " (<speed> is 0-65535)\n");
8211
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008212 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008213}
8214
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008215static int fan_write_cmd_level(const char *cmd, int *rc)
8216{
8217 int level;
8218
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02008219 if (strlencmp(cmd, "level auto") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008220 level = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008221 else if ((strlencmp(cmd, "level disengaged") == 0) |
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008222 (strlencmp(cmd, "level full-speed") == 0))
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008223 level = TP_EC_FAN_FULLSPEED;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02008224 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008225 return 0;
8226
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008227 *rc = fan_set_level_safe(level);
8228 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008229 printk(TPACPI_ERR "level command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008230 "access mode %d", fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008231 else if (!*rc)
8232 tpacpi_disclose_usertask("procfs fan",
8233 "set level to %d\n", level);
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008234
8235 return 1;
8236}
8237
8238static int fan_write_cmd_enable(const char *cmd, int *rc)
8239{
8240 if (strlencmp(cmd, "enable") != 0)
8241 return 0;
8242
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008243 *rc = fan_set_enable();
8244 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008245 printk(TPACPI_ERR "enable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008246 "access mode %d", fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008247 else if (!*rc)
8248 tpacpi_disclose_usertask("procfs fan", "enable\n");
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008249
8250 return 1;
8251}
8252
8253static int fan_write_cmd_disable(const char *cmd, int *rc)
8254{
8255 if (strlencmp(cmd, "disable") != 0)
8256 return 0;
8257
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008258 *rc = fan_set_disable();
8259 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008260 printk(TPACPI_ERR "disable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008261 "access mode %d", fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008262 else if (!*rc)
8263 tpacpi_disclose_usertask("procfs fan", "disable\n");
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008264
8265 return 1;
8266}
8267
8268static int fan_write_cmd_speed(const char *cmd, int *rc)
8269{
8270 int speed;
8271
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02008272 /* TODO:
8273 * Support speed <low> <medium> <high> ? */
8274
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008275 if (sscanf(cmd, "speed %d", &speed) != 1)
8276 return 0;
8277
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008278 *rc = fan_set_speed(speed);
8279 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008280 printk(TPACPI_ERR "speed command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008281 "access mode %d", fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008282 else if (!*rc)
8283 tpacpi_disclose_usertask("procfs fan",
8284 "set speed to %d\n", speed);
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008285
8286 return 1;
8287}
8288
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008289static int fan_write_cmd_watchdog(const char *cmd, int *rc)
8290{
8291 int interval;
8292
8293 if (sscanf(cmd, "watchdog %d", &interval) != 1)
8294 return 0;
8295
8296 if (interval < 0 || interval > 120)
8297 *rc = -EINVAL;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008298 else {
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008299 fan_watchdog_maxinterval = interval;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008300 tpacpi_disclose_usertask("procfs fan",
8301 "set watchdog timer to %d\n",
8302 interval);
8303 }
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008304
8305 return 1;
8306}
8307
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008308static int fan_write(char *buf)
8309{
8310 char *cmd;
8311 int rc = 0;
8312
8313 while (!rc && (cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008314 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008315 fan_write_cmd_level(cmd, &rc)) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008316 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008317 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008318 fan_write_cmd_disable(cmd, &rc) ||
8319 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008320 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008321 fan_write_cmd_speed(cmd, &rc))
8322 )
8323 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008324 else if (!rc)
8325 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008326 }
8327
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008328 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008329}
8330
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008331static struct ibm_struct fan_driver_data = {
8332 .name = "fan",
8333 .read = fan_read,
8334 .write = fan_write,
8335 .exit = fan_exit,
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008336 .suspend = fan_suspend,
8337 .resume = fan_resume,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008338};
8339
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008340/****************************************************************************
8341 ****************************************************************************
8342 *
8343 * Infrastructure
8344 *
8345 ****************************************************************************
8346 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07008347
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03008348/*
8349 * HKEY event callout for other subdrivers go here
8350 * (yes, it is ugly, but it is quick, safe, and gets the job done
8351 */
8352static void tpacpi_driver_event(const unsigned int hkey_event)
8353{
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00008354 if (ibm_backlight_device) {
8355 switch (hkey_event) {
8356 case TP_HKEY_EV_BRGHT_UP:
8357 case TP_HKEY_EV_BRGHT_DOWN:
8358 tpacpi_brightness_notify_change();
8359 }
8360 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02008361 if (alsa_card) {
8362 switch (hkey_event) {
8363 case TP_HKEY_EV_VOL_UP:
8364 case TP_HKEY_EV_VOL_DOWN:
8365 case TP_HKEY_EV_VOL_MUTE:
8366 volume_alsa_notify_change();
8367 }
8368 }
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03008369}
8370
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03008371static void hotkey_driver_event(const unsigned int scancode)
8372{
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03008373 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode);
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03008374}
8375
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008376/* sysfs name ---------------------------------------------------------- */
8377static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
8378 struct device_attribute *attr,
8379 char *buf)
8380{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008381 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008382}
8383
8384static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
8385 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
8386
8387/* --------------------------------------------------------------------- */
8388
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008389/* /proc support */
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -03008390static struct proc_dir_entry *proc_dir;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008391
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008392/*
8393 * Module and infrastructure proble, init and exit handling
8394 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008395
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008396static int force_load;
8397
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008398#ifdef CONFIG_THINKPAD_ACPI_DEBUG
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008399static const char * __init str_supported(int is_supported)
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008400{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008401 static char text_unsupported[] __initdata = "not supported";
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008402
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008403 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008404}
8405#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
8406
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008407static void ibm_exit(struct ibm_struct *ibm)
8408{
8409 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
8410
8411 list_del_init(&ibm->all_drivers);
8412
8413 if (ibm->flags.acpi_notify_installed) {
8414 dbg_printk(TPACPI_DBG_EXIT,
8415 "%s: acpi_remove_notify_handler\n", ibm->name);
8416 BUG_ON(!ibm->acpi);
8417 acpi_remove_notify_handler(*ibm->acpi->handle,
8418 ibm->acpi->type,
8419 dispatch_acpi_notify);
8420 ibm->flags.acpi_notify_installed = 0;
8421 ibm->flags.acpi_notify_installed = 0;
8422 }
8423
8424 if (ibm->flags.proc_created) {
8425 dbg_printk(TPACPI_DBG_EXIT,
8426 "%s: remove_proc_entry\n", ibm->name);
8427 remove_proc_entry(ibm->name, proc_dir);
8428 ibm->flags.proc_created = 0;
8429 }
8430
8431 if (ibm->flags.acpi_driver_registered) {
8432 dbg_printk(TPACPI_DBG_EXIT,
8433 "%s: acpi_bus_unregister_driver\n", ibm->name);
8434 BUG_ON(!ibm->acpi);
8435 acpi_bus_unregister_driver(ibm->acpi->driver);
8436 kfree(ibm->acpi->driver);
8437 ibm->acpi->driver = NULL;
8438 ibm->flags.acpi_driver_registered = 0;
8439 }
8440
8441 if (ibm->flags.init_called && ibm->exit) {
8442 ibm->exit();
8443 ibm->flags.init_called = 0;
8444 }
8445
8446 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
8447}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02008448
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008449static int __init ibm_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008450{
8451 int ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008452 struct ibm_struct *ibm = iibm->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008453 struct proc_dir_entry *entry;
8454
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008455 BUG_ON(ibm == NULL);
8456
8457 INIT_LIST_HEAD(&ibm->all_drivers);
8458
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03008459 if (ibm->flags.experimental && !experimental)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008460 return 0;
8461
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008462 dbg_printk(TPACPI_DBG_INIT,
8463 "probing for %s\n", ibm->name);
8464
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008465 if (iibm->init) {
8466 ret = iibm->init(iibm);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008467 if (ret > 0)
8468 return 0; /* probe failed */
8469 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008470 return ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008471
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03008472 ibm->flags.init_called = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008473 }
8474
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03008475 if (ibm->acpi) {
8476 if (ibm->acpi->hid) {
8477 ret = register_tpacpi_subdriver(ibm);
8478 if (ret)
8479 goto err_out;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008480 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03008481
8482 if (ibm->acpi->notify) {
8483 ret = setup_acpi_notify(ibm);
8484 if (ret == -ENODEV) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008485 printk(TPACPI_NOTICE "disabling subdriver %s\n",
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03008486 ibm->name);
8487 ret = 0;
8488 goto err_out;
8489 }
8490 if (ret < 0)
8491 goto err_out;
8492 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008493 }
8494
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008495 dbg_printk(TPACPI_DBG_INIT,
8496 "%s installed\n", ibm->name);
8497
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008498 if (ibm->read) {
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03008499 mode_t mode = iibm->base_procfs_mode;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008500
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03008501 if (!mode)
8502 mode = S_IRUGO;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008503 if (ibm->write)
8504 mode |= S_IWUSR;
8505 entry = proc_create_data(ibm->name, mode, proc_dir,
8506 &dispatch_proc_fops, ibm);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008507 if (!entry) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008508 printk(TPACPI_ERR "unable to create proc entry %s\n",
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008509 ibm->name);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008510 ret = -ENODEV;
8511 goto err_out;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008512 }
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03008513 ibm->flags.proc_created = 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008514 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008515
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008516 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
8517
Linus Torvalds1da177e2005-04-16 15:20:36 -07008518 return 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008519
8520err_out:
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008521 dbg_printk(TPACPI_DBG_INIT,
8522 "%s: at error exit path with result %d\n",
8523 ibm->name, ret);
8524
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008525 ibm_exit(ibm);
8526 return (ret < 0)? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008527}
8528
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008529/* Probing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008530
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008531static bool __pure __init tpacpi_is_fw_digit(const char c)
8532{
8533 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
8534}
8535
8536/* Most models: xxyTkkWW (#.##c); Ancient 570/600 and -SL lacks (#.##c) */
8537static bool __pure __init tpacpi_is_valid_fw_id(const char* const s,
8538 const char t)
8539{
8540 return s && strlen(s) >= 8 &&
8541 tpacpi_is_fw_digit(s[0]) &&
8542 tpacpi_is_fw_digit(s[1]) &&
8543 s[2] == t && s[3] == 'T' &&
8544 tpacpi_is_fw_digit(s[4]) &&
8545 tpacpi_is_fw_digit(s[5]) &&
8546 s[6] == 'W' && s[7] == 'W';
8547}
8548
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008549/* returns 0 - probe ok, or < 0 - probe error.
8550 * Probe ok doesn't mean thinkpad found.
8551 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
8552static int __must_check __init get_thinkpad_model_data(
8553 struct thinkpad_id_data *tp)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02008554{
Jeff Garzik18552562007-10-03 15:15:40 -04008555 const struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02008556 char ec_fw_string[18];
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008557 char const *s;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02008558
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008559 if (!tp)
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008560 return -EINVAL;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008561
8562 memset(tp, 0, sizeof(*tp));
8563
8564 if (dmi_name_in_vendors("IBM"))
8565 tp->vendor = PCI_VENDOR_ID_IBM;
8566 else if (dmi_name_in_vendors("LENOVO"))
8567 tp->vendor = PCI_VENDOR_ID_LENOVO;
8568 else
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008569 return 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008570
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008571 s = dmi_get_system_info(DMI_BIOS_VERSION);
8572 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
8573 if (s && !tp->bios_version_str)
8574 return -ENOMEM;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008575
8576 /* Really ancient ThinkPad 240X will fail this, which is fine */
8577 if (!tpacpi_is_valid_fw_id(tp->bios_version_str, 'E'))
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008578 return 0;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008579
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008580 tp->bios_model = tp->bios_version_str[0]
8581 | (tp->bios_version_str[1] << 8);
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008582 tp->bios_release = (tp->bios_version_str[4] << 8)
8583 | tp->bios_version_str[5];
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008584
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02008585 /*
8586 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
8587 * X32 or newer, all Z series; Some models must have an
8588 * up-to-date BIOS or they will not be detected.
8589 *
8590 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
8591 */
8592 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02008593 if (sscanf(dev->name,
8594 "IBM ThinkPad Embedded Controller -[%17c",
8595 ec_fw_string) == 1) {
8596 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
8597 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008598
8599 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008600 if (!tp->ec_version_str)
8601 return -ENOMEM;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008602
8603 if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) {
8604 tp->ec_model = ec_fw_string[0]
8605 | (ec_fw_string[1] << 8);
8606 tp->ec_release = (ec_fw_string[4] << 8)
8607 | ec_fw_string[5];
8608 } else {
8609 printk(TPACPI_NOTICE
8610 "ThinkPad firmware release %s "
8611 "doesn't match the known patterns\n",
8612 ec_fw_string);
8613 printk(TPACPI_NOTICE
8614 "please report this to %s\n",
8615 TPACPI_MAIL);
8616 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008617 break;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02008618 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02008619 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008620
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008621 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
8622 if (s && !strnicmp(s, "ThinkPad", 8)) {
8623 tp->model_str = kstrdup(s, GFP_KERNEL);
8624 if (!tp->model_str)
8625 return -ENOMEM;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008626 }
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03008627
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008628 s = dmi_get_system_info(DMI_PRODUCT_NAME);
8629 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
8630 if (s && !tp->nummodel_str)
8631 return -ENOMEM;
8632
8633 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008634}
8635
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008636static int __init probe_for_thinkpad(void)
8637{
8638 int is_thinkpad;
8639
8640 if (acpi_disabled)
8641 return -ENODEV;
8642
8643 /*
8644 * Non-ancient models have better DMI tagging, but very old models
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03008645 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008646 */
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03008647 is_thinkpad = (thinkpad_id.model_str != NULL) ||
8648 (thinkpad_id.ec_model != 0) ||
8649 tpacpi_is_fw_known();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008650
8651 /* ec is required because many other handles are relative to it */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008652 TPACPI_ACPIHANDLE_INIT(ec);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008653 if (!ec_handle) {
8654 if (is_thinkpad)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008655 printk(TPACPI_ERR
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008656 "Not yet supported ThinkPad detected!\n");
8657 return -ENODEV;
8658 }
8659
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03008660 if (!is_thinkpad && !force_load)
8661 return -ENODEV;
8662
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008663 return 0;
8664}
8665
8666
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008667/* Module init, exit, parameters */
8668
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008669static struct ibm_init_struct ibms_init[] __initdata = {
8670 {
8671 .init = thinkpad_acpi_driver_init,
8672 .data = &thinkpad_acpi_driver_data,
8673 },
8674 {
8675 .init = hotkey_init,
8676 .data = &hotkey_driver_data,
8677 },
8678 {
8679 .init = bluetooth_init,
8680 .data = &bluetooth_driver_data,
8681 },
8682 {
8683 .init = wan_init,
8684 .data = &wan_driver_data,
8685 },
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02008686 {
8687 .init = uwb_init,
8688 .data = &uwb_driver_data,
8689 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02008690#ifdef CONFIG_THINKPAD_ACPI_VIDEO
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008691 {
8692 .init = video_init,
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03008693 .base_procfs_mode = S_IRUSR,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008694 .data = &video_driver_data,
8695 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02008696#endif
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008697 {
8698 .init = light_init,
8699 .data = &light_driver_data,
8700 },
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008701 {
8702 .init = cmos_init,
8703 .data = &cmos_driver_data,
8704 },
8705 {
8706 .init = led_init,
8707 .data = &led_driver_data,
8708 },
8709 {
8710 .init = beep_init,
8711 .data = &beep_driver_data,
8712 },
8713 {
8714 .init = thermal_init,
8715 .data = &thermal_driver_data,
8716 },
8717 {
8718 .data = &ecdump_driver_data,
8719 },
8720 {
8721 .init = brightness_init,
8722 .data = &brightness_driver_data,
8723 },
8724 {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02008725 .init = volume_init,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008726 .data = &volume_driver_data,
8727 },
8728 {
8729 .init = fan_init,
8730 .data = &fan_driver_data,
8731 },
8732};
8733
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008734static int __init set_ibm_param(const char *val, struct kernel_param *kp)
8735{
8736 unsigned int i;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008737 struct ibm_struct *ibm;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008738
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02008739 if (!kp || !kp->name || !val)
8740 return -EINVAL;
8741
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008742 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
8743 ibm = ibms_init[i].data;
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02008744 WARN_ON(ibm == NULL);
8745
8746 if (!ibm || !ibm->name)
8747 continue;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008748
8749 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
8750 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008751 return -ENOSPC;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008752 strcpy(ibms_init[i].param, val);
8753 strcat(ibms_init[i].param, ",");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008754 return 0;
8755 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008756 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008757
8758 return -EINVAL;
8759}
8760
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008761module_param(experimental, int, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008762MODULE_PARM_DESC(experimental,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008763 "Enables experimental features when non-zero");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008764
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03008765module_param_named(debug, dbg_level, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008766MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03008767
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008768module_param(force_load, bool, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008769MODULE_PARM_DESC(force_load,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008770 "Attempts to load the driver even on a "
8771 "mis-identified ThinkPad when true");
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03008772
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008773module_param_named(fan_control, fan_control_allowed, bool, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008774MODULE_PARM_DESC(fan_control,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008775 "Enables setting fan parameters features when true");
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008776
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008777module_param_named(brightness_mode, brightness_mode, uint, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008778MODULE_PARM_DESC(brightness_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008779 "Selects brightness control strategy: "
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00008780 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03008781
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008782module_param(brightness_enable, uint, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008783MODULE_PARM_DESC(brightness_enable,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008784 "Enables backlight control when 1, disables when 0");
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02008785
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008786module_param(hotkey_report_mode, uint, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008787MODULE_PARM_DESC(hotkey_report_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008788 "used for backwards compatibility with userspace, "
8789 "see documentation");
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03008790
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02008791#ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02008792module_param_named(volume_mode, volume_mode, uint, 0444);
8793MODULE_PARM_DESC(volume_mode,
8794 "Selects volume control strategy: "
8795 "0=auto, 1=EC, 2=N/A, 3=EC+NVRAM");
8796
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02008797module_param_named(volume_capabilities, volume_capabilities, uint, 0444);
8798MODULE_PARM_DESC(volume_capabilities,
8799 "Selects the mixer capabilites: "
8800 "0=auto, 1=volume and mute, 2=mute only");
8801
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02008802module_param_named(volume_control, volume_control_allowed, bool, 0444);
8803MODULE_PARM_DESC(volume_control,
8804 "Enables software override for the console audio "
8805 "control when true");
8806
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02008807/* ALSA module API parameters */
8808module_param_named(index, alsa_index, int, 0444);
8809MODULE_PARM_DESC(index, "ALSA index for the ACPI EC Mixer");
8810module_param_named(id, alsa_id, charp, 0444);
8811MODULE_PARM_DESC(id, "ALSA id for the ACPI EC Mixer");
8812module_param_named(enable, alsa_enable, bool, 0444);
8813MODULE_PARM_DESC(enable, "Enable the ALSA interface for the ACPI EC Mixer");
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02008814#endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02008815
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008816#define TPACPI_PARAM(feature) \
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008817 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
Henrique de Moraes Holschuhcbb14842008-02-16 02:17:50 -02008818 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008819 "at module load, see documentation")
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008820
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008821TPACPI_PARAM(hotkey);
8822TPACPI_PARAM(bluetooth);
8823TPACPI_PARAM(video);
8824TPACPI_PARAM(light);
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008825TPACPI_PARAM(cmos);
8826TPACPI_PARAM(led);
8827TPACPI_PARAM(beep);
8828TPACPI_PARAM(ecdump);
8829TPACPI_PARAM(brightness);
8830TPACPI_PARAM(volume);
8831TPACPI_PARAM(fan);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008832
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02008833#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008834module_param(dbg_wlswemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02008835MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
8836module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
8837MODULE_PARM_DESC(wlsw_state,
8838 "Initial state of the emulated WLSW switch");
8839
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008840module_param(dbg_bluetoothemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02008841MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
8842module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
8843MODULE_PARM_DESC(bluetooth_state,
8844 "Initial state of the emulated bluetooth switch");
8845
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008846module_param(dbg_wwanemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02008847MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
8848module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
8849MODULE_PARM_DESC(wwan_state,
8850 "Initial state of the emulated WWAN switch");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02008851
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008852module_param(dbg_uwbemul, uint, 0444);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02008853MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
8854module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
8855MODULE_PARM_DESC(uwb_state,
8856 "Initial state of the emulated UWB switch");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02008857#endif
8858
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008859static void thinkpad_acpi_module_exit(void)
8860{
8861 struct ibm_struct *ibm, *itmp;
8862
8863 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
8864
8865 list_for_each_entry_safe_reverse(ibm, itmp,
8866 &tpacpi_all_drivers,
8867 all_drivers) {
8868 ibm_exit(ibm);
8869 }
8870
8871 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
8872
8873 if (tpacpi_inputdev) {
8874 if (tp_features.input_device_registered)
8875 input_unregister_device(tpacpi_inputdev);
8876 else
8877 input_free_device(tpacpi_inputdev);
8878 }
8879
8880 if (tpacpi_hwmon)
8881 hwmon_device_unregister(tpacpi_hwmon);
8882
8883 if (tp_features.sensors_pdev_attrs_registered)
8884 device_remove_file(&tpacpi_sensors_pdev->dev,
8885 &dev_attr_thinkpad_acpi_pdev_name);
8886 if (tpacpi_sensors_pdev)
8887 platform_device_unregister(tpacpi_sensors_pdev);
8888 if (tpacpi_pdev)
8889 platform_device_unregister(tpacpi_pdev);
8890
8891 if (tp_features.sensors_pdrv_attrs_registered)
8892 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
8893 if (tp_features.platform_drv_attrs_registered)
8894 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
8895
8896 if (tp_features.sensors_pdrv_registered)
8897 platform_driver_unregister(&tpacpi_hwmon_pdriver);
8898
8899 if (tp_features.platform_drv_registered)
8900 platform_driver_unregister(&tpacpi_pdriver);
8901
8902 if (proc_dir)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008903 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008904
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03008905 if (tpacpi_wq)
8906 destroy_workqueue(tpacpi_wq);
8907
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008908 kfree(thinkpad_id.bios_version_str);
8909 kfree(thinkpad_id.ec_version_str);
8910 kfree(thinkpad_id.model_str);
8911}
8912
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02008913
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03008914static int __init thinkpad_acpi_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008915{
8916 int ret, i;
8917
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03008918 tpacpi_lifecycle = TPACPI_LIFE_INIT;
8919
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03008920 /* Parameter checking */
8921 if (hotkey_report_mode > 2)
8922 return -EINVAL;
8923
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008924 /* Driver-level probe */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008925
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008926 ret = get_thinkpad_model_data(&thinkpad_id);
8927 if (ret) {
8928 printk(TPACPI_ERR
8929 "unable to get DMI data: %d\n", ret);
8930 thinkpad_acpi_module_exit();
8931 return ret;
8932 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008933 ret = probe_for_thinkpad();
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008934 if (ret) {
8935 thinkpad_acpi_module_exit();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008936 return ret;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008937 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008938
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008939 /* Driver initialization */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008940
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008941 TPACPI_ACPIHANDLE_INIT(ecrd);
8942 TPACPI_ACPIHANDLE_INIT(ecwr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008943
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03008944 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
8945 if (!tpacpi_wq) {
8946 thinkpad_acpi_module_exit();
8947 return -ENOMEM;
8948 }
8949
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008950 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008951 if (!proc_dir) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008952 printk(TPACPI_ERR
8953 "unable to create proc dir " TPACPI_PROC_DIR);
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03008954 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008955 return -ENODEV;
8956 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008957
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008958 ret = platform_driver_register(&tpacpi_pdriver);
8959 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008960 printk(TPACPI_ERR
8961 "unable to register main platform driver\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008962 thinkpad_acpi_module_exit();
8963 return ret;
8964 }
Henrique de Moraes Holschuhac363932007-07-27 17:04:40 -03008965 tp_features.platform_drv_registered = 1;
8966
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008967 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
8968 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008969 printk(TPACPI_ERR
8970 "unable to register hwmon platform driver\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008971 thinkpad_acpi_module_exit();
8972 return ret;
8973 }
8974 tp_features.sensors_pdrv_registered = 1;
8975
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03008976 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03008977 if (!ret) {
8978 tp_features.platform_drv_attrs_registered = 1;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008979 ret = tpacpi_create_driver_attributes(
8980 &tpacpi_hwmon_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03008981 }
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03008982 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008983 printk(TPACPI_ERR
8984 "unable to create sysfs driver attributes\n");
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03008985 thinkpad_acpi_module_exit();
8986 return ret;
8987 }
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03008988 tp_features.sensors_pdrv_attrs_registered = 1;
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03008989
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008990
8991 /* Device initialization */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008992 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008993 NULL, 0);
8994 if (IS_ERR(tpacpi_pdev)) {
8995 ret = PTR_ERR(tpacpi_pdev);
8996 tpacpi_pdev = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008997 printk(TPACPI_ERR "unable to register platform device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008998 thinkpad_acpi_module_exit();
8999 return ret;
9000 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009001 tpacpi_sensors_pdev = platform_device_register_simple(
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009002 TPACPI_HWMON_DRVR_NAME,
9003 -1, NULL, 0);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009004 if (IS_ERR(tpacpi_sensors_pdev)) {
9005 ret = PTR_ERR(tpacpi_sensors_pdev);
9006 tpacpi_sensors_pdev = NULL;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009007 printk(TPACPI_ERR
9008 "unable to register hwmon platform device\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009009 thinkpad_acpi_module_exit();
9010 return ret;
9011 }
9012 ret = device_create_file(&tpacpi_sensors_pdev->dev,
9013 &dev_attr_thinkpad_acpi_pdev_name);
9014 if (ret) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009015 printk(TPACPI_ERR
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009016 "unable to create sysfs hwmon device attributes\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009017 thinkpad_acpi_module_exit();
9018 return ret;
9019 }
9020 tp_features.sensors_pdev_attrs_registered = 1;
9021 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009022 if (IS_ERR(tpacpi_hwmon)) {
9023 ret = PTR_ERR(tpacpi_hwmon);
9024 tpacpi_hwmon = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009025 printk(TPACPI_ERR "unable to register hwmon device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009026 thinkpad_acpi_module_exit();
9027 return ret;
9028 }
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -03009029 mutex_init(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009030 tpacpi_inputdev = input_allocate_device();
9031 if (!tpacpi_inputdev) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009032 printk(TPACPI_ERR "unable to allocate input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009033 thinkpad_acpi_module_exit();
9034 return -ENOMEM;
9035 } else {
9036 /* Prepare input device, but don't register */
9037 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009038 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009039 tpacpi_inputdev->id.bustype = BUS_HOST;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03009040 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
9041 thinkpad_id.vendor :
9042 PCI_VENDOR_ID_IBM;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009043 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
9044 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
Henrique de Moraes Holschuhd112ef92009-12-09 01:36:26 +00009045 tpacpi_inputdev->dev.parent = &tpacpi_pdev->dev;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009046 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009047 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
9048 ret = ibm_init(&ibms_init[i]);
9049 if (ret >= 0 && *ibms_init[i].param)
9050 ret = ibms_init[i].data->write(ibms_init[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009051 if (ret < 0) {
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03009052 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07009053 return ret;
9054 }
9055 }
Henrique de Moraes Holschuhb589ea42010-02-25 21:28:58 -03009056
9057 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
9058
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009059 ret = input_register_device(tpacpi_inputdev);
9060 if (ret < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009061 printk(TPACPI_ERR "unable to register input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009062 thinkpad_acpi_module_exit();
9063 return ret;
9064 } else {
9065 tp_features.input_device_registered = 1;
9066 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009067
9068 return 0;
9069}
9070
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03009071MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
9072
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009073/*
Henrique de Moraes Holschuh922fe092009-04-14 02:44:13 +00009074 * This will autoload the driver in almost every ThinkPad
9075 * in widespread use.
9076 *
9077 * Only _VERY_ old models, like the 240, 240x and 570 lack
9078 * the HKEY event interface.
9079 */
9080MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
9081
9082/*
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009083 * DMI matching for module autoloading
9084 *
9085 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
9086 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
9087 *
9088 * Only models listed in thinkwiki will be supported, so add yours
9089 * if it is not there yet.
9090 */
9091#define IBM_BIOS_MODULE_ALIAS(__type) \
Mathieu Chouquet-Stringerb36a50f2009-03-14 16:35:26 +01009092 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009093
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009094/* Ancient thinkpad BIOSes have to be identified by
9095 * BIOS type or model number, and there are far less
9096 * BIOS types than model numbers... */
Henrique de Moraes Holschuh922fe092009-04-14 02:44:13 +00009097IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009098
Henrique de Moraes Holschuhf68f53a2009-04-14 02:44:12 +00009099MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
9100MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009101MODULE_DESCRIPTION(TPACPI_DESC);
9102MODULE_VERSION(TPACPI_VERSION);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009103MODULE_LICENSE("GPL");
9104
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03009105module_init(thinkpad_acpi_module_init);
9106module_exit(thinkpad_acpi_module_exit);