blob: 049041a47f8db08e5b2c3892c3a3b72a7e840788 [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 *);
289 struct ibm_struct *data;
290};
291
292static struct {
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200293 u32 bluetooth:1;
294 u32 hotkey:1;
295 u32 hotkey_mask:1;
296 u32 hotkey_wlsw:1;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -0200297 u32 hotkey_tablet:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200298 u32 light:1;
299 u32 light_status:1;
300 u32 bright_16levels:1;
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -0300301 u32 bright_acpimode:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200302 u32 wan:1;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -0200303 u32 uwb:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200304 u32 fan_ctrl_status_undef:1;
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -0300305 u32 second_fan:1;
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -0300306 u32 beep_needs_two_args:1;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -0200307 u32 mixer_no_level_control:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200308 u32 input_device_registered:1;
309 u32 platform_drv_registered:1;
310 u32 platform_drv_attrs_registered:1;
311 u32 sensors_pdrv_registered:1;
312 u32 sensors_pdrv_attrs_registered:1;
313 u32 sensors_pdev_attrs_registered:1;
314 u32 hotkey_poll_active:1;
315} tp_features;
316
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -0300317static struct {
318 u16 hotkey_mask_ff:1;
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -0200319 u16 volume_ctrl_forbidden:1;
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -0300320} tp_warned;
321
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200322struct thinkpad_id_data {
323 unsigned int vendor; /* ThinkPad vendor:
324 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
325
326 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
327 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
328
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -0300329 u16 bios_model; /* 1Y = 0x5931, 0 = unknown */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200330 u16 ec_model;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -0300331 u16 bios_release; /* 1ZETK1WW = 0x314b, 0 = unknown */
332 u16 ec_release;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200333
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -0300334 char *model_str; /* ThinkPad T43 */
335 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200336};
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200337static struct thinkpad_id_data thinkpad_id;
338
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300339static enum {
340 TPACPI_LIFE_INIT = 0,
341 TPACPI_LIFE_RUNNING,
342 TPACPI_LIFE_EXITING,
343} tpacpi_lifecycle;
344
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200345static int experimental;
346static u32 dbg_level;
347
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -0300348static struct workqueue_struct *tpacpi_wq;
349
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +0000350enum led_status_t {
351 TPACPI_LED_OFF = 0,
352 TPACPI_LED_ON,
353 TPACPI_LED_BLINK,
354};
355
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -0300356/* Special LED class that can defer work */
357struct tpacpi_led_classdev {
358 struct led_classdev led_classdev;
359 struct work_struct work;
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +0000360 enum led_status_t new_state;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -0300361 unsigned int led;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -0300362};
363
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200364#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
365static int dbg_wlswemul;
366static int tpacpi_wlsw_emulstate;
367static int dbg_bluetoothemul;
368static int tpacpi_bluetooth_emulstate;
369static int dbg_wwanemul;
370static int tpacpi_wwan_emulstate;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -0200371static int dbg_uwbemul;
372static int tpacpi_uwb_emulstate;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200373#endif
374
375
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000376/*************************************************************************
377 * Debugging helpers
378 */
379
380#define dbg_printk(a_dbg_level, format, arg...) \
381 do { if (dbg_level & (a_dbg_level)) \
382 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
383 } while (0)
384
385#ifdef CONFIG_THINKPAD_ACPI_DEBUG
386#define vdbg_printk dbg_printk
387static const char *str_supported(int is_supported);
388#else
389#define vdbg_printk(a_dbg_level, format, arg...) \
390 do { } while (0)
391#endif
392
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +0000393static void tpacpi_log_usertask(const char * const what)
394{
395 printk(TPACPI_DEBUG "%s: access by process with PID %d\n",
396 what, task_tgid_vnr(current));
397}
398
399#define tpacpi_disclose_usertask(what, format, arg...) \
400 do { \
401 if (unlikely( \
402 (dbg_level & TPACPI_DBG_DISCLOSETASK) && \
403 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
404 printk(TPACPI_DEBUG "%s: PID %d: " format, \
405 what, task_tgid_vnr(current), ## arg); \
406 } \
407 } while (0)
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000408
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -0300409/*
410 * Quirk handling helpers
411 *
412 * ThinkPad IDs and versions seen in the field so far
413 * are two-characters from the set [0-9A-Z], i.e. base 36.
414 *
415 * We use values well outside that range as specials.
416 */
417
418#define TPACPI_MATCH_ANY 0xffffU
419#define TPACPI_MATCH_UNKNOWN 0U
420
421/* TPID('1', 'Y') == 0x5931 */
422#define TPID(__c1, __c2) (((__c2) << 8) | (__c1))
423
424#define TPACPI_Q_IBM(__id1, __id2, __quirk) \
425 { .vendor = PCI_VENDOR_ID_IBM, \
426 .bios = TPID(__id1, __id2), \
427 .ec = TPACPI_MATCH_ANY, \
428 .quirks = (__quirk) }
429
430#define TPACPI_Q_LNV(__id1, __id2, __quirk) \
431 { .vendor = PCI_VENDOR_ID_LENOVO, \
432 .bios = TPID(__id1, __id2), \
433 .ec = TPACPI_MATCH_ANY, \
434 .quirks = (__quirk) }
435
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -0200436#define TPACPI_QEC_LNV(__id1, __id2, __quirk) \
437 { .vendor = PCI_VENDOR_ID_LENOVO, \
438 .bios = TPACPI_MATCH_ANY, \
439 .ec = TPID(__id1, __id2), \
440 .quirks = (__quirk) }
441
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -0300442struct tpacpi_quirk {
443 unsigned int vendor;
444 u16 bios;
445 u16 ec;
446 unsigned long quirks;
447};
448
449/**
450 * tpacpi_check_quirks() - search BIOS/EC version on a list
451 * @qlist: array of &struct tpacpi_quirk
452 * @qlist_size: number of elements in @qlist
453 *
454 * Iterates over a quirks list until one is found that matches the
455 * ThinkPad's vendor, BIOS and EC model.
456 *
457 * Returns 0 if nothing matches, otherwise returns the quirks field of
458 * the matching &struct tpacpi_quirk entry.
459 *
460 * The match criteria is: vendor, ec and bios much match.
461 */
462static unsigned long __init tpacpi_check_quirks(
463 const struct tpacpi_quirk *qlist,
464 unsigned int qlist_size)
465{
466 while (qlist_size) {
467 if ((qlist->vendor == thinkpad_id.vendor ||
468 qlist->vendor == TPACPI_MATCH_ANY) &&
469 (qlist->bios == thinkpad_id.bios_model ||
470 qlist->bios == TPACPI_MATCH_ANY) &&
471 (qlist->ec == thinkpad_id.ec_model ||
472 qlist->ec == TPACPI_MATCH_ANY))
473 return qlist->quirks;
474
475 qlist_size--;
476 qlist++;
477 }
478 return 0;
479}
480
481
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300482/****************************************************************************
483 ****************************************************************************
484 *
485 * ACPI Helpers and device model
486 *
487 ****************************************************************************
488 ****************************************************************************/
489
490/*************************************************************************
491 * ACPI basic handles
492 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -0300494static acpi_handle root_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200496#define TPACPI_HANDLE(object, parent, paths...) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 static acpi_handle object##_handle; \
498 static acpi_handle *object##_parent = &parent##_handle; \
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400499 static char *object##_path; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 static char *object##_paths[] = { paths }
501
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200502TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400503 "\\_SB.PCI.ISA.EC", /* 570 */
504 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
505 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
506 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
507 "\\_SB.PCI0.ICH3.EC0", /* R31 */
508 "\\_SB.PCI0.LPC.EC", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300509 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200511TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
512TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200514TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
515 /* T4x, X31, X40 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400516 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
517 "\\CMS", /* R40, R40e */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300518 ); /* all others */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400519
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200520TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400521 "^HKEY", /* R30, R31 */
522 "HKEY", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300523 ); /* 570 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400524
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -0200525TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
526 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
527 "\\_SB.PCI0.VID0", /* 770e */
528 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
529 "\\_SB.PCI0.AGP.VID", /* all others */
530 ); /* R30, R31 */
531
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400532
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300533/*************************************************************************
534 * ACPI helpers
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -0200535 */
536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537static int acpi_evalf(acpi_handle handle,
538 void *res, char *method, char *fmt, ...)
539{
540 char *fmt0 = fmt;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400541 struct acpi_object_list params;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200542 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400543 struct acpi_buffer result, *resultp;
544 union acpi_object out_obj;
545 acpi_status status;
546 va_list ap;
547 char res_type;
548 int success;
549 int quiet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
551 if (!*fmt) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200552 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 return 0;
554 }
555
556 if (*fmt == 'q') {
557 quiet = 1;
558 fmt++;
559 } else
560 quiet = 0;
561
562 res_type = *(fmt++);
563
564 params.count = 0;
565 params.pointer = &in_objs[0];
566
567 va_start(ap, fmt);
568 while (*fmt) {
569 char c = *(fmt++);
570 switch (c) {
571 case 'd': /* int */
572 in_objs[params.count].integer.value = va_arg(ap, int);
573 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
574 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400575 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 default:
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200577 printk(TPACPI_ERR "acpi_evalf() called "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 "with invalid format character '%c'\n", c);
579 return 0;
580 }
581 }
582 va_end(ap);
583
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400584 if (res_type != 'v') {
585 result.length = sizeof(out_obj);
586 result.pointer = &out_obj;
587 resultp = &result;
588 } else
589 resultp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400591 status = acpi_evaluate_object(handle, method, &params, resultp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
593 switch (res_type) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400594 case 'd': /* int */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 if (res)
596 *(int *)res = out_obj.integer.value;
597 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
598 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400599 case 'v': /* void */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 success = status == AE_OK;
601 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400602 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 default:
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200604 printk(TPACPI_ERR "acpi_evalf() called "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 "with invalid format character '%c'\n", res_type);
606 return 0;
607 }
608
609 if (!success && !quiet)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200610 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 method, fmt0, status);
612
613 return success;
614}
615
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200616static int acpi_ec_read(int i, u8 *p)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300617{
618 int v;
619
620 if (ecrd_handle) {
621 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
622 return 0;
623 *p = v;
624 } else {
625 if (ec_read(i, p) < 0)
626 return 0;
627 }
628
629 return 1;
630}
631
632static int acpi_ec_write(int i, u8 v)
633{
634 if (ecwr_handle) {
635 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
636 return 0;
637 } else {
638 if (ec_write(i, v) < 0)
639 return 0;
640 }
641
642 return 1;
643}
644
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -0300645static int issue_thinkpad_cmos_command(int cmos_cmd)
646{
647 if (!cmos_handle)
648 return -ENXIO;
649
650 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
651 return -EIO;
652
653 return 0;
654}
655
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300656/*************************************************************************
657 * ACPI device model
658 */
659
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200660#define TPACPI_ACPIHANDLE_INIT(object) \
661 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200662 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
663
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300664static void drv_acpi_handle_init(char *name,
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300665 acpi_handle *handle, acpi_handle parent,
666 char **paths, int num_paths, char **path)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300667{
668 int i;
669 acpi_status status;
670
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300671 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
672 name);
673
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300674 for (i = 0; i < num_paths; i++) {
675 status = acpi_get_handle(parent, paths[i], handle);
676 if (ACPI_SUCCESS(status)) {
677 *path = paths[i];
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300678 dbg_printk(TPACPI_DBG_INIT,
679 "Found ACPI handle %s for %s\n",
680 *path, name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300681 return;
682 }
683 }
684
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300685 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
686 name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300687 *handle = NULL;
688}
689
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300690static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300691{
692 struct ibm_struct *ibm = data;
693
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300694 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
695 return;
696
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300697 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300698 return;
699
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300700 ibm->acpi->notify(ibm, event);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300701}
702
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300703static int __init setup_acpi_notify(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300704{
705 acpi_status status;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300706 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300707
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300708 BUG_ON(!ibm->acpi);
709
710 if (!*ibm->acpi->handle)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300711 return 0;
712
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300713 vdbg_printk(TPACPI_DBG_INIT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300714 "setting up ACPI notify for %s\n", ibm->name);
715
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300716 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
717 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200718 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300719 ibm->name, rc);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300720 return -ENODEV;
721 }
722
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700723 ibm->acpi->device->driver_data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300724 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200725 TPACPI_ACPI_EVENT_PREFIX,
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -0300726 ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300727
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300728 status = acpi_install_notify_handler(*ibm->acpi->handle,
729 ibm->acpi->type, dispatch_acpi_notify, ibm);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300730 if (ACPI_FAILURE(status)) {
731 if (status == AE_ALREADY_EXISTS) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200732 printk(TPACPI_NOTICE
733 "another device driver is already "
734 "handling %s events\n", ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300735 } else {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200736 printk(TPACPI_ERR
737 "acpi_install_notify_handler(%s) failed: %d\n",
738 ibm->name, status);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300739 }
740 return -ENODEV;
741 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300742 ibm->flags.acpi_notify_installed = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300743 return 0;
744}
745
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300746static int __init tpacpi_device_add(struct acpi_device *device)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300747{
748 return 0;
749}
750
Henrique de Moraes Holschuh67001212007-04-21 11:08:25 -0300751static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300752{
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300753 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300754
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300755 dbg_printk(TPACPI_DBG_INIT,
756 "registering %s as an ACPI driver\n", ibm->name);
757
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300758 BUG_ON(!ibm->acpi);
759
760 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
761 if (!ibm->acpi->driver) {
Mariusz Kozlowski4f778b92008-10-18 14:23:53 -0300762 printk(TPACPI_ERR
763 "failed to allocate memory for ibm->acpi->driver\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300764 return -ENOMEM;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300765 }
766
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200767 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300768 ibm->acpi->driver->ids = ibm->acpi->hid;
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200769
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300770 ibm->acpi->driver->ops.add = &tpacpi_device_add;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300771
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300772 rc = acpi_bus_register_driver(ibm->acpi->driver);
773 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200774 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200775 ibm->name, rc);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300776 kfree(ibm->acpi->driver);
777 ibm->acpi->driver = NULL;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300778 } else if (!rc)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300779 ibm->flags.acpi_driver_registered = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300780
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300781 return rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300782}
783
784
785/****************************************************************************
786 ****************************************************************************
787 *
788 * Procfs Helpers
789 *
790 ****************************************************************************
791 ****************************************************************************/
792
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200793static int dispatch_proc_show(struct seq_file *m, void *v)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300794{
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200795 struct ibm_struct *ibm = m->private;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300796
797 if (!ibm || !ibm->read)
798 return -EINVAL;
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200799 return ibm->read(m);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300800}
801
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200802static int dispatch_proc_open(struct inode *inode, struct file *file)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300803{
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200804 return single_open(file, dispatch_proc_show, PDE(inode)->data);
805}
806
807static ssize_t dispatch_proc_write(struct file *file,
808 const char __user *userbuf,
809 size_t count, loff_t *pos)
810{
811 struct ibm_struct *ibm = PDE(file->f_path.dentry->d_inode)->data;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300812 char *kernbuf;
813 int ret;
814
815 if (!ibm || !ibm->write)
816 return -EINVAL;
Michael Buesch5b05d462009-08-01 12:04:19 -0300817 if (count > PAGE_SIZE - 2)
818 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300819
820 kernbuf = kmalloc(count + 2, GFP_KERNEL);
821 if (!kernbuf)
822 return -ENOMEM;
823
824 if (copy_from_user(kernbuf, userbuf, count)) {
825 kfree(kernbuf);
826 return -EFAULT;
827 }
828
829 kernbuf[count] = 0;
830 strcat(kernbuf, ",");
831 ret = ibm->write(kernbuf);
832 if (ret == 0)
833 ret = count;
834
835 kfree(kernbuf);
836
837 return ret;
838}
839
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200840static const struct file_operations dispatch_proc_fops = {
841 .owner = THIS_MODULE,
842 .open = dispatch_proc_open,
843 .read = seq_read,
844 .llseek = seq_lseek,
845 .release = single_release,
846 .write = dispatch_proc_write,
847};
848
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849static char *next_cmd(char **cmds)
850{
851 char *start = *cmds;
852 char *end;
853
854 while ((end = strchr(start, ',')) && end == start)
855 start = end + 1;
856
857 if (!end)
858 return NULL;
859
860 *end = 0;
861 *cmds = end + 1;
862 return start;
863}
864
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300865
866/****************************************************************************
867 ****************************************************************************
868 *
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300869 * Device model: input, hwmon and platform
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300870 *
871 ****************************************************************************
872 ****************************************************************************/
873
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -0300874static struct platform_device *tpacpi_pdev;
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300875static struct platform_device *tpacpi_sensors_pdev;
Tony Jones1beeffe2007-08-20 13:46:20 -0700876static struct device *tpacpi_hwmon;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300877static struct input_dev *tpacpi_inputdev;
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -0300878static struct mutex tpacpi_inputdev_send_mutex;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200879static LIST_HEAD(tpacpi_all_drivers);
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300880
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200881static int tpacpi_suspend_handler(struct platform_device *pdev,
882 pm_message_t state)
883{
884 struct ibm_struct *ibm, *itmp;
885
886 list_for_each_entry_safe(ibm, itmp,
887 &tpacpi_all_drivers,
888 all_drivers) {
889 if (ibm->suspend)
890 (ibm->suspend)(state);
891 }
892
893 return 0;
894}
895
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300896static int tpacpi_resume_handler(struct platform_device *pdev)
897{
898 struct ibm_struct *ibm, *itmp;
899
900 list_for_each_entry_safe(ibm, itmp,
901 &tpacpi_all_drivers,
902 all_drivers) {
903 if (ibm->resume)
904 (ibm->resume)();
905 }
906
907 return 0;
908}
909
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200910static void tpacpi_shutdown_handler(struct platform_device *pdev)
911{
912 struct ibm_struct *ibm, *itmp;
913
914 list_for_each_entry_safe(ibm, itmp,
915 &tpacpi_all_drivers,
916 all_drivers) {
917 if (ibm->shutdown)
918 (ibm->shutdown)();
919 }
920}
921
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300922static struct platform_driver tpacpi_pdriver = {
923 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200924 .name = TPACPI_DRVR_NAME,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300925 .owner = THIS_MODULE,
926 },
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200927 .suspend = tpacpi_suspend_handler,
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300928 .resume = tpacpi_resume_handler,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200929 .shutdown = tpacpi_shutdown_handler,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300930};
931
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300932static struct platform_driver tpacpi_hwmon_pdriver = {
933 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200934 .name = TPACPI_HWMON_DRVR_NAME,
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300935 .owner = THIS_MODULE,
936 },
937};
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300938
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -0300939/*************************************************************************
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300940 * sysfs support helpers
941 */
942
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200943struct attribute_set {
944 unsigned int members, max_members;
945 struct attribute_group group;
946};
947
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300948struct attribute_set_obj {
949 struct attribute_set s;
950 struct attribute *a;
951} __attribute__((packed));
952
953static struct attribute_set *create_attr_set(unsigned int max_members,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200954 const char *name)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300955{
956 struct attribute_set_obj *sobj;
957
958 if (max_members == 0)
959 return NULL;
960
961 /* Allocates space for implicit NULL at the end too */
962 sobj = kzalloc(sizeof(struct attribute_set_obj) +
963 max_members * sizeof(struct attribute *),
964 GFP_KERNEL);
965 if (!sobj)
966 return NULL;
967 sobj->s.max_members = max_members;
968 sobj->s.group.attrs = &sobj->a;
969 sobj->s.group.name = name;
970
971 return &sobj->s;
972}
973
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200974#define destroy_attr_set(_set) \
975 kfree(_set);
976
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300977/* not multi-threaded safe, use it in a single thread per set */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200978static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300979{
980 if (!s || !attr)
981 return -EINVAL;
982
983 if (s->members >= s->max_members)
984 return -ENOMEM;
985
986 s->group.attrs[s->members] = attr;
987 s->members++;
988
989 return 0;
990}
991
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200992static int add_many_to_attr_set(struct attribute_set *s,
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300993 struct attribute **attr,
994 unsigned int count)
995{
996 int i, res;
997
998 for (i = 0; i < count; i++) {
999 res = add_to_attr_set(s, attr[i]);
1000 if (res)
1001 return res;
1002 }
1003
1004 return 0;
1005}
1006
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001007static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001008{
1009 sysfs_remove_group(kobj, &s->group);
1010 destroy_attr_set(s);
1011}
1012
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001013#define register_attr_set_with_sysfs(_attr_set, _kobj) \
1014 sysfs_create_group(_kobj, &_attr_set->group)
1015
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001016static int parse_strtoul(const char *buf,
1017 unsigned long max, unsigned long *value)
1018{
1019 char *endp;
1020
André Goddard Rosae7d28602009-12-14 18:01:06 -08001021 *value = simple_strtoul(skip_spaces(buf), &endp, 0);
1022 endp = skip_spaces(endp);
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001023 if (*endp || *value > max)
1024 return -EINVAL;
1025
1026 return 0;
1027}
1028
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03001029static void tpacpi_disable_brightness_delay(void)
1030{
1031 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
1032 printk(TPACPI_NOTICE
1033 "ACPI backlight control delay disabled\n");
1034}
1035
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03001036static int __init tpacpi_query_bcl_levels(acpi_handle handle)
1037{
1038 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1039 union acpi_object *obj;
1040 int rc;
1041
1042 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
1043 obj = (union acpi_object *)buffer.pointer;
1044 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
1045 printk(TPACPI_ERR "Unknown _BCL data, "
1046 "please report this to %s\n", TPACPI_MAIL);
1047 rc = 0;
1048 } else {
1049 rc = obj->package.count;
1050 }
1051 } else {
1052 return 0;
1053 }
1054
1055 kfree(buffer.pointer);
1056 return rc;
1057}
1058
1059static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
1060 u32 lvl, void *context, void **rv)
1061{
1062 char name[ACPI_PATH_SEGMENT_LENGTH];
1063 struct acpi_buffer buffer = { sizeof(name), &name };
1064
1065 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
1066 !strncmp("_BCL", name, sizeof(name) - 1)) {
1067 BUG_ON(!rv || !*rv);
1068 **(int **)rv = tpacpi_query_bcl_levels(handle);
1069 return AE_CTRL_TERMINATE;
1070 } else {
1071 return AE_OK;
1072 }
1073}
1074
1075/*
1076 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
1077 */
1078static int __init tpacpi_check_std_acpi_brightness_support(void)
1079{
1080 int status;
1081 int bcl_levels = 0;
1082 void *bcl_ptr = &bcl_levels;
1083
1084 if (!vid_handle) {
1085 TPACPI_ACPIHANDLE_INIT(vid);
1086 }
1087 if (!vid_handle)
1088 return 0;
1089
1090 /*
1091 * Search for a _BCL method, and execute it. This is safe on all
1092 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
1093 * BIOS in ACPI backlight control mode. We do NOT have to care
1094 * about calling the _BCL method in an enabled video device, any
1095 * will do for our purposes.
1096 */
1097
1098 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
Lin Ming22635762009-11-13 10:06:08 +08001099 tpacpi_acpi_walk_find_bcl, NULL, NULL,
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03001100 &bcl_ptr);
1101
1102 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
1103 tp_features.bright_acpimode = 1;
1104 return (bcl_levels - 2);
1105 }
1106
1107 return 0;
1108}
1109
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00001110static void printk_deprecated_attribute(const char * const what,
1111 const char * const details)
1112{
1113 tpacpi_log_usertask("deprecated sysfs attribute");
1114 printk(TPACPI_WARN "WARNING: sysfs attribute %s is deprecated and "
1115 "will be removed. %s\n",
1116 what, details);
1117}
1118
Johannes Berg19d337d2009-06-02 13:01:37 +02001119/*************************************************************************
1120 * rfkill and radio control support helpers
1121 */
1122
1123/*
1124 * ThinkPad-ACPI firmware handling model:
1125 *
1126 * WLSW (master wireless switch) is event-driven, and is common to all
1127 * firmware-controlled radios. It cannot be controlled, just monitored,
1128 * as expected. It overrides all radio state in firmware
1129 *
1130 * The kernel, a masked-off hotkey, and WLSW can change the radio state
1131 * (TODO: verify how WLSW interacts with the returned radio state).
1132 *
1133 * The only time there are shadow radio state changes, is when
1134 * masked-off hotkeys are used.
1135 */
1136
1137/*
1138 * Internal driver API for radio state:
1139 *
1140 * int: < 0 = error, otherwise enum tpacpi_rfkill_state
1141 * bool: true means radio blocked (off)
1142 */
1143enum tpacpi_rfkill_state {
1144 TPACPI_RFK_RADIO_OFF = 0,
1145 TPACPI_RFK_RADIO_ON
1146};
1147
1148/* rfkill switches */
1149enum tpacpi_rfk_id {
1150 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
1151 TPACPI_RFK_WWAN_SW_ID,
1152 TPACPI_RFK_UWB_SW_ID,
1153 TPACPI_RFK_SW_MAX
1154};
1155
1156static const char *tpacpi_rfkill_names[] = {
1157 [TPACPI_RFK_BLUETOOTH_SW_ID] = "bluetooth",
1158 [TPACPI_RFK_WWAN_SW_ID] = "wwan",
1159 [TPACPI_RFK_UWB_SW_ID] = "uwb",
1160 [TPACPI_RFK_SW_MAX] = NULL
1161};
1162
1163/* ThinkPad-ACPI rfkill subdriver */
1164struct tpacpi_rfk {
1165 struct rfkill *rfkill;
1166 enum tpacpi_rfk_id id;
1167 const struct tpacpi_rfk_ops *ops;
1168};
1169
1170struct tpacpi_rfk_ops {
1171 /* firmware interface */
1172 int (*get_status)(void);
1173 int (*set_status)(const enum tpacpi_rfkill_state);
1174};
1175
1176static struct tpacpi_rfk *tpacpi_rfkill_switches[TPACPI_RFK_SW_MAX];
1177
1178/* Query FW and update rfkill sw state for a given rfkill switch */
1179static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk *tp_rfk)
1180{
1181 int status;
1182
1183 if (!tp_rfk)
1184 return -ENODEV;
1185
1186 status = (tp_rfk->ops->get_status)();
1187 if (status < 0)
1188 return status;
1189
1190 rfkill_set_sw_state(tp_rfk->rfkill,
1191 (status == TPACPI_RFK_RADIO_OFF));
1192
1193 return status;
1194}
1195
1196/* Query FW and update rfkill sw state for all rfkill switches */
1197static void tpacpi_rfk_update_swstate_all(void)
1198{
1199 unsigned int i;
1200
1201 for (i = 0; i < TPACPI_RFK_SW_MAX; i++)
1202 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[i]);
1203}
1204
1205/*
1206 * Sync the HW-blocking state of all rfkill switches,
1207 * do notice it causes the rfkill core to schedule uevents
1208 */
1209static void tpacpi_rfk_update_hwblock_state(bool blocked)
1210{
1211 unsigned int i;
1212 struct tpacpi_rfk *tp_rfk;
1213
1214 for (i = 0; i < TPACPI_RFK_SW_MAX; i++) {
1215 tp_rfk = tpacpi_rfkill_switches[i];
1216 if (tp_rfk) {
1217 if (rfkill_set_hw_state(tp_rfk->rfkill,
1218 blocked)) {
1219 /* ignore -- we track sw block */
1220 }
1221 }
1222 }
1223}
1224
1225/* Call to get the WLSW state from the firmware */
1226static int hotkey_get_wlsw(void);
1227
1228/* Call to query WLSW state and update all rfkill switches */
1229static bool tpacpi_rfk_check_hwblock_state(void)
1230{
1231 int res = hotkey_get_wlsw();
1232 int hw_blocked;
1233
1234 /* When unknown or unsupported, we have to assume it is unblocked */
1235 if (res < 0)
1236 return false;
1237
1238 hw_blocked = (res == TPACPI_RFK_RADIO_OFF);
1239 tpacpi_rfk_update_hwblock_state(hw_blocked);
1240
1241 return hw_blocked;
1242}
1243
1244static int tpacpi_rfk_hook_set_block(void *data, bool blocked)
1245{
1246 struct tpacpi_rfk *tp_rfk = data;
1247 int res;
1248
1249 dbg_printk(TPACPI_DBG_RFKILL,
1250 "request to change radio state to %s\n",
1251 blocked ? "blocked" : "unblocked");
1252
1253 /* try to set radio state */
1254 res = (tp_rfk->ops->set_status)(blocked ?
1255 TPACPI_RFK_RADIO_OFF : TPACPI_RFK_RADIO_ON);
1256
1257 /* and update the rfkill core with whatever the FW really did */
1258 tpacpi_rfk_update_swstate(tp_rfk);
1259
1260 return (res < 0) ? res : 0;
1261}
1262
1263static const struct rfkill_ops tpacpi_rfk_rfkill_ops = {
1264 .set_block = tpacpi_rfk_hook_set_block,
1265};
1266
1267static int __init tpacpi_new_rfkill(const enum tpacpi_rfk_id id,
1268 const struct tpacpi_rfk_ops *tp_rfkops,
1269 const enum rfkill_type rfktype,
1270 const char *name,
1271 const bool set_default)
1272{
1273 struct tpacpi_rfk *atp_rfk;
1274 int res;
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001275 bool sw_state = false;
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001276 bool hw_state;
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001277 int sw_status;
Johannes Berg19d337d2009-06-02 13:01:37 +02001278
1279 BUG_ON(id >= TPACPI_RFK_SW_MAX || tpacpi_rfkill_switches[id]);
1280
Johannes Berg19d337d2009-06-02 13:01:37 +02001281 atp_rfk = kzalloc(sizeof(struct tpacpi_rfk), GFP_KERNEL);
1282 if (atp_rfk)
1283 atp_rfk->rfkill = rfkill_alloc(name,
1284 &tpacpi_pdev->dev,
1285 rfktype,
1286 &tpacpi_rfk_rfkill_ops,
1287 atp_rfk);
1288 if (!atp_rfk || !atp_rfk->rfkill) {
1289 printk(TPACPI_ERR
1290 "failed to allocate memory for rfkill class\n");
1291 kfree(atp_rfk);
1292 return -ENOMEM;
1293 }
1294
1295 atp_rfk->id = id;
1296 atp_rfk->ops = tp_rfkops;
1297
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001298 sw_status = (tp_rfkops->get_status)();
1299 if (sw_status < 0) {
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001300 printk(TPACPI_ERR
1301 "failed to read initial state for %s, error %d\n",
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001302 name, sw_status);
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001303 } else {
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001304 sw_state = (sw_status == TPACPI_RFK_RADIO_OFF);
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001305 if (set_default) {
1306 /* try to keep the initial state, since we ask the
1307 * firmware to preserve it across S5 in NVRAM */
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001308 rfkill_init_sw_state(atp_rfk->rfkill, sw_state);
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001309 }
1310 }
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001311 hw_state = tpacpi_rfk_check_hwblock_state();
1312 rfkill_set_hw_state(atp_rfk->rfkill, hw_state);
Johannes Berg19d337d2009-06-02 13:01:37 +02001313
1314 res = rfkill_register(atp_rfk->rfkill);
1315 if (res < 0) {
1316 printk(TPACPI_ERR
1317 "failed to register %s rfkill switch: %d\n",
1318 name, res);
1319 rfkill_destroy(atp_rfk->rfkill);
1320 kfree(atp_rfk);
1321 return res;
1322 }
1323
1324 tpacpi_rfkill_switches[id] = atp_rfk;
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001325
1326 printk(TPACPI_INFO "rfkill switch %s: radio is %sblocked\n",
1327 name, (sw_state || hw_state) ? "" : "un");
Johannes Berg19d337d2009-06-02 13:01:37 +02001328 return 0;
1329}
1330
1331static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id)
1332{
1333 struct tpacpi_rfk *tp_rfk;
1334
1335 BUG_ON(id >= TPACPI_RFK_SW_MAX);
1336
1337 tp_rfk = tpacpi_rfkill_switches[id];
1338 if (tp_rfk) {
1339 rfkill_unregister(tp_rfk->rfkill);
Corentin Chary5f0dadb2009-09-14 12:43:52 +02001340 rfkill_destroy(tp_rfk->rfkill);
Johannes Berg19d337d2009-06-02 13:01:37 +02001341 tpacpi_rfkill_switches[id] = NULL;
1342 kfree(tp_rfk);
1343 }
1344}
1345
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00001346static void printk_deprecated_rfkill_attribute(const char * const what)
1347{
1348 printk_deprecated_attribute(what,
1349 "Please switch to generic rfkill before year 2010");
1350}
1351
Johannes Berg19d337d2009-06-02 13:01:37 +02001352/* sysfs <radio> enable ------------------------------------------------ */
1353static ssize_t tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id,
1354 struct device_attribute *attr,
1355 char *buf)
1356{
1357 int status;
1358
1359 printk_deprecated_rfkill_attribute(attr->attr.name);
1360
1361 /* This is in the ABI... */
1362 if (tpacpi_rfk_check_hwblock_state()) {
1363 status = TPACPI_RFK_RADIO_OFF;
1364 } else {
1365 status = tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1366 if (status < 0)
1367 return status;
1368 }
1369
1370 return snprintf(buf, PAGE_SIZE, "%d\n",
1371 (status == TPACPI_RFK_RADIO_ON) ? 1 : 0);
1372}
1373
1374static ssize_t tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id,
1375 struct device_attribute *attr,
1376 const char *buf, size_t count)
1377{
1378 unsigned long t;
1379 int res;
1380
1381 printk_deprecated_rfkill_attribute(attr->attr.name);
1382
1383 if (parse_strtoul(buf, 1, &t))
1384 return -EINVAL;
1385
1386 tpacpi_disclose_usertask(attr->attr.name, "set to %ld\n", t);
1387
1388 /* This is in the ABI... */
1389 if (tpacpi_rfk_check_hwblock_state() && !!t)
1390 return -EPERM;
1391
1392 res = tpacpi_rfkill_switches[id]->ops->set_status((!!t) ?
1393 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF);
1394 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1395
1396 return (res < 0) ? res : count;
1397}
1398
1399/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001400static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id, struct seq_file *m)
Johannes Berg19d337d2009-06-02 13:01:37 +02001401{
Johannes Berg19d337d2009-06-02 13:01:37 +02001402 if (id >= TPACPI_RFK_SW_MAX)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001403 seq_printf(m, "status:\t\tnot supported\n");
Johannes Berg19d337d2009-06-02 13:01:37 +02001404 else {
1405 int status;
1406
1407 /* This is in the ABI... */
1408 if (tpacpi_rfk_check_hwblock_state()) {
1409 status = TPACPI_RFK_RADIO_OFF;
1410 } else {
1411 status = tpacpi_rfk_update_swstate(
1412 tpacpi_rfkill_switches[id]);
1413 if (status < 0)
1414 return status;
1415 }
1416
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001417 seq_printf(m, "status:\t\t%s\n",
Johannes Berg19d337d2009-06-02 13:01:37 +02001418 (status == TPACPI_RFK_RADIO_ON) ?
1419 "enabled" : "disabled");
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001420 seq_printf(m, "commands:\tenable, disable\n");
Johannes Berg19d337d2009-06-02 13:01:37 +02001421 }
1422
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001423 return 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02001424}
1425
1426static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id, char *buf)
1427{
1428 char *cmd;
1429 int status = -1;
1430 int res = 0;
1431
1432 if (id >= TPACPI_RFK_SW_MAX)
1433 return -ENODEV;
1434
1435 while ((cmd = next_cmd(&buf))) {
1436 if (strlencmp(cmd, "enable") == 0)
1437 status = TPACPI_RFK_RADIO_ON;
1438 else if (strlencmp(cmd, "disable") == 0)
1439 status = TPACPI_RFK_RADIO_OFF;
1440 else
1441 return -EINVAL;
1442 }
1443
1444 if (status != -1) {
1445 tpacpi_disclose_usertask("procfs", "attempt to %s %s\n",
1446 (status == TPACPI_RFK_RADIO_ON) ?
1447 "enable" : "disable",
1448 tpacpi_rfkill_names[id]);
1449 res = (tpacpi_rfkill_switches[id]->ops->set_status)(status);
1450 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1451 }
1452
1453 return res;
1454}
1455
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001456/*************************************************************************
1457 * thinkpad-acpi driver attributes
1458 */
1459
1460/* interface_version --------------------------------------------------- */
1461static ssize_t tpacpi_driver_interface_version_show(
1462 struct device_driver *drv,
1463 char *buf)
1464{
1465 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1466}
1467
1468static DRIVER_ATTR(interface_version, S_IRUGO,
1469 tpacpi_driver_interface_version_show, NULL);
1470
1471/* debug_level --------------------------------------------------------- */
1472static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1473 char *buf)
1474{
1475 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1476}
1477
1478static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1479 const char *buf, size_t count)
1480{
1481 unsigned long t;
1482
1483 if (parse_strtoul(buf, 0xffff, &t))
1484 return -EINVAL;
1485
1486 dbg_level = t;
1487
1488 return count;
1489}
1490
1491static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1492 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1493
1494/* version ------------------------------------------------------------- */
1495static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1496 char *buf)
1497{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001498 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1499 TPACPI_DESC, TPACPI_VERSION);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001500}
1501
1502static DRIVER_ATTR(version, S_IRUGO,
1503 tpacpi_driver_version_show, NULL);
1504
1505/* --------------------------------------------------------------------- */
1506
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001507#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1508
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001509/* wlsw_emulstate ------------------------------------------------------ */
1510static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1511 char *buf)
1512{
1513 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1514}
1515
1516static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1517 const char *buf, size_t count)
1518{
1519 unsigned long t;
1520
1521 if (parse_strtoul(buf, 1, &t))
1522 return -EINVAL;
1523
Johannes Berg19d337d2009-06-02 13:01:37 +02001524 if (tpacpi_wlsw_emulstate != !!t) {
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001525 tpacpi_wlsw_emulstate = !!t;
Johannes Berg19d337d2009-06-02 13:01:37 +02001526 tpacpi_rfk_update_hwblock_state(!t); /* negative logic */
1527 }
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001528
1529 return count;
1530}
1531
1532static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1533 tpacpi_driver_wlsw_emulstate_show,
1534 tpacpi_driver_wlsw_emulstate_store);
1535
1536/* bluetooth_emulstate ------------------------------------------------- */
1537static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1538 struct device_driver *drv,
1539 char *buf)
1540{
1541 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1542}
1543
1544static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1545 struct device_driver *drv,
1546 const char *buf, size_t count)
1547{
1548 unsigned long t;
1549
1550 if (parse_strtoul(buf, 1, &t))
1551 return -EINVAL;
1552
1553 tpacpi_bluetooth_emulstate = !!t;
1554
1555 return count;
1556}
1557
1558static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1559 tpacpi_driver_bluetooth_emulstate_show,
1560 tpacpi_driver_bluetooth_emulstate_store);
1561
1562/* wwan_emulstate ------------------------------------------------- */
1563static ssize_t tpacpi_driver_wwan_emulstate_show(
1564 struct device_driver *drv,
1565 char *buf)
1566{
1567 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1568}
1569
1570static ssize_t tpacpi_driver_wwan_emulstate_store(
1571 struct device_driver *drv,
1572 const char *buf, size_t count)
1573{
1574 unsigned long t;
1575
1576 if (parse_strtoul(buf, 1, &t))
1577 return -EINVAL;
1578
1579 tpacpi_wwan_emulstate = !!t;
1580
1581 return count;
1582}
1583
1584static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1585 tpacpi_driver_wwan_emulstate_show,
1586 tpacpi_driver_wwan_emulstate_store);
1587
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001588/* uwb_emulstate ------------------------------------------------- */
1589static ssize_t tpacpi_driver_uwb_emulstate_show(
1590 struct device_driver *drv,
1591 char *buf)
1592{
1593 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1594}
1595
1596static ssize_t tpacpi_driver_uwb_emulstate_store(
1597 struct device_driver *drv,
1598 const char *buf, size_t count)
1599{
1600 unsigned long t;
1601
1602 if (parse_strtoul(buf, 1, &t))
1603 return -EINVAL;
1604
1605 tpacpi_uwb_emulstate = !!t;
1606
1607 return count;
1608}
1609
1610static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
1611 tpacpi_driver_uwb_emulstate_show,
1612 tpacpi_driver_uwb_emulstate_store);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001613#endif
1614
1615/* --------------------------------------------------------------------- */
1616
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001617static struct driver_attribute *tpacpi_driver_attributes[] = {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001618 &driver_attr_debug_level, &driver_attr_version,
1619 &driver_attr_interface_version,
1620};
1621
1622static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1623{
1624 int i, res;
1625
1626 i = 0;
1627 res = 0;
1628 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1629 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1630 i++;
1631 }
1632
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001633#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1634 if (!res && dbg_wlswemul)
1635 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1636 if (!res && dbg_bluetoothemul)
1637 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1638 if (!res && dbg_wwanemul)
1639 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001640 if (!res && dbg_uwbemul)
1641 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001642#endif
1643
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001644 return res;
1645}
1646
1647static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1648{
1649 int i;
1650
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001651 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001652 driver_remove_file(drv, tpacpi_driver_attributes[i]);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001653
1654#ifdef THINKPAD_ACPI_DEBUGFACILITIES
1655 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1656 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1657 driver_remove_file(drv, &driver_attr_wwan_emulstate);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001658 driver_remove_file(drv, &driver_attr_uwb_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001659#endif
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001660}
1661
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001662/*************************************************************************
1663 * Firmware Data
1664 */
1665
1666/*
1667 * Table of recommended minimum BIOS versions
1668 *
1669 * Reasons for listing:
1670 * 1. Stable BIOS, listed because the unknown ammount of
1671 * bugs and bad ACPI behaviour on older versions
1672 *
1673 * 2. BIOS or EC fw with known bugs that trigger on Linux
1674 *
1675 * 3. BIOS with known reduced functionality in older versions
1676 *
1677 * We recommend the latest BIOS and EC version.
1678 * We only support the latest BIOS and EC fw version as a rule.
1679 *
1680 * Sources: IBM ThinkPad Public Web Documents (update changelogs),
1681 * Information from users in ThinkWiki
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03001682 *
1683 * WARNING: we use this table also to detect that the machine is
1684 * a ThinkPad in some cases, so don't remove entries lightly.
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001685 */
1686
1687#define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1688 { .vendor = (__v), \
1689 .bios = TPID(__id1, __id2), \
1690 .ec = TPACPI_MATCH_ANY, \
1691 .quirks = TPACPI_MATCH_ANY << 16 \
1692 | (__bv1) << 8 | (__bv2) }
1693
1694#define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001695 __eid, __ev1, __ev2) \
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001696 { .vendor = (__v), \
1697 .bios = TPID(__bid1, __bid2), \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001698 .ec = __eid, \
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001699 .quirks = (__ev1) << 24 | (__ev2) << 16 \
1700 | (__bv1) << 8 | (__bv2) }
1701
1702#define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1703 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1704
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001705/* Outdated IBM BIOSes often lack the EC id string */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001706#define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1707 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001708 __bv1, __bv2, TPID(__id1, __id2), \
1709 __ev1, __ev2), \
1710 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1711 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1712 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001713
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001714/* Outdated IBM BIOSes often lack the EC id string */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001715#define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1716 __eid1, __eid2, __ev1, __ev2) \
1717 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001718 __bv1, __bv2, TPID(__eid1, __eid2), \
1719 __ev1, __ev2), \
1720 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1721 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1722 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001723
1724#define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1725 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1726
1727#define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1728 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001729 __bv1, __bv2, TPID(__id1, __id2), \
1730 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001731
1732#define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1733 __eid1, __eid2, __ev1, __ev2) \
1734 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001735 __bv1, __bv2, TPID(__eid1, __eid2), \
1736 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001737
1738static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
1739 /* Numeric models ------------------ */
1740 /* FW MODEL BIOS VERS */
1741 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1742 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1743 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1744 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1745 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1746 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1747 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1748 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1749 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1750
1751 /* A-series ------------------------- */
1752 /* FW MODEL BIOS VERS EC VERS */
1753 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1754 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1755 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1756 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1757 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1758 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1759 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1760 TPV_QI0('1', '3', '2', '0'), /* A22m */
1761 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1762 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1763 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1764
1765 /* G-series ------------------------- */
1766 /* FW MODEL BIOS VERS */
1767 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1768 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1769
1770 /* R-series, T-series --------------- */
1771 /* FW MODEL BIOS VERS EC VERS */
1772 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1773 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1774 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1775 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1776 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1777 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1778 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1779 T40/p, T41/p, T42/p (1) */
1780 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1781 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1782 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1783 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1784
1785 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1786 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1787 TPV_QI0('1', '6', '3', '2'), /* T22 */
1788 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1789 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1790 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1791
1792 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1793 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
Henrique de Moraes Holschuh90765c62009-12-09 01:36:23 +00001794 TPV_QL1('7', 'E', 'D', '0', '1', '5'), /* R60e, R60i */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001795
1796 /* BIOS FW BIOS VERS EC FW EC VERS */
1797 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1798 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1799
1800 /* X-series ------------------------- */
1801 /* FW MODEL BIOS VERS EC VERS */
1802 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1803 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1804 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1805 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1806 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1807 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1808 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1809
Henrique de Moraes Holschuh90765c62009-12-09 01:36:23 +00001810 TPV_QL1('7', 'B', 'D', '7', '4', '0'), /* X60/s */
1811 TPV_QL1('7', 'J', '3', '0', '1', '3'), /* X60t */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001812
1813 /* (0) - older versions lack DMI EC fw string and functionality */
1814 /* (1) - older versions known to lack functionality */
1815};
1816
1817#undef TPV_QL1
1818#undef TPV_QL0
1819#undef TPV_QI2
1820#undef TPV_QI1
1821#undef TPV_QI0
1822#undef TPV_Q_X
1823#undef TPV_Q
1824
1825static void __init tpacpi_check_outdated_fw(void)
1826{
1827 unsigned long fwvers;
1828 u16 ec_version, bios_version;
1829
1830 fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
1831 ARRAY_SIZE(tpacpi_bios_version_qtable));
1832
1833 if (!fwvers)
1834 return;
1835
1836 bios_version = fwvers & 0xffffU;
1837 ec_version = (fwvers >> 16) & 0xffffU;
1838
1839 /* note that unknown versions are set to 0x0000 and we use that */
1840 if ((bios_version > thinkpad_id.bios_release) ||
1841 (ec_version > thinkpad_id.ec_release &&
1842 ec_version != TPACPI_MATCH_ANY)) {
1843 /*
1844 * The changelogs would let us track down the exact
1845 * reason, but it is just too much of a pain to track
1846 * it. We only list BIOSes that are either really
1847 * broken, or really stable to begin with, so it is
1848 * best if the user upgrades the firmware anyway.
1849 */
1850 printk(TPACPI_WARN
1851 "WARNING: Outdated ThinkPad BIOS/EC firmware\n");
1852 printk(TPACPI_WARN
1853 "WARNING: This firmware may be missing critical bug "
1854 "fixes and/or important features\n");
1855 }
1856}
1857
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03001858static bool __init tpacpi_is_fw_known(void)
1859{
1860 return tpacpi_check_quirks(tpacpi_bios_version_qtable,
1861 ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
1862}
1863
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03001864/****************************************************************************
1865 ****************************************************************************
1866 *
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001867 * Subdrivers
1868 *
1869 ****************************************************************************
1870 ****************************************************************************/
1871
1872/*************************************************************************
Henrique de Moraes Holschuh142cfc92007-04-21 11:08:26 -03001873 * thinkpad-acpi init subdriver
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001874 */
1875
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001876static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001878 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
1879 printk(TPACPI_INFO "%s\n", TPACPI_URL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001881 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03001882 (thinkpad_id.bios_version_str) ?
1883 thinkpad_id.bios_version_str : "unknown",
1884 (thinkpad_id.ec_version_str) ?
1885 thinkpad_id.ec_version_str : "unknown");
1886
1887 if (thinkpad_id.vendor && thinkpad_id.model_str)
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03001888 printk(TPACPI_INFO "%s %s, model %s\n",
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03001889 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
1890 "IBM" : ((thinkpad_id.vendor ==
1891 PCI_VENDOR_ID_LENOVO) ?
1892 "Lenovo" : "Unknown vendor"),
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03001893 thinkpad_id.model_str,
1894 (thinkpad_id.nummodel_str) ?
1895 thinkpad_id.nummodel_str : "unknown");
Henrique de Moraes Holschuh3945ac32007-02-06 19:13:44 -02001896
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001897 tpacpi_check_outdated_fw();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 return 0;
1899}
1900
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001901static int thinkpad_acpi_driver_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902{
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001903 seq_printf(m, "driver:\t\t%s\n", TPACPI_DESC);
1904 seq_printf(m, "version:\t%s\n", TPACPI_VERSION);
1905 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906}
1907
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001908static struct ibm_struct thinkpad_acpi_driver_data = {
1909 .name = "driver",
1910 .read = thinkpad_acpi_driver_read,
1911};
1912
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001913/*************************************************************************
1914 * Hotkey subdriver
1915 */
1916
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001917/*
1918 * ThinkPad firmware event model
1919 *
1920 * The ThinkPad firmware has two main event interfaces: normal ACPI
1921 * notifications (which follow the ACPI standard), and a private event
1922 * interface.
1923 *
1924 * The private event interface also issues events for the hotkeys. As
1925 * the driver gained features, the event handling code ended up being
1926 * built around the hotkey subdriver. This will need to be refactored
1927 * to a more formal event API eventually.
1928 *
1929 * Some "hotkeys" are actually supposed to be used as event reports,
1930 * such as "brightness has changed", "volume has changed", depending on
1931 * the ThinkPad model and how the firmware is operating.
1932 *
1933 * Unlike other classes, hotkey-class events have mask/unmask control on
1934 * non-ancient firmware. However, how it behaves changes a lot with the
1935 * firmware model and version.
1936 */
1937
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001938enum { /* hot key scan codes (derived from ACPI DSDT) */
1939 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1940 TP_ACPI_HOTKEYSCAN_FNF2,
1941 TP_ACPI_HOTKEYSCAN_FNF3,
1942 TP_ACPI_HOTKEYSCAN_FNF4,
1943 TP_ACPI_HOTKEYSCAN_FNF5,
1944 TP_ACPI_HOTKEYSCAN_FNF6,
1945 TP_ACPI_HOTKEYSCAN_FNF7,
1946 TP_ACPI_HOTKEYSCAN_FNF8,
1947 TP_ACPI_HOTKEYSCAN_FNF9,
1948 TP_ACPI_HOTKEYSCAN_FNF10,
1949 TP_ACPI_HOTKEYSCAN_FNF11,
1950 TP_ACPI_HOTKEYSCAN_FNF12,
1951 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1952 TP_ACPI_HOTKEYSCAN_FNINSERT,
1953 TP_ACPI_HOTKEYSCAN_FNDELETE,
1954 TP_ACPI_HOTKEYSCAN_FNHOME,
1955 TP_ACPI_HOTKEYSCAN_FNEND,
1956 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1957 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1958 TP_ACPI_HOTKEYSCAN_FNSPACE,
1959 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1960 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1961 TP_ACPI_HOTKEYSCAN_MUTE,
1962 TP_ACPI_HOTKEYSCAN_THINKPAD,
1963};
1964
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001965enum { /* Keys/events available through NVRAM polling */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001966 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1967 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1968};
1969
1970enum { /* Positions of some of the keys in hotkey masks */
1971 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1972 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1973 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1974 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1975 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1976 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1977 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1978 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1979 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1980 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1981 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1982};
1983
1984enum { /* NVRAM to ACPI HKEY group map */
1985 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1986 TP_ACPI_HKEY_ZOOM_MASK |
1987 TP_ACPI_HKEY_DISPSWTCH_MASK |
1988 TP_ACPI_HKEY_HIBERNATE_MASK,
1989 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1990 TP_ACPI_HKEY_BRGHTDWN_MASK,
1991 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1992 TP_ACPI_HKEY_VOLDWN_MASK |
1993 TP_ACPI_HKEY_MUTE_MASK,
1994};
1995
1996#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1997struct tp_nvram_state {
1998 u16 thinkpad_toggle:1;
1999 u16 zoom_toggle:1;
2000 u16 display_toggle:1;
2001 u16 thinklight_toggle:1;
2002 u16 hibernate_toggle:1;
2003 u16 displayexp_toggle:1;
2004 u16 display_state:1;
2005 u16 brightness_toggle:1;
2006 u16 volume_toggle:1;
2007 u16 mute:1;
2008
2009 u8 brightness_level;
2010 u8 volume_level;
2011};
2012
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002013/* kthread for the hotkey poller */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002014static struct task_struct *tpacpi_hotkey_task;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002015
2016/* Acquired while the poller kthread is running, use to sync start/stop */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002017static struct mutex hotkey_thread_mutex;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002018
2019/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002020 * Acquire mutex to write poller control variables as an
2021 * atomic block.
2022 *
2023 * Increment hotkey_config_change when changing them if you
2024 * want the kthread to forget old state.
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002025 *
2026 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
2027 */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002028static struct mutex hotkey_thread_data_mutex;
2029static unsigned int hotkey_config_change;
2030
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002031/*
2032 * hotkey poller control variables
2033 *
2034 * Must be atomic or readers will also need to acquire mutex
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002035 *
2036 * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
2037 * should be used only when the changes need to be taken as
2038 * a block, OR when one needs to force the kthread to forget
2039 * old state.
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002040 */
2041static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
2042static unsigned int hotkey_poll_freq = 10; /* Hz */
2043
2044#define HOTKEY_CONFIG_CRITICAL_START \
2045 do { \
2046 mutex_lock(&hotkey_thread_data_mutex); \
2047 hotkey_config_change++; \
2048 } while (0);
2049#define HOTKEY_CONFIG_CRITICAL_END \
2050 mutex_unlock(&hotkey_thread_data_mutex);
2051
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002052#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2053
2054#define hotkey_source_mask 0U
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002055#define HOTKEY_CONFIG_CRITICAL_START
2056#define HOTKEY_CONFIG_CRITICAL_END
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002057
2058#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2059
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002060static struct mutex hotkey_mutex;
2061
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002062static enum { /* Reasons for waking up */
2063 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
2064 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
2065 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
2066} hotkey_wakeup_reason;
2067
2068static int hotkey_autosleep_ack;
2069
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002070static u32 hotkey_orig_mask; /* events the BIOS had enabled */
2071static u32 hotkey_all_mask; /* all events supported in fw */
2072static u32 hotkey_reserved_mask; /* events better left disabled */
2073static u32 hotkey_driver_mask; /* events needed by the driver */
2074static u32 hotkey_user_mask; /* events visible to userspace */
2075static u32 hotkey_acpi_mask; /* events enabled in firmware */
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002076
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02002077static unsigned int hotkey_report_mode;
2078
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002079static u16 *hotkey_keycode_map;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002080
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03002081static struct attribute_set *hotkey_dev_attributes;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002082
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002083static void tpacpi_driver_event(const unsigned int hkey_event);
2084static void hotkey_driver_event(const unsigned int scancode);
2085
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002086/* HKEY.MHKG() return bits */
2087#define TP_HOTKEY_TABLET_MASK (1 << 3)
2088
Johannes Berg19d337d2009-06-02 13:01:37 +02002089static int hotkey_get_wlsw(void)
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002090{
Johannes Berg19d337d2009-06-02 13:01:37 +02002091 int status;
2092
2093 if (!tp_features.hotkey_wlsw)
2094 return -ENODEV;
2095
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02002096#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
Johannes Berg19d337d2009-06-02 13:01:37 +02002097 if (dbg_wlswemul)
2098 return (tpacpi_wlsw_emulstate) ?
2099 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02002100#endif
Johannes Berg19d337d2009-06-02 13:01:37 +02002101
2102 if (!acpi_evalf(hkey_handle, &status, "WLSW", "d"))
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002103 return -EIO;
Johannes Berg19d337d2009-06-02 13:01:37 +02002104
2105 return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002106}
2107
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002108static int hotkey_get_tablet_mode(int *status)
2109{
2110 int s;
2111
2112 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
2113 return -EIO;
2114
Henrique de Moraes Holschuhcee47f52008-03-04 14:29:21 -08002115 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
2116 return 0;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002117}
2118
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002119/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002120 * Reads current event mask from firmware, and updates
2121 * hotkey_acpi_mask accordingly. Also resets any bits
2122 * from hotkey_user_mask that are unavailable to be
2123 * delivered (shadow requirement of the userspace ABI).
2124 *
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002125 * Call with hotkey_mutex held
2126 */
2127static int hotkey_mask_get(void)
2128{
2129 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002130 u32 m = 0;
2131
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002132 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002133 return -EIO;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002134
2135 hotkey_acpi_mask = m;
2136 } else {
2137 /* no mask support doesn't mean no event support... */
2138 hotkey_acpi_mask = hotkey_all_mask;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002139 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002140
2141 /* sync userspace-visible mask */
2142 hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002143
2144 return 0;
2145}
2146
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002147void static hotkey_mask_warn_incomplete_mask(void)
2148{
2149 /* log only what the user can fix... */
2150 const u32 wantedmask = hotkey_driver_mask &
2151 ~(hotkey_acpi_mask | hotkey_source_mask) &
2152 (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
2153
2154 if (wantedmask)
2155 printk(TPACPI_NOTICE
2156 "required events 0x%08x not enabled!\n",
2157 wantedmask);
2158}
2159
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002160/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002161 * Set the firmware mask when supported
2162 *
2163 * Also calls hotkey_mask_get to update hotkey_acpi_mask.
2164 *
2165 * NOTE: does not set bits in hotkey_user_mask, but may reset them.
2166 *
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002167 * Call with hotkey_mutex held
2168 */
2169static int hotkey_mask_set(u32 mask)
2170{
2171 int i;
2172 int rc = 0;
2173
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002174 const u32 fwmask = mask & ~hotkey_source_mask;
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -03002175
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002176 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002177 for (i = 0; i < 32; i++) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002178 if (!acpi_evalf(hkey_handle,
2179 NULL, "MHKM", "vdd", i + 1,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002180 !!(mask & (1 << i)))) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002181 rc = -EIO;
2182 break;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002183 }
2184 }
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002185 }
2186
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002187 /*
2188 * We *must* make an inconditional call to hotkey_mask_get to
2189 * refresh hotkey_acpi_mask and update hotkey_user_mask
2190 *
2191 * Take the opportunity to also log when we cannot _enable_
2192 * a given event.
2193 */
2194 if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
2195 printk(TPACPI_NOTICE
2196 "asked for hotkey mask 0x%08x, but "
2197 "firmware forced it to 0x%08x\n",
2198 fwmask, hotkey_acpi_mask);
2199 }
2200
Henrique de Moraes Holschuh6b30eb72009-12-09 01:36:25 +00002201 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING)
2202 hotkey_mask_warn_incomplete_mask();
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002203
2204 return rc;
2205}
2206
2207/*
2208 * Sets hotkey_user_mask and tries to set the firmware mask
2209 *
2210 * Call with hotkey_mutex held
2211 */
2212static int hotkey_user_mask_set(const u32 mask)
2213{
2214 int rc;
2215
2216 /* Give people a chance to notice they are doing something that
2217 * is bound to go boom on their users sooner or later */
2218 if (!tp_warned.hotkey_mask_ff &&
2219 (mask == 0xffff || mask == 0xffffff ||
2220 mask == 0xffffffff)) {
2221 tp_warned.hotkey_mask_ff = 1;
2222 printk(TPACPI_NOTICE
2223 "setting the hotkey mask to 0x%08x is likely "
2224 "not the best way to go about it\n", mask);
2225 printk(TPACPI_NOTICE
2226 "please consider using the driver defaults, "
2227 "and refer to up-to-date thinkpad-acpi "
2228 "documentation\n");
2229 }
2230
2231 /* Try to enable what the user asked for, plus whatever we need.
2232 * this syncs everything but won't enable bits in hotkey_user_mask */
2233 rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
2234
2235 /* Enable the available bits in hotkey_user_mask */
2236 hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
2237
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002238 return rc;
2239}
2240
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002241/*
2242 * Sets the driver hotkey mask.
2243 *
2244 * Can be called even if the hotkey subdriver is inactive
2245 */
2246static int tpacpi_hotkey_driver_mask_set(const u32 mask)
2247{
2248 int rc;
2249
2250 /* Do the right thing if hotkey_init has not been called yet */
2251 if (!tp_features.hotkey) {
2252 hotkey_driver_mask = mask;
2253 return 0;
2254 }
2255
2256 mutex_lock(&hotkey_mutex);
2257
2258 HOTKEY_CONFIG_CRITICAL_START
2259 hotkey_driver_mask = mask;
Henrique de Moraes Holschuhb684a362009-09-26 21:42:49 -03002260#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002261 hotkey_source_mask |= (mask & ~hotkey_all_mask);
Henrique de Moraes Holschuhb684a362009-09-26 21:42:49 -03002262#endif
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002263 HOTKEY_CONFIG_CRITICAL_END
2264
2265 rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) &
2266 ~hotkey_source_mask);
2267 mutex_unlock(&hotkey_mutex);
2268
2269 return rc;
2270}
2271
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002272static int hotkey_status_get(int *status)
2273{
2274 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
2275 return -EIO;
2276
2277 return 0;
2278}
2279
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002280static int hotkey_status_set(bool enable)
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002281{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002282 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002283 return -EIO;
2284
2285 return 0;
2286}
2287
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002288static void tpacpi_input_send_tabletsw(void)
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002289{
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002290 int state;
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002291
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002292 if (tp_features.hotkey_tablet &&
2293 !hotkey_get_tablet_mode(&state)) {
2294 mutex_lock(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002295
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002296 input_report_switch(tpacpi_inputdev,
2297 SW_TABLET_MODE, !!state);
2298 input_sync(tpacpi_inputdev);
2299
2300 mutex_unlock(&tpacpi_inputdev_send_mutex);
2301 }
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002302}
2303
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002304/* Do NOT call without validating scancode first */
2305static void tpacpi_input_send_key(const unsigned int scancode)
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002306{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002307 const unsigned int keycode = hotkey_keycode_map[scancode];
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002308
2309 if (keycode != KEY_RESERVED) {
2310 mutex_lock(&tpacpi_inputdev_send_mutex);
2311
2312 input_report_key(tpacpi_inputdev, keycode, 1);
2313 if (keycode == KEY_UNKNOWN)
2314 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
2315 scancode);
2316 input_sync(tpacpi_inputdev);
2317
2318 input_report_key(tpacpi_inputdev, keycode, 0);
2319 if (keycode == KEY_UNKNOWN)
2320 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
2321 scancode);
2322 input_sync(tpacpi_inputdev);
2323
2324 mutex_unlock(&tpacpi_inputdev_send_mutex);
2325 }
2326}
2327
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002328/* Do NOT call without validating scancode first */
2329static void tpacpi_input_send_key_masked(const unsigned int scancode)
2330{
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002331 hotkey_driver_event(scancode);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002332 if (hotkey_user_mask & (1 << scancode))
2333 tpacpi_input_send_key(scancode);
2334}
2335
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002336#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2337static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
2338
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002339/* Do NOT call without validating scancode first */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002340static void tpacpi_hotkey_send_key(unsigned int scancode)
2341{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002342 tpacpi_input_send_key_masked(scancode);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002343 if (hotkey_report_mode < 2) {
2344 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03002345 0x80, TP_HKEY_EV_HOTKEY_BASE + scancode);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002346 }
2347}
2348
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002349static void hotkey_read_nvram(struct tp_nvram_state *n, const u32 m)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002350{
2351 u8 d;
2352
2353 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
2354 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
2355 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
2356 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
2357 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
2358 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
2359 }
2360 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
2361 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
2362 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
2363 }
2364 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
2365 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
2366 n->displayexp_toggle =
2367 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
2368 }
2369 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
2370 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
2371 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
2372 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
2373 n->brightness_toggle =
2374 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
2375 }
2376 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
2377 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
2378 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
2379 >> TP_NVRAM_POS_LEVEL_VOLUME;
2380 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2381 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2382 }
2383}
2384
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002385static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
2386 struct tp_nvram_state *newn,
2387 const u32 event_mask)
2388{
2389
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002390#define TPACPI_COMPARE_KEY(__scancode, __member) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002391 do { \
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002392 if ((event_mask & (1 << __scancode)) && \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002393 oldn->__member != newn->__member) \
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002394 tpacpi_hotkey_send_key(__scancode); \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002395 } while (0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002396
2397#define TPACPI_MAY_SEND_KEY(__scancode) \
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002398 do { \
2399 if (event_mask & (1 << __scancode)) \
2400 tpacpi_hotkey_send_key(__scancode); \
2401 } while (0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002402
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002403 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2404 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2405 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2406 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2407
2408 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2409
2410 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2411
2412 /* handle volume */
2413 if (oldn->volume_toggle != newn->volume_toggle) {
2414 if (oldn->mute != newn->mute) {
2415 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2416 }
2417 if (oldn->volume_level > newn->volume_level) {
2418 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2419 } else if (oldn->volume_level < newn->volume_level) {
2420 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2421 } else if (oldn->mute == newn->mute) {
2422 /* repeated key presses that didn't change state */
2423 if (newn->mute) {
2424 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2425 } else if (newn->volume_level != 0) {
2426 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2427 } else {
2428 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2429 }
2430 }
2431 }
2432
2433 /* handle brightness */
2434 if (oldn->brightness_toggle != newn->brightness_toggle) {
2435 if (oldn->brightness_level < newn->brightness_level) {
2436 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2437 } else if (oldn->brightness_level > newn->brightness_level) {
2438 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2439 } else {
2440 /* repeated key presses that didn't change state */
2441 if (newn->brightness_level != 0) {
2442 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2443 } else {
2444 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2445 }
2446 }
2447 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002448
2449#undef TPACPI_COMPARE_KEY
2450#undef TPACPI_MAY_SEND_KEY
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002451}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002452
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002453/*
2454 * Polling driver
2455 *
2456 * We track all events in hotkey_source_mask all the time, since
2457 * most of them are edge-based. We only issue those requested by
2458 * hotkey_user_mask or hotkey_driver_mask, though.
2459 */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002460static int hotkey_kthread(void *data)
2461{
2462 struct tp_nvram_state s[2];
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002463 u32 poll_mask, event_mask;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002464 unsigned int si, so;
2465 unsigned long t;
2466 unsigned int change_detector, must_reset;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002467 unsigned int poll_freq;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002468
2469 mutex_lock(&hotkey_thread_mutex);
2470
2471 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2472 goto exit;
2473
2474 set_freezable();
2475
2476 so = 0;
2477 si = 1;
2478 t = 0;
2479
2480 /* Initial state for compares */
2481 mutex_lock(&hotkey_thread_data_mutex);
2482 change_detector = hotkey_config_change;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002483 poll_mask = hotkey_source_mask;
2484 event_mask = hotkey_source_mask &
2485 (hotkey_driver_mask | hotkey_user_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002486 poll_freq = hotkey_poll_freq;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002487 mutex_unlock(&hotkey_thread_data_mutex);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002488 hotkey_read_nvram(&s[so], poll_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002489
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002490 while (!kthread_should_stop()) {
2491 if (t == 0) {
2492 if (likely(poll_freq))
2493 t = 1000/poll_freq;
2494 else
2495 t = 100; /* should never happen... */
2496 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002497 t = msleep_interruptible(t);
2498 if (unlikely(kthread_should_stop()))
2499 break;
2500 must_reset = try_to_freeze();
2501 if (t > 0 && !must_reset)
2502 continue;
2503
2504 mutex_lock(&hotkey_thread_data_mutex);
2505 if (must_reset || hotkey_config_change != change_detector) {
2506 /* forget old state on thaw or config change */
2507 si = so;
2508 t = 0;
2509 change_detector = hotkey_config_change;
2510 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002511 poll_mask = hotkey_source_mask;
2512 event_mask = hotkey_source_mask &
2513 (hotkey_driver_mask | hotkey_user_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002514 poll_freq = hotkey_poll_freq;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002515 mutex_unlock(&hotkey_thread_data_mutex);
2516
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002517 if (likely(poll_mask)) {
2518 hotkey_read_nvram(&s[si], poll_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002519 if (likely(si != so)) {
2520 hotkey_compare_and_issue_event(&s[so], &s[si],
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002521 event_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002522 }
2523 }
2524
2525 so = si;
2526 si ^= 1;
2527 }
2528
2529exit:
2530 mutex_unlock(&hotkey_thread_mutex);
2531 return 0;
2532}
2533
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002534/* call with hotkey_mutex held */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002535static void hotkey_poll_stop_sync(void)
2536{
2537 if (tpacpi_hotkey_task) {
2538 if (frozen(tpacpi_hotkey_task) ||
2539 freezing(tpacpi_hotkey_task))
2540 thaw_process(tpacpi_hotkey_task);
2541
2542 kthread_stop(tpacpi_hotkey_task);
2543 tpacpi_hotkey_task = NULL;
2544 mutex_lock(&hotkey_thread_mutex);
2545 /* at this point, the thread did exit */
2546 mutex_unlock(&hotkey_thread_mutex);
2547 }
2548}
2549
2550/* call with hotkey_mutex held */
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002551static void hotkey_poll_setup(bool may_warn)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002552{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002553 const u32 poll_driver_mask = hotkey_driver_mask & hotkey_source_mask;
2554 const u32 poll_user_mask = hotkey_user_mask & hotkey_source_mask;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002555
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002556 if (hotkey_poll_freq > 0 &&
2557 (poll_driver_mask ||
2558 (poll_user_mask && tpacpi_inputdev->users > 0))) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002559 if (!tpacpi_hotkey_task) {
2560 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03002561 NULL, TPACPI_NVRAM_KTHREAD_NAME);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002562 if (IS_ERR(tpacpi_hotkey_task)) {
2563 tpacpi_hotkey_task = NULL;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002564 printk(TPACPI_ERR
2565 "could not create kernel thread "
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002566 "for hotkey polling\n");
2567 }
2568 }
2569 } else {
2570 hotkey_poll_stop_sync();
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002571 if (may_warn && (poll_driver_mask || poll_user_mask) &&
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002572 hotkey_poll_freq == 0) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002573 printk(TPACPI_NOTICE
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002574 "hot keys 0x%08x and/or events 0x%08x "
2575 "require polling, which is currently "
2576 "disabled\n",
2577 poll_user_mask, poll_driver_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002578 }
2579 }
2580}
2581
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002582static void hotkey_poll_setup_safe(bool may_warn)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002583{
2584 mutex_lock(&hotkey_mutex);
2585 hotkey_poll_setup(may_warn);
2586 mutex_unlock(&hotkey_mutex);
2587}
2588
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002589/* call with hotkey_mutex held */
2590static void hotkey_poll_set_freq(unsigned int freq)
2591{
2592 if (!freq)
2593 hotkey_poll_stop_sync();
2594
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002595 hotkey_poll_freq = freq;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002596}
2597
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02002598#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2599
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002600static void hotkey_poll_setup_safe(bool __unused)
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02002601{
2602}
2603
2604#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2605
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002606static int hotkey_inputdev_open(struct input_dev *dev)
2607{
2608 switch (tpacpi_lifecycle) {
2609 case TPACPI_LIFE_INIT:
2610 /*
2611 * hotkey_init will call hotkey_poll_setup_safe
2612 * at the appropriate moment
2613 */
2614 return 0;
2615 case TPACPI_LIFE_EXITING:
2616 return -EBUSY;
2617 case TPACPI_LIFE_RUNNING:
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002618 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002619 return 0;
2620 }
2621
2622 /* Should only happen if tpacpi_lifecycle is corrupt */
2623 BUG();
2624 return -EBUSY;
2625}
2626
2627static void hotkey_inputdev_close(struct input_dev *dev)
2628{
2629 /* disable hotkey polling when possible */
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002630 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING &&
2631 !(hotkey_source_mask & hotkey_driver_mask))
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002632 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002633}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002634
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002635/* sysfs hotkey enable ------------------------------------------------- */
2636static ssize_t hotkey_enable_show(struct device *dev,
2637 struct device_attribute *attr,
2638 char *buf)
2639{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002640 int res, status;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002641
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002642 printk_deprecated_attribute("hotkey_enable",
2643 "Hotkey reporting is always enabled");
2644
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002645 res = hotkey_status_get(&status);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002646 if (res)
2647 return res;
2648
2649 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2650}
2651
2652static ssize_t hotkey_enable_store(struct device *dev,
2653 struct device_attribute *attr,
2654 const char *buf, size_t count)
2655{
2656 unsigned long t;
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002657
2658 printk_deprecated_attribute("hotkey_enable",
2659 "Hotkeys can be disabled through hotkey_mask");
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002660
2661 if (parse_strtoul(buf, 1, &t))
2662 return -EINVAL;
2663
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002664 if (t == 0)
2665 return -EPERM;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002666
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002667 return count;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002668}
2669
2670static struct device_attribute dev_attr_hotkey_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002671 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002672 hotkey_enable_show, hotkey_enable_store);
2673
2674/* sysfs hotkey mask --------------------------------------------------- */
2675static ssize_t hotkey_mask_show(struct device *dev,
2676 struct device_attribute *attr,
2677 char *buf)
2678{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002679 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_user_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002680}
2681
2682static ssize_t hotkey_mask_store(struct device *dev,
2683 struct device_attribute *attr,
2684 const char *buf, size_t count)
2685{
2686 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002687 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002688
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002689 if (parse_strtoul(buf, 0xffffffffUL, &t))
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002690 return -EINVAL;
2691
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002692 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002693 return -ERESTARTSYS;
2694
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002695 res = hotkey_user_mask_set(t);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002696
2697#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002698 hotkey_poll_setup(true);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002699#endif
2700
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002701 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002702
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002703 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2704
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002705 return (res) ? res : count;
2706}
2707
2708static struct device_attribute dev_attr_hotkey_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002709 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002710 hotkey_mask_show, hotkey_mask_store);
2711
2712/* sysfs hotkey bios_enabled ------------------------------------------- */
2713static ssize_t hotkey_bios_enabled_show(struct device *dev,
2714 struct device_attribute *attr,
2715 char *buf)
2716{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002717 return sprintf(buf, "0\n");
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002718}
2719
2720static struct device_attribute dev_attr_hotkey_bios_enabled =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002721 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002722
2723/* sysfs hotkey bios_mask ---------------------------------------------- */
2724static ssize_t hotkey_bios_mask_show(struct device *dev,
2725 struct device_attribute *attr,
2726 char *buf)
2727{
Henrique de Moraes Holschuh06777be2009-09-12 15:22:15 -03002728 printk_deprecated_attribute("hotkey_bios_mask",
2729 "This attribute is useless.");
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002730 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002731}
2732
2733static struct device_attribute dev_attr_hotkey_bios_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002734 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002735
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002736/* sysfs hotkey all_mask ----------------------------------------------- */
2737static ssize_t hotkey_all_mask_show(struct device *dev,
2738 struct device_attribute *attr,
2739 char *buf)
2740{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002741 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2742 hotkey_all_mask | hotkey_source_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002743}
2744
2745static struct device_attribute dev_attr_hotkey_all_mask =
2746 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
2747
2748/* sysfs hotkey recommended_mask --------------------------------------- */
2749static ssize_t hotkey_recommended_mask_show(struct device *dev,
2750 struct device_attribute *attr,
2751 char *buf)
2752{
2753 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002754 (hotkey_all_mask | hotkey_source_mask)
2755 & ~hotkey_reserved_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002756}
2757
2758static struct device_attribute dev_attr_hotkey_recommended_mask =
2759 __ATTR(hotkey_recommended_mask, S_IRUGO,
2760 hotkey_recommended_mask_show, NULL);
2761
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002762#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2763
2764/* sysfs hotkey hotkey_source_mask ------------------------------------- */
2765static ssize_t hotkey_source_mask_show(struct device *dev,
2766 struct device_attribute *attr,
2767 char *buf)
2768{
2769 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2770}
2771
2772static ssize_t hotkey_source_mask_store(struct device *dev,
2773 struct device_attribute *attr,
2774 const char *buf, size_t count)
2775{
2776 unsigned long t;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002777 u32 r_ev;
2778 int rc;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002779
2780 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2781 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2782 return -EINVAL;
2783
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002784 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002785 return -ERESTARTSYS;
2786
2787 HOTKEY_CONFIG_CRITICAL_START
2788 hotkey_source_mask = t;
2789 HOTKEY_CONFIG_CRITICAL_END
2790
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002791 rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
2792 ~hotkey_source_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002793 hotkey_poll_setup(true);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002794
2795 /* check if events needed by the driver got disabled */
2796 r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
2797 & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002798
2799 mutex_unlock(&hotkey_mutex);
2800
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002801 if (rc < 0)
2802 printk(TPACPI_ERR "hotkey_source_mask: failed to update the"
2803 "firmware event mask!\n");
2804
2805 if (r_ev)
2806 printk(TPACPI_NOTICE "hotkey_source_mask: "
2807 "some important events were disabled: "
2808 "0x%04x\n", r_ev);
2809
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002810 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2811
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002812 return (rc < 0) ? rc : count;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002813}
2814
2815static struct device_attribute dev_attr_hotkey_source_mask =
2816 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2817 hotkey_source_mask_show, hotkey_source_mask_store);
2818
2819/* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2820static ssize_t hotkey_poll_freq_show(struct device *dev,
2821 struct device_attribute *attr,
2822 char *buf)
2823{
2824 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2825}
2826
2827static ssize_t hotkey_poll_freq_store(struct device *dev,
2828 struct device_attribute *attr,
2829 const char *buf, size_t count)
2830{
2831 unsigned long t;
2832
2833 if (parse_strtoul(buf, 25, &t))
2834 return -EINVAL;
2835
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002836 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002837 return -ERESTARTSYS;
2838
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002839 hotkey_poll_set_freq(t);
2840 hotkey_poll_setup(true);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002841
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002842 mutex_unlock(&hotkey_mutex);
2843
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002844 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2845
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002846 return count;
2847}
2848
2849static struct device_attribute dev_attr_hotkey_poll_freq =
2850 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2851 hotkey_poll_freq_show, hotkey_poll_freq_store);
2852
2853#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2854
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002855/* sysfs hotkey radio_sw (pollable) ------------------------------------ */
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002856static ssize_t hotkey_radio_sw_show(struct device *dev,
2857 struct device_attribute *attr,
2858 char *buf)
2859{
Johannes Berg19d337d2009-06-02 13:01:37 +02002860 int res;
2861 res = hotkey_get_wlsw();
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002862 if (res < 0)
2863 return res;
2864
Johannes Berg19d337d2009-06-02 13:01:37 +02002865 /* Opportunistic update */
2866 tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
2867
2868 return snprintf(buf, PAGE_SIZE, "%d\n",
2869 (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002870}
2871
2872static struct device_attribute dev_attr_hotkey_radio_sw =
2873 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2874
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002875static void hotkey_radio_sw_notify_change(void)
2876{
2877 if (tp_features.hotkey_wlsw)
2878 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2879 "hotkey_radio_sw");
2880}
2881
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002882/* sysfs hotkey tablet mode (pollable) --------------------------------- */
2883static ssize_t hotkey_tablet_mode_show(struct device *dev,
2884 struct device_attribute *attr,
2885 char *buf)
2886{
2887 int res, s;
2888 res = hotkey_get_tablet_mode(&s);
2889 if (res < 0)
2890 return res;
2891
2892 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2893}
2894
2895static struct device_attribute dev_attr_hotkey_tablet_mode =
2896 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2897
2898static void hotkey_tablet_mode_notify_change(void)
2899{
2900 if (tp_features.hotkey_tablet)
2901 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2902 "hotkey_tablet_mode");
2903}
2904
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002905/* sysfs hotkey report_mode -------------------------------------------- */
2906static ssize_t hotkey_report_mode_show(struct device *dev,
2907 struct device_attribute *attr,
2908 char *buf)
2909{
2910 return snprintf(buf, PAGE_SIZE, "%d\n",
2911 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2912}
2913
2914static struct device_attribute dev_attr_hotkey_report_mode =
2915 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2916
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002917/* sysfs wakeup reason (pollable) -------------------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002918static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2919 struct device_attribute *attr,
2920 char *buf)
2921{
2922 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2923}
2924
2925static struct device_attribute dev_attr_hotkey_wakeup_reason =
2926 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2927
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02002928static void hotkey_wakeup_reason_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002929{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002930 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2931 "wakeup_reason");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002932}
2933
2934/* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002935static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2936 struct device_attribute *attr,
2937 char *buf)
2938{
2939 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2940}
2941
2942static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2943 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2944 hotkey_wakeup_hotunplug_complete_show, NULL);
2945
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02002946static void hotkey_wakeup_hotunplug_complete_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002947{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002948 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2949 "wakeup_hotunplug_complete");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002950}
2951
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002952/* --------------------------------------------------------------------- */
2953
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002954static struct attribute *hotkey_attributes[] __initdata = {
2955 &dev_attr_hotkey_enable.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002956 &dev_attr_hotkey_bios_enabled.attr,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002957 &dev_attr_hotkey_bios_mask.attr,
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002958 &dev_attr_hotkey_report_mode.attr,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002959 &dev_attr_hotkey_wakeup_reason.attr,
2960 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002961 &dev_attr_hotkey_mask.attr,
2962 &dev_attr_hotkey_all_mask.attr,
2963 &dev_attr_hotkey_recommended_mask.attr,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002964#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002965 &dev_attr_hotkey_source_mask.attr,
2966 &dev_attr_hotkey_poll_freq.attr,
2967#endif
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002968};
2969
Johannes Berg19d337d2009-06-02 13:01:37 +02002970/*
2971 * Sync both the hw and sw blocking state of all switches
2972 */
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002973static void tpacpi_send_radiosw_update(void)
2974{
2975 int wlsw;
2976
Johannes Berg19d337d2009-06-02 13:01:37 +02002977 /*
2978 * We must sync all rfkill controllers *before* issuing any
2979 * rfkill input events, or we will race the rfkill core input
2980 * handler.
2981 *
2982 * tpacpi_inputdev_send_mutex works as a syncronization point
2983 * for the above.
2984 *
2985 * We optimize to avoid numerous calls to hotkey_get_wlsw.
2986 */
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002987
Johannes Berg19d337d2009-06-02 13:01:37 +02002988 wlsw = hotkey_get_wlsw();
2989
2990 /* Sync hw blocking state first if it is hw-blocked */
2991 if (wlsw == TPACPI_RFK_RADIO_OFF)
2992 tpacpi_rfk_update_hwblock_state(true);
2993
2994 /* Sync sw blocking state */
2995 tpacpi_rfk_update_swstate_all();
2996
2997 /* Sync hw blocking state last if it is hw-unblocked */
2998 if (wlsw == TPACPI_RFK_RADIO_ON)
2999 tpacpi_rfk_update_hwblock_state(false);
3000
3001 /* Issue rfkill input event for WLSW switch */
3002 if (!(wlsw < 0)) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003003 mutex_lock(&tpacpi_inputdev_send_mutex);
3004
3005 input_report_switch(tpacpi_inputdev,
Johannes Berg19d337d2009-06-02 13:01:37 +02003006 SW_RFKILL_ALL, (wlsw > 0));
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003007 input_sync(tpacpi_inputdev);
3008
3009 mutex_unlock(&tpacpi_inputdev_send_mutex);
3010 }
Johannes Berg19d337d2009-06-02 13:01:37 +02003011
3012 /*
3013 * this can be unconditional, as we will poll state again
3014 * if userspace uses the notify to read data
3015 */
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003016 hotkey_radio_sw_notify_change();
3017}
3018
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003019static void hotkey_exit(void)
3020{
3021#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003022 mutex_lock(&hotkey_mutex);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003023 hotkey_poll_stop_sync();
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003024 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003025#endif
3026
3027 if (hotkey_dev_attributes)
3028 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3029
3030 kfree(hotkey_keycode_map);
3031
Henrique de Moraes Holschuh4be73002009-09-20 14:09:23 -03003032 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003033 "restoring original HKEY status and mask\n");
3034 /* yes, there is a bitwise or below, we want the
3035 * functions to be called even if one of them fail */
3036 if (((tp_features.hotkey_mask &&
3037 hotkey_mask_set(hotkey_orig_mask)) |
3038 hotkey_status_set(false)) != 0)
Henrique de Moraes Holschuh4be73002009-09-20 14:09:23 -03003039 printk(TPACPI_ERR
3040 "failed to restore hot key mask "
3041 "to BIOS defaults\n");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003042}
3043
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003044static void __init hotkey_unmap(const unsigned int scancode)
3045{
3046 if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
3047 clear_bit(hotkey_keycode_map[scancode],
3048 tpacpi_inputdev->keybit);
3049 hotkey_keycode_map[scancode] = KEY_RESERVED;
3050 }
3051}
3052
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003053/*
3054 * HKEY quirks:
3055 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
3056 */
3057
3058#define TPACPI_HK_Q_INIMASK 0x0001
3059
3060static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
3061 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
3062 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
3063 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
3064 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
3065 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
3066 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
3067 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
3068 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
3069 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
3070 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
3071 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
3072 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
3073 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
3074 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
3075 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
3076 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
3077 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
3078 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
3079 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
3080};
3081
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003082static int __init hotkey_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003083{
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003084 /* Requirements for changing the default keymaps:
3085 *
3086 * 1. Many of the keys are mapped to KEY_RESERVED for very
3087 * good reasons. Do not change them unless you have deep
3088 * knowledge on the IBM and Lenovo ThinkPad firmware for
3089 * the various ThinkPad models. The driver behaves
3090 * differently for KEY_RESERVED: such keys have their
3091 * hot key mask *unset* in mask_recommended, and also
3092 * in the initial hot key mask programmed into the
3093 * firmware at driver load time, which means the firm-
3094 * ware may react very differently if you change them to
3095 * something else;
3096 *
3097 * 2. You must be subscribed to the linux-thinkpad and
3098 * ibm-acpi-devel mailing lists, and you should read the
3099 * list archives since 2007 if you want to change the
3100 * keymaps. This requirement exists so that you will
3101 * know the past history of problems with the thinkpad-
3102 * acpi driver keymaps, and also that you will be
3103 * listening to any bug reports;
3104 *
3105 * 3. Do not send thinkpad-acpi specific patches directly to
3106 * for merging, *ever*. Send them to the linux-acpi
3107 * mailinglist for comments. Merging is to be done only
3108 * through acpi-test and the ACPI maintainer.
3109 *
3110 * If the above is too much to ask, don't change the keymap.
3111 * Ask the thinkpad-acpi maintainer to do it, instead.
3112 */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003113 static u16 ibm_keycode_map[] __initdata = {
3114 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3115 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
3116 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3117 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003118
3119 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003120 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3121 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3122 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003123
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003124 /* brightness: firmware always reacts to them */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003125 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003126 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003127
3128 /* Thinklight: firmware always react to it */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003129 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003130
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003131 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3132 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003133
3134 /* Volume: firmware always react to it and reprograms
3135 * the built-in *extra* mixer. Never map it to control
3136 * another mixer by default. */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003137 KEY_RESERVED, /* 0x14: VOLUME UP */
3138 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3139 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003140
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003141 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003142
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003143 /* (assignments unknown, please report if found) */
3144 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3145 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3146 };
3147 static u16 lenovo_keycode_map[] __initdata = {
3148 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3149 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
3150 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3151 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003152
3153 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003154 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3155 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3156 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003157
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003158 /* These should be enabled --only-- when ACPI video
3159 * is disabled (i.e. in "vendor" mode), and are handled
3160 * in a special way by the init code */
3161 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
3162 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003163
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003164 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003165
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003166 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3167 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003168
3169 /* Volume: z60/z61, T60 (BIOS version?): firmware always
3170 * react to it and reprograms the built-in *extra* mixer.
3171 * Never map it to control another mixer by default.
3172 *
3173 * T60?, T61, R60?, R61: firmware and EC tries to send
3174 * these over the regular keyboard, so these are no-ops,
3175 * but there are still weird bugs re. MUTE, so do not
3176 * change unless you get test reports from all Lenovo
3177 * models. May cause the BIOS to interfere with the
3178 * HDA mixer.
3179 */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003180 KEY_RESERVED, /* 0x14: VOLUME UP */
3181 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3182 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003183
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003184 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003185
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003186 /* (assignments unknown, please report if found) */
3187 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3188 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3189 };
3190
3191#define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
3192#define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
3193#define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
3194
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003195 int res, i;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003196 int status;
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03003197 int hkeyv;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003198 bool radiosw_state = false;
3199 bool tabletsw_state = false;
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03003200
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003201 unsigned long quirks;
3202
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003203 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3204 "initializing hotkey subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003205
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003206 BUG_ON(!tpacpi_inputdev);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003207 BUG_ON(tpacpi_inputdev->open != NULL ||
3208 tpacpi_inputdev->close != NULL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003209
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003210 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003211 mutex_init(&hotkey_mutex);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003212
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003213#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3214 mutex_init(&hotkey_thread_mutex);
3215 mutex_init(&hotkey_thread_data_mutex);
3216#endif
3217
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003218 /* hotkey not supported on 570 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003219 tp_features.hotkey = hkey_handle != NULL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003220
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003221 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3222 "hotkeys are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003223 str_supported(tp_features.hotkey));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003224
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003225 if (!tp_features.hotkey)
3226 return 1;
3227
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003228 quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
3229 ARRAY_SIZE(tpacpi_hotkey_qtable));
3230
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03003231 tpacpi_disable_brightness_delay();
3232
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003233 /* MUST have enough space for all attributes to be added to
3234 * hotkey_dev_attributes */
3235 hotkey_dev_attributes = create_attr_set(
3236 ARRAY_SIZE(hotkey_attributes) + 2,
3237 NULL);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003238 if (!hotkey_dev_attributes)
3239 return -ENOMEM;
3240 res = add_many_to_attr_set(hotkey_dev_attributes,
3241 hotkey_attributes,
3242 ARRAY_SIZE(hotkey_attributes));
3243 if (res)
3244 goto err_exit;
3245
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003246 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003247 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
3248 for HKEY interface version 0x100 */
3249 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
3250 if ((hkeyv >> 8) != 1) {
3251 printk(TPACPI_ERR "unknown version of the "
3252 "HKEY interface: 0x%x\n", hkeyv);
3253 printk(TPACPI_ERR "please report this to %s\n",
3254 TPACPI_MAIL);
3255 } else {
3256 /*
3257 * MHKV 0x100 in A31, R40, R40e,
3258 * T4x, X31, and later
3259 */
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003260 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3261 "firmware HKEY interface version: 0x%x\n",
3262 hkeyv);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003263
3264 /* Paranoia check AND init hotkey_all_mask */
3265 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3266 "MHKA", "qd")) {
3267 printk(TPACPI_ERR
3268 "missing MHKA handler, "
3269 "please report this to %s\n",
3270 TPACPI_MAIL);
3271 /* Fallback: pre-init for FN+F3,F4,F12 */
3272 hotkey_all_mask = 0x080cU;
3273 } else {
3274 tp_features.hotkey_mask = 1;
3275 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003276 }
3277 }
3278
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003279 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3280 "hotkey masks are %s\n",
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003281 str_supported(tp_features.hotkey_mask));
3282
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003283 /* Init hotkey_all_mask if not initialized yet */
3284 if (!tp_features.hotkey_mask && !hotkey_all_mask &&
3285 (quirks & TPACPI_HK_Q_INIMASK))
3286 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003287
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003288 /* Init hotkey_acpi_mask and hotkey_orig_mask */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003289 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003290 /* hotkey_source_mask *must* be zero for
3291 * the first hotkey_mask_get to return hotkey_orig_mask */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003292 res = hotkey_mask_get();
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003293 if (res)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003294 goto err_exit;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003295
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003296 hotkey_orig_mask = hotkey_acpi_mask;
3297 } else {
3298 hotkey_orig_mask = hotkey_all_mask;
3299 hotkey_acpi_mask = hotkey_all_mask;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003300 }
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003301
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003302#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3303 if (dbg_wlswemul) {
3304 tp_features.hotkey_wlsw = 1;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003305 radiosw_state = !!tpacpi_wlsw_emulstate;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003306 printk(TPACPI_INFO
3307 "radio switch emulation enabled\n");
3308 } else
3309#endif
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003310 /* Not all thinkpads have a hardware radio switch */
3311 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
3312 tp_features.hotkey_wlsw = 1;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003313 radiosw_state = !!status;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003314 printk(TPACPI_INFO
3315 "radio switch found; radios are %s\n",
3316 enabled(status, 0));
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003317 }
3318 if (tp_features.hotkey_wlsw)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003319 res = add_to_attr_set(hotkey_dev_attributes,
3320 &dev_attr_hotkey_radio_sw.attr);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003321
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003322 /* For X41t, X60t, X61t Tablets... */
3323 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
3324 tp_features.hotkey_tablet = 1;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003325 tabletsw_state = !!(status & TP_HOTKEY_TABLET_MASK);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003326 printk(TPACPI_INFO
3327 "possible tablet mode switch found; "
3328 "ThinkPad in %s mode\n",
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003329 (tabletsw_state) ? "tablet" : "laptop");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003330 res = add_to_attr_set(hotkey_dev_attributes,
3331 &dev_attr_hotkey_tablet_mode.attr);
3332 }
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02003333
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003334 if (!res)
3335 res = register_attr_set_with_sysfs(
3336 hotkey_dev_attributes,
3337 &tpacpi_pdev->dev.kobj);
3338 if (res)
3339 goto err_exit;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003340
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003341 /* Set up key map */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003342
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003343 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
3344 GFP_KERNEL);
3345 if (!hotkey_keycode_map) {
3346 printk(TPACPI_ERR
3347 "failed to allocate memory for key map\n");
3348 res = -ENOMEM;
3349 goto err_exit;
3350 }
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003351
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003352 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003353 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003354 "using Lenovo default hot key map\n");
3355 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
3356 TPACPI_HOTKEY_MAP_SIZE);
3357 } else {
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003358 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003359 "using IBM default hot key map\n");
3360 memcpy(hotkey_keycode_map, &ibm_keycode_map,
3361 TPACPI_HOTKEY_MAP_SIZE);
3362 }
3363
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003364 input_set_capability(tpacpi_inputdev, EV_MSC, MSC_SCAN);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003365 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
3366 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3367 tpacpi_inputdev->keycode = hotkey_keycode_map;
3368 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3369 if (hotkey_keycode_map[i] != KEY_RESERVED) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003370 input_set_capability(tpacpi_inputdev, EV_KEY,
3371 hotkey_keycode_map[i]);
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003372 } else {
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003373 if (i < sizeof(hotkey_reserved_mask)*8)
3374 hotkey_reserved_mask |= 1 << i;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003375 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003376 }
3377
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003378 if (tp_features.hotkey_wlsw) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003379 input_set_capability(tpacpi_inputdev, EV_SW, SW_RFKILL_ALL);
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003380 input_report_switch(tpacpi_inputdev,
3381 SW_RFKILL_ALL, radiosw_state);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003382 }
3383 if (tp_features.hotkey_tablet) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003384 input_set_capability(tpacpi_inputdev, EV_SW, SW_TABLET_MODE);
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003385 input_report_switch(tpacpi_inputdev,
3386 SW_TABLET_MODE, tabletsw_state);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003387 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003388
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003389 /* Do not issue duplicate brightness change events to
3390 * userspace */
3391 if (!tp_features.bright_acpimode)
3392 /* update bright_acpimode... */
3393 tpacpi_check_std_acpi_brightness_support();
3394
Henrique de Moraes Holschuh8bf3d4c2009-05-30 13:25:09 -03003395 if (tp_features.bright_acpimode && acpi_video_backlight_support()) {
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003396 printk(TPACPI_INFO
3397 "This ThinkPad has standard ACPI backlight "
3398 "brightness control, supported by the ACPI "
3399 "video driver\n");
3400 printk(TPACPI_NOTICE
3401 "Disabling thinkpad-acpi brightness events "
3402 "by default...\n");
3403
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003404 /* Disable brightness up/down on Lenovo thinkpads when
3405 * ACPI is handling them, otherwise it is plain impossible
3406 * for userspace to do something even remotely sane */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003407 hotkey_reserved_mask |=
3408 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3409 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003410 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
3411 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003412 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003413
Henrique de Moraes Holschuh230d8cf252009-09-12 15:22:17 -03003414#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003415 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3416 & ~hotkey_all_mask
3417 & ~hotkey_reserved_mask;
Henrique de Moraes Holschuh230d8cf252009-09-12 15:22:17 -03003418
3419 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3420 "hotkey source mask 0x%08x, polling freq %u\n",
3421 hotkey_source_mask, hotkey_poll_freq);
3422#endif
3423
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003424 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3425 "enabling firmware HKEY event interface...\n");
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003426 res = hotkey_status_set(true);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003427 if (res) {
3428 hotkey_exit();
3429 return res;
3430 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003431 res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
3432 | hotkey_driver_mask)
3433 & ~hotkey_source_mask);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003434 if (res < 0 && res != -ENXIO) {
3435 hotkey_exit();
3436 return res;
3437 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003438 hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
3439 & ~hotkey_reserved_mask;
3440 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3441 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3442 hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003443
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003444 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3445 "legacy ibm/hotkey event reporting over procfs %s\n",
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003446 (hotkey_report_mode < 2) ?
3447 "enabled" : "disabled");
3448
3449 tpacpi_inputdev->open = &hotkey_inputdev_open;
3450 tpacpi_inputdev->close = &hotkey_inputdev_close;
3451
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003452 hotkey_poll_setup_safe(true);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003453
3454 return 0;
3455
3456err_exit:
3457 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3458 hotkey_dev_attributes = NULL;
3459
3460 return (res < 0)? res : 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003461}
3462
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003463static bool hotkey_notify_hotkey(const u32 hkey,
3464 bool *send_acpi_ev,
3465 bool *ignore_acpi_ev)
3466{
3467 /* 0x1000-0x1FFF: key presses */
3468 unsigned int scancode = hkey & 0xfff;
3469 *send_acpi_ev = true;
3470 *ignore_acpi_ev = false;
3471
3472 if (scancode > 0 && scancode < 0x21) {
3473 scancode--;
3474 if (!(hotkey_source_mask & (1 << scancode))) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003475 tpacpi_input_send_key_masked(scancode);
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003476 *send_acpi_ev = false;
3477 } else {
3478 *ignore_acpi_ev = true;
3479 }
3480 return true;
3481 }
3482 return false;
3483}
3484
3485static bool hotkey_notify_wakeup(const u32 hkey,
3486 bool *send_acpi_ev,
3487 bool *ignore_acpi_ev)
3488{
3489 /* 0x2000-0x2FFF: Wakeup reason */
3490 *send_acpi_ev = true;
3491 *ignore_acpi_ev = false;
3492
3493 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003494 case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */
3495 case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003496 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3497 *ignore_acpi_ev = true;
3498 break;
3499
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003500 case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */
3501 case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003502 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3503 *ignore_acpi_ev = true;
3504 break;
3505
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003506 case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */
3507 case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003508 printk(TPACPI_ALERT
3509 "EMERGENCY WAKEUP: battery almost empty\n");
3510 /* how to auto-heal: */
3511 /* 2313: woke up from S3, go to S4/S5 */
3512 /* 2413: woke up from S4, go to S5 */
3513 break;
3514
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003515 default:
3516 return false;
3517 }
3518
3519 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
3520 printk(TPACPI_INFO
3521 "woke up due to a hot-unplug "
3522 "request...\n");
3523 hotkey_wakeup_reason_notify_change();
3524 }
3525 return true;
3526}
3527
3528static bool hotkey_notify_usrevent(const u32 hkey,
3529 bool *send_acpi_ev,
3530 bool *ignore_acpi_ev)
3531{
3532 /* 0x5000-0x5FFF: human interface helpers */
3533 *send_acpi_ev = true;
3534 *ignore_acpi_ev = false;
3535
3536 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003537 case TP_HKEY_EV_PEN_INSERTED: /* X61t: tablet pen inserted into bay */
3538 case TP_HKEY_EV_PEN_REMOVED: /* X61t: tablet pen removed from bay */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003539 return true;
3540
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003541 case TP_HKEY_EV_TABLET_TABLET: /* X41t-X61t: tablet mode */
3542 case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003543 tpacpi_input_send_tabletsw();
3544 hotkey_tablet_mode_notify_change();
3545 *send_acpi_ev = false;
3546 return true;
3547
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003548 case TP_HKEY_EV_LID_CLOSE: /* Lid closed */
3549 case TP_HKEY_EV_LID_OPEN: /* Lid opened */
3550 case TP_HKEY_EV_BRGHT_CHANGED: /* brightness changed */
Henrique de Moraes Holschuh176dd982009-09-20 14:09:24 -03003551 /* do not propagate these events */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003552 *ignore_acpi_ev = true;
3553 return true;
3554
3555 default:
3556 return false;
3557 }
3558}
3559
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003560static void thermal_dump_all_sensors(void);
3561
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003562static bool hotkey_notify_thermal(const u32 hkey,
3563 bool *send_acpi_ev,
3564 bool *ignore_acpi_ev)
3565{
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003566 bool known = true;
3567
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003568 /* 0x6000-0x6FFF: thermal alarms */
3569 *send_acpi_ev = true;
3570 *ignore_acpi_ev = false;
3571
3572 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003573 case TP_HKEY_EV_THM_TABLE_CHANGED:
Henrique de Moraes Holschuh54926ce2009-01-11 03:01:09 -02003574 printk(TPACPI_INFO
3575 "EC reports that Thermal Table has changed\n");
3576 /* recommended action: do nothing, we don't have
3577 * Lenovo ATM information */
3578 return true;
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003579 case TP_HKEY_EV_ALARM_BAT_HOT:
3580 printk(TPACPI_CRIT
3581 "THERMAL ALARM: battery is too hot!\n");
3582 /* recommended action: warn user through gui */
3583 break;
3584 case TP_HKEY_EV_ALARM_BAT_XHOT:
3585 printk(TPACPI_ALERT
3586 "THERMAL EMERGENCY: battery is extremely hot!\n");
3587 /* recommended action: immediate sleep/hibernate */
3588 break;
3589 case TP_HKEY_EV_ALARM_SENSOR_HOT:
3590 printk(TPACPI_CRIT
3591 "THERMAL ALARM: "
3592 "a sensor reports something is too hot!\n");
3593 /* recommended action: warn user through gui, that */
3594 /* some internal component is too hot */
3595 break;
3596 case TP_HKEY_EV_ALARM_SENSOR_XHOT:
3597 printk(TPACPI_ALERT
3598 "THERMAL EMERGENCY: "
3599 "a sensor reports something is extremely hot!\n");
3600 /* recommended action: immediate sleep/hibernate */
3601 break;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003602 default:
3603 printk(TPACPI_ALERT
3604 "THERMAL ALERT: unknown thermal alarm received\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003605 known = false;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003606 }
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003607
3608 thermal_dump_all_sensors();
3609
3610 return known;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003611}
3612
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003613static void hotkey_notify(struct ibm_struct *ibm, u32 event)
3614{
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003615 u32 hkey;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003616 bool send_acpi_ev;
3617 bool ignore_acpi_ev;
3618 bool known_ev;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003619
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003620 if (event != 0x80) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003621 printk(TPACPI_ERR
3622 "unknown HKEY notification event %d\n", event);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003623 /* forward it to userspace, maybe it knows how to handle it */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003624 acpi_bus_generate_netlink_event(
3625 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02003626 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003627 event, 0);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003628 return;
3629 }
3630
3631 while (1) {
3632 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003633 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003634 return;
3635 }
3636
3637 if (hkey == 0) {
3638 /* queue empty */
3639 return;
3640 }
3641
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003642 send_acpi_ev = true;
3643 ignore_acpi_ev = false;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003644
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003645 switch (hkey >> 12) {
3646 case 1:
3647 /* 0x1000-0x1FFF: key presses */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003648 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
3649 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003650 break;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003651 case 2:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003652 /* 0x2000-0x2FFF: Wakeup reason */
3653 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
3654 &ignore_acpi_ev);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003655 break;
3656 case 3:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003657 /* 0x3000-0x3FFF: bay-related wakeups */
Henrique de Moraes Holschuhbf8b29c2010-02-25 21:28:56 -03003658 switch (hkey) {
3659 case TP_HKEY_EV_BAYEJ_ACK:
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003660 hotkey_autosleep_ack = 1;
3661 printk(TPACPI_INFO
3662 "bay ejected\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003663 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003664 known_ev = true;
Henrique de Moraes Holschuhbf8b29c2010-02-25 21:28:56 -03003665 break;
3666 case TP_HKEY_EV_OPTDRV_EJ:
3667 /* FIXME: kick libata if SATA link offline */
3668 known_ev = true;
3669 break;
3670 default:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003671 known_ev = false;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003672 }
3673 break;
3674 case 4:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003675 /* 0x4000-0x4FFF: dock-related wakeups */
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003676 if (hkey == TP_HKEY_EV_UNDOCK_ACK) {
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003677 hotkey_autosleep_ack = 1;
3678 printk(TPACPI_INFO
3679 "undocked\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003680 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003681 known_ev = true;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003682 } else {
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003683 known_ev = false;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003684 }
3685 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003686 case 5:
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02003687 /* 0x5000-0x5FFF: human interface helpers */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003688 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
3689 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003690 break;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003691 case 6:
3692 /* 0x6000-0x6FFF: thermal alarms */
3693 known_ev = hotkey_notify_thermal(hkey, &send_acpi_ev,
3694 &ignore_acpi_ev);
3695 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003696 case 7:
3697 /* 0x7000-0x7FFF: misc */
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003698 if (tp_features.hotkey_wlsw &&
3699 hkey == TP_HKEY_EV_RFKILL_CHANGED) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003700 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02003701 send_acpi_ev = 0;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003702 known_ev = true;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003703 break;
3704 }
3705 /* fallthrough to default */
3706 default:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003707 known_ev = false;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02003708 }
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003709 if (!known_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003710 printk(TPACPI_NOTICE
3711 "unhandled HKEY event 0x%04x\n", hkey);
Henrique de Moraes Holschuhcb429352009-01-11 03:01:07 -02003712 printk(TPACPI_NOTICE
3713 "please report the conditions when this "
3714 "event happened to %s\n", TPACPI_MAIL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003715 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003716
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003717 /* Legacy events */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003718 if (!ignore_acpi_ev &&
3719 (send_acpi_ev || hotkey_report_mode < 2)) {
3720 acpi_bus_generate_proc_event(ibm->acpi->device,
3721 event, hkey);
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03003722 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003723
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003724 /* netlink events */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03003725 if (!ignore_acpi_ev && send_acpi_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003726 acpi_bus_generate_netlink_event(
3727 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02003728 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003729 event, hkey);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003730 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003731 }
3732}
3733
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003734static void hotkey_suspend(pm_message_t state)
3735{
3736 /* Do these on suspend, we get the events on early resume! */
3737 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
3738 hotkey_autosleep_ack = 0;
3739}
3740
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03003741static void hotkey_resume(void)
3742{
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03003743 tpacpi_disable_brightness_delay();
3744
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003745 if (hotkey_status_set(true) < 0 ||
3746 hotkey_mask_set(hotkey_acpi_mask) < 0)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003747 printk(TPACPI_ERR
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003748 "error while attempting to reset the event "
3749 "firmware interface\n");
3750
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003751 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02003752 hotkey_tablet_mode_notify_change();
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003753 hotkey_wakeup_reason_notify_change();
3754 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003755 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03003756}
3757
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003758/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003759static int hotkey_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03003761 int res, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003763 if (!tp_features.hotkey) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003764 seq_printf(m, "status:\t\tnot supported\n");
3765 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003766 }
3767
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02003768 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02003769 return -ERESTARTSYS;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02003770 res = hotkey_status_get(&status);
3771 if (!res)
3772 res = hotkey_mask_get();
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003773 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03003774 if (res)
3775 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003777 seq_printf(m, "status:\t\t%s\n", enabled(status, 0));
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003778 if (hotkey_all_mask) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003779 seq_printf(m, "mask:\t\t0x%08x\n", hotkey_user_mask);
3780 seq_printf(m, "commands:\tenable, disable, reset, <mask>\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781 } else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003782 seq_printf(m, "mask:\t\tnot supported\n");
3783 seq_printf(m, "commands:\tenable, disable, reset\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784 }
3785
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003786 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787}
3788
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003789static void hotkey_enabledisable_warn(bool enable)
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003790{
3791 tpacpi_log_usertask("procfs hotkey enable/disable");
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003792 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
3793 TPACPI_WARN
3794 "hotkey enable/disable functionality has been "
3795 "removed from the driver. Hotkeys are always "
3796 "enabled\n"))
3797 printk(TPACPI_ERR
3798 "Please remove the hotkey=enable module "
3799 "parameter, it is deprecated. Hotkeys are always "
3800 "enabled\n");
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003801}
3802
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003803static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003805 int res;
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03003806 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003809 if (!tp_features.hotkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810 return -ENODEV;
3811
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02003812 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02003813 return -ERESTARTSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003814
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003815 mask = hotkey_user_mask;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003816
3817 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818 while ((cmd = next_cmd(&buf))) {
3819 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003820 hotkey_enabledisable_warn(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003822 hotkey_enabledisable_warn(0);
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003823 res = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824 } else if (strlencmp(cmd, "reset") == 0) {
Henrique de Moraes Holschuh20c9aa42009-09-12 15:22:16 -03003825 mask = (hotkey_all_mask | hotkey_source_mask)
3826 & ~hotkey_reserved_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
3828 /* mask set */
3829 } else if (sscanf(cmd, "%x", &mask) == 1) {
3830 /* mask set */
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003831 } else {
3832 res = -EINVAL;
3833 goto errexit;
3834 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835 }
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003836
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003837 if (!res) {
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003838 tpacpi_disclose_usertask("procfs hotkey",
3839 "set mask to 0x%08x\n", mask);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003840 res = hotkey_user_mask_set(mask);
3841 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003842
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003843errexit:
3844 mutex_unlock(&hotkey_mutex);
3845 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003846}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003848static const struct acpi_device_id ibm_htk_device_ids[] = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003849 {TPACPI_ACPI_HKEY_HID, 0},
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003850 {"", 0},
3851};
3852
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003853static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003854 .hid = ibm_htk_device_ids,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003855 .notify = hotkey_notify,
3856 .handle = &hkey_handle,
3857 .type = ACPI_DEVICE_NOTIFY,
3858};
3859
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003860static struct ibm_struct hotkey_driver_data = {
3861 .name = "hotkey",
3862 .read = hotkey_read,
3863 .write = hotkey_write,
3864 .exit = hotkey_exit,
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03003865 .resume = hotkey_resume,
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003866 .suspend = hotkey_suspend,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003867 .acpi = &ibm_hotkey_acpidriver,
3868};
3869
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003870/*************************************************************************
3871 * Bluetooth subdriver
3872 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003874enum {
3875 /* ACPI GBDC/SBDC bits */
3876 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
3877 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003878 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
3879 off / last state */
3880};
3881
3882enum {
3883 /* ACPI \BLTH commands */
3884 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
3885 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
3886 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
3887 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
3888 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003889};
3890
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003891#define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
3892
Johannes Berg19d337d2009-06-02 13:01:37 +02003893static int bluetooth_get_status(void)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003894{
3895 int status;
3896
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003897#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3898 if (dbg_bluetoothemul)
3899 return (tpacpi_bluetooth_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02003900 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003901#endif
3902
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003903 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
3904 return -EIO;
3905
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003906 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02003907 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003908}
3909
Johannes Berg19d337d2009-06-02 13:01:37 +02003910static int bluetooth_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003911{
3912 int status;
3913
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003914 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02003915 "will attempt to %s bluetooth\n",
3916 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003917
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003918#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3919 if (dbg_bluetoothemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02003920 tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003921 return 0;
3922 }
3923#endif
3924
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003925 /* We make sure to keep TP_ACPI_BLUETOOTH_RESUMECTRL off */
Henrique de Moraes Holschuh208b9962009-12-09 01:36:22 +00003926 status = TP_ACPI_BLUETOOTH_RESUMECTRL;
Johannes Berg19d337d2009-06-02 13:01:37 +02003927 if (state == TPACPI_RFK_RADIO_ON)
Henrique de Moraes Holschuh208b9962009-12-09 01:36:22 +00003928 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
Johannes Berg19d337d2009-06-02 13:01:37 +02003929
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003930 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
3931 return -EIO;
3932
3933 return 0;
3934}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003935
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003936/* sysfs bluetooth enable ---------------------------------------------- */
3937static ssize_t bluetooth_enable_show(struct device *dev,
3938 struct device_attribute *attr,
3939 char *buf)
3940{
Johannes Berg19d337d2009-06-02 13:01:37 +02003941 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID,
3942 attr, buf);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003943}
3944
3945static ssize_t bluetooth_enable_store(struct device *dev,
3946 struct device_attribute *attr,
3947 const char *buf, size_t count)
3948{
Johannes Berg19d337d2009-06-02 13:01:37 +02003949 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID,
3950 attr, buf, count);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003951}
3952
3953static struct device_attribute dev_attr_bluetooth_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03003954 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003955 bluetooth_enable_show, bluetooth_enable_store);
3956
3957/* --------------------------------------------------------------------- */
3958
3959static struct attribute *bluetooth_attributes[] = {
3960 &dev_attr_bluetooth_enable.attr,
3961 NULL
3962};
3963
3964static const struct attribute_group bluetooth_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003965 .attrs = bluetooth_attributes,
3966};
3967
Johannes Berg19d337d2009-06-02 13:01:37 +02003968static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = {
3969 .get_status = bluetooth_get_status,
3970 .set_status = bluetooth_set_status,
3971};
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003972
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003973static void bluetooth_shutdown(void)
3974{
3975 /* Order firmware to save current state to NVRAM */
3976 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3977 TP_ACPI_BLTH_SAVE_STATE))
3978 printk(TPACPI_NOTICE
3979 "failed to save bluetooth state to NVRAM\n");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003980 else
3981 vdbg_printk(TPACPI_DBG_RFKILL,
3982 "bluestooth state saved to NVRAM\n");
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003983}
3984
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003985static void bluetooth_exit(void)
3986{
3987 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3988 &bluetooth_attr_group);
Johannes Berg19d337d2009-06-02 13:01:37 +02003989
3990 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
3991
3992 bluetooth_shutdown();
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003993}
3994
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003995static int __init bluetooth_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003997 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003998 int status = 0;
3999
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004000 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4001 "initializing bluetooth subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004002
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004003 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004004
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004005 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4006 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004007 tp_features.bluetooth = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004008 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004010 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4011 "bluetooth is %s, status 0x%02x\n",
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004012 str_supported(tp_features.bluetooth),
4013 status);
4014
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004015#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4016 if (dbg_bluetoothemul) {
4017 tp_features.bluetooth = 1;
4018 printk(TPACPI_INFO
4019 "bluetooth switch emulation enabled\n");
4020 } else
4021#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004022 if (tp_features.bluetooth &&
4023 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
4024 /* no bluetooth hardware present in system */
4025 tp_features.bluetooth = 0;
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004026 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004027 "bluetooth hardware not installed\n");
4028 }
4029
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004030 if (!tp_features.bluetooth)
4031 return 1;
4032
Johannes Berg19d337d2009-06-02 13:01:37 +02004033 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
4034 &bluetooth_tprfk_ops,
4035 RFKILL_TYPE_BLUETOOTH,
4036 TPACPI_RFK_BLUETOOTH_SW_NAME,
4037 true);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004038 if (res)
4039 return res;
4040
Johannes Berg19d337d2009-06-02 13:01:37 +02004041 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4042 &bluetooth_attr_group);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004043 if (res) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004044 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004045 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004046 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004047
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004048 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049}
4050
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004051/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004052static int bluetooth_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053{
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004054 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055}
4056
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004057static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058{
Johannes Berg19d337d2009-06-02 13:01:37 +02004059 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060}
4061
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004062static struct ibm_struct bluetooth_driver_data = {
4063 .name = "bluetooth",
4064 .read = bluetooth_read,
4065 .write = bluetooth_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004066 .exit = bluetooth_exit,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004067 .shutdown = bluetooth_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004068};
4069
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004070/*************************************************************************
4071 * Wan subdriver
4072 */
4073
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004074enum {
4075 /* ACPI GWAN/SWAN bits */
4076 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
4077 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02004078 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
4079 off / last state */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004080};
4081
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004082#define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
4083
Johannes Berg19d337d2009-06-02 13:01:37 +02004084static int wan_get_status(void)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004085{
4086 int status;
4087
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004088#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4089 if (dbg_wwanemul)
4090 return (tpacpi_wwan_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004091 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004092#endif
4093
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004094 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
4095 return -EIO;
4096
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004097 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004098 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004099}
4100
Johannes Berg19d337d2009-06-02 13:01:37 +02004101static int wan_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004102{
4103 int status;
4104
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004105 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02004106 "will attempt to %s wwan\n",
4107 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004108
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004109#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4110 if (dbg_wwanemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004111 tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004112 return 0;
4113 }
4114#endif
4115
Henrique de Moraes Holschuh208b9962009-12-09 01:36:22 +00004116 /* We make sure to set TP_ACPI_WANCARD_RESUMECTRL */
4117 status = TP_ACPI_WANCARD_RESUMECTRL;
Johannes Berg19d337d2009-06-02 13:01:37 +02004118 if (state == TPACPI_RFK_RADIO_ON)
Henrique de Moraes Holschuh208b9962009-12-09 01:36:22 +00004119 status |= TP_ACPI_WANCARD_RADIOSSW;
Johannes Berg19d337d2009-06-02 13:01:37 +02004120
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004121 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
4122 return -EIO;
4123
4124 return 0;
4125}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004126
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004127/* sysfs wan enable ---------------------------------------------------- */
4128static ssize_t wan_enable_show(struct device *dev,
4129 struct device_attribute *attr,
4130 char *buf)
4131{
Johannes Berg19d337d2009-06-02 13:01:37 +02004132 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
4133 attr, buf);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004134}
4135
4136static ssize_t wan_enable_store(struct device *dev,
4137 struct device_attribute *attr,
4138 const char *buf, size_t count)
4139{
Johannes Berg19d337d2009-06-02 13:01:37 +02004140 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
4141 attr, buf, count);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004142}
4143
4144static struct device_attribute dev_attr_wan_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03004145 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004146 wan_enable_show, wan_enable_store);
4147
4148/* --------------------------------------------------------------------- */
4149
4150static struct attribute *wan_attributes[] = {
4151 &dev_attr_wan_enable.attr,
4152 NULL
4153};
4154
4155static const struct attribute_group wan_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004156 .attrs = wan_attributes,
4157};
4158
Johannes Berg19d337d2009-06-02 13:01:37 +02004159static const struct tpacpi_rfk_ops wan_tprfk_ops = {
4160 .get_status = wan_get_status,
4161 .set_status = wan_set_status,
4162};
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004163
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004164static void wan_shutdown(void)
4165{
4166 /* Order firmware to save current state to NVRAM */
4167 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
4168 TP_ACPI_WGSV_SAVE_STATE))
4169 printk(TPACPI_NOTICE
4170 "failed to save WWAN state to NVRAM\n");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004171 else
4172 vdbg_printk(TPACPI_DBG_RFKILL,
4173 "WWAN state saved to NVRAM\n");
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004174}
4175
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004176static void wan_exit(void)
4177{
4178 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4179 &wan_attr_group);
Johannes Berg19d337d2009-06-02 13:01:37 +02004180
4181 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4182
4183 wan_shutdown();
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004184}
4185
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004186static int __init wan_init(struct ibm_init_struct *iibm)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004187{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004188 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004189 int status = 0;
4190
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004191 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4192 "initializing wan subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004193
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004194 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004195
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004196 tp_features.wan = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004197 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004198
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004199 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4200 "wan is %s, status 0x%02x\n",
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004201 str_supported(tp_features.wan),
4202 status);
4203
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004204#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4205 if (dbg_wwanemul) {
4206 tp_features.wan = 1;
4207 printk(TPACPI_INFO
4208 "wwan switch emulation enabled\n");
4209 } else
4210#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004211 if (tp_features.wan &&
4212 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
4213 /* no wan hardware present in system */
4214 tp_features.wan = 0;
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004215 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004216 "wan hardware not installed\n");
4217 }
4218
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004219 if (!tp_features.wan)
4220 return 1;
4221
Johannes Berg19d337d2009-06-02 13:01:37 +02004222 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
4223 &wan_tprfk_ops,
4224 RFKILL_TYPE_WWAN,
4225 TPACPI_RFK_WWAN_SW_NAME,
4226 true);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004227 if (res)
4228 return res;
4229
Johannes Berg19d337d2009-06-02 13:01:37 +02004230 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4231 &wan_attr_group);
4232
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004233 if (res) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004234 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004235 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004236 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004237
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004238 return 0;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004239}
4240
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004241/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004242static int wan_read(struct seq_file *m)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004243{
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004244 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, m);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004245}
4246
4247static int wan_write(char *buf)
4248{
Johannes Berg19d337d2009-06-02 13:01:37 +02004249 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004250}
4251
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004252static struct ibm_struct wan_driver_data = {
4253 .name = "wan",
4254 .read = wan_read,
4255 .write = wan_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004256 .exit = wan_exit,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004257 .shutdown = wan_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004258};
4259
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004260/*************************************************************************
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004261 * UWB subdriver
4262 */
4263
4264enum {
4265 /* ACPI GUWB/SUWB bits */
4266 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4267 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4268};
4269
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004270#define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4271
Johannes Berg19d337d2009-06-02 13:01:37 +02004272static int uwb_get_status(void)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004273{
4274 int status;
4275
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004276#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4277 if (dbg_uwbemul)
4278 return (tpacpi_uwb_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004279 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004280#endif
4281
4282 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4283 return -EIO;
4284
4285 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004286 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004287}
4288
Johannes Berg19d337d2009-06-02 13:01:37 +02004289static int uwb_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004290{
4291 int status;
4292
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004293 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02004294 "will attempt to %s UWB\n",
4295 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004296
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004297#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4298 if (dbg_uwbemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004299 tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004300 return 0;
4301 }
4302#endif
4303
Johannes Berg19d337d2009-06-02 13:01:37 +02004304 if (state == TPACPI_RFK_RADIO_ON)
4305 status = TP_ACPI_UWB_RADIOSSW;
4306 else
4307 status = 0;
4308
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004309 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4310 return -EIO;
4311
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004312 return 0;
4313}
4314
4315/* --------------------------------------------------------------------- */
4316
Johannes Berg19d337d2009-06-02 13:01:37 +02004317static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
4318 .get_status = uwb_get_status,
4319 .set_status = uwb_set_status,
4320};
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004321
4322static void uwb_exit(void)
4323{
Johannes Berg19d337d2009-06-02 13:01:37 +02004324 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004325}
4326
4327static int __init uwb_init(struct ibm_init_struct *iibm)
4328{
4329 int res;
4330 int status = 0;
4331
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004332 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4333 "initializing uwb subdriver\n");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004334
4335 TPACPI_ACPIHANDLE_INIT(hkey);
4336
4337 tp_features.uwb = hkey_handle &&
4338 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4339
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004340 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4341 "uwb is %s, status 0x%02x\n",
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004342 str_supported(tp_features.uwb),
4343 status);
4344
4345#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4346 if (dbg_uwbemul) {
4347 tp_features.uwb = 1;
4348 printk(TPACPI_INFO
4349 "uwb switch emulation enabled\n");
4350 } else
4351#endif
4352 if (tp_features.uwb &&
4353 !(status & TP_ACPI_UWB_HWPRESENT)) {
4354 /* no uwb hardware present in system */
4355 tp_features.uwb = 0;
4356 dbg_printk(TPACPI_DBG_INIT,
4357 "uwb hardware not installed\n");
4358 }
4359
4360 if (!tp_features.uwb)
4361 return 1;
4362
4363 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
Johannes Berg19d337d2009-06-02 13:01:37 +02004364 &uwb_tprfk_ops,
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004365 RFKILL_TYPE_UWB,
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004366 TPACPI_RFK_UWB_SW_NAME,
Johannes Berg19d337d2009-06-02 13:01:37 +02004367 false);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004368 return res;
4369}
4370
4371static struct ibm_struct uwb_driver_data = {
4372 .name = "uwb",
4373 .exit = uwb_exit,
4374 .flags.experimental = 1,
4375};
4376
4377/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004378 * Video subdriver
4379 */
4380
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02004381#ifdef CONFIG_THINKPAD_ACPI_VIDEO
4382
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004383enum video_access_mode {
4384 TPACPI_VIDEO_NONE = 0,
4385 TPACPI_VIDEO_570, /* 570 */
4386 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4387 TPACPI_VIDEO_NEW, /* all others */
4388};
4389
4390enum { /* video status flags, based on VIDEO_570 */
4391 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4392 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4393 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4394};
4395
4396enum { /* TPACPI_VIDEO_570 constants */
4397 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4398 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4399 * video_status_flags */
4400 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4401 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4402};
4403
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02004404static enum video_access_mode video_supported;
4405static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004406
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004407static int video_autosw_get(void);
4408static int video_autosw_set(int enable);
4409
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004410TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004411
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004412static int __init video_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004414 int ivga;
4415
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004416 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4417
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004418 TPACPI_ACPIHANDLE_INIT(vid);
4419 TPACPI_ACPIHANDLE_INIT(vid2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004420
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004421 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4422 /* G41, assume IVGA doesn't change */
4423 vid_handle = vid2_handle;
4424
4425 if (!vid_handle)
4426 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004427 video_supported = TPACPI_VIDEO_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004428 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
4429 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004430 video_supported = TPACPI_VIDEO_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004431 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
4432 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004433 video_supported = TPACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004434 else
4435 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004436 video_supported = TPACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004438 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
4439 str_supported(video_supported != TPACPI_VIDEO_NONE),
4440 video_supported);
4441
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004442 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443}
4444
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004445static void video_exit(void)
4446{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004447 dbg_printk(TPACPI_DBG_EXIT,
4448 "restoring original video autoswitch mode\n");
4449 if (video_autosw_set(video_orig_autosw))
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004450 printk(TPACPI_ERR "error while trying to restore original "
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004451 "video autoswitch mode\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004452}
4453
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004454static int video_outputsw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455{
4456 int status = 0;
4457 int i;
4458
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004459 switch (video_supported) {
4460 case TPACPI_VIDEO_570:
4461 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
4462 TP_ACPI_VIDEO_570_PHSCMD))
4463 return -EIO;
4464 status = i & TP_ACPI_VIDEO_570_PHSMASK;
4465 break;
4466 case TPACPI_VIDEO_770:
4467 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4468 return -EIO;
4469 if (i)
4470 status |= TP_ACPI_VIDEO_S_LCD;
4471 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4472 return -EIO;
4473 if (i)
4474 status |= TP_ACPI_VIDEO_S_CRT;
4475 break;
4476 case TPACPI_VIDEO_NEW:
4477 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4478 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4479 return -EIO;
4480 if (i)
4481 status |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004483 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4484 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4485 return -EIO;
4486 if (i)
4487 status |= TP_ACPI_VIDEO_S_LCD;
4488 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4489 return -EIO;
4490 if (i)
4491 status |= TP_ACPI_VIDEO_S_DVI;
4492 break;
4493 default:
4494 return -ENOSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004495 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496
4497 return status;
4498}
4499
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004500static int video_outputsw_set(int status)
4501{
4502 int autosw;
4503 int res = 0;
4504
4505 switch (video_supported) {
4506 case TPACPI_VIDEO_570:
4507 res = acpi_evalf(NULL, NULL,
4508 "\\_SB.PHS2", "vdd",
4509 TP_ACPI_VIDEO_570_PHS2CMD,
4510 status | TP_ACPI_VIDEO_570_PHS2SET);
4511 break;
4512 case TPACPI_VIDEO_770:
4513 autosw = video_autosw_get();
4514 if (autosw < 0)
4515 return autosw;
4516
4517 res = video_autosw_set(1);
4518 if (res)
4519 return res;
4520 res = acpi_evalf(vid_handle, NULL,
4521 "ASWT", "vdd", status * 0x100, 0);
4522 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004523 printk(TPACPI_ERR
4524 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004525 return -EIO;
4526 }
4527 break;
4528 case TPACPI_VIDEO_NEW:
4529 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004530 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004531 break;
4532 default:
4533 return -ENOSYS;
4534 }
4535
4536 return (res)? 0 : -EIO;
4537}
4538
4539static int video_autosw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004540{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004541 int autosw = 0;
4542
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004543 switch (video_supported) {
4544 case TPACPI_VIDEO_570:
4545 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
4546 return -EIO;
4547 break;
4548 case TPACPI_VIDEO_770:
4549 case TPACPI_VIDEO_NEW:
4550 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4551 return -EIO;
4552 break;
4553 default:
4554 return -ENOSYS;
4555 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004556
4557 return autosw & 1;
4558}
4559
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004560static int video_autosw_set(int enable)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004561{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004562 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004563 return -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004564 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004565}
4566
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004567static int video_outputsw_cycle(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004568{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004569 int autosw = video_autosw_get();
4570 int res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004571
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004572 if (autosw < 0)
4573 return autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004574
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004575 switch (video_supported) {
4576 case TPACPI_VIDEO_570:
4577 res = video_autosw_set(1);
4578 if (res)
4579 return res;
4580 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4581 break;
4582 case TPACPI_VIDEO_770:
4583 case TPACPI_VIDEO_NEW:
4584 res = video_autosw_set(1);
4585 if (res)
4586 return res;
4587 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4588 break;
4589 default:
4590 return -ENOSYS;
4591 }
4592 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004593 printk(TPACPI_ERR
4594 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004595 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004596 }
4597
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004598 return (res)? 0 : -EIO;
4599}
4600
4601static int video_expand_toggle(void)
4602{
4603 switch (video_supported) {
4604 case TPACPI_VIDEO_570:
4605 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
4606 0 : -EIO;
4607 case TPACPI_VIDEO_770:
4608 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
4609 0 : -EIO;
4610 case TPACPI_VIDEO_NEW:
4611 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
4612 0 : -EIO;
4613 default:
4614 return -ENOSYS;
4615 }
4616 /* not reached */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004617}
4618
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004619static int video_read(struct seq_file *m)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004620{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004621 int status, autosw;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004622
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004623 if (video_supported == TPACPI_VIDEO_NONE) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004624 seq_printf(m, "status:\t\tnot supported\n");
4625 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004626 }
4627
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004628 status = video_outputsw_get();
4629 if (status < 0)
4630 return status;
4631
4632 autosw = video_autosw_get();
4633 if (autosw < 0)
4634 return autosw;
4635
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004636 seq_printf(m, "status:\t\tsupported\n");
4637 seq_printf(m, "lcd:\t\t%s\n", enabled(status, 0));
4638 seq_printf(m, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004639 if (video_supported == TPACPI_VIDEO_NEW)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004640 seq_printf(m, "dvi:\t\t%s\n", enabled(status, 3));
4641 seq_printf(m, "auto:\t\t%s\n", enabled(autosw, 0));
4642 seq_printf(m, "commands:\tlcd_enable, lcd_disable\n");
4643 seq_printf(m, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004644 if (video_supported == TPACPI_VIDEO_NEW)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004645 seq_printf(m, "commands:\tdvi_enable, dvi_disable\n");
4646 seq_printf(m, "commands:\tauto_enable, auto_disable\n");
4647 seq_printf(m, "commands:\tvideo_switch, expand_toggle\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004648
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004649 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004650}
4651
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004652static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653{
4654 char *cmd;
4655 int enable, disable, status;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004656 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004658 if (video_supported == TPACPI_VIDEO_NONE)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004659 return -ENODEV;
4660
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004661 enable = 0;
4662 disable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663
4664 while ((cmd = next_cmd(&buf))) {
4665 if (strlencmp(cmd, "lcd_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004666 enable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667 } else if (strlencmp(cmd, "lcd_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004668 disable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669 } else if (strlencmp(cmd, "crt_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004670 enable |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004671 } else if (strlencmp(cmd, "crt_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004672 disable |= TP_ACPI_VIDEO_S_CRT;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004673 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004674 strlencmp(cmd, "dvi_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004675 enable |= TP_ACPI_VIDEO_S_DVI;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004676 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004677 strlencmp(cmd, "dvi_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004678 disable |= TP_ACPI_VIDEO_S_DVI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679 } else if (strlencmp(cmd, "auto_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004680 res = video_autosw_set(1);
4681 if (res)
4682 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683 } else if (strlencmp(cmd, "auto_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004684 res = video_autosw_set(0);
4685 if (res)
4686 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687 } else if (strlencmp(cmd, "video_switch") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004688 res = video_outputsw_cycle();
4689 if (res)
4690 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004691 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004692 res = video_expand_toggle();
4693 if (res)
4694 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695 } else
4696 return -EINVAL;
4697 }
4698
4699 if (enable || disable) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004700 status = video_outputsw_get();
4701 if (status < 0)
4702 return status;
4703 res = video_outputsw_set((status & ~disable) | enable);
4704 if (res)
4705 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706 }
4707
4708 return 0;
4709}
4710
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004711static struct ibm_struct video_driver_data = {
4712 .name = "video",
4713 .read = video_read,
4714 .write = video_write,
4715 .exit = video_exit,
4716};
4717
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02004718#endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4719
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004720/*************************************************************************
4721 * Light (thinklight) subdriver
4722 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004723
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004724TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4725TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004726
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004727static int light_get_status(void)
4728{
4729 int status = 0;
4730
4731 if (tp_features.light_status) {
4732 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4733 return -EIO;
4734 return (!!status);
4735 }
4736
4737 return -ENXIO;
4738}
4739
4740static int light_set_status(int status)
4741{
4742 int rc;
4743
4744 if (tp_features.light) {
4745 if (cmos_handle) {
4746 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4747 (status)?
4748 TP_CMOS_THINKLIGHT_ON :
4749 TP_CMOS_THINKLIGHT_OFF);
4750 } else {
4751 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4752 (status)? 1 : 0);
4753 }
4754 return (rc)? 0 : -EIO;
4755 }
4756
4757 return -ENXIO;
4758}
4759
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004760static void light_set_status_worker(struct work_struct *work)
4761{
4762 struct tpacpi_led_classdev *data =
4763 container_of(work, struct tpacpi_led_classdev, work);
4764
4765 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00004766 light_set_status((data->new_state != TPACPI_LED_OFF));
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004767}
4768
4769static void light_sysfs_set(struct led_classdev *led_cdev,
4770 enum led_brightness brightness)
4771{
4772 struct tpacpi_led_classdev *data =
4773 container_of(led_cdev,
4774 struct tpacpi_led_classdev,
4775 led_classdev);
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00004776 data->new_state = (brightness != LED_OFF) ?
4777 TPACPI_LED_ON : TPACPI_LED_OFF;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004778 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004779}
4780
4781static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4782{
4783 return (light_get_status() == 1)? LED_FULL : LED_OFF;
4784}
4785
4786static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4787 .led_classdev = {
4788 .name = "tpacpi::thinklight",
4789 .brightness_set = &light_sysfs_set,
4790 .brightness_get = &light_sysfs_get,
4791 }
4792};
4793
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004794static int __init light_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004795{
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004796 int rc;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004797
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004798 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4799
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004800 TPACPI_ACPIHANDLE_INIT(ledb);
4801 TPACPI_ACPIHANDLE_INIT(lght);
4802 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004803 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004804
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004805 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004806 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004807
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004808 if (tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004809 /* light status not supported on
4810 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004811 tp_features.light_status =
4812 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004814 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4815 str_supported(tp_features.light),
4816 str_supported(tp_features.light_status));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004817
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004818 if (!tp_features.light)
4819 return 1;
4820
4821 rc = led_classdev_register(&tpacpi_pdev->dev,
4822 &tpacpi_led_thinklight.led_classdev);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004823
4824 if (rc < 0) {
4825 tp_features.light = 0;
4826 tp_features.light_status = 0;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004827 } else {
4828 rc = 0;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004829 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004830
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004831 return rc;
4832}
4833
4834static void light_exit(void)
4835{
4836 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
4837 if (work_pending(&tpacpi_led_thinklight.work))
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004838 flush_workqueue(tpacpi_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004839}
4840
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004841static int light_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004842{
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004843 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004845 if (!tp_features.light) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004846 seq_printf(m, "status:\t\tnot supported\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004847 } else if (!tp_features.light_status) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004848 seq_printf(m, "status:\t\tunknown\n");
4849 seq_printf(m, "commands:\ton, off\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004850 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004851 status = light_get_status();
4852 if (status < 0)
4853 return status;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004854 seq_printf(m, "status:\t\t%s\n", onoff(status, 0));
4855 seq_printf(m, "commands:\ton, off\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004856 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004857
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004858 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004859}
4860
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004861static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004862{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004864 int newstatus = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004865
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004866 if (!tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004867 return -ENODEV;
4868
Linus Torvalds1da177e2005-04-16 15:20:36 -07004869 while ((cmd = next_cmd(&buf))) {
4870 if (strlencmp(cmd, "on") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004871 newstatus = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872 } else if (strlencmp(cmd, "off") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004873 newstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874 } else
4875 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876 }
4877
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004878 return light_set_status(newstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004879}
4880
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004881static struct ibm_struct light_driver_data = {
4882 .name = "light",
4883 .read = light_read,
4884 .write = light_write,
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004885 .exit = light_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004886};
4887
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004888/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004889 * CMOS subdriver
4890 */
4891
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004892/* sysfs cmos_command -------------------------------------------------- */
4893static ssize_t cmos_command_store(struct device *dev,
4894 struct device_attribute *attr,
4895 const char *buf, size_t count)
4896{
4897 unsigned long cmos_cmd;
4898 int res;
4899
4900 if (parse_strtoul(buf, 21, &cmos_cmd))
4901 return -EINVAL;
4902
4903 res = issue_thinkpad_cmos_command(cmos_cmd);
4904 return (res)? res : count;
4905}
4906
4907static struct device_attribute dev_attr_cmos_command =
4908 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4909
4910/* --------------------------------------------------------------------- */
4911
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004912static int __init cmos_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004913{
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004914 int res;
4915
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004916 vdbg_printk(TPACPI_DBG_INIT,
4917 "initializing cmos commands subdriver\n");
4918
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004919 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004920
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004921 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4922 str_supported(cmos_handle != NULL));
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004923
4924 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4925 if (res)
4926 return res;
4927
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004928 return (cmos_handle)? 0 : 1;
4929}
4930
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004931static void cmos_exit(void)
4932{
4933 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4934}
4935
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004936static int cmos_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004938 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4939 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004940 if (!cmos_handle)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004941 seq_printf(m, "status:\t\tnot supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942 else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004943 seq_printf(m, "status:\t\tsupported\n");
4944 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945 }
4946
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004947 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004948}
4949
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004950static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951{
4952 char *cmd;
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004953 int cmos_cmd, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004954
4955 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004956 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
4957 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004958 /* cmos_cmd set */
4959 } else
4960 return -EINVAL;
4961
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004962 res = issue_thinkpad_cmos_command(cmos_cmd);
4963 if (res)
4964 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004965 }
4966
4967 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004968}
4969
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004970static struct ibm_struct cmos_driver_data = {
4971 .name = "cmos",
4972 .read = cmos_read,
4973 .write = cmos_write,
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004974 .exit = cmos_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004975};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004976
4977/*************************************************************************
4978 * LED subdriver
4979 */
4980
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004981enum led_access_mode {
4982 TPACPI_LED_NONE = 0,
4983 TPACPI_LED_570, /* 570 */
4984 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4985 TPACPI_LED_NEW, /* all others */
4986};
4987
4988enum { /* For TPACPI_LED_OLD */
4989 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
4990 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
4991 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
4992};
4993
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02004994static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004995
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004996TPACPI_HANDLE(led, ec, "SLED", /* 570 */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004997 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
4998 /* T20-22, X20-21 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004999 "LED", /* all others */
5000 ); /* R30, R31 */
5001
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005002#define TPACPI_LED_NUMLEDS 16
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005003static struct tpacpi_led_classdev *tpacpi_leds;
5004static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
Harvey Harrisone3aa51f2008-05-29 17:51:57 -07005005static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005006 /* there's a limit of 19 chars + NULL before 2.6.26 */
5007 "tpacpi::power",
5008 "tpacpi:orange:batt",
5009 "tpacpi:green:batt",
5010 "tpacpi::dock_active",
5011 "tpacpi::bay_active",
5012 "tpacpi::dock_batt",
5013 "tpacpi::unknown_led",
5014 "tpacpi::standby",
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005015 "tpacpi::dock_status1",
5016 "tpacpi::dock_status2",
5017 "tpacpi::unknown_led2",
5018 "tpacpi::unknown_led3",
5019 "tpacpi::thinkvantage",
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005020};
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005021#define TPACPI_SAFE_LEDS 0x1081U
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005022
5023static inline bool tpacpi_is_led_restricted(const unsigned int led)
5024{
5025#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5026 return false;
5027#else
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005028 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005029#endif
5030}
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005031
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005032static int led_get_status(const unsigned int led)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005033{
5034 int status;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005035 enum led_status_t led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005036
5037 switch (led_supported) {
5038 case TPACPI_LED_570:
5039 if (!acpi_evalf(ec_handle,
5040 &status, "GLED", "dd", 1 << led))
5041 return -EIO;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005042 led_s = (status == 0)?
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005043 TPACPI_LED_OFF :
5044 ((status == 1)?
5045 TPACPI_LED_ON :
5046 TPACPI_LED_BLINK);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005047 tpacpi_led_state_cache[led] = led_s;
5048 return led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005049 default:
5050 return -ENXIO;
5051 }
5052
5053 /* not reached */
5054}
5055
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005056static int led_set_status(const unsigned int led,
5057 const enum led_status_t ledstatus)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005058{
5059 /* off, on, blink. Index is led_status_t */
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005060 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
5061 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005062
5063 int rc = 0;
5064
5065 switch (led_supported) {
5066 case TPACPI_LED_570:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005067 /* 570 */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005068 if (unlikely(led > 7))
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005069 return -EINVAL;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005070 if (unlikely(tpacpi_is_led_restricted(led)))
5071 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005072 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5073 (1 << led), led_sled_arg1[ledstatus]))
5074 rc = -EIO;
5075 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005076 case TPACPI_LED_OLD:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005077 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005078 if (unlikely(led > 7))
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005079 return -EINVAL;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005080 if (unlikely(tpacpi_is_led_restricted(led)))
5081 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005082 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
5083 if (rc >= 0)
5084 rc = ec_write(TPACPI_LED_EC_HLBL,
5085 (ledstatus == TPACPI_LED_BLINK) << led);
5086 if (rc >= 0)
5087 rc = ec_write(TPACPI_LED_EC_HLCL,
5088 (ledstatus != TPACPI_LED_OFF) << led);
5089 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005090 case TPACPI_LED_NEW:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005091 /* all others */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005092 if (unlikely(led >= TPACPI_LED_NUMLEDS))
5093 return -EINVAL;
5094 if (unlikely(tpacpi_is_led_restricted(led)))
5095 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005096 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5097 led, led_led_arg1[ledstatus]))
5098 rc = -EIO;
5099 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005100 default:
5101 rc = -ENXIO;
5102 }
5103
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005104 if (!rc)
5105 tpacpi_led_state_cache[led] = ledstatus;
5106
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005107 return rc;
5108}
5109
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005110static void led_set_status_worker(struct work_struct *work)
5111{
5112 struct tpacpi_led_classdev *data =
5113 container_of(work, struct tpacpi_led_classdev, work);
5114
5115 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005116 led_set_status(data->led, data->new_state);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005117}
5118
5119static void led_sysfs_set(struct led_classdev *led_cdev,
5120 enum led_brightness brightness)
5121{
5122 struct tpacpi_led_classdev *data = container_of(led_cdev,
5123 struct tpacpi_led_classdev, led_classdev);
5124
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005125 if (brightness == LED_OFF)
5126 data->new_state = TPACPI_LED_OFF;
5127 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
5128 data->new_state = TPACPI_LED_ON;
5129 else
5130 data->new_state = TPACPI_LED_BLINK;
5131
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03005132 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005133}
5134
5135static int led_sysfs_blink_set(struct led_classdev *led_cdev,
5136 unsigned long *delay_on, unsigned long *delay_off)
5137{
5138 struct tpacpi_led_classdev *data = container_of(led_cdev,
5139 struct tpacpi_led_classdev, led_classdev);
5140
5141 /* Can we choose the flash rate? */
5142 if (*delay_on == 0 && *delay_off == 0) {
5143 /* yes. set them to the hardware blink rate (1 Hz) */
5144 *delay_on = 500; /* ms */
5145 *delay_off = 500; /* ms */
5146 } else if ((*delay_on != 500) || (*delay_off != 500))
5147 return -EINVAL;
5148
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005149 data->new_state = TPACPI_LED_BLINK;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03005150 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005151
5152 return 0;
5153}
5154
5155static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
5156{
5157 int rc;
5158
5159 struct tpacpi_led_classdev *data = container_of(led_cdev,
5160 struct tpacpi_led_classdev, led_classdev);
5161
5162 rc = led_get_status(data->led);
5163
5164 if (rc == TPACPI_LED_OFF || rc < 0)
5165 rc = LED_OFF; /* no error handling in led class :( */
5166 else
5167 rc = LED_FULL;
5168
5169 return rc;
5170}
5171
5172static void led_exit(void)
5173{
5174 unsigned int i;
5175
5176 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5177 if (tpacpi_leds[i].led_classdev.name)
5178 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
5179 }
5180
5181 kfree(tpacpi_leds);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005182}
5183
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005184static int __init tpacpi_init_led(unsigned int led)
5185{
5186 int rc;
5187
5188 tpacpi_leds[led].led = led;
5189
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005190 /* LEDs with no name don't get registered */
5191 if (!tpacpi_led_names[led])
5192 return 0;
5193
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005194 tpacpi_leds[led].led_classdev.brightness_set = &led_sysfs_set;
5195 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5196 if (led_supported == TPACPI_LED_570)
5197 tpacpi_leds[led].led_classdev.brightness_get =
5198 &led_sysfs_get;
5199
5200 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5201
5202 INIT_WORK(&tpacpi_leds[led].work, led_set_status_worker);
5203
5204 rc = led_classdev_register(&tpacpi_pdev->dev,
5205 &tpacpi_leds[led].led_classdev);
5206 if (rc < 0)
5207 tpacpi_leds[led].led_classdev.name = NULL;
5208
5209 return rc;
5210}
5211
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005212static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
5213 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5214 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5215 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5216
5217 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5218 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5219 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5220 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5221 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5222 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5223 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5224 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5225
5226 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5227 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5228 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5229 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5230 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5231
5232 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5233 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5234 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5235 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5236
5237 /* (1) - may have excess leds enabled on MSB */
5238
5239 /* Defaults (order matters, keep last, don't reorder!) */
5240 { /* Lenovo */
5241 .vendor = PCI_VENDOR_ID_LENOVO,
5242 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5243 .quirks = 0x1fffU,
5244 },
5245 { /* IBM ThinkPads with no EC version string */
5246 .vendor = PCI_VENDOR_ID_IBM,
5247 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
5248 .quirks = 0x00ffU,
5249 },
5250 { /* IBM ThinkPads with EC version string */
5251 .vendor = PCI_VENDOR_ID_IBM,
5252 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5253 .quirks = 0x00bfU,
5254 },
5255};
5256
5257#undef TPACPI_LEDQ_IBM
5258#undef TPACPI_LEDQ_LNV
5259
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005260static int __init led_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005261{
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005262 unsigned int i;
5263 int rc;
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005264 unsigned long useful_leds;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005265
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005266 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
5267
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005268 TPACPI_ACPIHANDLE_INIT(led);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005269
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005270 if (!led_handle)
5271 /* led not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005272 led_supported = TPACPI_LED_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005273 else if (strlencmp(led_path, "SLED") == 0)
5274 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005275 led_supported = TPACPI_LED_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005276 else if (strlencmp(led_path, "SYSL") == 0)
5277 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005278 led_supported = TPACPI_LED_OLD;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005279 else
5280 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005281 led_supported = TPACPI_LED_NEW;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005282
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005283 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
5284 str_supported(led_supported), led_supported);
5285
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005286 if (led_supported == TPACPI_LED_NONE)
5287 return 1;
5288
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005289 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
5290 GFP_KERNEL);
5291 if (!tpacpi_leds) {
5292 printk(TPACPI_ERR "Out of memory for LED data\n");
5293 return -ENOMEM;
5294 }
5295
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005296 useful_leds = tpacpi_check_quirks(led_useful_qtable,
5297 ARRAY_SIZE(led_useful_qtable));
5298
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005299 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005300 if (!tpacpi_is_led_restricted(i) &&
5301 test_bit(i, &useful_leds)) {
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005302 rc = tpacpi_init_led(i);
5303 if (rc < 0) {
5304 led_exit();
5305 return rc;
5306 }
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005307 }
5308 }
5309
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005310#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005311 printk(TPACPI_NOTICE
5312 "warning: userspace override of important "
5313 "firmware LEDs is enabled\n");
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005314#endif
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005315 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005316}
5317
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005318#define str_led_status(s) \
5319 ((s) == TPACPI_LED_OFF ? "off" : \
5320 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005321
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005322static int led_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005323{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005324 if (!led_supported) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005325 seq_printf(m, "status:\t\tnot supported\n");
5326 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005327 }
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005328 seq_printf(m, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005329
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005330 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005331 /* 570 */
5332 int i, status;
5333 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005334 status = led_get_status(i);
5335 if (status < 0)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005336 return -EIO;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005337 seq_printf(m, "%d:\t\t%s\n",
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005338 i, str_led_status(status));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005339 }
5340 }
5341
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005342 seq_printf(m, "commands:\t"
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005343 "<led> on, <led> off, <led> blink (<led> is 0-15)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005344
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005345 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005346}
5347
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005348static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005349{
5350 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005351 int led, rc;
5352 enum led_status_t s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005353
5354 if (!led_supported)
5355 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005356
5357 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005358 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 15)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005359 return -EINVAL;
5360
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005361 if (strstr(cmd, "off")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005362 s = TPACPI_LED_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005363 } else if (strstr(cmd, "on")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005364 s = TPACPI_LED_ON;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005365 } else if (strstr(cmd, "blink")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005366 s = TPACPI_LED_BLINK;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005367 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005368 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005369 }
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005370
5371 rc = led_set_status(led, s);
5372 if (rc < 0)
5373 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005374 }
5375
5376 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005377}
5378
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005379static struct ibm_struct led_driver_data = {
5380 .name = "led",
5381 .read = led_read,
5382 .write = led_write,
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005383 .exit = led_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005384};
5385
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005386/*************************************************************************
5387 * Beep subdriver
5388 */
5389
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005390TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005391
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005392#define TPACPI_BEEP_Q1 0x0001
5393
5394static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
5395 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
5396 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
5397};
5398
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005399static int __init beep_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005400{
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005401 unsigned long quirks;
5402
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005403 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
5404
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005405 TPACPI_ACPIHANDLE_INIT(beep);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005406
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005407 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
5408 str_supported(beep_handle != NULL));
5409
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005410 quirks = tpacpi_check_quirks(beep_quirk_table,
5411 ARRAY_SIZE(beep_quirk_table));
5412
5413 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
5414
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005415 return (beep_handle)? 0 : 1;
5416}
5417
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005418static int beep_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005419{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005420 if (!beep_handle)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005421 seq_printf(m, "status:\t\tnot supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005422 else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005423 seq_printf(m, "status:\t\tsupported\n");
5424 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-17)\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005425 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005426
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005427 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005428}
5429
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005430static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005431{
5432 char *cmd;
5433 int beep_cmd;
5434
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005435 if (!beep_handle)
5436 return -ENODEV;
5437
Linus Torvalds1da177e2005-04-16 15:20:36 -07005438 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005439 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
5440 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005441 /* beep_cmd set */
5442 } else
5443 return -EINVAL;
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005444 if (tp_features.beep_needs_two_args) {
5445 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
5446 beep_cmd, 0))
5447 return -EIO;
5448 } else {
5449 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
5450 beep_cmd))
5451 return -EIO;
5452 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005453 }
5454
5455 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005456}
5457
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005458static struct ibm_struct beep_driver_data = {
5459 .name = "beep",
5460 .read = beep_read,
5461 .write = beep_write,
5462};
5463
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005464/*************************************************************************
5465 * Thermal subdriver
5466 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005467
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005468enum thermal_access_mode {
5469 TPACPI_THERMAL_NONE = 0, /* No thermal support */
5470 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
5471 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
5472 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
5473 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
5474};
5475
5476enum { /* TPACPI_THERMAL_TPEC_* */
5477 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
5478 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
5479 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005480
5481 TPACPI_THERMAL_SENSOR_NA = -128000, /* Sensor not available */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005482};
5483
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005484
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005485#define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5486struct ibm_thermal_sensors_struct {
5487 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
5488};
5489
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005490static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005491
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005492/* idx is zero-based */
5493static int thermal_get_sensor(int idx, s32 *value)
5494{
5495 int t;
5496 s8 tmp;
5497 char tmpi[5];
5498
5499 t = TP_EC_THERMAL_TMP0;
5500
5501 switch (thermal_read_mode) {
5502#if TPACPI_MAX_THERMAL_SENSORS >= 16
5503 case TPACPI_THERMAL_TPEC_16:
5504 if (idx >= 8 && idx <= 15) {
5505 t = TP_EC_THERMAL_TMP8;
5506 idx -= 8;
5507 }
5508 /* fallthrough */
5509#endif
5510 case TPACPI_THERMAL_TPEC_8:
5511 if (idx <= 7) {
5512 if (!acpi_ec_read(t + idx, &tmp))
5513 return -EIO;
5514 *value = tmp * 1000;
5515 return 0;
5516 }
5517 break;
5518
5519 case TPACPI_THERMAL_ACPI_UPDT:
5520 if (idx <= 7) {
5521 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5522 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
5523 return -EIO;
5524 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5525 return -EIO;
5526 *value = (t - 2732) * 100;
5527 return 0;
5528 }
5529 break;
5530
5531 case TPACPI_THERMAL_ACPI_TMP07:
5532 if (idx <= 7) {
5533 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5534 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5535 return -EIO;
5536 if (t > 127 || t < -127)
5537 t = TP_EC_THERMAL_TMP_NA;
5538 *value = t * 1000;
5539 return 0;
5540 }
5541 break;
5542
5543 case TPACPI_THERMAL_NONE:
5544 default:
5545 return -ENOSYS;
5546 }
5547
5548 return -EINVAL;
5549}
5550
5551static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5552{
5553 int res, i;
5554 int n;
5555
5556 n = 8;
5557 i = 0;
5558
5559 if (!s)
5560 return -EINVAL;
5561
5562 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5563 n = 16;
5564
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005565 for (i = 0 ; i < n; i++) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005566 res = thermal_get_sensor(i, &s->temp[i]);
5567 if (res)
5568 return res;
5569 }
5570
5571 return n;
5572}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005573
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005574static void thermal_dump_all_sensors(void)
5575{
5576 int n, i;
5577 struct ibm_thermal_sensors_struct t;
5578
5579 n = thermal_get_sensors(&t);
5580 if (n <= 0)
5581 return;
5582
5583 printk(TPACPI_NOTICE
5584 "temperatures (Celsius):");
5585
5586 for (i = 0; i < n; i++) {
5587 if (t.temp[i] != TPACPI_THERMAL_SENSOR_NA)
5588 printk(KERN_CONT " %d", (int)(t.temp[i] / 1000));
5589 else
5590 printk(KERN_CONT " N/A");
5591 }
5592
5593 printk(KERN_CONT "\n");
5594}
5595
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005596/* sysfs temp##_input -------------------------------------------------- */
5597
5598static ssize_t thermal_temp_input_show(struct device *dev,
5599 struct device_attribute *attr,
5600 char *buf)
5601{
5602 struct sensor_device_attribute *sensor_attr =
5603 to_sensor_dev_attr(attr);
5604 int idx = sensor_attr->index;
5605 s32 value;
5606 int res;
5607
5608 res = thermal_get_sensor(idx, &value);
5609 if (res)
5610 return res;
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005611 if (value == TPACPI_THERMAL_SENSOR_NA)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005612 return -ENXIO;
5613
5614 return snprintf(buf, PAGE_SIZE, "%d\n", value);
5615}
5616
5617#define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005618 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5619 thermal_temp_input_show, NULL, _idxB)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005620
5621static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5622 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5623 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5624 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5625 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5626 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5627 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5628 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5629 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5630 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5631 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5632 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5633 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5634 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5635 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5636 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5637 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5638};
5639
5640#define THERMAL_ATTRS(X) \
5641 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5642
5643static struct attribute *thermal_temp_input_attr[] = {
5644 THERMAL_ATTRS(8),
5645 THERMAL_ATTRS(9),
5646 THERMAL_ATTRS(10),
5647 THERMAL_ATTRS(11),
5648 THERMAL_ATTRS(12),
5649 THERMAL_ATTRS(13),
5650 THERMAL_ATTRS(14),
5651 THERMAL_ATTRS(15),
5652 THERMAL_ATTRS(0),
5653 THERMAL_ATTRS(1),
5654 THERMAL_ATTRS(2),
5655 THERMAL_ATTRS(3),
5656 THERMAL_ATTRS(4),
5657 THERMAL_ATTRS(5),
5658 THERMAL_ATTRS(6),
5659 THERMAL_ATTRS(7),
5660 NULL
5661};
5662
5663static const struct attribute_group thermal_temp_input16_group = {
5664 .attrs = thermal_temp_input_attr
5665};
5666
5667static const struct attribute_group thermal_temp_input8_group = {
5668 .attrs = &thermal_temp_input_attr[8]
5669};
5670
5671#undef THERMAL_SENSOR_ATTR_TEMP
5672#undef THERMAL_ATTRS
5673
5674/* --------------------------------------------------------------------- */
5675
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005676static int __init thermal_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005677{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005678 u8 t, ta1, ta2;
5679 int i;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005680 int acpi_tmp7;
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005681 int res;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005682
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005683 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5684
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005685 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005686
Henrique de Moraes Holschuh3d6f99c2007-07-18 23:45:46 -03005687 if (thinkpad_id.ec_model) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005688 /*
5689 * Direct EC access mode: sensors at registers
5690 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5691 * non-implemented, thermal sensors return 0x80 when
5692 * not available
5693 */
5694
5695 ta1 = ta2 = 0;
5696 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03005697 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005698 ta1 |= t;
5699 } else {
5700 ta1 = 0;
5701 break;
5702 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03005703 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005704 ta2 |= t;
5705 } else {
5706 ta1 = 0;
5707 break;
5708 }
5709 }
5710 if (ta1 == 0) {
5711 /* This is sheer paranoia, but we handle it anyway */
5712 if (acpi_tmp7) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005713 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005714 "ThinkPad ACPI EC access misbehaving, "
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005715 "falling back to ACPI TMPx access "
5716 "mode\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005717 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005718 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005719 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005720 "ThinkPad ACPI EC access misbehaving, "
5721 "disabling thermal sensors access\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005722 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005723 }
5724 } else {
5725 thermal_read_mode =
5726 (ta2 != 0) ?
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005727 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005728 }
5729 } else if (acpi_tmp7) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005730 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
5731 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005732 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005733 } else {
5734 /* Standard ACPI TMPx access, max 8 sensors */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005735 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005736 }
5737 } else {
5738 /* temperatures not supported on 570, G4x, R30, R31, R32 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005739 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005740 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005741
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005742 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5743 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5744 thermal_read_mode);
5745
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005746 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005747 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005748 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005749 &thermal_temp_input16_group);
5750 if (res)
5751 return res;
5752 break;
5753 case TPACPI_THERMAL_TPEC_8:
5754 case TPACPI_THERMAL_ACPI_TMP07:
5755 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005756 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005757 &thermal_temp_input8_group);
5758 if (res)
5759 return res;
5760 break;
5761 case TPACPI_THERMAL_NONE:
5762 default:
5763 return 1;
5764 }
5765
5766 return 0;
5767}
5768
5769static void thermal_exit(void)
5770{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005771 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005772 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005773 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005774 &thermal_temp_input16_group);
5775 break;
5776 case TPACPI_THERMAL_TPEC_8:
5777 case TPACPI_THERMAL_ACPI_TMP07:
5778 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005779 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Roel Kluinf04d5e02010-02-02 14:37:58 -08005780 &thermal_temp_input8_group);
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005781 break;
5782 case TPACPI_THERMAL_NONE:
5783 default:
5784 break;
5785 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005786}
5787
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005788static int thermal_read(struct seq_file *m)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005789{
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005790 int n, i;
5791 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005792
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005793 n = thermal_get_sensors(&t);
5794 if (unlikely(n < 0))
5795 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005796
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005797 seq_printf(m, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005798
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005799 if (n > 0) {
5800 for (i = 0; i < (n - 1); i++)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005801 seq_printf(m, "%d ", t.temp[i] / 1000);
5802 seq_printf(m, "%d\n", t.temp[i] / 1000);
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005803 } else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005804 seq_printf(m, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005805
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005806 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005807}
5808
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005809static struct ibm_struct thermal_driver_data = {
5810 .name = "thermal",
5811 .read = thermal_read,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005812 .exit = thermal_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005813};
5814
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005815/*************************************************************************
5816 * EC Dump subdriver
5817 */
5818
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005819static u8 ecdump_regs[256];
5820
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005821static int ecdump_read(struct seq_file *m)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005822{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005823 int i, j;
5824 u8 v;
5825
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005826 seq_printf(m, "EC "
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005827 " +00 +01 +02 +03 +04 +05 +06 +07"
5828 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
5829 for (i = 0; i < 256; i += 16) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005830 seq_printf(m, "EC 0x%02x:", i);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005831 for (j = 0; j < 16; j++) {
5832 if (!acpi_ec_read(i + j, &v))
5833 break;
5834 if (v != ecdump_regs[i + j])
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005835 seq_printf(m, " *%02x", v);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005836 else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005837 seq_printf(m, " %02x", v);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005838 ecdump_regs[i + j] = v;
5839 }
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005840 seq_putc(m, '\n');
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005841 if (j != 16)
5842 break;
5843 }
5844
5845 /* These are way too dangerous to advertise openly... */
5846#if 0
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005847 seq_printf(m, "commands:\t0x<offset> 0x<value>"
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005848 " (<offset> is 00-ff, <value> is 00-ff)\n");
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005849 seq_printf(m, "commands:\t0x<offset> <value> "
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005850 " (<offset> is 00-ff, <value> is 0-255)\n");
5851#endif
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005852 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005853}
5854
5855static int ecdump_write(char *buf)
5856{
5857 char *cmd;
5858 int i, v;
5859
5860 while ((cmd = next_cmd(&buf))) {
5861 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
5862 /* i and v set */
5863 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
5864 /* i and v set */
5865 } else
5866 return -EINVAL;
5867 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
5868 if (!acpi_ec_write(i, v))
5869 return -EIO;
5870 } else
5871 return -EINVAL;
5872 }
5873
5874 return 0;
5875}
5876
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005877static struct ibm_struct ecdump_driver_data = {
5878 .name = "ecdump",
5879 .read = ecdump_read,
5880 .write = ecdump_write,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03005881 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005882};
5883
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005884/*************************************************************************
5885 * Backlight/brightness subdriver
5886 */
Holger Macht8acb0252006-10-20 14:30:28 -07005887
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005888#define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5889
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005890/*
5891 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
5892 * CMOS NVRAM byte 0x5E, bits 0-3.
5893 *
5894 * EC HBRV (0x31) has the following layout
5895 * Bit 7: unknown function
5896 * Bit 6: unknown function
5897 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5898 * Bit 4: must be set to zero to avoid problems
5899 * Bit 3-0: backlight brightness level
5900 *
5901 * brightness_get_raw returns status data in the HBRV layout
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03005902 *
5903 * WARNING: The X61 has been verified to use HBRV for something else, so
5904 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
5905 * testing on the very early *60 Lenovo models...
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005906 */
5907
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005908enum {
5909 TP_EC_BACKLIGHT = 0x31,
5910
5911 /* TP_EC_BACKLIGHT bitmasks */
5912 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5913 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5914 TP_EC_BACKLIGHT_MAPSW = 0x20,
5915};
5916
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005917enum tpacpi_brightness_access_mode {
5918 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
5919 TPACPI_BRGHT_MODE_EC, /* EC control */
5920 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
5921 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
5922 TPACPI_BRGHT_MODE_MAX
5923};
5924
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03005925static struct backlight_device *ibm_backlight_device;
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005926
5927static enum tpacpi_brightness_access_mode brightness_mode =
5928 TPACPI_BRGHT_MODE_MAX;
5929
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005930static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5931
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005932static struct mutex brightness_mutex;
5933
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005934/* NVRAM brightness access,
5935 * call with brightness_mutex held! */
5936static unsigned int tpacpi_brightness_nvram_get(void)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005937{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005938 u8 lnvram;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005939
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005940 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5941 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5942 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
5943 lnvram &= (tp_features.bright_16levels) ? 0x0f : 0x07;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005944
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005945 return lnvram;
5946}
5947
5948static void tpacpi_brightness_checkpoint_nvram(void)
5949{
5950 u8 lec = 0;
5951 u8 b_nvram;
5952
5953 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
5954 return;
5955
5956 vdbg_printk(TPACPI_DBG_BRGHT,
5957 "trying to checkpoint backlight level to NVRAM...\n");
5958
5959 if (mutex_lock_killable(&brightness_mutex) < 0)
5960 return;
5961
5962 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5963 goto unlock;
5964 lec &= TP_EC_BACKLIGHT_LVLMSK;
5965 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
5966
5967 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5968 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
5969 /* NVRAM needs update */
5970 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
5971 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
5972 b_nvram |= lec;
5973 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
5974 dbg_printk(TPACPI_DBG_BRGHT,
5975 "updated NVRAM backlight level to %u (0x%02x)\n",
5976 (unsigned int) lec, (unsigned int) b_nvram);
5977 } else
5978 vdbg_printk(TPACPI_DBG_BRGHT,
5979 "NVRAM backlight level already is %u (0x%02x)\n",
5980 (unsigned int) lec, (unsigned int) b_nvram);
5981
5982unlock:
5983 mutex_unlock(&brightness_mutex);
5984}
5985
5986
5987/* call with brightness_mutex held! */
5988static int tpacpi_brightness_get_raw(int *status)
5989{
5990 u8 lec = 0;
5991
5992 switch (brightness_mode) {
5993 case TPACPI_BRGHT_MODE_UCMS_STEP:
5994 *status = tpacpi_brightness_nvram_get();
5995 return 0;
5996 case TPACPI_BRGHT_MODE_EC:
5997 case TPACPI_BRGHT_MODE_ECNVRAM:
5998 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
Henrique de Moraes Holschuh2d5e94d2008-04-26 01:02:20 -03005999 return -EIO;
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006000 *status = lec;
6001 return 0;
6002 default:
6003 return -ENXIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006004 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006005}
6006
6007/* call with brightness_mutex held! */
6008/* do NOT call with illegal backlight level value */
6009static int tpacpi_brightness_set_ec(unsigned int value)
6010{
6011 u8 lec = 0;
6012
6013 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6014 return -EIO;
6015
6016 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
6017 (lec & TP_EC_BACKLIGHT_CMDMSK) |
6018 (value & TP_EC_BACKLIGHT_LVLMSK))))
6019 return -EIO;
6020
6021 return 0;
6022}
6023
6024/* call with brightness_mutex held! */
6025static int tpacpi_brightness_set_ucmsstep(unsigned int value)
6026{
6027 int cmos_cmd, inc;
6028 unsigned int current_value, i;
6029
6030 current_value = tpacpi_brightness_nvram_get();
6031
6032 if (value == current_value)
6033 return 0;
6034
6035 cmos_cmd = (value > current_value) ?
6036 TP_CMOS_BRIGHTNESS_UP :
6037 TP_CMOS_BRIGHTNESS_DOWN;
6038 inc = (value > current_value) ? 1 : -1;
6039
6040 for (i = current_value; i != value; i += inc)
6041 if (issue_thinkpad_cmos_command(cmos_cmd))
6042 return -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006043
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006044 return 0;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006045}
6046
6047/* May return EINTR which can always be mapped to ERESTARTSYS */
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006048static int brightness_set(unsigned int value)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006049{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006050 int res;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006051
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006052 if (value > ((tp_features.bright_16levels)? 15 : 7) ||
6053 value < 0)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006054 return -EINVAL;
6055
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006056 vdbg_printk(TPACPI_DBG_BRGHT,
6057 "set backlight level to %d\n", value);
6058
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006059 res = mutex_lock_killable(&brightness_mutex);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006060 if (res < 0)
6061 return res;
6062
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006063 switch (brightness_mode) {
6064 case TPACPI_BRGHT_MODE_EC:
6065 case TPACPI_BRGHT_MODE_ECNVRAM:
6066 res = tpacpi_brightness_set_ec(value);
6067 break;
6068 case TPACPI_BRGHT_MODE_UCMS_STEP:
6069 res = tpacpi_brightness_set_ucmsstep(value);
6070 break;
6071 default:
6072 res = -ENXIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006073 }
6074
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006075 mutex_unlock(&brightness_mutex);
6076 return res;
6077}
6078
6079/* sysfs backlight class ----------------------------------------------- */
6080
6081static int brightness_update_status(struct backlight_device *bd)
6082{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006083 unsigned int level =
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006084 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
6085 bd->props.power == FB_BLANK_UNBLANK) ?
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006086 bd->props.brightness : 0;
6087
6088 dbg_printk(TPACPI_DBG_BRGHT,
6089 "backlight: attempt to set level to %d\n",
6090 level);
6091
6092 /* it is the backlight class's job (caller) to handle
6093 * EINTR and other errors properly */
6094 return brightness_set(level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006095}
Holger Macht8acb0252006-10-20 14:30:28 -07006096
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006097static int brightness_get(struct backlight_device *bd)
6098{
6099 int status, res;
6100
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006101 res = mutex_lock_killable(&brightness_mutex);
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006102 if (res < 0)
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006103 return 0;
6104
6105 res = tpacpi_brightness_get_raw(&status);
6106
6107 mutex_unlock(&brightness_mutex);
6108
6109 if (res < 0)
6110 return 0;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006111
6112 return status & TP_EC_BACKLIGHT_LVLMSK;
6113}
6114
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00006115static void tpacpi_brightness_notify_change(void)
6116{
6117 backlight_force_update(ibm_backlight_device,
6118 BACKLIGHT_UPDATE_HOTKEY);
6119}
6120
Richard Purdie599a52d2007-02-10 23:07:48 +00006121static struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006122 .get_brightness = brightness_get,
6123 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006124};
6125
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006126/* --------------------------------------------------------------------- */
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03006127
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006128/*
6129 * These are only useful for models that have only one possibility
6130 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6131 * these quirks.
6132 */
6133#define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6134#define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6135#define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6136
6137static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
6138 /* Models with ATI GPUs known to require ECNVRAM mode */
6139 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
6140
Henrique de Moraes Holschuh6da25bf2009-09-12 15:22:11 -03006141 /* Models with ATI GPUs that can use ECNVRAM */
6142 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC),
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006143 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6144 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6145 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6146
Henrique de Moraes Holschuh6da25bf2009-09-12 15:22:11 -03006147 /* Models with Intel Extreme Graphics 2 */
6148 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC),
Henrique de Moraes Holschuha9f8eac2009-12-09 01:36:21 +00006149 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6150 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006151
6152 /* Models with Intel GMA900 */
6153 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
6154 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
6155 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
6156};
6157
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006158static int __init brightness_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006159{
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006160 int b;
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006161 unsigned long quirks;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006162
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006163 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
6164
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03006165 mutex_init(&brightness_mutex);
6166
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006167 quirks = tpacpi_check_quirks(brightness_quirk_table,
6168 ARRAY_SIZE(brightness_quirk_table));
6169
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03006170 /*
6171 * We always attempt to detect acpi support, so as to switch
6172 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6173 * going to publish a backlight interface
6174 */
6175 b = tpacpi_check_std_acpi_brightness_support();
6176 if (b > 0) {
Thomas Renninger2dba1b52008-08-01 17:38:03 +02006177
6178 if (acpi_video_backlight_support()) {
6179 if (brightness_enable > 1) {
6180 printk(TPACPI_NOTICE
6181 "Standard ACPI backlight interface "
6182 "available, not loading native one.\n");
6183 return 1;
6184 } else if (brightness_enable == 1) {
6185 printk(TPACPI_NOTICE
6186 "Backlight control force enabled, even if standard "
6187 "ACPI backlight interface is available\n");
6188 }
6189 } else {
6190 if (brightness_enable > 1) {
6191 printk(TPACPI_NOTICE
6192 "Standard ACPI backlight interface not "
6193 "available, thinkpad_acpi native "
6194 "brightness control enabled\n");
6195 }
Henrique de Moraes Holschuhe11e2112007-10-30 17:46:22 -02006196 }
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02006197 }
6198
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03006199 if (!brightness_enable) {
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006200 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03006201 "brightness support disabled by "
6202 "module parameter\n");
6203 return 1;
6204 }
6205
6206 if (b > 16) {
6207 printk(TPACPI_ERR
6208 "Unsupported brightness interface, "
6209 "please contact %s\n", TPACPI_MAIL);
6210 return 1;
6211 }
6212 if (b == 16)
6213 tp_features.bright_16levels = 1;
6214
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006215 /*
6216 * Check for module parameter bogosity, note that we
6217 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
6218 * able to detect "unspecified"
6219 */
6220 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03006221 return -EINVAL;
6222
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006223 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
6224 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
6225 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006226 if (quirks & TPACPI_BRGHT_Q_EC)
6227 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
6228 else
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006229 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
6230
6231 dbg_printk(TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006232 "driver auto-selected brightness_mode=%d\n",
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006233 brightness_mode);
6234 }
6235
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03006236 /* Safety */
6237 if (thinkpad_id.vendor != PCI_VENDOR_ID_IBM &&
6238 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
6239 brightness_mode == TPACPI_BRGHT_MODE_EC))
6240 return -EINVAL;
6241
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006242 if (tpacpi_brightness_get_raw(&b) < 0)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03006243 return 1;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006244
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02006245 if (tp_features.bright_16levels)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006246 printk(TPACPI_INFO
6247 "detected a 16-level brightness capable ThinkPad\n");
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02006248
Henrique de Moraes Holschuh7d5a0152007-04-24 11:48:20 -03006249 ibm_backlight_device = backlight_device_register(
6250 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
6251 &ibm_backlight_data);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006252 if (IS_ERR(ibm_backlight_device)) {
Henrique de Moraes Holschuh435c47e2009-09-20 14:09:22 -03006253 int rc = PTR_ERR(ibm_backlight_device);
6254 ibm_backlight_device = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006255 printk(TPACPI_ERR "Could not register backlight device\n");
Henrique de Moraes Holschuh435c47e2009-09-20 14:09:22 -03006256 return rc;
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006257 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006258 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6259 "brightness is supported\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006260
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006261 if (quirks & TPACPI_BRGHT_Q_ASK) {
6262 printk(TPACPI_NOTICE
6263 "brightness: will use unverified default: "
6264 "brightness_mode=%d\n", brightness_mode);
6265 printk(TPACPI_NOTICE
6266 "brightness: please report to %s whether it works well "
6267 "or not on your ThinkPad\n", TPACPI_MAIL);
6268 }
6269
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02006270 ibm_backlight_device->props.max_brightness =
6271 (tp_features.bright_16levels)? 15 : 7;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006272 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006273 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00006274
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00006275 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6276 "brightness: registering brightness hotkeys "
6277 "as change notification\n");
6278 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6279 | TP_ACPI_HKEY_BRGHTUP_MASK
6280 | TP_ACPI_HKEY_BRGHTDWN_MASK);;
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006281 return 0;
6282}
6283
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006284static void brightness_suspend(pm_message_t state)
6285{
6286 tpacpi_brightness_checkpoint_nvram();
6287}
6288
6289static void brightness_shutdown(void)
6290{
6291 tpacpi_brightness_checkpoint_nvram();
6292}
6293
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006294static void brightness_exit(void)
6295{
6296 if (ibm_backlight_device) {
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006297 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006298 "calling backlight_device_unregister()\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006299 backlight_device_unregister(ibm_backlight_device);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006300 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006301
6302 tpacpi_brightness_checkpoint_nvram();
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006303}
6304
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006305static int brightness_read(struct seq_file *m)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006306{
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006307 int level;
6308
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006309 level = brightness_get(NULL);
6310 if (level < 0) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006311 seq_printf(m, "level:\t\tunreadable\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006312 } else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006313 seq_printf(m, "level:\t\t%d\n", level);
6314 seq_printf(m, "commands:\tup, down\n");
6315 seq_printf(m, "commands:\tlevel <level>"
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02006316 " (<level> is 0-%d)\n",
6317 (tp_features.bright_16levels) ? 15 : 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006318 }
6319
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006320 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006321}
6322
6323static int brightness_write(char *buf)
6324{
6325 int level;
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006326 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006327 char *cmd;
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02006328 int max_level = (tp_features.bright_16levels) ? 15 : 7;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006329
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006330 level = brightness_get(NULL);
6331 if (level < 0)
6332 return level;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006333
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006334 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006335 if (strlencmp(cmd, "up") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006336 if (level < max_level)
6337 level++;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006338 } else if (strlencmp(cmd, "down") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006339 if (level > 0)
6340 level--;
6341 } else if (sscanf(cmd, "level %d", &level) == 1 &&
6342 level >= 0 && level <= max_level) {
6343 /* new level set */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006344 } else
6345 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006346 }
6347
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006348 tpacpi_disclose_usertask("procfs brightness",
6349 "set level to %d\n", level);
6350
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006351 /*
6352 * Now we know what the final level should be, so we try to set it.
6353 * Doing it this way makes the syscall restartable in case of EINTR
6354 */
6355 rc = brightness_set(level);
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00006356 if (!rc && ibm_backlight_device)
6357 backlight_force_update(ibm_backlight_device,
6358 BACKLIGHT_UPDATE_SYSFS);
Roel Kluin80a8d122009-11-20 19:48:23 +01006359 return (rc == -EINTR)? -ERESTARTSYS : rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006360}
6361
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006362static struct ibm_struct brightness_driver_data = {
6363 .name = "brightness",
6364 .read = brightness_read,
6365 .write = brightness_write,
6366 .exit = brightness_exit,
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006367 .suspend = brightness_suspend,
6368 .shutdown = brightness_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006369};
6370
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006371/*************************************************************************
6372 * Volume subdriver
6373 */
6374
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006375/*
6376 * IBM ThinkPads have a simple volume controller with MUTE gating.
6377 * Very early Lenovo ThinkPads follow the IBM ThinkPad spec.
6378 *
6379 * Since the *61 series (and probably also the later *60 series), Lenovo
6380 * ThinkPads only implement the MUTE gate.
6381 *
6382 * EC register 0x30
6383 * Bit 6: MUTE (1 mutes sound)
6384 * Bit 3-0: Volume
6385 * Other bits should be zero as far as we know.
6386 *
6387 * This is also stored in CMOS NVRAM, byte 0x60, bit 6 (MUTE), and
6388 * bits 3-0 (volume). Other bits in NVRAM may have other functions,
6389 * such as bit 7 which is used to detect repeated presses of MUTE,
6390 * and we leave them unchanged.
6391 */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006392
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02006393#ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
6394
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006395#define TPACPI_ALSA_DRVNAME "ThinkPad EC"
6396#define TPACPI_ALSA_SHRTNAME "ThinkPad Console Audio Control"
6397#define TPACPI_ALSA_MIXERNAME TPACPI_ALSA_SHRTNAME
6398
Henrique de Moraes Holschuhead510c2009-12-26 22:52:13 -02006399static int alsa_index = ~((1 << (SNDRV_CARDS - 3)) - 1); /* last three slots */
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006400static char *alsa_id = "ThinkPadEC";
6401static int alsa_enable = SNDRV_DEFAULT_ENABLE1;
6402
6403struct tpacpi_alsa_data {
6404 struct snd_card *card;
6405 struct snd_ctl_elem_id *ctl_mute_id;
6406 struct snd_ctl_elem_id *ctl_vol_id;
6407};
6408
6409static struct snd_card *alsa_card;
6410
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006411enum {
6412 TP_EC_AUDIO = 0x30,
6413
6414 /* TP_EC_AUDIO bits */
6415 TP_EC_AUDIO_MUTESW = 6,
6416
6417 /* TP_EC_AUDIO bitmasks */
6418 TP_EC_AUDIO_LVL_MSK = 0x0F,
6419 TP_EC_AUDIO_MUTESW_MSK = (1 << TP_EC_AUDIO_MUTESW),
6420
6421 /* Maximum volume */
6422 TP_EC_VOLUME_MAX = 14,
6423};
6424
6425enum tpacpi_volume_access_mode {
6426 TPACPI_VOL_MODE_AUTO = 0, /* Not implemented yet */
6427 TPACPI_VOL_MODE_EC, /* Pure EC control */
6428 TPACPI_VOL_MODE_UCMS_STEP, /* UCMS step-based control: N/A */
6429 TPACPI_VOL_MODE_ECNVRAM, /* EC control w/ NVRAM store */
6430 TPACPI_VOL_MODE_MAX
6431};
6432
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006433enum tpacpi_volume_capabilities {
6434 TPACPI_VOL_CAP_AUTO = 0, /* Use white/blacklist */
6435 TPACPI_VOL_CAP_VOLMUTE, /* Output vol and mute */
6436 TPACPI_VOL_CAP_MUTEONLY, /* Output mute only */
6437 TPACPI_VOL_CAP_MAX
6438};
6439
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006440static enum tpacpi_volume_access_mode volume_mode =
6441 TPACPI_VOL_MODE_MAX;
6442
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006443static enum tpacpi_volume_capabilities volume_capabilities;
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006444static int volume_control_allowed;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006445
6446/*
6447 * Used to syncronize writers to TP_EC_AUDIO and
6448 * TP_NVRAM_ADDR_MIXER, as we need to do read-modify-write
6449 */
6450static struct mutex volume_mutex;
6451
6452static void tpacpi_volume_checkpoint_nvram(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006453{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006454 u8 lec = 0;
6455 u8 b_nvram;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006456 u8 ec_mask;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006457
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006458 if (volume_mode != TPACPI_VOL_MODE_ECNVRAM)
6459 return;
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006460 if (!volume_control_allowed)
6461 return;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006462
6463 vdbg_printk(TPACPI_DBG_MIXER,
6464 "trying to checkpoint mixer state to NVRAM...\n");
6465
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006466 if (tp_features.mixer_no_level_control)
6467 ec_mask = TP_EC_AUDIO_MUTESW_MSK;
6468 else
6469 ec_mask = TP_EC_AUDIO_MUTESW_MSK | TP_EC_AUDIO_LVL_MSK;
6470
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006471 if (mutex_lock_killable(&volume_mutex) < 0)
6472 return;
6473
6474 if (unlikely(!acpi_ec_read(TP_EC_AUDIO, &lec)))
6475 goto unlock;
6476 lec &= ec_mask;
6477 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
6478
6479 if (lec != (b_nvram & ec_mask)) {
6480 /* NVRAM needs update */
6481 b_nvram &= ~ec_mask;
6482 b_nvram |= lec;
6483 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_MIXER);
6484 dbg_printk(TPACPI_DBG_MIXER,
6485 "updated NVRAM mixer status to 0x%02x (0x%02x)\n",
6486 (unsigned int) lec, (unsigned int) b_nvram);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006487 } else {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006488 vdbg_printk(TPACPI_DBG_MIXER,
6489 "NVRAM mixer status already is 0x%02x (0x%02x)\n",
6490 (unsigned int) lec, (unsigned int) b_nvram);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006491 }
6492
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006493unlock:
6494 mutex_unlock(&volume_mutex);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006495}
6496
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006497static int volume_get_status_ec(u8 *status)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006498{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006499 u8 s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006500
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006501 if (!acpi_ec_read(TP_EC_AUDIO, &s))
6502 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006503
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006504 *status = s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006505
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006506 dbg_printk(TPACPI_DBG_MIXER, "status 0x%02x\n", s);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006507
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006508 return 0;
6509}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006510
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006511static int volume_get_status(u8 *status)
6512{
6513 return volume_get_status_ec(status);
6514}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006515
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006516static int volume_set_status_ec(const u8 status)
6517{
6518 if (!acpi_ec_write(TP_EC_AUDIO, status))
6519 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006520
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006521 dbg_printk(TPACPI_DBG_MIXER, "set EC mixer to 0x%02x\n", status);
6522
6523 return 0;
6524}
6525
6526static int volume_set_status(const u8 status)
6527{
6528 return volume_set_status_ec(status);
6529}
6530
6531static int volume_set_mute_ec(const bool mute)
6532{
6533 int rc;
6534 u8 s, n;
6535
6536 if (mutex_lock_killable(&volume_mutex) < 0)
6537 return -EINTR;
6538
6539 rc = volume_get_status_ec(&s);
6540 if (rc)
6541 goto unlock;
6542
6543 n = (mute) ? s | TP_EC_AUDIO_MUTESW_MSK :
6544 s & ~TP_EC_AUDIO_MUTESW_MSK;
6545
6546 if (n != s)
6547 rc = volume_set_status_ec(n);
6548
6549unlock:
6550 mutex_unlock(&volume_mutex);
6551 return rc;
6552}
6553
6554static int volume_set_mute(const bool mute)
6555{
6556 dbg_printk(TPACPI_DBG_MIXER, "trying to %smute\n",
6557 (mute) ? "" : "un");
6558 return volume_set_mute_ec(mute);
6559}
6560
6561static int volume_set_volume_ec(const u8 vol)
6562{
6563 int rc;
6564 u8 s, n;
6565
6566 if (vol > TP_EC_VOLUME_MAX)
6567 return -EINVAL;
6568
6569 if (mutex_lock_killable(&volume_mutex) < 0)
6570 return -EINTR;
6571
6572 rc = volume_get_status_ec(&s);
6573 if (rc)
6574 goto unlock;
6575
6576 n = (s & ~TP_EC_AUDIO_LVL_MSK) | vol;
6577
6578 if (n != s)
6579 rc = volume_set_status_ec(n);
6580
6581unlock:
6582 mutex_unlock(&volume_mutex);
6583 return rc;
6584}
6585
6586static int volume_set_volume(const u8 vol)
6587{
6588 dbg_printk(TPACPI_DBG_MIXER,
6589 "trying to set volume level to %hu\n", vol);
6590 return volume_set_volume_ec(vol);
6591}
6592
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006593static void volume_alsa_notify_change(void)
6594{
6595 struct tpacpi_alsa_data *d;
6596
6597 if (alsa_card && alsa_card->private_data) {
6598 d = alsa_card->private_data;
6599 if (d->ctl_mute_id)
6600 snd_ctl_notify(alsa_card,
6601 SNDRV_CTL_EVENT_MASK_VALUE,
6602 d->ctl_mute_id);
6603 if (d->ctl_vol_id)
6604 snd_ctl_notify(alsa_card,
6605 SNDRV_CTL_EVENT_MASK_VALUE,
6606 d->ctl_vol_id);
6607 }
6608}
6609
6610static int volume_alsa_vol_info(struct snd_kcontrol *kcontrol,
6611 struct snd_ctl_elem_info *uinfo)
6612{
6613 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
6614 uinfo->count = 1;
6615 uinfo->value.integer.min = 0;
6616 uinfo->value.integer.max = TP_EC_VOLUME_MAX;
6617 return 0;
6618}
6619
6620static int volume_alsa_vol_get(struct snd_kcontrol *kcontrol,
6621 struct snd_ctl_elem_value *ucontrol)
6622{
6623 u8 s;
6624 int rc;
6625
6626 rc = volume_get_status(&s);
6627 if (rc < 0)
6628 return rc;
6629
6630 ucontrol->value.integer.value[0] = s & TP_EC_AUDIO_LVL_MSK;
6631 return 0;
6632}
6633
6634static int volume_alsa_vol_put(struct snd_kcontrol *kcontrol,
6635 struct snd_ctl_elem_value *ucontrol)
6636{
6637 return volume_set_volume(ucontrol->value.integer.value[0]);
6638}
6639
6640#define volume_alsa_mute_info snd_ctl_boolean_mono_info
6641
6642static int volume_alsa_mute_get(struct snd_kcontrol *kcontrol,
6643 struct snd_ctl_elem_value *ucontrol)
6644{
6645 u8 s;
6646 int rc;
6647
6648 rc = volume_get_status(&s);
6649 if (rc < 0)
6650 return rc;
6651
6652 ucontrol->value.integer.value[0] =
6653 (s & TP_EC_AUDIO_MUTESW_MSK) ? 0 : 1;
6654 return 0;
6655}
6656
6657static int volume_alsa_mute_put(struct snd_kcontrol *kcontrol,
6658 struct snd_ctl_elem_value *ucontrol)
6659{
6660 return volume_set_mute(!ucontrol->value.integer.value[0]);
6661}
6662
6663static struct snd_kcontrol_new volume_alsa_control_vol __devinitdata = {
6664 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6665 .name = "Console Playback Volume",
6666 .index = 0,
6667 .access = SNDRV_CTL_ELEM_ACCESS_READ,
6668 .info = volume_alsa_vol_info,
6669 .get = volume_alsa_vol_get,
6670};
6671
6672static struct snd_kcontrol_new volume_alsa_control_mute __devinitdata = {
6673 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6674 .name = "Console Playback Switch",
6675 .index = 0,
6676 .access = SNDRV_CTL_ELEM_ACCESS_READ,
6677 .info = volume_alsa_mute_info,
6678 .get = volume_alsa_mute_get,
6679};
6680
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006681static void volume_suspend(pm_message_t state)
6682{
6683 tpacpi_volume_checkpoint_nvram();
6684}
6685
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006686static void volume_resume(void)
6687{
6688 volume_alsa_notify_change();
6689}
6690
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006691static void volume_shutdown(void)
6692{
6693 tpacpi_volume_checkpoint_nvram();
6694}
6695
6696static void volume_exit(void)
6697{
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006698 if (alsa_card) {
6699 snd_card_free(alsa_card);
6700 alsa_card = NULL;
6701 }
6702
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006703 tpacpi_volume_checkpoint_nvram();
6704}
6705
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006706static int __init volume_create_alsa_mixer(void)
6707{
6708 struct snd_card *card;
6709 struct tpacpi_alsa_data *data;
6710 struct snd_kcontrol *ctl_vol;
6711 struct snd_kcontrol *ctl_mute;
6712 int rc;
6713
6714 rc = snd_card_create(alsa_index, alsa_id, THIS_MODULE,
6715 sizeof(struct tpacpi_alsa_data), &card);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02006716 if (rc < 0 || !card) {
6717 printk(TPACPI_ERR
6718 "Failed to create ALSA card structures: %d\n", rc);
6719 return 1;
6720 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006721
6722 BUG_ON(!card->private_data);
6723 data = card->private_data;
6724 data->card = card;
6725
6726 strlcpy(card->driver, TPACPI_ALSA_DRVNAME,
6727 sizeof(card->driver));
6728 strlcpy(card->shortname, TPACPI_ALSA_SHRTNAME,
6729 sizeof(card->shortname));
6730 snprintf(card->mixername, sizeof(card->mixername), "ThinkPad EC %s",
6731 (thinkpad_id.ec_version_str) ?
6732 thinkpad_id.ec_version_str : "(unknown)");
6733 snprintf(card->longname, sizeof(card->longname),
6734 "%s at EC reg 0x%02x, fw %s", card->shortname, TP_EC_AUDIO,
6735 (thinkpad_id.ec_version_str) ?
6736 thinkpad_id.ec_version_str : "unknown");
6737
6738 if (volume_control_allowed) {
6739 volume_alsa_control_vol.put = volume_alsa_vol_put;
6740 volume_alsa_control_vol.access =
6741 SNDRV_CTL_ELEM_ACCESS_READWRITE;
6742
6743 volume_alsa_control_mute.put = volume_alsa_mute_put;
6744 volume_alsa_control_mute.access =
6745 SNDRV_CTL_ELEM_ACCESS_READWRITE;
6746 }
6747
6748 if (!tp_features.mixer_no_level_control) {
6749 ctl_vol = snd_ctl_new1(&volume_alsa_control_vol, NULL);
6750 rc = snd_ctl_add(card, ctl_vol);
6751 if (rc < 0) {
6752 printk(TPACPI_ERR
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02006753 "Failed to create ALSA volume control: %d\n",
6754 rc);
6755 goto err_exit;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006756 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006757 data->ctl_vol_id = &ctl_vol->id;
6758 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006759
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006760 ctl_mute = snd_ctl_new1(&volume_alsa_control_mute, NULL);
6761 rc = snd_ctl_add(card, ctl_mute);
6762 if (rc < 0) {
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02006763 printk(TPACPI_ERR "Failed to create ALSA mute control: %d\n",
6764 rc);
6765 goto err_exit;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006766 }
6767 data->ctl_mute_id = &ctl_mute->id;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006768
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006769 snd_card_set_dev(card, &tpacpi_pdev->dev);
6770 rc = snd_card_register(card);
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006771 if (rc < 0) {
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02006772 printk(TPACPI_ERR "Failed to register ALSA card: %d\n", rc);
6773 goto err_exit;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006774 }
6775
6776 alsa_card = card;
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02006777 return 0;
6778
6779err_exit:
6780 snd_card_free(card);
6781 return 1;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006782}
6783
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006784#define TPACPI_VOL_Q_MUTEONLY 0x0001 /* Mute-only control available */
6785#define TPACPI_VOL_Q_LEVEL 0x0002 /* Volume control available */
6786
6787static const struct tpacpi_quirk volume_quirk_table[] __initconst = {
6788 /* Whitelist volume level on all IBM by default */
6789 { .vendor = PCI_VENDOR_ID_IBM,
6790 .bios = TPACPI_MATCH_ANY,
6791 .ec = TPACPI_MATCH_ANY,
6792 .quirks = TPACPI_VOL_Q_LEVEL },
6793
6794 /* Lenovo models with volume control (needs confirmation) */
6795 TPACPI_QEC_LNV('7', 'C', TPACPI_VOL_Q_LEVEL), /* R60/i */
6796 TPACPI_QEC_LNV('7', 'E', TPACPI_VOL_Q_LEVEL), /* R60e/i */
6797 TPACPI_QEC_LNV('7', '9', TPACPI_VOL_Q_LEVEL), /* T60/p */
6798 TPACPI_QEC_LNV('7', 'B', TPACPI_VOL_Q_LEVEL), /* X60/s */
6799 TPACPI_QEC_LNV('7', 'J', TPACPI_VOL_Q_LEVEL), /* X60t */
6800 TPACPI_QEC_LNV('7', '7', TPACPI_VOL_Q_LEVEL), /* Z60 */
6801 TPACPI_QEC_LNV('7', 'F', TPACPI_VOL_Q_LEVEL), /* Z61 */
6802
6803 /* Whitelist mute-only on all Lenovo by default */
6804 { .vendor = PCI_VENDOR_ID_LENOVO,
6805 .bios = TPACPI_MATCH_ANY,
6806 .ec = TPACPI_MATCH_ANY,
6807 .quirks = TPACPI_VOL_Q_MUTEONLY }
6808};
6809
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006810static int __init volume_init(struct ibm_init_struct *iibm)
6811{
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006812 unsigned long quirks;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006813 int rc;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006814
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006815 vdbg_printk(TPACPI_DBG_INIT, "initializing volume subdriver\n");
6816
6817 mutex_init(&volume_mutex);
6818
6819 /*
6820 * Check for module parameter bogosity, note that we
6821 * init volume_mode to TPACPI_VOL_MODE_MAX in order to be
6822 * able to detect "unspecified"
6823 */
6824 if (volume_mode > TPACPI_VOL_MODE_MAX)
6825 return -EINVAL;
6826
6827 if (volume_mode == TPACPI_VOL_MODE_UCMS_STEP) {
6828 printk(TPACPI_ERR
6829 "UCMS step volume mode not implemented, "
6830 "please contact %s\n", TPACPI_MAIL);
6831 return 1;
6832 }
6833
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006834 if (volume_capabilities >= TPACPI_VOL_CAP_MAX)
6835 return -EINVAL;
6836
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006837 /*
6838 * The ALSA mixer is our primary interface.
6839 * When disabled, don't install the subdriver at all
6840 */
6841 if (!alsa_enable) {
6842 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6843 "ALSA mixer disabled by parameter, "
6844 "not loading volume subdriver...\n");
6845 return 1;
6846 }
6847
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006848 quirks = tpacpi_check_quirks(volume_quirk_table,
6849 ARRAY_SIZE(volume_quirk_table));
6850
6851 switch (volume_capabilities) {
6852 case TPACPI_VOL_CAP_AUTO:
6853 if (quirks & TPACPI_VOL_Q_MUTEONLY)
6854 tp_features.mixer_no_level_control = 1;
6855 else if (quirks & TPACPI_VOL_Q_LEVEL)
6856 tp_features.mixer_no_level_control = 0;
6857 else
6858 return 1; /* no mixer */
6859 break;
6860 case TPACPI_VOL_CAP_VOLMUTE:
6861 tp_features.mixer_no_level_control = 0;
6862 break;
6863 case TPACPI_VOL_CAP_MUTEONLY:
6864 tp_features.mixer_no_level_control = 1;
6865 break;
6866 default:
6867 return 1;
6868 }
6869
6870 if (volume_capabilities != TPACPI_VOL_CAP_AUTO)
6871 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6872 "using user-supplied volume_capabilities=%d\n",
6873 volume_capabilities);
6874
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006875 if (volume_mode == TPACPI_VOL_MODE_AUTO ||
6876 volume_mode == TPACPI_VOL_MODE_MAX) {
6877 volume_mode = TPACPI_VOL_MODE_ECNVRAM;
6878
6879 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6880 "driver auto-selected volume_mode=%d\n",
6881 volume_mode);
6882 } else {
6883 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6884 "using user-supplied volume_mode=%d\n",
6885 volume_mode);
6886 }
6887
6888 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006889 "mute is supported, volume control is %s\n",
6890 str_supported(!tp_features.mixer_no_level_control));
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006891
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006892 rc = volume_create_alsa_mixer();
6893 if (rc) {
6894 printk(TPACPI_ERR
6895 "Could not create the ALSA mixer interface\n");
6896 return rc;
6897 }
6898
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006899 printk(TPACPI_INFO
6900 "Console audio control enabled, mode: %s\n",
6901 (volume_control_allowed) ?
6902 "override (read/write)" :
6903 "monitor (read only)");
6904
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006905 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6906 "registering volume hotkeys as change notification\n");
6907 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6908 | TP_ACPI_HKEY_VOLUP_MASK
6909 | TP_ACPI_HKEY_VOLDWN_MASK
6910 | TP_ACPI_HKEY_MUTE_MASK);
6911
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006912 return 0;
6913}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006914
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006915static int volume_read(struct seq_file *m)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006916{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006917 u8 status;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006918
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006919 if (volume_get_status(&status) < 0) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006920 seq_printf(m, "level:\t\tunreadable\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006921 } else {
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006922 if (tp_features.mixer_no_level_control)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006923 seq_printf(m, "level:\t\tunsupported\n");
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006924 else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006925 seq_printf(m, "level:\t\t%d\n",
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006926 status & TP_EC_AUDIO_LVL_MSK);
6927
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006928 seq_printf(m, "mute:\t\t%s\n",
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006929 onoff(status, TP_EC_AUDIO_MUTESW));
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006930
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006931 if (volume_control_allowed) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006932 seq_printf(m, "commands:\tunmute, mute\n");
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006933 if (!tp_features.mixer_no_level_control) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006934 seq_printf(m,
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006935 "commands:\tup, down\n");
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006936 seq_printf(m,
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006937 "commands:\tlevel <level>"
6938 " (<level> is 0-%d)\n",
6939 TP_EC_VOLUME_MAX);
6940 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006941 }
6942 }
6943
6944 return 0;
6945}
6946
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006947static int volume_write(char *buf)
6948{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006949 u8 s;
6950 u8 new_level, new_mute;
6951 int l;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006952 char *cmd;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006953 int rc;
6954
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006955 /*
6956 * We do allow volume control at driver startup, so that the
6957 * user can set initial state through the volume=... parameter hack.
6958 */
6959 if (!volume_control_allowed && tpacpi_lifecycle != TPACPI_LIFE_INIT) {
6960 if (unlikely(!tp_warned.volume_ctrl_forbidden)) {
6961 tp_warned.volume_ctrl_forbidden = 1;
6962 printk(TPACPI_NOTICE
6963 "Console audio control in monitor mode, "
6964 "changes are not allowed.\n");
6965 printk(TPACPI_NOTICE
6966 "Use the volume_control=1 module parameter "
6967 "to enable volume control\n");
6968 }
6969 return -EPERM;
6970 }
6971
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006972 rc = volume_get_status(&s);
6973 if (rc < 0)
6974 return rc;
6975
6976 new_level = s & TP_EC_AUDIO_LVL_MSK;
6977 new_mute = s & TP_EC_AUDIO_MUTESW_MSK;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006978
6979 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006980 if (!tp_features.mixer_no_level_control) {
6981 if (strlencmp(cmd, "up") == 0) {
6982 if (new_mute)
6983 new_mute = 0;
6984 else if (new_level < TP_EC_VOLUME_MAX)
6985 new_level++;
6986 continue;
6987 } else if (strlencmp(cmd, "down") == 0) {
6988 if (new_mute)
6989 new_mute = 0;
6990 else if (new_level > 0)
6991 new_level--;
6992 continue;
6993 } else if (sscanf(cmd, "level %u", &l) == 1 &&
6994 l >= 0 && l <= TP_EC_VOLUME_MAX) {
6995 new_level = l;
6996 continue;
6997 }
6998 }
6999 if (strlencmp(cmd, "mute") == 0)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007000 new_mute = TP_EC_AUDIO_MUTESW_MSK;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007001 else if (strlencmp(cmd, "unmute") == 0)
7002 new_mute = 0;
7003 else
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007004 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007005 }
7006
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007007 if (tp_features.mixer_no_level_control) {
7008 tpacpi_disclose_usertask("procfs volume", "%smute\n",
7009 new_mute ? "" : "un");
7010 rc = volume_set_mute(!!new_mute);
7011 } else {
7012 tpacpi_disclose_usertask("procfs volume",
7013 "%smute and set level to %d\n",
7014 new_mute ? "" : "un", new_level);
7015 rc = volume_set_status(new_mute | new_level);
7016 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007017 volume_alsa_notify_change();
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007018
7019 return (rc == -EINTR) ? -ERESTARTSYS : rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007020}
7021
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007022static struct ibm_struct volume_driver_data = {
7023 .name = "volume",
7024 .read = volume_read,
7025 .write = volume_write,
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007026 .exit = volume_exit,
7027 .suspend = volume_suspend,
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007028 .resume = volume_resume,
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007029 .shutdown = volume_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007030};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007031
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02007032#else /* !CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7033
7034#define alsa_card NULL
7035
7036static void inline volume_alsa_notify_change(void)
7037{
7038}
7039
7040static int __init volume_init(struct ibm_init_struct *iibm)
7041{
7042 printk(TPACPI_INFO
7043 "volume: disabled as there is no ALSA support in this kernel\n");
7044
7045 return 1;
7046}
7047
7048static struct ibm_struct volume_driver_data = {
7049 .name = "volume",
7050};
7051
7052#endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7053
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007054/*************************************************************************
7055 * Fan subdriver
7056 */
7057
7058/*
7059 * FAN ACCESS MODES
7060 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007061 * TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007062 * ACPI GFAN method: returns fan level
7063 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007064 * see TPACPI_FAN_WR_ACPI_SFAN
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007065 * EC 0x2f (HFSP) not available if GFAN exists
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007066 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007067 * TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007068 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
7069 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007070 * EC 0x2f (HFSP) might be available *for reading*, but do not use
7071 * it for writing.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007072 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007073 * TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007074 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
7075 * Supported on almost all ThinkPads
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007076 *
7077 * Fan speed changes of any sort (including those caused by the
7078 * disengaged mode) are usually done slowly by the firmware as the
7079 * maximum ammount of fan duty cycle change per second seems to be
7080 * limited.
7081 *
7082 * Reading is not available if GFAN exists.
7083 * Writing is not available if SFAN exists.
7084 *
7085 * Bits
7086 * 7 automatic mode engaged;
7087 * (default operation mode of the ThinkPad)
7088 * fan level is ignored in this mode.
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007089 * 6 full speed mode (takes precedence over bit 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007090 * not available on all thinkpads. May disable
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007091 * the tachometer while the fan controller ramps up
7092 * the speed (which can take up to a few *minutes*).
7093 * Speeds up fan to 100% duty-cycle, which is far above
7094 * the standard RPM levels. It is not impossible that
7095 * it could cause hardware damage.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007096 * 5-3 unused in some models. Extra bits for fan level
7097 * in others, but still useless as all values above
7098 * 7 map to the same speed as level 7 in these models.
7099 * 2-0 fan level (0..7 usually)
7100 * 0x00 = stop
7101 * 0x07 = max (set when temperatures critical)
7102 * Some ThinkPads may have other levels, see
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007103 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007104 *
7105 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
7106 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
7107 * does so, its initial value is meaningless (0x07).
7108 *
7109 * For firmware bugs, refer to:
7110 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7111 *
7112 * ----
7113 *
7114 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
7115 * Main fan tachometer reading (in RPM)
7116 *
7117 * This register is present on all ThinkPads with a new-style EC, and
7118 * it is known not to be present on the A21m/e, and T22, as there is
7119 * something else in offset 0x84 according to the ACPI DSDT. Other
7120 * ThinkPads from this same time period (and earlier) probably lack the
7121 * tachometer as well.
7122 *
Nick Andrew877d0312009-01-26 11:06:57 +01007123 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007124 * was never fixed by IBM to report the EC firmware version string
7125 * probably support the tachometer (like the early X models), so
7126 * detecting it is quite hard. We need more data to know for sure.
7127 *
7128 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
7129 * might result.
7130 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007131 * FIRMWARE BUG: may go stale while the EC is switching to full speed
7132 * mode.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007133 *
7134 * For firmware bugs, refer to:
7135 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7136 *
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007137 * ----
7138 *
7139 * ThinkPad EC register 0x31 bit 0 (only on select models)
7140 *
7141 * When bit 0 of EC register 0x31 is zero, the tachometer registers
7142 * show the speed of the main fan. When bit 0 of EC register 0x31
7143 * is one, the tachometer registers show the speed of the auxiliary
7144 * fan.
7145 *
7146 * Fan control seems to affect both fans, regardless of the state
7147 * of this bit.
7148 *
7149 * So far, only the firmware for the X60/X61 non-tablet versions
7150 * seem to support this (firmware TP-7M).
7151 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007152 * TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007153 * ThinkPad X31, X40, X41. Not available in the X60.
7154 *
7155 * FANS ACPI handle: takes three arguments: low speed, medium speed,
7156 * high speed. ACPI DSDT seems to map these three speeds to levels
7157 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
7158 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
7159 *
7160 * The speeds are stored on handles
7161 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
7162 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007163 * There are three default speed sets, accessible as handles:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007164 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
7165 *
7166 * ACPI DSDT switches which set is in use depending on various
7167 * factors.
7168 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007169 * TPACPI_FAN_WR_TPEC is also available and should be used to
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007170 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
7171 * but the ACPI tables just mention level 7.
7172 */
7173
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007174enum { /* Fan control constants */
7175 fan_status_offset = 0x2f, /* EC register 0x2f */
7176 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
7177 * 0x84 must be read before 0x85 */
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007178 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
7179 bit 0 selects which fan is active */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007180
7181 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
7182 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
7183
7184 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
7185};
7186
7187enum fan_status_access_mode {
7188 TPACPI_FAN_NONE = 0, /* No fan status or control */
7189 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
7190 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
7191};
7192
7193enum fan_control_access_mode {
7194 TPACPI_FAN_WR_NONE = 0, /* No fan control */
7195 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
7196 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
7197 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
7198};
7199
7200enum fan_control_commands {
7201 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
7202 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
7203 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
7204 * and also watchdog cmd */
7205};
7206
7207static int fan_control_allowed;
7208
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007209static enum fan_status_access_mode fan_status_access_mode;
7210static enum fan_control_access_mode fan_control_access_mode;
7211static enum fan_control_commands fan_control_commands;
7212
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02007213static u8 fan_control_initial_status;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007214static u8 fan_control_desired_level;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007215static u8 fan_control_resume_level;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007216static int fan_watchdog_maxinterval;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007217
7218static struct mutex fan_mutex;
7219
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007220static void fan_watchdog_fire(struct work_struct *ignored);
Len Brown25c68a32006-12-08 04:43:41 -05007221static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007222
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007223TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
7224TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007225 "\\FSPD", /* 600e/x, 770e, 770x */
7226 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007227TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007228 "JFNS", /* 770x-JL */
7229 ); /* all others */
7230
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007231/*
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007232 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
7233 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
7234 * be in auto mode (0x80).
7235 *
7236 * This is corrected by any write to HFSP either by the driver, or
7237 * by the firmware.
7238 *
7239 * We assume 0x07 really means auto mode while this quirk is active,
7240 * as this is far more likely than the ThinkPad being in level 7,
7241 * which is only used by the firmware during thermal emergencies.
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007242 *
7243 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
7244 * TP-70 (T43, R52), which are known to be buggy.
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007245 */
7246
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007247static void fan_quirk1_setup(void)
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007248{
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007249 if (fan_control_initial_status == 0x07) {
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007250 printk(TPACPI_NOTICE
7251 "fan_init: initial fan status is unknown, "
7252 "assuming it is in auto mode\n");
7253 tp_features.fan_ctrl_status_undef = 1;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007254 }
7255}
7256
7257static void fan_quirk1_handle(u8 *fan_status)
7258{
7259 if (unlikely(tp_features.fan_ctrl_status_undef)) {
7260 if (*fan_status != fan_control_initial_status) {
7261 /* something changed the HFSP regisnter since
7262 * driver init time, so it is not undefined
7263 * anymore */
7264 tp_features.fan_ctrl_status_undef = 0;
7265 } else {
7266 /* Return most likely status. In fact, it
7267 * might be the only possible status */
7268 *fan_status = TP_EC_FAN_AUTO;
7269 }
7270 }
7271}
7272
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007273/* Select main fan on X60/X61, NOOP on others */
7274static bool fan_select_fan1(void)
7275{
7276 if (tp_features.second_fan) {
7277 u8 val;
7278
7279 if (ec_read(fan_select_offset, &val) < 0)
7280 return false;
7281 val &= 0xFEU;
7282 if (ec_write(fan_select_offset, val) < 0)
7283 return false;
7284 }
7285 return true;
7286}
7287
7288/* Select secondary fan on X60/X61 */
7289static bool fan_select_fan2(void)
7290{
7291 u8 val;
7292
7293 if (!tp_features.second_fan)
7294 return false;
7295
7296 if (ec_read(fan_select_offset, &val) < 0)
7297 return false;
7298 val |= 0x01U;
7299 if (ec_write(fan_select_offset, val) < 0)
7300 return false;
7301
7302 return true;
7303}
7304
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007305/*
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007306 * Call with fan_mutex held
7307 */
7308static void fan_update_desired_level(u8 status)
7309{
7310 if ((status &
7311 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7312 if (status > 7)
7313 fan_control_desired_level = 7;
7314 else
7315 fan_control_desired_level = status;
7316 }
7317}
7318
7319static int fan_get_status(u8 *status)
7320{
7321 u8 s;
7322
7323 /* TODO:
7324 * Add TPACPI_FAN_RD_ACPI_FANS ? */
7325
7326 switch (fan_status_access_mode) {
7327 case TPACPI_FAN_RD_ACPI_GFAN:
7328 /* 570, 600e/x, 770e, 770x */
7329
7330 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
7331 return -EIO;
7332
7333 if (likely(status))
7334 *status = s & 0x07;
7335
7336 break;
7337
7338 case TPACPI_FAN_RD_TPEC:
7339 /* all except 570, 600e/x, 770e, 770x */
7340 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
7341 return -EIO;
7342
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007343 if (likely(status)) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007344 *status = s;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007345 fan_quirk1_handle(status);
7346 }
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007347
7348 break;
7349
7350 default:
7351 return -ENXIO;
7352 }
7353
7354 return 0;
7355}
7356
7357static int fan_get_status_safe(u8 *status)
7358{
7359 int rc;
7360 u8 s;
7361
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007362 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007363 return -ERESTARTSYS;
7364 rc = fan_get_status(&s);
7365 if (!rc)
7366 fan_update_desired_level(s);
7367 mutex_unlock(&fan_mutex);
7368
7369 if (status)
7370 *status = s;
7371
7372 return rc;
7373}
7374
7375static int fan_get_speed(unsigned int *speed)
7376{
7377 u8 hi, lo;
7378
7379 switch (fan_status_access_mode) {
7380 case TPACPI_FAN_RD_TPEC:
7381 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007382 if (unlikely(!fan_select_fan1()))
7383 return -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007384 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
7385 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
7386 return -EIO;
7387
7388 if (likely(speed))
7389 *speed = (hi << 8) | lo;
7390
7391 break;
7392
7393 default:
7394 return -ENXIO;
7395 }
7396
7397 return 0;
7398}
7399
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007400static int fan2_get_speed(unsigned int *speed)
7401{
7402 u8 hi, lo;
7403 bool rc;
7404
7405 switch (fan_status_access_mode) {
7406 case TPACPI_FAN_RD_TPEC:
7407 /* all except 570, 600e/x, 770e, 770x */
7408 if (unlikely(!fan_select_fan2()))
7409 return -EIO;
7410 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
7411 !acpi_ec_read(fan_rpm_offset + 1, &hi);
7412 fan_select_fan1(); /* play it safe */
7413 if (rc)
7414 return -EIO;
7415
7416 if (likely(speed))
7417 *speed = (hi << 8) | lo;
7418
7419 break;
7420
7421 default:
7422 return -ENXIO;
7423 }
7424
7425 return 0;
7426}
7427
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007428static int fan_set_level(int level)
7429{
7430 if (!fan_control_allowed)
7431 return -EPERM;
7432
7433 switch (fan_control_access_mode) {
7434 case TPACPI_FAN_WR_ACPI_SFAN:
7435 if (level >= 0 && level <= 7) {
7436 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
7437 return -EIO;
7438 } else
7439 return -EINVAL;
7440 break;
7441
7442 case TPACPI_FAN_WR_ACPI_FANS:
7443 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007444 if (!(level & TP_EC_FAN_AUTO) &&
7445 !(level & TP_EC_FAN_FULLSPEED) &&
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007446 ((level < 0) || (level > 7)))
7447 return -EINVAL;
7448
7449 /* safety net should the EC not support AUTO
7450 * or FULLSPEED mode bits and just ignore them */
7451 if (level & TP_EC_FAN_FULLSPEED)
7452 level |= 7; /* safety min speed 7 */
Roel Kluin547266e2008-02-05 00:24:56 +01007453 else if (level & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007454 level |= 4; /* safety min speed 4 */
7455
7456 if (!acpi_ec_write(fan_status_offset, level))
7457 return -EIO;
7458 else
7459 tp_features.fan_ctrl_status_undef = 0;
7460 break;
7461
7462 default:
7463 return -ENXIO;
7464 }
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007465
7466 vdbg_printk(TPACPI_DBG_FAN,
7467 "fan control: set fan control register to 0x%02x\n", level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007468 return 0;
7469}
7470
7471static int fan_set_level_safe(int level)
7472{
7473 int rc;
7474
7475 if (!fan_control_allowed)
7476 return -EPERM;
7477
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007478 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007479 return -ERESTARTSYS;
7480
7481 if (level == TPACPI_FAN_LAST_LEVEL)
7482 level = fan_control_desired_level;
7483
7484 rc = fan_set_level(level);
7485 if (!rc)
7486 fan_update_desired_level(level);
7487
7488 mutex_unlock(&fan_mutex);
7489 return rc;
7490}
7491
7492static int fan_set_enable(void)
7493{
7494 u8 s;
7495 int rc;
7496
7497 if (!fan_control_allowed)
7498 return -EPERM;
7499
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007500 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007501 return -ERESTARTSYS;
7502
7503 switch (fan_control_access_mode) {
7504 case TPACPI_FAN_WR_ACPI_FANS:
7505 case TPACPI_FAN_WR_TPEC:
7506 rc = fan_get_status(&s);
7507 if (rc < 0)
7508 break;
7509
7510 /* Don't go out of emergency fan mode */
7511 if (s != 7) {
7512 s &= 0x07;
7513 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
7514 }
7515
7516 if (!acpi_ec_write(fan_status_offset, s))
7517 rc = -EIO;
7518 else {
7519 tp_features.fan_ctrl_status_undef = 0;
7520 rc = 0;
7521 }
7522 break;
7523
7524 case TPACPI_FAN_WR_ACPI_SFAN:
7525 rc = fan_get_status(&s);
7526 if (rc < 0)
7527 break;
7528
7529 s &= 0x07;
7530
7531 /* Set fan to at least level 4 */
7532 s |= 4;
7533
7534 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007535 rc = -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007536 else
7537 rc = 0;
7538 break;
7539
7540 default:
7541 rc = -ENXIO;
7542 }
7543
7544 mutex_unlock(&fan_mutex);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007545
7546 if (!rc)
7547 vdbg_printk(TPACPI_DBG_FAN,
7548 "fan control: set fan control register to 0x%02x\n",
7549 s);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007550 return rc;
7551}
7552
7553static int fan_set_disable(void)
7554{
7555 int rc;
7556
7557 if (!fan_control_allowed)
7558 return -EPERM;
7559
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007560 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007561 return -ERESTARTSYS;
7562
7563 rc = 0;
7564 switch (fan_control_access_mode) {
7565 case TPACPI_FAN_WR_ACPI_FANS:
7566 case TPACPI_FAN_WR_TPEC:
7567 if (!acpi_ec_write(fan_status_offset, 0x00))
7568 rc = -EIO;
7569 else {
7570 fan_control_desired_level = 0;
7571 tp_features.fan_ctrl_status_undef = 0;
7572 }
7573 break;
7574
7575 case TPACPI_FAN_WR_ACPI_SFAN:
7576 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
7577 rc = -EIO;
7578 else
7579 fan_control_desired_level = 0;
7580 break;
7581
7582 default:
7583 rc = -ENXIO;
7584 }
7585
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007586 if (!rc)
7587 vdbg_printk(TPACPI_DBG_FAN,
7588 "fan control: set fan control register to 0\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007589
7590 mutex_unlock(&fan_mutex);
7591 return rc;
7592}
7593
7594static int fan_set_speed(int speed)
7595{
7596 int rc;
7597
7598 if (!fan_control_allowed)
7599 return -EPERM;
7600
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007601 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007602 return -ERESTARTSYS;
7603
7604 rc = 0;
7605 switch (fan_control_access_mode) {
7606 case TPACPI_FAN_WR_ACPI_FANS:
7607 if (speed >= 0 && speed <= 65535) {
7608 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
7609 speed, speed, speed))
7610 rc = -EIO;
7611 } else
7612 rc = -EINVAL;
7613 break;
7614
7615 default:
7616 rc = -ENXIO;
7617 }
7618
7619 mutex_unlock(&fan_mutex);
7620 return rc;
7621}
7622
7623static void fan_watchdog_reset(void)
7624{
7625 static int fan_watchdog_active;
7626
7627 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
7628 return;
7629
7630 if (fan_watchdog_active)
7631 cancel_delayed_work(&fan_watchdog_task);
7632
7633 if (fan_watchdog_maxinterval > 0 &&
7634 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
7635 fan_watchdog_active = 1;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03007636 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007637 msecs_to_jiffies(fan_watchdog_maxinterval
7638 * 1000))) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007639 printk(TPACPI_ERR
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03007640 "failed to queue the fan watchdog, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007641 "watchdog will not trigger\n");
7642 }
7643 } else
7644 fan_watchdog_active = 0;
7645}
7646
7647static void fan_watchdog_fire(struct work_struct *ignored)
7648{
7649 int rc;
7650
7651 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
7652 return;
7653
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007654 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007655 rc = fan_set_enable();
7656 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007657 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007658 "will try again later...\n", -rc);
7659 /* reschedule for later */
7660 fan_watchdog_reset();
7661 }
7662}
7663
7664/*
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007665 * SYSFS fan layout: hwmon compatible (device)
7666 *
7667 * pwm*_enable:
7668 * 0: "disengaged" mode
7669 * 1: manual mode
7670 * 2: native EC "auto" mode (recommended, hardware default)
7671 *
7672 * pwm*: set speed in manual mode, ignored otherwise.
7673 * 0 is level 0; 255 is level 7. Intermediate points done with linear
7674 * interpolation.
7675 *
7676 * fan*_input: tachometer reading, RPM
7677 *
7678 *
7679 * SYSFS fan layout: extensions
7680 *
7681 * fan_watchdog (driver):
7682 * fan watchdog interval in seconds, 0 disables (default), max 120
7683 */
7684
7685/* sysfs fan pwm1_enable ----------------------------------------------- */
7686static ssize_t fan_pwm1_enable_show(struct device *dev,
7687 struct device_attribute *attr,
7688 char *buf)
7689{
7690 int res, mode;
7691 u8 status;
7692
7693 res = fan_get_status_safe(&status);
7694 if (res)
7695 return res;
7696
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007697 if (status & TP_EC_FAN_FULLSPEED) {
7698 mode = 0;
7699 } else if (status & TP_EC_FAN_AUTO) {
7700 mode = 2;
7701 } else
7702 mode = 1;
7703
7704 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
7705}
7706
7707static ssize_t fan_pwm1_enable_store(struct device *dev,
7708 struct device_attribute *attr,
7709 const char *buf, size_t count)
7710{
7711 unsigned long t;
7712 int res, level;
7713
7714 if (parse_strtoul(buf, 2, &t))
7715 return -EINVAL;
7716
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007717 tpacpi_disclose_usertask("hwmon pwm1_enable",
7718 "set fan mode to %lu\n", t);
7719
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007720 switch (t) {
7721 case 0:
7722 level = TP_EC_FAN_FULLSPEED;
7723 break;
7724 case 1:
7725 level = TPACPI_FAN_LAST_LEVEL;
7726 break;
7727 case 2:
7728 level = TP_EC_FAN_AUTO;
7729 break;
7730 case 3:
7731 /* reserved for software-controlled auto mode */
7732 return -ENOSYS;
7733 default:
7734 return -EINVAL;
7735 }
7736
7737 res = fan_set_level_safe(level);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03007738 if (res == -ENXIO)
7739 return -EINVAL;
7740 else if (res < 0)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007741 return res;
7742
7743 fan_watchdog_reset();
7744
7745 return count;
7746}
7747
7748static struct device_attribute dev_attr_fan_pwm1_enable =
7749 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
7750 fan_pwm1_enable_show, fan_pwm1_enable_store);
7751
7752/* sysfs fan pwm1 ------------------------------------------------------ */
7753static ssize_t fan_pwm1_show(struct device *dev,
7754 struct device_attribute *attr,
7755 char *buf)
7756{
7757 int res;
7758 u8 status;
7759
7760 res = fan_get_status_safe(&status);
7761 if (res)
7762 return res;
7763
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007764 if ((status &
7765 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
7766 status = fan_control_desired_level;
7767
7768 if (status > 7)
7769 status = 7;
7770
7771 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
7772}
7773
7774static ssize_t fan_pwm1_store(struct device *dev,
7775 struct device_attribute *attr,
7776 const char *buf, size_t count)
7777{
7778 unsigned long s;
7779 int rc;
7780 u8 status, newlevel;
7781
7782 if (parse_strtoul(buf, 255, &s))
7783 return -EINVAL;
7784
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007785 tpacpi_disclose_usertask("hwmon pwm1",
7786 "set fan speed to %lu\n", s);
7787
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007788 /* scale down from 0-255 to 0-7 */
7789 newlevel = (s >> 5) & 0x07;
7790
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007791 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02007792 return -ERESTARTSYS;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007793
7794 rc = fan_get_status(&status);
7795 if (!rc && (status &
7796 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7797 rc = fan_set_level(newlevel);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03007798 if (rc == -ENXIO)
7799 rc = -EINVAL;
7800 else if (!rc) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007801 fan_update_desired_level(newlevel);
Henrique de Moraes Holschuhca4ac2f2007-04-27 22:00:11 -03007802 fan_watchdog_reset();
7803 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007804 }
7805
7806 mutex_unlock(&fan_mutex);
7807 return (rc)? rc : count;
7808}
7809
7810static struct device_attribute dev_attr_fan_pwm1 =
7811 __ATTR(pwm1, S_IWUSR | S_IRUGO,
7812 fan_pwm1_show, fan_pwm1_store);
7813
7814/* sysfs fan fan1_input ------------------------------------------------ */
7815static ssize_t fan_fan1_input_show(struct device *dev,
7816 struct device_attribute *attr,
7817 char *buf)
7818{
7819 int res;
7820 unsigned int speed;
7821
7822 res = fan_get_speed(&speed);
7823 if (res < 0)
7824 return res;
7825
7826 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7827}
7828
7829static struct device_attribute dev_attr_fan_fan1_input =
7830 __ATTR(fan1_input, S_IRUGO,
7831 fan_fan1_input_show, NULL);
7832
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007833/* sysfs fan fan2_input ------------------------------------------------ */
7834static ssize_t fan_fan2_input_show(struct device *dev,
7835 struct device_attribute *attr,
7836 char *buf)
7837{
7838 int res;
7839 unsigned int speed;
7840
7841 res = fan2_get_speed(&speed);
7842 if (res < 0)
7843 return res;
7844
7845 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7846}
7847
7848static struct device_attribute dev_attr_fan_fan2_input =
7849 __ATTR(fan2_input, S_IRUGO,
7850 fan_fan2_input_show, NULL);
7851
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007852/* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007853static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
7854 char *buf)
7855{
7856 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
7857}
7858
7859static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
7860 const char *buf, size_t count)
7861{
7862 unsigned long t;
7863
7864 if (parse_strtoul(buf, 120, &t))
7865 return -EINVAL;
7866
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03007867 if (!fan_control_allowed)
7868 return -EPERM;
7869
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007870 fan_watchdog_maxinterval = t;
7871 fan_watchdog_reset();
7872
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007873 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
7874
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007875 return count;
7876}
7877
7878static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
7879 fan_fan_watchdog_show, fan_fan_watchdog_store);
7880
7881/* --------------------------------------------------------------------- */
7882static struct attribute *fan_attributes[] = {
7883 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
7884 &dev_attr_fan_fan1_input.attr,
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007885 NULL, /* for fan2_input */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007886 NULL
7887};
7888
7889static const struct attribute_group fan_attr_group = {
7890 .attrs = fan_attributes,
7891};
7892
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007893#define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
7894#define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007895
7896#define TPACPI_FAN_QI(__id1, __id2, __quirks) \
7897 { .vendor = PCI_VENDOR_ID_IBM, \
7898 .bios = TPACPI_MATCH_ANY, \
7899 .ec = TPID(__id1, __id2), \
7900 .quirks = __quirks }
7901
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007902#define TPACPI_FAN_QL(__id1, __id2, __quirks) \
7903 { .vendor = PCI_VENDOR_ID_LENOVO, \
7904 .bios = TPACPI_MATCH_ANY, \
7905 .ec = TPID(__id1, __id2), \
7906 .quirks = __quirks }
7907
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007908static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
7909 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
7910 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
7911 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
7912 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007913 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007914};
7915
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007916#undef TPACPI_FAN_QL
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007917#undef TPACPI_FAN_QI
7918
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007919static int __init fan_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007920{
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007921 int rc;
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007922 unsigned long quirks;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007923
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007924 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7925 "initializing fan subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007926
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03007927 mutex_init(&fan_mutex);
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007928 fan_status_access_mode = TPACPI_FAN_NONE;
7929 fan_control_access_mode = TPACPI_FAN_WR_NONE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007930 fan_control_commands = 0;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007931 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03007932 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007933 tp_features.second_fan = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007934 fan_control_desired_level = 7;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007935
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007936 TPACPI_ACPIHANDLE_INIT(fans);
7937 TPACPI_ACPIHANDLE_INIT(gfan);
7938 TPACPI_ACPIHANDLE_INIT(sfan);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007939
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007940 quirks = tpacpi_check_quirks(fan_quirk_table,
7941 ARRAY_SIZE(fan_quirk_table));
7942
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007943 if (gfan_handle) {
7944 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007945 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007946 } else {
7947 /* all other ThinkPads: note that even old-style
7948 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02007949 if (likely(acpi_ec_read(fan_status_offset,
7950 &fan_control_initial_status))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007951 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007952 if (quirks & TPACPI_FAN_Q1)
7953 fan_quirk1_setup();
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007954 if (quirks & TPACPI_FAN_2FAN) {
7955 tp_features.second_fan = 1;
7956 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7957 "secondary fan support enabled\n");
7958 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007959 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007960 printk(TPACPI_ERR
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007961 "ThinkPad ACPI EC access misbehaving, "
7962 "fan status and control unavailable\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007963 return 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007964 }
7965 }
7966
7967 if (sfan_handle) {
7968 /* 570, 770x-JL */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007969 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02007970 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007971 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007972 } else {
7973 if (!gfan_handle) {
7974 /* gfan without sfan means no fan control */
7975 /* all other models implement TP EC 0x2f control */
7976
7977 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02007978 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007979 fan_control_access_mode =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007980 TPACPI_FAN_WR_ACPI_FANS;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007981 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007982 TPACPI_FAN_CMD_SPEED |
7983 TPACPI_FAN_CMD_LEVEL |
7984 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007985 } else {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007986 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02007987 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007988 TPACPI_FAN_CMD_LEVEL |
7989 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007990 }
7991 }
7992 }
7993
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007994 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7995 "fan is %s, modes %d, %d\n",
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007996 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
7997 fan_control_access_mode != TPACPI_FAN_WR_NONE),
7998 fan_status_access_mode, fan_control_access_mode);
7999
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008000 /* fan control master switch */
8001 if (!fan_control_allowed) {
8002 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8003 fan_control_commands = 0;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008004 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008005 "fan control features disabled by parameter\n");
8006 }
8007
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008008 /* update fan_control_desired_level */
8009 if (fan_status_access_mode != TPACPI_FAN_NONE)
8010 fan_get_status_safe(NULL);
8011
8012 if (fan_status_access_mode != TPACPI_FAN_NONE ||
8013 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008014 if (tp_features.second_fan) {
8015 /* attach second fan tachometer */
8016 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
8017 &dev_attr_fan_fan2_input.attr;
8018 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008019 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008020 &fan_attr_group);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008021 if (rc < 0)
8022 return rc;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03008023
8024 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
8025 &driver_attr_fan_watchdog);
8026 if (rc < 0) {
8027 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
8028 &fan_attr_group);
8029 return rc;
8030 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008031 return 0;
8032 } else
8033 return 1;
8034}
8035
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008036static void fan_exit(void)
8037{
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008038 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008039 "cancelling any pending fan watchdog tasks\n");
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008040
8041 /* FIXME: can we really do this unconditionally? */
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008042 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008043 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
8044 &driver_attr_fan_watchdog);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008045
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008046 cancel_delayed_work(&fan_watchdog_task);
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03008047 flush_workqueue(tpacpi_wq);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008048}
8049
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008050static void fan_suspend(pm_message_t state)
8051{
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008052 int rc;
8053
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008054 if (!fan_control_allowed)
8055 return;
8056
8057 /* Store fan status in cache */
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008058 fan_control_resume_level = 0;
8059 rc = fan_get_status_safe(&fan_control_resume_level);
8060 if (rc < 0)
8061 printk(TPACPI_NOTICE
8062 "failed to read fan level for later "
8063 "restore during resume: %d\n", rc);
8064
8065 /* if it is undefined, don't attempt to restore it.
8066 * KEEP THIS LAST */
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008067 if (tp_features.fan_ctrl_status_undef)
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008068 fan_control_resume_level = 0;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008069}
8070
8071static void fan_resume(void)
8072{
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008073 u8 current_level = 7;
8074 bool do_set = false;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008075 int rc;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008076
8077 /* DSDT *always* updates status on resume */
8078 tp_features.fan_ctrl_status_undef = 0;
8079
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008080 if (!fan_control_allowed ||
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008081 !fan_control_resume_level ||
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008082 (fan_get_status_safe(&current_level) < 0))
8083 return;
8084
8085 switch (fan_control_access_mode) {
8086 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008087 /* never decrease fan level */
8088 do_set = (fan_control_resume_level > current_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008089 break;
8090 case TPACPI_FAN_WR_ACPI_FANS:
8091 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008092 /* never decrease fan level, scale is:
8093 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
8094 *
8095 * We expect the firmware to set either 7 or AUTO, but we
8096 * handle FULLSPEED out of paranoia.
8097 *
8098 * So, we can safely only restore FULLSPEED or 7, anything
8099 * else could slow the fan. Restoring AUTO is useless, at
8100 * best that's exactly what the DSDT already set (it is the
8101 * slower it uses).
8102 *
8103 * Always keep in mind that the DSDT *will* have set the
8104 * fans to what the vendor supposes is the best level. We
8105 * muck with it only to speed the fan up.
8106 */
8107 if (fan_control_resume_level != 7 &&
8108 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
8109 return;
8110 else
8111 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
8112 (current_level != fan_control_resume_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008113 break;
8114 default:
8115 return;
8116 }
8117 if (do_set) {
8118 printk(TPACPI_NOTICE
8119 "restoring fan level to 0x%02x\n",
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008120 fan_control_resume_level);
8121 rc = fan_set_level_safe(fan_control_resume_level);
8122 if (rc < 0)
8123 printk(TPACPI_NOTICE
8124 "failed to restore fan level: %d\n", rc);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008125 }
8126}
8127
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008128static int fan_read(struct seq_file *m)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008129{
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008130 int rc;
8131 u8 status;
8132 unsigned int speed = 0;
8133
8134 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008135 case TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008136 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008137 rc = fan_get_status_safe(&status);
8138 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008139 return rc;
8140
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008141 seq_printf(m, "status:\t\t%s\n"
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008142 "level:\t\t%d\n",
8143 (status != 0) ? "enabled" : "disabled", status);
8144 break;
8145
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008146 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008147 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008148 rc = fan_get_status_safe(&status);
8149 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008150 return rc;
8151
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008152 seq_printf(m, "status:\t\t%s\n",
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008153 (status != 0) ? "enabled" : "disabled");
8154
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008155 rc = fan_get_speed(&speed);
8156 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008157 return rc;
8158
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008159 seq_printf(m, "speed:\t\t%d\n", speed);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008160
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008161 if (status & TP_EC_FAN_FULLSPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008162 /* Disengaged mode takes precedence */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008163 seq_printf(m, "level:\t\tdisengaged\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008164 else if (status & TP_EC_FAN_AUTO)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008165 seq_printf(m, "level:\t\tauto\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008166 else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008167 seq_printf(m, "level:\t\t%d\n", status);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008168 break;
8169
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008170 case TPACPI_FAN_NONE:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008171 default:
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008172 seq_printf(m, "status:\t\tnot supported\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008173 }
8174
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008175 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008176 seq_printf(m, "commands:\tlevel <level>");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008177
8178 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008179 case TPACPI_FAN_WR_ACPI_SFAN:
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008180 seq_printf(m, " (<level> is 0-7)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008181 break;
8182
8183 default:
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008184 seq_printf(m, " (<level> is 0-7, "
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008185 "auto, disengaged, full-speed)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008186 break;
8187 }
8188 }
8189
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008190 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008191 seq_printf(m, "commands:\tenable, disable\n"
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008192 "commands:\twatchdog <timeout> (<timeout> "
8193 "is 0 (off), 1-120 (seconds))\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008194
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008195 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008196 seq_printf(m, "commands:\tspeed <speed>"
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008197 " (<speed> is 0-65535)\n");
8198
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008199 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008200}
8201
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008202static int fan_write_cmd_level(const char *cmd, int *rc)
8203{
8204 int level;
8205
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02008206 if (strlencmp(cmd, "level auto") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008207 level = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008208 else if ((strlencmp(cmd, "level disengaged") == 0) |
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008209 (strlencmp(cmd, "level full-speed") == 0))
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008210 level = TP_EC_FAN_FULLSPEED;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02008211 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008212 return 0;
8213
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008214 *rc = fan_set_level_safe(level);
8215 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008216 printk(TPACPI_ERR "level command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008217 "access mode %d", fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008218 else if (!*rc)
8219 tpacpi_disclose_usertask("procfs fan",
8220 "set level to %d\n", level);
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008221
8222 return 1;
8223}
8224
8225static int fan_write_cmd_enable(const char *cmd, int *rc)
8226{
8227 if (strlencmp(cmd, "enable") != 0)
8228 return 0;
8229
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008230 *rc = fan_set_enable();
8231 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008232 printk(TPACPI_ERR "enable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008233 "access mode %d", fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008234 else if (!*rc)
8235 tpacpi_disclose_usertask("procfs fan", "enable\n");
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008236
8237 return 1;
8238}
8239
8240static int fan_write_cmd_disable(const char *cmd, int *rc)
8241{
8242 if (strlencmp(cmd, "disable") != 0)
8243 return 0;
8244
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008245 *rc = fan_set_disable();
8246 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008247 printk(TPACPI_ERR "disable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008248 "access mode %d", fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008249 else if (!*rc)
8250 tpacpi_disclose_usertask("procfs fan", "disable\n");
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008251
8252 return 1;
8253}
8254
8255static int fan_write_cmd_speed(const char *cmd, int *rc)
8256{
8257 int speed;
8258
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02008259 /* TODO:
8260 * Support speed <low> <medium> <high> ? */
8261
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008262 if (sscanf(cmd, "speed %d", &speed) != 1)
8263 return 0;
8264
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008265 *rc = fan_set_speed(speed);
8266 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008267 printk(TPACPI_ERR "speed command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008268 "access mode %d", fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008269 else if (!*rc)
8270 tpacpi_disclose_usertask("procfs fan",
8271 "set speed to %d\n", speed);
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008272
8273 return 1;
8274}
8275
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008276static int fan_write_cmd_watchdog(const char *cmd, int *rc)
8277{
8278 int interval;
8279
8280 if (sscanf(cmd, "watchdog %d", &interval) != 1)
8281 return 0;
8282
8283 if (interval < 0 || interval > 120)
8284 *rc = -EINVAL;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008285 else {
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008286 fan_watchdog_maxinterval = interval;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008287 tpacpi_disclose_usertask("procfs fan",
8288 "set watchdog timer to %d\n",
8289 interval);
8290 }
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008291
8292 return 1;
8293}
8294
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008295static int fan_write(char *buf)
8296{
8297 char *cmd;
8298 int rc = 0;
8299
8300 while (!rc && (cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008301 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008302 fan_write_cmd_level(cmd, &rc)) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008303 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008304 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008305 fan_write_cmd_disable(cmd, &rc) ||
8306 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008307 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008308 fan_write_cmd_speed(cmd, &rc))
8309 )
8310 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008311 else if (!rc)
8312 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008313 }
8314
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008315 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008316}
8317
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008318static struct ibm_struct fan_driver_data = {
8319 .name = "fan",
8320 .read = fan_read,
8321 .write = fan_write,
8322 .exit = fan_exit,
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008323 .suspend = fan_suspend,
8324 .resume = fan_resume,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008325};
8326
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008327/****************************************************************************
8328 ****************************************************************************
8329 *
8330 * Infrastructure
8331 *
8332 ****************************************************************************
8333 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07008334
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03008335/*
8336 * HKEY event callout for other subdrivers go here
8337 * (yes, it is ugly, but it is quick, safe, and gets the job done
8338 */
8339static void tpacpi_driver_event(const unsigned int hkey_event)
8340{
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00008341 if (ibm_backlight_device) {
8342 switch (hkey_event) {
8343 case TP_HKEY_EV_BRGHT_UP:
8344 case TP_HKEY_EV_BRGHT_DOWN:
8345 tpacpi_brightness_notify_change();
8346 }
8347 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02008348 if (alsa_card) {
8349 switch (hkey_event) {
8350 case TP_HKEY_EV_VOL_UP:
8351 case TP_HKEY_EV_VOL_DOWN:
8352 case TP_HKEY_EV_VOL_MUTE:
8353 volume_alsa_notify_change();
8354 }
8355 }
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03008356}
8357
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03008358static void hotkey_driver_event(const unsigned int scancode)
8359{
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03008360 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode);
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03008361}
8362
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008363/* sysfs name ---------------------------------------------------------- */
8364static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
8365 struct device_attribute *attr,
8366 char *buf)
8367{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008368 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008369}
8370
8371static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
8372 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
8373
8374/* --------------------------------------------------------------------- */
8375
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008376/* /proc support */
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03008377static struct proc_dir_entry *proc_dir;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008378
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008379/*
8380 * Module and infrastructure proble, init and exit handling
8381 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008382
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008383static int force_load;
8384
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008385#ifdef CONFIG_THINKPAD_ACPI_DEBUG
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008386static const char * __init str_supported(int is_supported)
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008387{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008388 static char text_unsupported[] __initdata = "not supported";
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008389
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008390 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008391}
8392#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
8393
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008394static void ibm_exit(struct ibm_struct *ibm)
8395{
8396 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
8397
8398 list_del_init(&ibm->all_drivers);
8399
8400 if (ibm->flags.acpi_notify_installed) {
8401 dbg_printk(TPACPI_DBG_EXIT,
8402 "%s: acpi_remove_notify_handler\n", ibm->name);
8403 BUG_ON(!ibm->acpi);
8404 acpi_remove_notify_handler(*ibm->acpi->handle,
8405 ibm->acpi->type,
8406 dispatch_acpi_notify);
8407 ibm->flags.acpi_notify_installed = 0;
8408 ibm->flags.acpi_notify_installed = 0;
8409 }
8410
8411 if (ibm->flags.proc_created) {
8412 dbg_printk(TPACPI_DBG_EXIT,
8413 "%s: remove_proc_entry\n", ibm->name);
8414 remove_proc_entry(ibm->name, proc_dir);
8415 ibm->flags.proc_created = 0;
8416 }
8417
8418 if (ibm->flags.acpi_driver_registered) {
8419 dbg_printk(TPACPI_DBG_EXIT,
8420 "%s: acpi_bus_unregister_driver\n", ibm->name);
8421 BUG_ON(!ibm->acpi);
8422 acpi_bus_unregister_driver(ibm->acpi->driver);
8423 kfree(ibm->acpi->driver);
8424 ibm->acpi->driver = NULL;
8425 ibm->flags.acpi_driver_registered = 0;
8426 }
8427
8428 if (ibm->flags.init_called && ibm->exit) {
8429 ibm->exit();
8430 ibm->flags.init_called = 0;
8431 }
8432
8433 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
8434}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02008435
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008436static int __init ibm_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008437{
8438 int ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008439 struct ibm_struct *ibm = iibm->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008440 struct proc_dir_entry *entry;
8441
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008442 BUG_ON(ibm == NULL);
8443
8444 INIT_LIST_HEAD(&ibm->all_drivers);
8445
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03008446 if (ibm->flags.experimental && !experimental)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008447 return 0;
8448
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008449 dbg_printk(TPACPI_DBG_INIT,
8450 "probing for %s\n", ibm->name);
8451
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008452 if (iibm->init) {
8453 ret = iibm->init(iibm);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008454 if (ret > 0)
8455 return 0; /* probe failed */
8456 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008457 return ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008458
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03008459 ibm->flags.init_called = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008460 }
8461
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03008462 if (ibm->acpi) {
8463 if (ibm->acpi->hid) {
8464 ret = register_tpacpi_subdriver(ibm);
8465 if (ret)
8466 goto err_out;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008467 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03008468
8469 if (ibm->acpi->notify) {
8470 ret = setup_acpi_notify(ibm);
8471 if (ret == -ENODEV) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008472 printk(TPACPI_NOTICE "disabling subdriver %s\n",
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03008473 ibm->name);
8474 ret = 0;
8475 goto err_out;
8476 }
8477 if (ret < 0)
8478 goto err_out;
8479 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008480 }
8481
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008482 dbg_printk(TPACPI_DBG_INIT,
8483 "%s installed\n", ibm->name);
8484
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008485 if (ibm->read) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008486 mode_t mode;
8487
8488 mode = S_IRUGO;
8489 if (ibm->write)
8490 mode |= S_IWUSR;
8491 entry = proc_create_data(ibm->name, mode, proc_dir,
8492 &dispatch_proc_fops, ibm);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008493 if (!entry) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008494 printk(TPACPI_ERR "unable to create proc entry %s\n",
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008495 ibm->name);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008496 ret = -ENODEV;
8497 goto err_out;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008498 }
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03008499 ibm->flags.proc_created = 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008500 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008501
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008502 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
8503
Linus Torvalds1da177e2005-04-16 15:20:36 -07008504 return 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008505
8506err_out:
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008507 dbg_printk(TPACPI_DBG_INIT,
8508 "%s: at error exit path with result %d\n",
8509 ibm->name, ret);
8510
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008511 ibm_exit(ibm);
8512 return (ret < 0)? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008513}
8514
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008515/* Probing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008516
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008517static bool __pure __init tpacpi_is_fw_digit(const char c)
8518{
8519 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
8520}
8521
8522/* Most models: xxyTkkWW (#.##c); Ancient 570/600 and -SL lacks (#.##c) */
8523static bool __pure __init tpacpi_is_valid_fw_id(const char* const s,
8524 const char t)
8525{
8526 return s && strlen(s) >= 8 &&
8527 tpacpi_is_fw_digit(s[0]) &&
8528 tpacpi_is_fw_digit(s[1]) &&
8529 s[2] == t && s[3] == 'T' &&
8530 tpacpi_is_fw_digit(s[4]) &&
8531 tpacpi_is_fw_digit(s[5]) &&
8532 s[6] == 'W' && s[7] == 'W';
8533}
8534
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008535/* returns 0 - probe ok, or < 0 - probe error.
8536 * Probe ok doesn't mean thinkpad found.
8537 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
8538static int __must_check __init get_thinkpad_model_data(
8539 struct thinkpad_id_data *tp)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02008540{
Jeff Garzik18552562007-10-03 15:15:40 -04008541 const struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02008542 char ec_fw_string[18];
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008543 char const *s;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02008544
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008545 if (!tp)
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008546 return -EINVAL;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008547
8548 memset(tp, 0, sizeof(*tp));
8549
8550 if (dmi_name_in_vendors("IBM"))
8551 tp->vendor = PCI_VENDOR_ID_IBM;
8552 else if (dmi_name_in_vendors("LENOVO"))
8553 tp->vendor = PCI_VENDOR_ID_LENOVO;
8554 else
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008555 return 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008556
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008557 s = dmi_get_system_info(DMI_BIOS_VERSION);
8558 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
8559 if (s && !tp->bios_version_str)
8560 return -ENOMEM;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008561
8562 /* Really ancient ThinkPad 240X will fail this, which is fine */
8563 if (!tpacpi_is_valid_fw_id(tp->bios_version_str, 'E'))
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008564 return 0;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008565
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008566 tp->bios_model = tp->bios_version_str[0]
8567 | (tp->bios_version_str[1] << 8);
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008568 tp->bios_release = (tp->bios_version_str[4] << 8)
8569 | tp->bios_version_str[5];
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008570
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02008571 /*
8572 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
8573 * X32 or newer, all Z series; Some models must have an
8574 * up-to-date BIOS or they will not be detected.
8575 *
8576 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
8577 */
8578 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02008579 if (sscanf(dev->name,
8580 "IBM ThinkPad Embedded Controller -[%17c",
8581 ec_fw_string) == 1) {
8582 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
8583 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008584
8585 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008586 if (!tp->ec_version_str)
8587 return -ENOMEM;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008588
8589 if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) {
8590 tp->ec_model = ec_fw_string[0]
8591 | (ec_fw_string[1] << 8);
8592 tp->ec_release = (ec_fw_string[4] << 8)
8593 | ec_fw_string[5];
8594 } else {
8595 printk(TPACPI_NOTICE
8596 "ThinkPad firmware release %s "
8597 "doesn't match the known patterns\n",
8598 ec_fw_string);
8599 printk(TPACPI_NOTICE
8600 "please report this to %s\n",
8601 TPACPI_MAIL);
8602 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008603 break;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02008604 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02008605 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008606
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008607 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
8608 if (s && !strnicmp(s, "ThinkPad", 8)) {
8609 tp->model_str = kstrdup(s, GFP_KERNEL);
8610 if (!tp->model_str)
8611 return -ENOMEM;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008612 }
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03008613
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008614 s = dmi_get_system_info(DMI_PRODUCT_NAME);
8615 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
8616 if (s && !tp->nummodel_str)
8617 return -ENOMEM;
8618
8619 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008620}
8621
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008622static int __init probe_for_thinkpad(void)
8623{
8624 int is_thinkpad;
8625
8626 if (acpi_disabled)
8627 return -ENODEV;
8628
8629 /*
8630 * Non-ancient models have better DMI tagging, but very old models
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03008631 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008632 */
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03008633 is_thinkpad = (thinkpad_id.model_str != NULL) ||
8634 (thinkpad_id.ec_model != 0) ||
8635 tpacpi_is_fw_known();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008636
8637 /* ec is required because many other handles are relative to it */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008638 TPACPI_ACPIHANDLE_INIT(ec);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008639 if (!ec_handle) {
8640 if (is_thinkpad)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008641 printk(TPACPI_ERR
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008642 "Not yet supported ThinkPad detected!\n");
8643 return -ENODEV;
8644 }
8645
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03008646 if (!is_thinkpad && !force_load)
8647 return -ENODEV;
8648
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008649 return 0;
8650}
8651
8652
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008653/* Module init, exit, parameters */
8654
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008655static struct ibm_init_struct ibms_init[] __initdata = {
8656 {
8657 .init = thinkpad_acpi_driver_init,
8658 .data = &thinkpad_acpi_driver_data,
8659 },
8660 {
8661 .init = hotkey_init,
8662 .data = &hotkey_driver_data,
8663 },
8664 {
8665 .init = bluetooth_init,
8666 .data = &bluetooth_driver_data,
8667 },
8668 {
8669 .init = wan_init,
8670 .data = &wan_driver_data,
8671 },
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02008672 {
8673 .init = uwb_init,
8674 .data = &uwb_driver_data,
8675 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02008676#ifdef CONFIG_THINKPAD_ACPI_VIDEO
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008677 {
8678 .init = video_init,
8679 .data = &video_driver_data,
8680 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02008681#endif
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008682 {
8683 .init = light_init,
8684 .data = &light_driver_data,
8685 },
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008686 {
8687 .init = cmos_init,
8688 .data = &cmos_driver_data,
8689 },
8690 {
8691 .init = led_init,
8692 .data = &led_driver_data,
8693 },
8694 {
8695 .init = beep_init,
8696 .data = &beep_driver_data,
8697 },
8698 {
8699 .init = thermal_init,
8700 .data = &thermal_driver_data,
8701 },
8702 {
8703 .data = &ecdump_driver_data,
8704 },
8705 {
8706 .init = brightness_init,
8707 .data = &brightness_driver_data,
8708 },
8709 {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02008710 .init = volume_init,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008711 .data = &volume_driver_data,
8712 },
8713 {
8714 .init = fan_init,
8715 .data = &fan_driver_data,
8716 },
8717};
8718
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008719static int __init set_ibm_param(const char *val, struct kernel_param *kp)
8720{
8721 unsigned int i;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008722 struct ibm_struct *ibm;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008723
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02008724 if (!kp || !kp->name || !val)
8725 return -EINVAL;
8726
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008727 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
8728 ibm = ibms_init[i].data;
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02008729 WARN_ON(ibm == NULL);
8730
8731 if (!ibm || !ibm->name)
8732 continue;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008733
8734 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
8735 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008736 return -ENOSPC;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008737 strcpy(ibms_init[i].param, val);
8738 strcat(ibms_init[i].param, ",");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008739 return 0;
8740 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008741 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008742
8743 return -EINVAL;
8744}
8745
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008746module_param(experimental, int, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008747MODULE_PARM_DESC(experimental,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008748 "Enables experimental features when non-zero");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008749
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03008750module_param_named(debug, dbg_level, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008751MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03008752
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008753module_param(force_load, bool, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008754MODULE_PARM_DESC(force_load,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008755 "Attempts to load the driver even on a "
8756 "mis-identified ThinkPad when true");
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03008757
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008758module_param_named(fan_control, fan_control_allowed, bool, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008759MODULE_PARM_DESC(fan_control,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008760 "Enables setting fan parameters features when true");
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008761
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008762module_param_named(brightness_mode, brightness_mode, uint, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008763MODULE_PARM_DESC(brightness_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008764 "Selects brightness control strategy: "
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00008765 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03008766
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008767module_param(brightness_enable, uint, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008768MODULE_PARM_DESC(brightness_enable,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008769 "Enables backlight control when 1, disables when 0");
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02008770
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008771module_param(hotkey_report_mode, uint, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008772MODULE_PARM_DESC(hotkey_report_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008773 "used for backwards compatibility with userspace, "
8774 "see documentation");
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03008775
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02008776#ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02008777module_param_named(volume_mode, volume_mode, uint, 0444);
8778MODULE_PARM_DESC(volume_mode,
8779 "Selects volume control strategy: "
8780 "0=auto, 1=EC, 2=N/A, 3=EC+NVRAM");
8781
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02008782module_param_named(volume_capabilities, volume_capabilities, uint, 0444);
8783MODULE_PARM_DESC(volume_capabilities,
8784 "Selects the mixer capabilites: "
8785 "0=auto, 1=volume and mute, 2=mute only");
8786
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02008787module_param_named(volume_control, volume_control_allowed, bool, 0444);
8788MODULE_PARM_DESC(volume_control,
8789 "Enables software override for the console audio "
8790 "control when true");
8791
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02008792/* ALSA module API parameters */
8793module_param_named(index, alsa_index, int, 0444);
8794MODULE_PARM_DESC(index, "ALSA index for the ACPI EC Mixer");
8795module_param_named(id, alsa_id, charp, 0444);
8796MODULE_PARM_DESC(id, "ALSA id for the ACPI EC Mixer");
8797module_param_named(enable, alsa_enable, bool, 0444);
8798MODULE_PARM_DESC(enable, "Enable the ALSA interface for the ACPI EC Mixer");
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02008799#endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02008800
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008801#define TPACPI_PARAM(feature) \
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008802 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
Henrique de Moraes Holschuhcbb14842008-02-16 02:17:50 -02008803 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008804 "at module load, see documentation")
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008805
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008806TPACPI_PARAM(hotkey);
8807TPACPI_PARAM(bluetooth);
8808TPACPI_PARAM(video);
8809TPACPI_PARAM(light);
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008810TPACPI_PARAM(cmos);
8811TPACPI_PARAM(led);
8812TPACPI_PARAM(beep);
8813TPACPI_PARAM(ecdump);
8814TPACPI_PARAM(brightness);
8815TPACPI_PARAM(volume);
8816TPACPI_PARAM(fan);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008817
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02008818#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008819module_param(dbg_wlswemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02008820MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
8821module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
8822MODULE_PARM_DESC(wlsw_state,
8823 "Initial state of the emulated WLSW switch");
8824
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008825module_param(dbg_bluetoothemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02008826MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
8827module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
8828MODULE_PARM_DESC(bluetooth_state,
8829 "Initial state of the emulated bluetooth switch");
8830
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008831module_param(dbg_wwanemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02008832MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
8833module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
8834MODULE_PARM_DESC(wwan_state,
8835 "Initial state of the emulated WWAN switch");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02008836
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008837module_param(dbg_uwbemul, uint, 0444);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02008838MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
8839module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
8840MODULE_PARM_DESC(uwb_state,
8841 "Initial state of the emulated UWB switch");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02008842#endif
8843
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008844static void thinkpad_acpi_module_exit(void)
8845{
8846 struct ibm_struct *ibm, *itmp;
8847
8848 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
8849
8850 list_for_each_entry_safe_reverse(ibm, itmp,
8851 &tpacpi_all_drivers,
8852 all_drivers) {
8853 ibm_exit(ibm);
8854 }
8855
8856 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
8857
8858 if (tpacpi_inputdev) {
8859 if (tp_features.input_device_registered)
8860 input_unregister_device(tpacpi_inputdev);
8861 else
8862 input_free_device(tpacpi_inputdev);
8863 }
8864
8865 if (tpacpi_hwmon)
8866 hwmon_device_unregister(tpacpi_hwmon);
8867
8868 if (tp_features.sensors_pdev_attrs_registered)
8869 device_remove_file(&tpacpi_sensors_pdev->dev,
8870 &dev_attr_thinkpad_acpi_pdev_name);
8871 if (tpacpi_sensors_pdev)
8872 platform_device_unregister(tpacpi_sensors_pdev);
8873 if (tpacpi_pdev)
8874 platform_device_unregister(tpacpi_pdev);
8875
8876 if (tp_features.sensors_pdrv_attrs_registered)
8877 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
8878 if (tp_features.platform_drv_attrs_registered)
8879 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
8880
8881 if (tp_features.sensors_pdrv_registered)
8882 platform_driver_unregister(&tpacpi_hwmon_pdriver);
8883
8884 if (tp_features.platform_drv_registered)
8885 platform_driver_unregister(&tpacpi_pdriver);
8886
8887 if (proc_dir)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008888 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008889
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03008890 if (tpacpi_wq)
8891 destroy_workqueue(tpacpi_wq);
8892
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008893 kfree(thinkpad_id.bios_version_str);
8894 kfree(thinkpad_id.ec_version_str);
8895 kfree(thinkpad_id.model_str);
8896}
8897
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02008898
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03008899static int __init thinkpad_acpi_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008900{
8901 int ret, i;
8902
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03008903 tpacpi_lifecycle = TPACPI_LIFE_INIT;
8904
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03008905 /* Parameter checking */
8906 if (hotkey_report_mode > 2)
8907 return -EINVAL;
8908
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008909 /* Driver-level probe */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008910
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008911 ret = get_thinkpad_model_data(&thinkpad_id);
8912 if (ret) {
8913 printk(TPACPI_ERR
8914 "unable to get DMI data: %d\n", ret);
8915 thinkpad_acpi_module_exit();
8916 return ret;
8917 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008918 ret = probe_for_thinkpad();
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008919 if (ret) {
8920 thinkpad_acpi_module_exit();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008921 return ret;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008922 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008923
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008924 /* Driver initialization */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008925
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008926 TPACPI_ACPIHANDLE_INIT(ecrd);
8927 TPACPI_ACPIHANDLE_INIT(ecwr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008928
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03008929 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
8930 if (!tpacpi_wq) {
8931 thinkpad_acpi_module_exit();
8932 return -ENOMEM;
8933 }
8934
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008935 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008936 if (!proc_dir) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008937 printk(TPACPI_ERR
8938 "unable to create proc dir " TPACPI_PROC_DIR);
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03008939 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008940 return -ENODEV;
8941 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008942
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008943 ret = platform_driver_register(&tpacpi_pdriver);
8944 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008945 printk(TPACPI_ERR
8946 "unable to register main platform driver\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008947 thinkpad_acpi_module_exit();
8948 return ret;
8949 }
Henrique de Moraes Holschuhac363932007-07-27 17:04:40 -03008950 tp_features.platform_drv_registered = 1;
8951
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008952 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
8953 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008954 printk(TPACPI_ERR
8955 "unable to register hwmon platform driver\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008956 thinkpad_acpi_module_exit();
8957 return ret;
8958 }
8959 tp_features.sensors_pdrv_registered = 1;
8960
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03008961 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03008962 if (!ret) {
8963 tp_features.platform_drv_attrs_registered = 1;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008964 ret = tpacpi_create_driver_attributes(
8965 &tpacpi_hwmon_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03008966 }
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03008967 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008968 printk(TPACPI_ERR
8969 "unable to create sysfs driver attributes\n");
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03008970 thinkpad_acpi_module_exit();
8971 return ret;
8972 }
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03008973 tp_features.sensors_pdrv_attrs_registered = 1;
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03008974
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008975
8976 /* Device initialization */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008977 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008978 NULL, 0);
8979 if (IS_ERR(tpacpi_pdev)) {
8980 ret = PTR_ERR(tpacpi_pdev);
8981 tpacpi_pdev = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008982 printk(TPACPI_ERR "unable to register platform device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008983 thinkpad_acpi_module_exit();
8984 return ret;
8985 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008986 tpacpi_sensors_pdev = platform_device_register_simple(
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008987 TPACPI_HWMON_DRVR_NAME,
8988 -1, NULL, 0);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008989 if (IS_ERR(tpacpi_sensors_pdev)) {
8990 ret = PTR_ERR(tpacpi_sensors_pdev);
8991 tpacpi_sensors_pdev = NULL;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008992 printk(TPACPI_ERR
8993 "unable to register hwmon platform device\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008994 thinkpad_acpi_module_exit();
8995 return ret;
8996 }
8997 ret = device_create_file(&tpacpi_sensors_pdev->dev,
8998 &dev_attr_thinkpad_acpi_pdev_name);
8999 if (ret) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009000 printk(TPACPI_ERR
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009001 "unable to create sysfs hwmon device attributes\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009002 thinkpad_acpi_module_exit();
9003 return ret;
9004 }
9005 tp_features.sensors_pdev_attrs_registered = 1;
9006 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009007 if (IS_ERR(tpacpi_hwmon)) {
9008 ret = PTR_ERR(tpacpi_hwmon);
9009 tpacpi_hwmon = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009010 printk(TPACPI_ERR "unable to register hwmon device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009011 thinkpad_acpi_module_exit();
9012 return ret;
9013 }
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -03009014 mutex_init(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009015 tpacpi_inputdev = input_allocate_device();
9016 if (!tpacpi_inputdev) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009017 printk(TPACPI_ERR "unable to allocate input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009018 thinkpad_acpi_module_exit();
9019 return -ENOMEM;
9020 } else {
9021 /* Prepare input device, but don't register */
9022 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009023 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009024 tpacpi_inputdev->id.bustype = BUS_HOST;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03009025 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
9026 thinkpad_id.vendor :
9027 PCI_VENDOR_ID_IBM;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009028 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
9029 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
Henrique de Moraes Holschuhd112ef92009-12-09 01:36:26 +00009030 tpacpi_inputdev->dev.parent = &tpacpi_pdev->dev;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009031 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009032 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
9033 ret = ibm_init(&ibms_init[i]);
9034 if (ret >= 0 && *ibms_init[i].param)
9035 ret = ibms_init[i].data->write(ibms_init[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009036 if (ret < 0) {
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03009037 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07009038 return ret;
9039 }
9040 }
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009041 ret = input_register_device(tpacpi_inputdev);
9042 if (ret < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009043 printk(TPACPI_ERR "unable to register input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009044 thinkpad_acpi_module_exit();
9045 return ret;
9046 } else {
9047 tp_features.input_device_registered = 1;
9048 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009049
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03009050 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009051 return 0;
9052}
9053
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03009054MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
9055
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009056/*
Henrique de Moraes Holschuh922fe092009-04-14 02:44:13 +00009057 * This will autoload the driver in almost every ThinkPad
9058 * in widespread use.
9059 *
9060 * Only _VERY_ old models, like the 240, 240x and 570 lack
9061 * the HKEY event interface.
9062 */
9063MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
9064
9065/*
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009066 * DMI matching for module autoloading
9067 *
9068 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
9069 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
9070 *
9071 * Only models listed in thinkwiki will be supported, so add yours
9072 * if it is not there yet.
9073 */
9074#define IBM_BIOS_MODULE_ALIAS(__type) \
Mathieu Chouquet-Stringerb36a50f2009-03-14 16:35:26 +01009075 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009076
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009077/* Ancient thinkpad BIOSes have to be identified by
9078 * BIOS type or model number, and there are far less
9079 * BIOS types than model numbers... */
Henrique de Moraes Holschuh922fe092009-04-14 02:44:13 +00009080IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009081
Henrique de Moraes Holschuhf68f53a2009-04-14 02:44:12 +00009082MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
9083MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009084MODULE_DESCRIPTION(TPACPI_DESC);
9085MODULE_VERSION(TPACPI_VERSION);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009086MODULE_LICENSE("GPL");
9087
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03009088module_init(thinkpad_acpi_module_init);
9089module_exit(thinkpad_acpi_module_exit);