blob: 3af4628d7dd19c21ad6ee59ae738b4e12551016a [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);
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002085static void hotkey_poll_setup(const bool may_warn);
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002086
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002087/* HKEY.MHKG() return bits */
2088#define TP_HOTKEY_TABLET_MASK (1 << 3)
2089
Johannes Berg19d337d2009-06-02 13:01:37 +02002090static int hotkey_get_wlsw(void)
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002091{
Johannes Berg19d337d2009-06-02 13:01:37 +02002092 int status;
2093
2094 if (!tp_features.hotkey_wlsw)
2095 return -ENODEV;
2096
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02002097#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
Johannes Berg19d337d2009-06-02 13:01:37 +02002098 if (dbg_wlswemul)
2099 return (tpacpi_wlsw_emulstate) ?
2100 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02002101#endif
Johannes Berg19d337d2009-06-02 13:01:37 +02002102
2103 if (!acpi_evalf(hkey_handle, &status, "WLSW", "d"))
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002104 return -EIO;
Johannes Berg19d337d2009-06-02 13:01:37 +02002105
2106 return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002107}
2108
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002109static int hotkey_get_tablet_mode(int *status)
2110{
2111 int s;
2112
2113 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
2114 return -EIO;
2115
Henrique de Moraes Holschuhcee47f52008-03-04 14:29:21 -08002116 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
2117 return 0;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002118}
2119
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002120/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002121 * Reads current event mask from firmware, and updates
2122 * hotkey_acpi_mask accordingly. Also resets any bits
2123 * from hotkey_user_mask that are unavailable to be
2124 * delivered (shadow requirement of the userspace ABI).
2125 *
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002126 * Call with hotkey_mutex held
2127 */
2128static int hotkey_mask_get(void)
2129{
2130 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002131 u32 m = 0;
2132
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002133 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002134 return -EIO;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002135
2136 hotkey_acpi_mask = m;
2137 } else {
2138 /* no mask support doesn't mean no event support... */
2139 hotkey_acpi_mask = hotkey_all_mask;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002140 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002141
2142 /* sync userspace-visible mask */
2143 hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002144
2145 return 0;
2146}
2147
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002148void static hotkey_mask_warn_incomplete_mask(void)
2149{
2150 /* log only what the user can fix... */
2151 const u32 wantedmask = hotkey_driver_mask &
2152 ~(hotkey_acpi_mask | hotkey_source_mask) &
2153 (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
2154
2155 if (wantedmask)
2156 printk(TPACPI_NOTICE
2157 "required events 0x%08x not enabled!\n",
2158 wantedmask);
2159}
2160
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002161/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002162 * Set the firmware mask when supported
2163 *
2164 * Also calls hotkey_mask_get to update hotkey_acpi_mask.
2165 *
2166 * NOTE: does not set bits in hotkey_user_mask, but may reset them.
2167 *
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002168 * Call with hotkey_mutex held
2169 */
2170static int hotkey_mask_set(u32 mask)
2171{
2172 int i;
2173 int rc = 0;
2174
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002175 const u32 fwmask = mask & ~hotkey_source_mask;
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -03002176
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002177 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002178 for (i = 0; i < 32; i++) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002179 if (!acpi_evalf(hkey_handle,
2180 NULL, "MHKM", "vdd", i + 1,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002181 !!(mask & (1 << i)))) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002182 rc = -EIO;
2183 break;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002184 }
2185 }
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002186 }
2187
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002188 /*
2189 * We *must* make an inconditional call to hotkey_mask_get to
2190 * refresh hotkey_acpi_mask and update hotkey_user_mask
2191 *
2192 * Take the opportunity to also log when we cannot _enable_
2193 * a given event.
2194 */
2195 if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
2196 printk(TPACPI_NOTICE
2197 "asked for hotkey mask 0x%08x, but "
2198 "firmware forced it to 0x%08x\n",
2199 fwmask, hotkey_acpi_mask);
2200 }
2201
Henrique de Moraes Holschuh6b30eb72009-12-09 01:36:25 +00002202 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING)
2203 hotkey_mask_warn_incomplete_mask();
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002204
2205 return rc;
2206}
2207
2208/*
2209 * Sets hotkey_user_mask and tries to set the firmware mask
2210 *
2211 * Call with hotkey_mutex held
2212 */
2213static int hotkey_user_mask_set(const u32 mask)
2214{
2215 int rc;
2216
2217 /* Give people a chance to notice they are doing something that
2218 * is bound to go boom on their users sooner or later */
2219 if (!tp_warned.hotkey_mask_ff &&
2220 (mask == 0xffff || mask == 0xffffff ||
2221 mask == 0xffffffff)) {
2222 tp_warned.hotkey_mask_ff = 1;
2223 printk(TPACPI_NOTICE
2224 "setting the hotkey mask to 0x%08x is likely "
2225 "not the best way to go about it\n", mask);
2226 printk(TPACPI_NOTICE
2227 "please consider using the driver defaults, "
2228 "and refer to up-to-date thinkpad-acpi "
2229 "documentation\n");
2230 }
2231
2232 /* Try to enable what the user asked for, plus whatever we need.
2233 * this syncs everything but won't enable bits in hotkey_user_mask */
2234 rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
2235
2236 /* Enable the available bits in hotkey_user_mask */
2237 hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
2238
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002239 return rc;
2240}
2241
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002242/*
2243 * Sets the driver hotkey mask.
2244 *
2245 * Can be called even if the hotkey subdriver is inactive
2246 */
2247static int tpacpi_hotkey_driver_mask_set(const u32 mask)
2248{
2249 int rc;
2250
2251 /* Do the right thing if hotkey_init has not been called yet */
2252 if (!tp_features.hotkey) {
2253 hotkey_driver_mask = mask;
2254 return 0;
2255 }
2256
2257 mutex_lock(&hotkey_mutex);
2258
2259 HOTKEY_CONFIG_CRITICAL_START
2260 hotkey_driver_mask = mask;
Henrique de Moraes Holschuhb684a362009-09-26 21:42:49 -03002261#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002262 hotkey_source_mask |= (mask & ~hotkey_all_mask);
Henrique de Moraes Holschuhb684a362009-09-26 21:42:49 -03002263#endif
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002264 HOTKEY_CONFIG_CRITICAL_END
2265
2266 rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) &
2267 ~hotkey_source_mask);
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002268 hotkey_poll_setup(true);
2269
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002270 mutex_unlock(&hotkey_mutex);
2271
2272 return rc;
2273}
2274
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002275static int hotkey_status_get(int *status)
2276{
2277 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
2278 return -EIO;
2279
2280 return 0;
2281}
2282
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002283static int hotkey_status_set(bool enable)
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002284{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002285 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002286 return -EIO;
2287
2288 return 0;
2289}
2290
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002291static void tpacpi_input_send_tabletsw(void)
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002292{
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002293 int state;
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002294
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002295 if (tp_features.hotkey_tablet &&
2296 !hotkey_get_tablet_mode(&state)) {
2297 mutex_lock(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002298
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002299 input_report_switch(tpacpi_inputdev,
2300 SW_TABLET_MODE, !!state);
2301 input_sync(tpacpi_inputdev);
2302
2303 mutex_unlock(&tpacpi_inputdev_send_mutex);
2304 }
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002305}
2306
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002307/* Do NOT call without validating scancode first */
2308static void tpacpi_input_send_key(const unsigned int scancode)
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002309{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002310 const unsigned int keycode = hotkey_keycode_map[scancode];
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002311
2312 if (keycode != KEY_RESERVED) {
2313 mutex_lock(&tpacpi_inputdev_send_mutex);
2314
2315 input_report_key(tpacpi_inputdev, keycode, 1);
2316 if (keycode == KEY_UNKNOWN)
2317 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
2318 scancode);
2319 input_sync(tpacpi_inputdev);
2320
2321 input_report_key(tpacpi_inputdev, keycode, 0);
2322 if (keycode == KEY_UNKNOWN)
2323 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
2324 scancode);
2325 input_sync(tpacpi_inputdev);
2326
2327 mutex_unlock(&tpacpi_inputdev_send_mutex);
2328 }
2329}
2330
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002331/* Do NOT call without validating scancode first */
2332static void tpacpi_input_send_key_masked(const unsigned int scancode)
2333{
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002334 hotkey_driver_event(scancode);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002335 if (hotkey_user_mask & (1 << scancode))
2336 tpacpi_input_send_key(scancode);
2337}
2338
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002339#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2340static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
2341
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002342/* Do NOT call without validating scancode first */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002343static void tpacpi_hotkey_send_key(unsigned int scancode)
2344{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002345 tpacpi_input_send_key_masked(scancode);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002346 if (hotkey_report_mode < 2) {
2347 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03002348 0x80, TP_HKEY_EV_HOTKEY_BASE + scancode);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002349 }
2350}
2351
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002352static void hotkey_read_nvram(struct tp_nvram_state *n, const u32 m)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002353{
2354 u8 d;
2355
2356 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
2357 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
2358 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
2359 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
2360 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
2361 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
2362 }
2363 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
2364 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
2365 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
2366 }
2367 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
2368 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
2369 n->displayexp_toggle =
2370 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
2371 }
2372 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
2373 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
2374 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
2375 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
2376 n->brightness_toggle =
2377 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
2378 }
2379 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
2380 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
2381 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
2382 >> TP_NVRAM_POS_LEVEL_VOLUME;
2383 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2384 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2385 }
2386}
2387
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002388static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
2389 struct tp_nvram_state *newn,
2390 const u32 event_mask)
2391{
2392
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002393#define TPACPI_COMPARE_KEY(__scancode, __member) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002394 do { \
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002395 if ((event_mask & (1 << __scancode)) && \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002396 oldn->__member != newn->__member) \
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002397 tpacpi_hotkey_send_key(__scancode); \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002398 } while (0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002399
2400#define TPACPI_MAY_SEND_KEY(__scancode) \
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002401 do { \
2402 if (event_mask & (1 << __scancode)) \
2403 tpacpi_hotkey_send_key(__scancode); \
2404 } while (0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002405
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002406 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2407 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2408 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2409 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2410
2411 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2412
2413 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2414
2415 /* handle volume */
2416 if (oldn->volume_toggle != newn->volume_toggle) {
2417 if (oldn->mute != newn->mute) {
2418 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2419 }
2420 if (oldn->volume_level > newn->volume_level) {
2421 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2422 } else if (oldn->volume_level < newn->volume_level) {
2423 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2424 } else if (oldn->mute == newn->mute) {
2425 /* repeated key presses that didn't change state */
2426 if (newn->mute) {
2427 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2428 } else if (newn->volume_level != 0) {
2429 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2430 } else {
2431 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2432 }
2433 }
2434 }
2435
2436 /* handle brightness */
2437 if (oldn->brightness_toggle != newn->brightness_toggle) {
2438 if (oldn->brightness_level < newn->brightness_level) {
2439 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2440 } else if (oldn->brightness_level > newn->brightness_level) {
2441 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2442 } else {
2443 /* repeated key presses that didn't change state */
2444 if (newn->brightness_level != 0) {
2445 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2446 } else {
2447 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2448 }
2449 }
2450 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002451
2452#undef TPACPI_COMPARE_KEY
2453#undef TPACPI_MAY_SEND_KEY
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002454}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002455
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002456/*
2457 * Polling driver
2458 *
2459 * We track all events in hotkey_source_mask all the time, since
2460 * most of them are edge-based. We only issue those requested by
2461 * hotkey_user_mask or hotkey_driver_mask, though.
2462 */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002463static int hotkey_kthread(void *data)
2464{
2465 struct tp_nvram_state s[2];
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002466 u32 poll_mask, event_mask;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002467 unsigned int si, so;
2468 unsigned long t;
2469 unsigned int change_detector, must_reset;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002470 unsigned int poll_freq;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002471
2472 mutex_lock(&hotkey_thread_mutex);
2473
2474 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2475 goto exit;
2476
2477 set_freezable();
2478
2479 so = 0;
2480 si = 1;
2481 t = 0;
2482
2483 /* Initial state for compares */
2484 mutex_lock(&hotkey_thread_data_mutex);
2485 change_detector = hotkey_config_change;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002486 poll_mask = hotkey_source_mask;
2487 event_mask = hotkey_source_mask &
2488 (hotkey_driver_mask | hotkey_user_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002489 poll_freq = hotkey_poll_freq;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002490 mutex_unlock(&hotkey_thread_data_mutex);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002491 hotkey_read_nvram(&s[so], poll_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002492
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002493 while (!kthread_should_stop()) {
2494 if (t == 0) {
2495 if (likely(poll_freq))
2496 t = 1000/poll_freq;
2497 else
2498 t = 100; /* should never happen... */
2499 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002500 t = msleep_interruptible(t);
2501 if (unlikely(kthread_should_stop()))
2502 break;
2503 must_reset = try_to_freeze();
2504 if (t > 0 && !must_reset)
2505 continue;
2506
2507 mutex_lock(&hotkey_thread_data_mutex);
2508 if (must_reset || hotkey_config_change != change_detector) {
2509 /* forget old state on thaw or config change */
2510 si = so;
2511 t = 0;
2512 change_detector = hotkey_config_change;
2513 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002514 poll_mask = hotkey_source_mask;
2515 event_mask = hotkey_source_mask &
2516 (hotkey_driver_mask | hotkey_user_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002517 poll_freq = hotkey_poll_freq;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002518 mutex_unlock(&hotkey_thread_data_mutex);
2519
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002520 if (likely(poll_mask)) {
2521 hotkey_read_nvram(&s[si], poll_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002522 if (likely(si != so)) {
2523 hotkey_compare_and_issue_event(&s[so], &s[si],
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002524 event_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002525 }
2526 }
2527
2528 so = si;
2529 si ^= 1;
2530 }
2531
2532exit:
2533 mutex_unlock(&hotkey_thread_mutex);
2534 return 0;
2535}
2536
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002537/* call with hotkey_mutex held */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002538static void hotkey_poll_stop_sync(void)
2539{
2540 if (tpacpi_hotkey_task) {
2541 if (frozen(tpacpi_hotkey_task) ||
2542 freezing(tpacpi_hotkey_task))
2543 thaw_process(tpacpi_hotkey_task);
2544
2545 kthread_stop(tpacpi_hotkey_task);
2546 tpacpi_hotkey_task = NULL;
2547 mutex_lock(&hotkey_thread_mutex);
2548 /* at this point, the thread did exit */
2549 mutex_unlock(&hotkey_thread_mutex);
2550 }
2551}
2552
2553/* call with hotkey_mutex held */
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002554static void hotkey_poll_setup(const bool may_warn)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002555{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002556 const u32 poll_driver_mask = hotkey_driver_mask & hotkey_source_mask;
2557 const u32 poll_user_mask = hotkey_user_mask & hotkey_source_mask;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002558
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002559 if (hotkey_poll_freq > 0 &&
2560 (poll_driver_mask ||
2561 (poll_user_mask && tpacpi_inputdev->users > 0))) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002562 if (!tpacpi_hotkey_task) {
2563 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03002564 NULL, TPACPI_NVRAM_KTHREAD_NAME);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002565 if (IS_ERR(tpacpi_hotkey_task)) {
2566 tpacpi_hotkey_task = NULL;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002567 printk(TPACPI_ERR
2568 "could not create kernel thread "
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002569 "for hotkey polling\n");
2570 }
2571 }
2572 } else {
2573 hotkey_poll_stop_sync();
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002574 if (may_warn && (poll_driver_mask || poll_user_mask) &&
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002575 hotkey_poll_freq == 0) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002576 printk(TPACPI_NOTICE
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002577 "hot keys 0x%08x and/or events 0x%08x "
2578 "require polling, which is currently "
2579 "disabled\n",
2580 poll_user_mask, poll_driver_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002581 }
2582 }
2583}
2584
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002585static void hotkey_poll_setup_safe(const bool may_warn)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002586{
2587 mutex_lock(&hotkey_mutex);
2588 hotkey_poll_setup(may_warn);
2589 mutex_unlock(&hotkey_mutex);
2590}
2591
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002592/* call with hotkey_mutex held */
2593static void hotkey_poll_set_freq(unsigned int freq)
2594{
2595 if (!freq)
2596 hotkey_poll_stop_sync();
2597
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002598 hotkey_poll_freq = freq;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002599}
2600
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02002601#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2602
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002603static void hotkey_poll_setup(const bool __unused)
2604{
2605}
2606
2607static void hotkey_poll_setup_safe(const bool __unused)
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02002608{
2609}
2610
2611#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2612
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002613static int hotkey_inputdev_open(struct input_dev *dev)
2614{
2615 switch (tpacpi_lifecycle) {
2616 case TPACPI_LIFE_INIT:
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002617 case TPACPI_LIFE_RUNNING:
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002618 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002619 return 0;
Henrique de Moraes Holschuhb589ea42010-02-25 21:28:58 -03002620 case TPACPI_LIFE_EXITING:
2621 return -EBUSY;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002622 }
2623
2624 /* Should only happen if tpacpi_lifecycle is corrupt */
2625 BUG();
2626 return -EBUSY;
2627}
2628
2629static void hotkey_inputdev_close(struct input_dev *dev)
2630{
2631 /* disable hotkey polling when possible */
Henrique de Moraes Holschuhb589ea42010-02-25 21:28:58 -03002632 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING &&
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002633 !(hotkey_source_mask & hotkey_driver_mask))
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002634 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002635}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002636
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002637/* sysfs hotkey enable ------------------------------------------------- */
2638static ssize_t hotkey_enable_show(struct device *dev,
2639 struct device_attribute *attr,
2640 char *buf)
2641{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002642 int res, status;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002643
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002644 printk_deprecated_attribute("hotkey_enable",
2645 "Hotkey reporting is always enabled");
2646
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002647 res = hotkey_status_get(&status);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002648 if (res)
2649 return res;
2650
2651 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2652}
2653
2654static ssize_t hotkey_enable_store(struct device *dev,
2655 struct device_attribute *attr,
2656 const char *buf, size_t count)
2657{
2658 unsigned long t;
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002659
2660 printk_deprecated_attribute("hotkey_enable",
2661 "Hotkeys can be disabled through hotkey_mask");
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002662
2663 if (parse_strtoul(buf, 1, &t))
2664 return -EINVAL;
2665
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002666 if (t == 0)
2667 return -EPERM;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002668
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002669 return count;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002670}
2671
2672static struct device_attribute dev_attr_hotkey_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002673 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002674 hotkey_enable_show, hotkey_enable_store);
2675
2676/* sysfs hotkey mask --------------------------------------------------- */
2677static ssize_t hotkey_mask_show(struct device *dev,
2678 struct device_attribute *attr,
2679 char *buf)
2680{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002681 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_user_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002682}
2683
2684static ssize_t hotkey_mask_store(struct device *dev,
2685 struct device_attribute *attr,
2686 const char *buf, size_t count)
2687{
2688 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002689 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002690
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002691 if (parse_strtoul(buf, 0xffffffffUL, &t))
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002692 return -EINVAL;
2693
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002694 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002695 return -ERESTARTSYS;
2696
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002697 res = hotkey_user_mask_set(t);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002698
2699#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002700 hotkey_poll_setup(true);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002701#endif
2702
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002703 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002704
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002705 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2706
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002707 return (res) ? res : count;
2708}
2709
2710static struct device_attribute dev_attr_hotkey_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002711 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002712 hotkey_mask_show, hotkey_mask_store);
2713
2714/* sysfs hotkey bios_enabled ------------------------------------------- */
2715static ssize_t hotkey_bios_enabled_show(struct device *dev,
2716 struct device_attribute *attr,
2717 char *buf)
2718{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002719 return sprintf(buf, "0\n");
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002720}
2721
2722static struct device_attribute dev_attr_hotkey_bios_enabled =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002723 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002724
2725/* sysfs hotkey bios_mask ---------------------------------------------- */
2726static ssize_t hotkey_bios_mask_show(struct device *dev,
2727 struct device_attribute *attr,
2728 char *buf)
2729{
Henrique de Moraes Holschuh06777be2009-09-12 15:22:15 -03002730 printk_deprecated_attribute("hotkey_bios_mask",
2731 "This attribute is useless.");
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002732 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002733}
2734
2735static struct device_attribute dev_attr_hotkey_bios_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002736 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002737
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002738/* sysfs hotkey all_mask ----------------------------------------------- */
2739static ssize_t hotkey_all_mask_show(struct device *dev,
2740 struct device_attribute *attr,
2741 char *buf)
2742{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002743 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2744 hotkey_all_mask | hotkey_source_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002745}
2746
2747static struct device_attribute dev_attr_hotkey_all_mask =
2748 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
2749
2750/* sysfs hotkey recommended_mask --------------------------------------- */
2751static ssize_t hotkey_recommended_mask_show(struct device *dev,
2752 struct device_attribute *attr,
2753 char *buf)
2754{
2755 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002756 (hotkey_all_mask | hotkey_source_mask)
2757 & ~hotkey_reserved_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002758}
2759
2760static struct device_attribute dev_attr_hotkey_recommended_mask =
2761 __ATTR(hotkey_recommended_mask, S_IRUGO,
2762 hotkey_recommended_mask_show, NULL);
2763
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002764#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2765
2766/* sysfs hotkey hotkey_source_mask ------------------------------------- */
2767static ssize_t hotkey_source_mask_show(struct device *dev,
2768 struct device_attribute *attr,
2769 char *buf)
2770{
2771 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2772}
2773
2774static ssize_t hotkey_source_mask_store(struct device *dev,
2775 struct device_attribute *attr,
2776 const char *buf, size_t count)
2777{
2778 unsigned long t;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002779 u32 r_ev;
2780 int rc;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002781
2782 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2783 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2784 return -EINVAL;
2785
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002786 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002787 return -ERESTARTSYS;
2788
2789 HOTKEY_CONFIG_CRITICAL_START
2790 hotkey_source_mask = t;
2791 HOTKEY_CONFIG_CRITICAL_END
2792
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002793 rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
2794 ~hotkey_source_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002795 hotkey_poll_setup(true);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002796
2797 /* check if events needed by the driver got disabled */
2798 r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
2799 & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002800
2801 mutex_unlock(&hotkey_mutex);
2802
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002803 if (rc < 0)
2804 printk(TPACPI_ERR "hotkey_source_mask: failed to update the"
2805 "firmware event mask!\n");
2806
2807 if (r_ev)
2808 printk(TPACPI_NOTICE "hotkey_source_mask: "
2809 "some important events were disabled: "
2810 "0x%04x\n", r_ev);
2811
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002812 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2813
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002814 return (rc < 0) ? rc : count;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002815}
2816
2817static struct device_attribute dev_attr_hotkey_source_mask =
2818 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2819 hotkey_source_mask_show, hotkey_source_mask_store);
2820
2821/* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2822static ssize_t hotkey_poll_freq_show(struct device *dev,
2823 struct device_attribute *attr,
2824 char *buf)
2825{
2826 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2827}
2828
2829static ssize_t hotkey_poll_freq_store(struct device *dev,
2830 struct device_attribute *attr,
2831 const char *buf, size_t count)
2832{
2833 unsigned long t;
2834
2835 if (parse_strtoul(buf, 25, &t))
2836 return -EINVAL;
2837
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002838 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002839 return -ERESTARTSYS;
2840
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002841 hotkey_poll_set_freq(t);
2842 hotkey_poll_setup(true);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002843
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002844 mutex_unlock(&hotkey_mutex);
2845
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002846 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2847
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002848 return count;
2849}
2850
2851static struct device_attribute dev_attr_hotkey_poll_freq =
2852 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2853 hotkey_poll_freq_show, hotkey_poll_freq_store);
2854
2855#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2856
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002857/* sysfs hotkey radio_sw (pollable) ------------------------------------ */
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002858static ssize_t hotkey_radio_sw_show(struct device *dev,
2859 struct device_attribute *attr,
2860 char *buf)
2861{
Johannes Berg19d337d2009-06-02 13:01:37 +02002862 int res;
2863 res = hotkey_get_wlsw();
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002864 if (res < 0)
2865 return res;
2866
Johannes Berg19d337d2009-06-02 13:01:37 +02002867 /* Opportunistic update */
2868 tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
2869
2870 return snprintf(buf, PAGE_SIZE, "%d\n",
2871 (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002872}
2873
2874static struct device_attribute dev_attr_hotkey_radio_sw =
2875 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2876
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002877static void hotkey_radio_sw_notify_change(void)
2878{
2879 if (tp_features.hotkey_wlsw)
2880 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2881 "hotkey_radio_sw");
2882}
2883
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002884/* sysfs hotkey tablet mode (pollable) --------------------------------- */
2885static ssize_t hotkey_tablet_mode_show(struct device *dev,
2886 struct device_attribute *attr,
2887 char *buf)
2888{
2889 int res, s;
2890 res = hotkey_get_tablet_mode(&s);
2891 if (res < 0)
2892 return res;
2893
2894 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2895}
2896
2897static struct device_attribute dev_attr_hotkey_tablet_mode =
2898 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2899
2900static void hotkey_tablet_mode_notify_change(void)
2901{
2902 if (tp_features.hotkey_tablet)
2903 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2904 "hotkey_tablet_mode");
2905}
2906
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002907/* sysfs hotkey report_mode -------------------------------------------- */
2908static ssize_t hotkey_report_mode_show(struct device *dev,
2909 struct device_attribute *attr,
2910 char *buf)
2911{
2912 return snprintf(buf, PAGE_SIZE, "%d\n",
2913 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2914}
2915
2916static struct device_attribute dev_attr_hotkey_report_mode =
2917 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2918
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002919/* sysfs wakeup reason (pollable) -------------------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002920static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2921 struct device_attribute *attr,
2922 char *buf)
2923{
2924 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2925}
2926
2927static struct device_attribute dev_attr_hotkey_wakeup_reason =
2928 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2929
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02002930static void hotkey_wakeup_reason_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002931{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002932 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2933 "wakeup_reason");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002934}
2935
2936/* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002937static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2938 struct device_attribute *attr,
2939 char *buf)
2940{
2941 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2942}
2943
2944static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2945 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2946 hotkey_wakeup_hotunplug_complete_show, NULL);
2947
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02002948static void hotkey_wakeup_hotunplug_complete_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002949{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002950 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2951 "wakeup_hotunplug_complete");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002952}
2953
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002954/* --------------------------------------------------------------------- */
2955
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002956static struct attribute *hotkey_attributes[] __initdata = {
2957 &dev_attr_hotkey_enable.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002958 &dev_attr_hotkey_bios_enabled.attr,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002959 &dev_attr_hotkey_bios_mask.attr,
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002960 &dev_attr_hotkey_report_mode.attr,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002961 &dev_attr_hotkey_wakeup_reason.attr,
2962 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002963 &dev_attr_hotkey_mask.attr,
2964 &dev_attr_hotkey_all_mask.attr,
2965 &dev_attr_hotkey_recommended_mask.attr,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002966#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002967 &dev_attr_hotkey_source_mask.attr,
2968 &dev_attr_hotkey_poll_freq.attr,
2969#endif
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002970};
2971
Johannes Berg19d337d2009-06-02 13:01:37 +02002972/*
2973 * Sync both the hw and sw blocking state of all switches
2974 */
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002975static void tpacpi_send_radiosw_update(void)
2976{
2977 int wlsw;
2978
Johannes Berg19d337d2009-06-02 13:01:37 +02002979 /*
2980 * We must sync all rfkill controllers *before* issuing any
2981 * rfkill input events, or we will race the rfkill core input
2982 * handler.
2983 *
2984 * tpacpi_inputdev_send_mutex works as a syncronization point
2985 * for the above.
2986 *
2987 * We optimize to avoid numerous calls to hotkey_get_wlsw.
2988 */
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002989
Johannes Berg19d337d2009-06-02 13:01:37 +02002990 wlsw = hotkey_get_wlsw();
2991
2992 /* Sync hw blocking state first if it is hw-blocked */
2993 if (wlsw == TPACPI_RFK_RADIO_OFF)
2994 tpacpi_rfk_update_hwblock_state(true);
2995
2996 /* Sync sw blocking state */
2997 tpacpi_rfk_update_swstate_all();
2998
2999 /* Sync hw blocking state last if it is hw-unblocked */
3000 if (wlsw == TPACPI_RFK_RADIO_ON)
3001 tpacpi_rfk_update_hwblock_state(false);
3002
3003 /* Issue rfkill input event for WLSW switch */
3004 if (!(wlsw < 0)) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003005 mutex_lock(&tpacpi_inputdev_send_mutex);
3006
3007 input_report_switch(tpacpi_inputdev,
Johannes Berg19d337d2009-06-02 13:01:37 +02003008 SW_RFKILL_ALL, (wlsw > 0));
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003009 input_sync(tpacpi_inputdev);
3010
3011 mutex_unlock(&tpacpi_inputdev_send_mutex);
3012 }
Johannes Berg19d337d2009-06-02 13:01:37 +02003013
3014 /*
3015 * this can be unconditional, as we will poll state again
3016 * if userspace uses the notify to read data
3017 */
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003018 hotkey_radio_sw_notify_change();
3019}
3020
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003021static void hotkey_exit(void)
3022{
3023#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003024 mutex_lock(&hotkey_mutex);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003025 hotkey_poll_stop_sync();
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003026 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003027#endif
3028
3029 if (hotkey_dev_attributes)
3030 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3031
3032 kfree(hotkey_keycode_map);
3033
Henrique de Moraes Holschuh4be73002009-09-20 14:09:23 -03003034 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003035 "restoring original HKEY status and mask\n");
3036 /* yes, there is a bitwise or below, we want the
3037 * functions to be called even if one of them fail */
3038 if (((tp_features.hotkey_mask &&
3039 hotkey_mask_set(hotkey_orig_mask)) |
3040 hotkey_status_set(false)) != 0)
Henrique de Moraes Holschuh4be73002009-09-20 14:09:23 -03003041 printk(TPACPI_ERR
3042 "failed to restore hot key mask "
3043 "to BIOS defaults\n");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003044}
3045
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003046static void __init hotkey_unmap(const unsigned int scancode)
3047{
3048 if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
3049 clear_bit(hotkey_keycode_map[scancode],
3050 tpacpi_inputdev->keybit);
3051 hotkey_keycode_map[scancode] = KEY_RESERVED;
3052 }
3053}
3054
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003055/*
3056 * HKEY quirks:
3057 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
3058 */
3059
3060#define TPACPI_HK_Q_INIMASK 0x0001
3061
3062static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
3063 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
3064 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
3065 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
3066 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
3067 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
3068 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
3069 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
3070 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
3071 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
3072 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
3073 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
3074 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
3075 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
3076 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
3077 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
3078 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
3079 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
3080 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
3081 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
3082};
3083
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003084static int __init hotkey_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003085{
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003086 /* Requirements for changing the default keymaps:
3087 *
3088 * 1. Many of the keys are mapped to KEY_RESERVED for very
3089 * good reasons. Do not change them unless you have deep
3090 * knowledge on the IBM and Lenovo ThinkPad firmware for
3091 * the various ThinkPad models. The driver behaves
3092 * differently for KEY_RESERVED: such keys have their
3093 * hot key mask *unset* in mask_recommended, and also
3094 * in the initial hot key mask programmed into the
3095 * firmware at driver load time, which means the firm-
3096 * ware may react very differently if you change them to
3097 * something else;
3098 *
3099 * 2. You must be subscribed to the linux-thinkpad and
3100 * ibm-acpi-devel mailing lists, and you should read the
3101 * list archives since 2007 if you want to change the
3102 * keymaps. This requirement exists so that you will
3103 * know the past history of problems with the thinkpad-
3104 * acpi driver keymaps, and also that you will be
3105 * listening to any bug reports;
3106 *
3107 * 3. Do not send thinkpad-acpi specific patches directly to
3108 * for merging, *ever*. Send them to the linux-acpi
3109 * mailinglist for comments. Merging is to be done only
3110 * through acpi-test and the ACPI maintainer.
3111 *
3112 * If the above is too much to ask, don't change the keymap.
3113 * Ask the thinkpad-acpi maintainer to do it, instead.
3114 */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003115 static u16 ibm_keycode_map[] __initdata = {
3116 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3117 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
3118 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3119 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003120
3121 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003122 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3123 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3124 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003125
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003126 /* brightness: firmware always reacts to them */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003127 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003128 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003129
3130 /* Thinklight: firmware always react to it */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003131 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003132
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003133 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3134 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003135
3136 /* Volume: firmware always react to it and reprograms
3137 * the built-in *extra* mixer. Never map it to control
3138 * another mixer by default. */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003139 KEY_RESERVED, /* 0x14: VOLUME UP */
3140 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3141 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003142
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003143 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003144
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003145 /* (assignments unknown, please report if found) */
3146 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3147 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3148 };
3149 static u16 lenovo_keycode_map[] __initdata = {
3150 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3151 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
3152 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3153 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003154
3155 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003156 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3157 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3158 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003159
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003160 /* These should be enabled --only-- when ACPI video
3161 * is disabled (i.e. in "vendor" mode), and are handled
3162 * in a special way by the init code */
3163 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
3164 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003165
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003166 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003167
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003168 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3169 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003170
3171 /* Volume: z60/z61, T60 (BIOS version?): firmware always
3172 * react to it and reprograms the built-in *extra* mixer.
3173 * Never map it to control another mixer by default.
3174 *
3175 * T60?, T61, R60?, R61: firmware and EC tries to send
3176 * these over the regular keyboard, so these are no-ops,
3177 * but there are still weird bugs re. MUTE, so do not
3178 * change unless you get test reports from all Lenovo
3179 * models. May cause the BIOS to interfere with the
3180 * HDA mixer.
3181 */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003182 KEY_RESERVED, /* 0x14: VOLUME UP */
3183 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3184 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003185
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003186 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003187
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003188 /* (assignments unknown, please report if found) */
3189 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3190 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3191 };
3192
3193#define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
3194#define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
3195#define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
3196
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003197 int res, i;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003198 int status;
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03003199 int hkeyv;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003200 bool radiosw_state = false;
3201 bool tabletsw_state = false;
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03003202
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003203 unsigned long quirks;
3204
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003205 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3206 "initializing hotkey subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003207
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003208 BUG_ON(!tpacpi_inputdev);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003209 BUG_ON(tpacpi_inputdev->open != NULL ||
3210 tpacpi_inputdev->close != NULL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003211
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003212 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003213 mutex_init(&hotkey_mutex);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003214
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003215#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3216 mutex_init(&hotkey_thread_mutex);
3217 mutex_init(&hotkey_thread_data_mutex);
3218#endif
3219
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003220 /* hotkey not supported on 570 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003221 tp_features.hotkey = hkey_handle != NULL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003222
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003223 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3224 "hotkeys are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003225 str_supported(tp_features.hotkey));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003226
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003227 if (!tp_features.hotkey)
3228 return 1;
3229
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003230 quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
3231 ARRAY_SIZE(tpacpi_hotkey_qtable));
3232
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03003233 tpacpi_disable_brightness_delay();
3234
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003235 /* MUST have enough space for all attributes to be added to
3236 * hotkey_dev_attributes */
3237 hotkey_dev_attributes = create_attr_set(
3238 ARRAY_SIZE(hotkey_attributes) + 2,
3239 NULL);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003240 if (!hotkey_dev_attributes)
3241 return -ENOMEM;
3242 res = add_many_to_attr_set(hotkey_dev_attributes,
3243 hotkey_attributes,
3244 ARRAY_SIZE(hotkey_attributes));
3245 if (res)
3246 goto err_exit;
3247
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003248 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003249 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
3250 for HKEY interface version 0x100 */
3251 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
3252 if ((hkeyv >> 8) != 1) {
3253 printk(TPACPI_ERR "unknown version of the "
3254 "HKEY interface: 0x%x\n", hkeyv);
3255 printk(TPACPI_ERR "please report this to %s\n",
3256 TPACPI_MAIL);
3257 } else {
3258 /*
3259 * MHKV 0x100 in A31, R40, R40e,
3260 * T4x, X31, and later
3261 */
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003262 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3263 "firmware HKEY interface version: 0x%x\n",
3264 hkeyv);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003265
3266 /* Paranoia check AND init hotkey_all_mask */
3267 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3268 "MHKA", "qd")) {
3269 printk(TPACPI_ERR
3270 "missing MHKA handler, "
3271 "please report this to %s\n",
3272 TPACPI_MAIL);
3273 /* Fallback: pre-init for FN+F3,F4,F12 */
3274 hotkey_all_mask = 0x080cU;
3275 } else {
3276 tp_features.hotkey_mask = 1;
3277 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003278 }
3279 }
3280
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003281 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3282 "hotkey masks are %s\n",
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003283 str_supported(tp_features.hotkey_mask));
3284
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003285 /* Init hotkey_all_mask if not initialized yet */
3286 if (!tp_features.hotkey_mask && !hotkey_all_mask &&
3287 (quirks & TPACPI_HK_Q_INIMASK))
3288 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003289
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003290 /* Init hotkey_acpi_mask and hotkey_orig_mask */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003291 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003292 /* hotkey_source_mask *must* be zero for
3293 * the first hotkey_mask_get to return hotkey_orig_mask */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003294 res = hotkey_mask_get();
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003295 if (res)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003296 goto err_exit;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003297
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003298 hotkey_orig_mask = hotkey_acpi_mask;
3299 } else {
3300 hotkey_orig_mask = hotkey_all_mask;
3301 hotkey_acpi_mask = hotkey_all_mask;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003302 }
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003303
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003304#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3305 if (dbg_wlswemul) {
3306 tp_features.hotkey_wlsw = 1;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003307 radiosw_state = !!tpacpi_wlsw_emulstate;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003308 printk(TPACPI_INFO
3309 "radio switch emulation enabled\n");
3310 } else
3311#endif
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003312 /* Not all thinkpads have a hardware radio switch */
3313 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
3314 tp_features.hotkey_wlsw = 1;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003315 radiosw_state = !!status;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003316 printk(TPACPI_INFO
3317 "radio switch found; radios are %s\n",
3318 enabled(status, 0));
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003319 }
3320 if (tp_features.hotkey_wlsw)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003321 res = add_to_attr_set(hotkey_dev_attributes,
3322 &dev_attr_hotkey_radio_sw.attr);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003323
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003324 /* For X41t, X60t, X61t Tablets... */
3325 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
3326 tp_features.hotkey_tablet = 1;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003327 tabletsw_state = !!(status & TP_HOTKEY_TABLET_MASK);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003328 printk(TPACPI_INFO
3329 "possible tablet mode switch found; "
3330 "ThinkPad in %s mode\n",
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003331 (tabletsw_state) ? "tablet" : "laptop");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003332 res = add_to_attr_set(hotkey_dev_attributes,
3333 &dev_attr_hotkey_tablet_mode.attr);
3334 }
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02003335
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003336 if (!res)
3337 res = register_attr_set_with_sysfs(
3338 hotkey_dev_attributes,
3339 &tpacpi_pdev->dev.kobj);
3340 if (res)
3341 goto err_exit;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003342
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003343 /* Set up key map */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003344
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003345 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
3346 GFP_KERNEL);
3347 if (!hotkey_keycode_map) {
3348 printk(TPACPI_ERR
3349 "failed to allocate memory for key map\n");
3350 res = -ENOMEM;
3351 goto err_exit;
3352 }
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003353
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003354 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003355 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003356 "using Lenovo default hot key map\n");
3357 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
3358 TPACPI_HOTKEY_MAP_SIZE);
3359 } else {
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003360 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003361 "using IBM default hot key map\n");
3362 memcpy(hotkey_keycode_map, &ibm_keycode_map,
3363 TPACPI_HOTKEY_MAP_SIZE);
3364 }
3365
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003366 input_set_capability(tpacpi_inputdev, EV_MSC, MSC_SCAN);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003367 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
3368 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3369 tpacpi_inputdev->keycode = hotkey_keycode_map;
3370 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3371 if (hotkey_keycode_map[i] != KEY_RESERVED) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003372 input_set_capability(tpacpi_inputdev, EV_KEY,
3373 hotkey_keycode_map[i]);
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003374 } else {
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003375 if (i < sizeof(hotkey_reserved_mask)*8)
3376 hotkey_reserved_mask |= 1 << i;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003377 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003378 }
3379
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003380 if (tp_features.hotkey_wlsw) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003381 input_set_capability(tpacpi_inputdev, EV_SW, SW_RFKILL_ALL);
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003382 input_report_switch(tpacpi_inputdev,
3383 SW_RFKILL_ALL, radiosw_state);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003384 }
3385 if (tp_features.hotkey_tablet) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003386 input_set_capability(tpacpi_inputdev, EV_SW, SW_TABLET_MODE);
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003387 input_report_switch(tpacpi_inputdev,
3388 SW_TABLET_MODE, tabletsw_state);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003389 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003390
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003391 /* Do not issue duplicate brightness change events to
3392 * userspace */
3393 if (!tp_features.bright_acpimode)
3394 /* update bright_acpimode... */
3395 tpacpi_check_std_acpi_brightness_support();
3396
Henrique de Moraes Holschuh8bf3d4c2009-05-30 13:25:09 -03003397 if (tp_features.bright_acpimode && acpi_video_backlight_support()) {
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003398 printk(TPACPI_INFO
3399 "This ThinkPad has standard ACPI backlight "
3400 "brightness control, supported by the ACPI "
3401 "video driver\n");
3402 printk(TPACPI_NOTICE
3403 "Disabling thinkpad-acpi brightness events "
3404 "by default...\n");
3405
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003406 /* Disable brightness up/down on Lenovo thinkpads when
3407 * ACPI is handling them, otherwise it is plain impossible
3408 * for userspace to do something even remotely sane */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003409 hotkey_reserved_mask |=
3410 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3411 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003412 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
3413 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003414 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003415
Henrique de Moraes Holschuh230d8cf252009-09-12 15:22:17 -03003416#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003417 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3418 & ~hotkey_all_mask
3419 & ~hotkey_reserved_mask;
Henrique de Moraes Holschuh230d8cf252009-09-12 15:22:17 -03003420
3421 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3422 "hotkey source mask 0x%08x, polling freq %u\n",
3423 hotkey_source_mask, hotkey_poll_freq);
3424#endif
3425
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003426 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3427 "enabling firmware HKEY event interface...\n");
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003428 res = hotkey_status_set(true);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003429 if (res) {
3430 hotkey_exit();
3431 return res;
3432 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003433 res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
3434 | hotkey_driver_mask)
3435 & ~hotkey_source_mask);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003436 if (res < 0 && res != -ENXIO) {
3437 hotkey_exit();
3438 return res;
3439 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003440 hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
3441 & ~hotkey_reserved_mask;
3442 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3443 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3444 hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003445
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003446 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3447 "legacy ibm/hotkey event reporting over procfs %s\n",
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003448 (hotkey_report_mode < 2) ?
3449 "enabled" : "disabled");
3450
3451 tpacpi_inputdev->open = &hotkey_inputdev_open;
3452 tpacpi_inputdev->close = &hotkey_inputdev_close;
3453
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003454 hotkey_poll_setup_safe(true);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003455
3456 return 0;
3457
3458err_exit:
3459 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3460 hotkey_dev_attributes = NULL;
3461
3462 return (res < 0)? res : 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003463}
3464
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003465static bool hotkey_notify_hotkey(const u32 hkey,
3466 bool *send_acpi_ev,
3467 bool *ignore_acpi_ev)
3468{
3469 /* 0x1000-0x1FFF: key presses */
3470 unsigned int scancode = hkey & 0xfff;
3471 *send_acpi_ev = true;
3472 *ignore_acpi_ev = false;
3473
3474 if (scancode > 0 && scancode < 0x21) {
3475 scancode--;
3476 if (!(hotkey_source_mask & (1 << scancode))) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003477 tpacpi_input_send_key_masked(scancode);
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003478 *send_acpi_ev = false;
3479 } else {
3480 *ignore_acpi_ev = true;
3481 }
3482 return true;
3483 }
3484 return false;
3485}
3486
3487static bool hotkey_notify_wakeup(const u32 hkey,
3488 bool *send_acpi_ev,
3489 bool *ignore_acpi_ev)
3490{
3491 /* 0x2000-0x2FFF: Wakeup reason */
3492 *send_acpi_ev = true;
3493 *ignore_acpi_ev = false;
3494
3495 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003496 case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */
3497 case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003498 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3499 *ignore_acpi_ev = true;
3500 break;
3501
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003502 case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */
3503 case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003504 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3505 *ignore_acpi_ev = true;
3506 break;
3507
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003508 case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */
3509 case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003510 printk(TPACPI_ALERT
3511 "EMERGENCY WAKEUP: battery almost empty\n");
3512 /* how to auto-heal: */
3513 /* 2313: woke up from S3, go to S4/S5 */
3514 /* 2413: woke up from S4, go to S5 */
3515 break;
3516
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003517 default:
3518 return false;
3519 }
3520
3521 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
3522 printk(TPACPI_INFO
3523 "woke up due to a hot-unplug "
3524 "request...\n");
3525 hotkey_wakeup_reason_notify_change();
3526 }
3527 return true;
3528}
3529
3530static bool hotkey_notify_usrevent(const u32 hkey,
3531 bool *send_acpi_ev,
3532 bool *ignore_acpi_ev)
3533{
3534 /* 0x5000-0x5FFF: human interface helpers */
3535 *send_acpi_ev = true;
3536 *ignore_acpi_ev = false;
3537
3538 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003539 case TP_HKEY_EV_PEN_INSERTED: /* X61t: tablet pen inserted into bay */
3540 case TP_HKEY_EV_PEN_REMOVED: /* X61t: tablet pen removed from bay */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003541 return true;
3542
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003543 case TP_HKEY_EV_TABLET_TABLET: /* X41t-X61t: tablet mode */
3544 case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003545 tpacpi_input_send_tabletsw();
3546 hotkey_tablet_mode_notify_change();
3547 *send_acpi_ev = false;
3548 return true;
3549
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003550 case TP_HKEY_EV_LID_CLOSE: /* Lid closed */
3551 case TP_HKEY_EV_LID_OPEN: /* Lid opened */
3552 case TP_HKEY_EV_BRGHT_CHANGED: /* brightness changed */
Henrique de Moraes Holschuh176dd982009-09-20 14:09:24 -03003553 /* do not propagate these events */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003554 *ignore_acpi_ev = true;
3555 return true;
3556
3557 default:
3558 return false;
3559 }
3560}
3561
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003562static void thermal_dump_all_sensors(void);
3563
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003564static bool hotkey_notify_thermal(const u32 hkey,
3565 bool *send_acpi_ev,
3566 bool *ignore_acpi_ev)
3567{
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003568 bool known = true;
3569
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003570 /* 0x6000-0x6FFF: thermal alarms */
3571 *send_acpi_ev = true;
3572 *ignore_acpi_ev = false;
3573
3574 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003575 case TP_HKEY_EV_THM_TABLE_CHANGED:
Henrique de Moraes Holschuh54926ce2009-01-11 03:01:09 -02003576 printk(TPACPI_INFO
3577 "EC reports that Thermal Table has changed\n");
3578 /* recommended action: do nothing, we don't have
3579 * Lenovo ATM information */
3580 return true;
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003581 case TP_HKEY_EV_ALARM_BAT_HOT:
3582 printk(TPACPI_CRIT
3583 "THERMAL ALARM: battery is too hot!\n");
3584 /* recommended action: warn user through gui */
3585 break;
3586 case TP_HKEY_EV_ALARM_BAT_XHOT:
3587 printk(TPACPI_ALERT
3588 "THERMAL EMERGENCY: battery is extremely hot!\n");
3589 /* recommended action: immediate sleep/hibernate */
3590 break;
3591 case TP_HKEY_EV_ALARM_SENSOR_HOT:
3592 printk(TPACPI_CRIT
3593 "THERMAL ALARM: "
3594 "a sensor reports something is too hot!\n");
3595 /* recommended action: warn user through gui, that */
3596 /* some internal component is too hot */
3597 break;
3598 case TP_HKEY_EV_ALARM_SENSOR_XHOT:
3599 printk(TPACPI_ALERT
3600 "THERMAL EMERGENCY: "
3601 "a sensor reports something is extremely hot!\n");
3602 /* recommended action: immediate sleep/hibernate */
3603 break;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003604 default:
3605 printk(TPACPI_ALERT
3606 "THERMAL ALERT: unknown thermal alarm received\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003607 known = false;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003608 }
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003609
3610 thermal_dump_all_sensors();
3611
3612 return known;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003613}
3614
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003615static void hotkey_notify(struct ibm_struct *ibm, u32 event)
3616{
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003617 u32 hkey;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003618 bool send_acpi_ev;
3619 bool ignore_acpi_ev;
3620 bool known_ev;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003621
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003622 if (event != 0x80) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003623 printk(TPACPI_ERR
3624 "unknown HKEY notification event %d\n", event);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003625 /* forward it to userspace, maybe it knows how to handle it */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003626 acpi_bus_generate_netlink_event(
3627 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02003628 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003629 event, 0);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003630 return;
3631 }
3632
3633 while (1) {
3634 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003635 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003636 return;
3637 }
3638
3639 if (hkey == 0) {
3640 /* queue empty */
3641 return;
3642 }
3643
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003644 send_acpi_ev = true;
3645 ignore_acpi_ev = false;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003646
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003647 switch (hkey >> 12) {
3648 case 1:
3649 /* 0x1000-0x1FFF: key presses */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003650 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
3651 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003652 break;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003653 case 2:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003654 /* 0x2000-0x2FFF: Wakeup reason */
3655 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
3656 &ignore_acpi_ev);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003657 break;
3658 case 3:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003659 /* 0x3000-0x3FFF: bay-related wakeups */
Henrique de Moraes Holschuhbf8b29c2010-02-25 21:28:56 -03003660 switch (hkey) {
3661 case TP_HKEY_EV_BAYEJ_ACK:
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003662 hotkey_autosleep_ack = 1;
3663 printk(TPACPI_INFO
3664 "bay ejected\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003665 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003666 known_ev = true;
Henrique de Moraes Holschuhbf8b29c2010-02-25 21:28:56 -03003667 break;
3668 case TP_HKEY_EV_OPTDRV_EJ:
3669 /* FIXME: kick libata if SATA link offline */
3670 known_ev = true;
3671 break;
3672 default:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003673 known_ev = false;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003674 }
3675 break;
3676 case 4:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003677 /* 0x4000-0x4FFF: dock-related wakeups */
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003678 if (hkey == TP_HKEY_EV_UNDOCK_ACK) {
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003679 hotkey_autosleep_ack = 1;
3680 printk(TPACPI_INFO
3681 "undocked\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003682 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003683 known_ev = true;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003684 } else {
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003685 known_ev = false;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003686 }
3687 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003688 case 5:
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02003689 /* 0x5000-0x5FFF: human interface helpers */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003690 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
3691 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003692 break;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003693 case 6:
3694 /* 0x6000-0x6FFF: thermal alarms */
3695 known_ev = hotkey_notify_thermal(hkey, &send_acpi_ev,
3696 &ignore_acpi_ev);
3697 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003698 case 7:
3699 /* 0x7000-0x7FFF: misc */
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003700 if (tp_features.hotkey_wlsw &&
3701 hkey == TP_HKEY_EV_RFKILL_CHANGED) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003702 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02003703 send_acpi_ev = 0;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003704 known_ev = true;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003705 break;
3706 }
3707 /* fallthrough to default */
3708 default:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003709 known_ev = false;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02003710 }
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003711 if (!known_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003712 printk(TPACPI_NOTICE
3713 "unhandled HKEY event 0x%04x\n", hkey);
Henrique de Moraes Holschuhcb429352009-01-11 03:01:07 -02003714 printk(TPACPI_NOTICE
3715 "please report the conditions when this "
3716 "event happened to %s\n", TPACPI_MAIL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003717 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003718
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003719 /* Legacy events */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003720 if (!ignore_acpi_ev &&
3721 (send_acpi_ev || hotkey_report_mode < 2)) {
3722 acpi_bus_generate_proc_event(ibm->acpi->device,
3723 event, hkey);
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03003724 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003725
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003726 /* netlink events */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03003727 if (!ignore_acpi_ev && send_acpi_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003728 acpi_bus_generate_netlink_event(
3729 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02003730 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003731 event, hkey);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003732 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003733 }
3734}
3735
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003736static void hotkey_suspend(pm_message_t state)
3737{
3738 /* Do these on suspend, we get the events on early resume! */
3739 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
3740 hotkey_autosleep_ack = 0;
3741}
3742
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03003743static void hotkey_resume(void)
3744{
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03003745 tpacpi_disable_brightness_delay();
3746
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003747 if (hotkey_status_set(true) < 0 ||
3748 hotkey_mask_set(hotkey_acpi_mask) < 0)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003749 printk(TPACPI_ERR
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003750 "error while attempting to reset the event "
3751 "firmware interface\n");
3752
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003753 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02003754 hotkey_tablet_mode_notify_change();
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003755 hotkey_wakeup_reason_notify_change();
3756 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003757 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03003758}
3759
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003760/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003761static int hotkey_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03003763 int res, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003765 if (!tp_features.hotkey) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003766 seq_printf(m, "status:\t\tnot supported\n");
3767 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003768 }
3769
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02003770 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02003771 return -ERESTARTSYS;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02003772 res = hotkey_status_get(&status);
3773 if (!res)
3774 res = hotkey_mask_get();
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003775 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03003776 if (res)
3777 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003779 seq_printf(m, "status:\t\t%s\n", enabled(status, 0));
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003780 if (hotkey_all_mask) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003781 seq_printf(m, "mask:\t\t0x%08x\n", hotkey_user_mask);
3782 seq_printf(m, "commands:\tenable, disable, reset, <mask>\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783 } else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003784 seq_printf(m, "mask:\t\tnot supported\n");
3785 seq_printf(m, "commands:\tenable, disable, reset\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786 }
3787
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003788 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789}
3790
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003791static void hotkey_enabledisable_warn(bool enable)
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003792{
3793 tpacpi_log_usertask("procfs hotkey enable/disable");
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003794 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
3795 TPACPI_WARN
3796 "hotkey enable/disable functionality has been "
3797 "removed from the driver. Hotkeys are always "
3798 "enabled\n"))
3799 printk(TPACPI_ERR
3800 "Please remove the hotkey=enable module "
3801 "parameter, it is deprecated. Hotkeys are always "
3802 "enabled\n");
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003803}
3804
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003805static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003807 int res;
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03003808 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003811 if (!tp_features.hotkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812 return -ENODEV;
3813
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02003814 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02003815 return -ERESTARTSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003816
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003817 mask = hotkey_user_mask;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003818
3819 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820 while ((cmd = next_cmd(&buf))) {
3821 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003822 hotkey_enabledisable_warn(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003824 hotkey_enabledisable_warn(0);
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003825 res = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826 } else if (strlencmp(cmd, "reset") == 0) {
Henrique de Moraes Holschuh20c9aa42009-09-12 15:22:16 -03003827 mask = (hotkey_all_mask | hotkey_source_mask)
3828 & ~hotkey_reserved_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
3830 /* mask set */
3831 } else if (sscanf(cmd, "%x", &mask) == 1) {
3832 /* mask set */
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003833 } else {
3834 res = -EINVAL;
3835 goto errexit;
3836 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837 }
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003838
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003839 if (!res) {
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003840 tpacpi_disclose_usertask("procfs hotkey",
3841 "set mask to 0x%08x\n", mask);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003842 res = hotkey_user_mask_set(mask);
3843 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003845errexit:
3846 mutex_unlock(&hotkey_mutex);
3847 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003848}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003850static const struct acpi_device_id ibm_htk_device_ids[] = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003851 {TPACPI_ACPI_HKEY_HID, 0},
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003852 {"", 0},
3853};
3854
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003855static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003856 .hid = ibm_htk_device_ids,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003857 .notify = hotkey_notify,
3858 .handle = &hkey_handle,
3859 .type = ACPI_DEVICE_NOTIFY,
3860};
3861
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003862static struct ibm_struct hotkey_driver_data = {
3863 .name = "hotkey",
3864 .read = hotkey_read,
3865 .write = hotkey_write,
3866 .exit = hotkey_exit,
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03003867 .resume = hotkey_resume,
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003868 .suspend = hotkey_suspend,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003869 .acpi = &ibm_hotkey_acpidriver,
3870};
3871
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003872/*************************************************************************
3873 * Bluetooth subdriver
3874 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003876enum {
3877 /* ACPI GBDC/SBDC bits */
3878 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
3879 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003880 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03003881 0 = disable, 1 = enable */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003882};
3883
3884enum {
3885 /* ACPI \BLTH commands */
3886 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
3887 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
3888 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
3889 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
3890 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003891};
3892
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003893#define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
3894
Johannes Berg19d337d2009-06-02 13:01:37 +02003895static int bluetooth_get_status(void)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003896{
3897 int status;
3898
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003899#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3900 if (dbg_bluetoothemul)
3901 return (tpacpi_bluetooth_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02003902 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003903#endif
3904
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003905 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
3906 return -EIO;
3907
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003908 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02003909 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003910}
3911
Johannes Berg19d337d2009-06-02 13:01:37 +02003912static int bluetooth_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003913{
3914 int status;
3915
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003916 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02003917 "will attempt to %s bluetooth\n",
3918 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003919
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003920#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3921 if (dbg_bluetoothemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02003922 tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003923 return 0;
3924 }
3925#endif
3926
Johannes Berg19d337d2009-06-02 13:01:37 +02003927 if (state == TPACPI_RFK_RADIO_ON)
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03003928 status = TP_ACPI_BLUETOOTH_RADIOSSW
3929 | TP_ACPI_BLUETOOTH_RESUMECTRL;
3930 else
3931 status = 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02003932
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003933 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
3934 return -EIO;
3935
3936 return 0;
3937}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003938
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003939/* sysfs bluetooth enable ---------------------------------------------- */
3940static ssize_t bluetooth_enable_show(struct device *dev,
3941 struct device_attribute *attr,
3942 char *buf)
3943{
Johannes Berg19d337d2009-06-02 13:01:37 +02003944 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID,
3945 attr, buf);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003946}
3947
3948static ssize_t bluetooth_enable_store(struct device *dev,
3949 struct device_attribute *attr,
3950 const char *buf, size_t count)
3951{
Johannes Berg19d337d2009-06-02 13:01:37 +02003952 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID,
3953 attr, buf, count);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003954}
3955
3956static struct device_attribute dev_attr_bluetooth_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03003957 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003958 bluetooth_enable_show, bluetooth_enable_store);
3959
3960/* --------------------------------------------------------------------- */
3961
3962static struct attribute *bluetooth_attributes[] = {
3963 &dev_attr_bluetooth_enable.attr,
3964 NULL
3965};
3966
3967static const struct attribute_group bluetooth_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003968 .attrs = bluetooth_attributes,
3969};
3970
Johannes Berg19d337d2009-06-02 13:01:37 +02003971static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = {
3972 .get_status = bluetooth_get_status,
3973 .set_status = bluetooth_set_status,
3974};
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003975
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003976static void bluetooth_shutdown(void)
3977{
3978 /* Order firmware to save current state to NVRAM */
3979 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3980 TP_ACPI_BLTH_SAVE_STATE))
3981 printk(TPACPI_NOTICE
3982 "failed to save bluetooth state to NVRAM\n");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003983 else
3984 vdbg_printk(TPACPI_DBG_RFKILL,
3985 "bluestooth state saved to NVRAM\n");
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003986}
3987
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003988static void bluetooth_exit(void)
3989{
3990 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3991 &bluetooth_attr_group);
Johannes Berg19d337d2009-06-02 13:01:37 +02003992
3993 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
3994
3995 bluetooth_shutdown();
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003996}
3997
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003998static int __init bluetooth_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003999{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004000 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004001 int status = 0;
4002
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004003 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4004 "initializing bluetooth subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004005
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004006 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004007
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004008 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4009 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004010 tp_features.bluetooth = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004011 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004013 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4014 "bluetooth is %s, status 0x%02x\n",
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004015 str_supported(tp_features.bluetooth),
4016 status);
4017
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004018#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4019 if (dbg_bluetoothemul) {
4020 tp_features.bluetooth = 1;
4021 printk(TPACPI_INFO
4022 "bluetooth switch emulation enabled\n");
4023 } else
4024#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004025 if (tp_features.bluetooth &&
4026 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
4027 /* no bluetooth hardware present in system */
4028 tp_features.bluetooth = 0;
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004029 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004030 "bluetooth hardware not installed\n");
4031 }
4032
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004033 if (!tp_features.bluetooth)
4034 return 1;
4035
Johannes Berg19d337d2009-06-02 13:01:37 +02004036 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
4037 &bluetooth_tprfk_ops,
4038 RFKILL_TYPE_BLUETOOTH,
4039 TPACPI_RFK_BLUETOOTH_SW_NAME,
4040 true);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004041 if (res)
4042 return res;
4043
Johannes Berg19d337d2009-06-02 13:01:37 +02004044 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4045 &bluetooth_attr_group);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004046 if (res) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004047 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004048 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004049 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004050
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004051 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052}
4053
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004054/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004055static int bluetooth_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056{
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004057 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058}
4059
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004060static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061{
Johannes Berg19d337d2009-06-02 13:01:37 +02004062 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063}
4064
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004065static struct ibm_struct bluetooth_driver_data = {
4066 .name = "bluetooth",
4067 .read = bluetooth_read,
4068 .write = bluetooth_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004069 .exit = bluetooth_exit,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004070 .shutdown = bluetooth_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004071};
4072
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004073/*************************************************************************
4074 * Wan subdriver
4075 */
4076
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004077enum {
4078 /* ACPI GWAN/SWAN bits */
4079 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
4080 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02004081 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03004082 0 = disable, 1 = enable */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004083};
4084
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004085#define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
4086
Johannes Berg19d337d2009-06-02 13:01:37 +02004087static int wan_get_status(void)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004088{
4089 int status;
4090
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004091#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4092 if (dbg_wwanemul)
4093 return (tpacpi_wwan_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004094 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004095#endif
4096
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004097 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
4098 return -EIO;
4099
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004100 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004101 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004102}
4103
Johannes Berg19d337d2009-06-02 13:01:37 +02004104static int wan_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004105{
4106 int status;
4107
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004108 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02004109 "will attempt to %s wwan\n",
4110 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004111
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004112#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4113 if (dbg_wwanemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004114 tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004115 return 0;
4116 }
4117#endif
4118
Johannes Berg19d337d2009-06-02 13:01:37 +02004119 if (state == TPACPI_RFK_RADIO_ON)
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03004120 status = TP_ACPI_WANCARD_RADIOSSW
4121 | TP_ACPI_WANCARD_RESUMECTRL;
4122 else
4123 status = 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02004124
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004125 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
4126 return -EIO;
4127
4128 return 0;
4129}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004130
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004131/* sysfs wan enable ---------------------------------------------------- */
4132static ssize_t wan_enable_show(struct device *dev,
4133 struct device_attribute *attr,
4134 char *buf)
4135{
Johannes Berg19d337d2009-06-02 13:01:37 +02004136 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
4137 attr, buf);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004138}
4139
4140static ssize_t wan_enable_store(struct device *dev,
4141 struct device_attribute *attr,
4142 const char *buf, size_t count)
4143{
Johannes Berg19d337d2009-06-02 13:01:37 +02004144 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
4145 attr, buf, count);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004146}
4147
4148static struct device_attribute dev_attr_wan_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03004149 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004150 wan_enable_show, wan_enable_store);
4151
4152/* --------------------------------------------------------------------- */
4153
4154static struct attribute *wan_attributes[] = {
4155 &dev_attr_wan_enable.attr,
4156 NULL
4157};
4158
4159static const struct attribute_group wan_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004160 .attrs = wan_attributes,
4161};
4162
Johannes Berg19d337d2009-06-02 13:01:37 +02004163static const struct tpacpi_rfk_ops wan_tprfk_ops = {
4164 .get_status = wan_get_status,
4165 .set_status = wan_set_status,
4166};
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004167
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004168static void wan_shutdown(void)
4169{
4170 /* Order firmware to save current state to NVRAM */
4171 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
4172 TP_ACPI_WGSV_SAVE_STATE))
4173 printk(TPACPI_NOTICE
4174 "failed to save WWAN state to NVRAM\n");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004175 else
4176 vdbg_printk(TPACPI_DBG_RFKILL,
4177 "WWAN state saved to NVRAM\n");
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004178}
4179
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004180static void wan_exit(void)
4181{
4182 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4183 &wan_attr_group);
Johannes Berg19d337d2009-06-02 13:01:37 +02004184
4185 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4186
4187 wan_shutdown();
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004188}
4189
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004190static int __init wan_init(struct ibm_init_struct *iibm)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004191{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004192 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004193 int status = 0;
4194
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004195 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4196 "initializing wan subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004197
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004198 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004199
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004200 tp_features.wan = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004201 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004202
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004203 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4204 "wan is %s, status 0x%02x\n",
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004205 str_supported(tp_features.wan),
4206 status);
4207
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004208#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4209 if (dbg_wwanemul) {
4210 tp_features.wan = 1;
4211 printk(TPACPI_INFO
4212 "wwan switch emulation enabled\n");
4213 } else
4214#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004215 if (tp_features.wan &&
4216 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
4217 /* no wan hardware present in system */
4218 tp_features.wan = 0;
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004219 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004220 "wan hardware not installed\n");
4221 }
4222
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004223 if (!tp_features.wan)
4224 return 1;
4225
Johannes Berg19d337d2009-06-02 13:01:37 +02004226 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
4227 &wan_tprfk_ops,
4228 RFKILL_TYPE_WWAN,
4229 TPACPI_RFK_WWAN_SW_NAME,
4230 true);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004231 if (res)
4232 return res;
4233
Johannes Berg19d337d2009-06-02 13:01:37 +02004234 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4235 &wan_attr_group);
4236
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004237 if (res) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004238 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004239 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004240 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004241
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004242 return 0;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004243}
4244
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004245/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004246static int wan_read(struct seq_file *m)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004247{
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004248 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, m);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004249}
4250
4251static int wan_write(char *buf)
4252{
Johannes Berg19d337d2009-06-02 13:01:37 +02004253 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004254}
4255
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004256static struct ibm_struct wan_driver_data = {
4257 .name = "wan",
4258 .read = wan_read,
4259 .write = wan_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004260 .exit = wan_exit,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004261 .shutdown = wan_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004262};
4263
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004264/*************************************************************************
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004265 * UWB subdriver
4266 */
4267
4268enum {
4269 /* ACPI GUWB/SUWB bits */
4270 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4271 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4272};
4273
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004274#define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4275
Johannes Berg19d337d2009-06-02 13:01:37 +02004276static int uwb_get_status(void)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004277{
4278 int status;
4279
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004280#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4281 if (dbg_uwbemul)
4282 return (tpacpi_uwb_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004283 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004284#endif
4285
4286 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4287 return -EIO;
4288
4289 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004290 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004291}
4292
Johannes Berg19d337d2009-06-02 13:01:37 +02004293static int uwb_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004294{
4295 int status;
4296
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004297 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02004298 "will attempt to %s UWB\n",
4299 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004300
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004301#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4302 if (dbg_uwbemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004303 tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004304 return 0;
4305 }
4306#endif
4307
Johannes Berg19d337d2009-06-02 13:01:37 +02004308 if (state == TPACPI_RFK_RADIO_ON)
4309 status = TP_ACPI_UWB_RADIOSSW;
4310 else
4311 status = 0;
4312
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004313 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4314 return -EIO;
4315
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004316 return 0;
4317}
4318
4319/* --------------------------------------------------------------------- */
4320
Johannes Berg19d337d2009-06-02 13:01:37 +02004321static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
4322 .get_status = uwb_get_status,
4323 .set_status = uwb_set_status,
4324};
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004325
4326static void uwb_exit(void)
4327{
Johannes Berg19d337d2009-06-02 13:01:37 +02004328 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004329}
4330
4331static int __init uwb_init(struct ibm_init_struct *iibm)
4332{
4333 int res;
4334 int status = 0;
4335
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004336 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4337 "initializing uwb subdriver\n");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004338
4339 TPACPI_ACPIHANDLE_INIT(hkey);
4340
4341 tp_features.uwb = hkey_handle &&
4342 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4343
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004344 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4345 "uwb is %s, status 0x%02x\n",
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004346 str_supported(tp_features.uwb),
4347 status);
4348
4349#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4350 if (dbg_uwbemul) {
4351 tp_features.uwb = 1;
4352 printk(TPACPI_INFO
4353 "uwb switch emulation enabled\n");
4354 } else
4355#endif
4356 if (tp_features.uwb &&
4357 !(status & TP_ACPI_UWB_HWPRESENT)) {
4358 /* no uwb hardware present in system */
4359 tp_features.uwb = 0;
4360 dbg_printk(TPACPI_DBG_INIT,
4361 "uwb hardware not installed\n");
4362 }
4363
4364 if (!tp_features.uwb)
4365 return 1;
4366
4367 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
Johannes Berg19d337d2009-06-02 13:01:37 +02004368 &uwb_tprfk_ops,
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004369 RFKILL_TYPE_UWB,
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004370 TPACPI_RFK_UWB_SW_NAME,
Johannes Berg19d337d2009-06-02 13:01:37 +02004371 false);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004372 return res;
4373}
4374
4375static struct ibm_struct uwb_driver_data = {
4376 .name = "uwb",
4377 .exit = uwb_exit,
4378 .flags.experimental = 1,
4379};
4380
4381/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004382 * Video subdriver
4383 */
4384
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02004385#ifdef CONFIG_THINKPAD_ACPI_VIDEO
4386
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004387enum video_access_mode {
4388 TPACPI_VIDEO_NONE = 0,
4389 TPACPI_VIDEO_570, /* 570 */
4390 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4391 TPACPI_VIDEO_NEW, /* all others */
4392};
4393
4394enum { /* video status flags, based on VIDEO_570 */
4395 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4396 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4397 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4398};
4399
4400enum { /* TPACPI_VIDEO_570 constants */
4401 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4402 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4403 * video_status_flags */
4404 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4405 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4406};
4407
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02004408static enum video_access_mode video_supported;
4409static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004410
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004411static int video_autosw_get(void);
4412static int video_autosw_set(int enable);
4413
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004414TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004415
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004416static int __init video_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004418 int ivga;
4419
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004420 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4421
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004422 TPACPI_ACPIHANDLE_INIT(vid);
4423 TPACPI_ACPIHANDLE_INIT(vid2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004424
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004425 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4426 /* G41, assume IVGA doesn't change */
4427 vid_handle = vid2_handle;
4428
4429 if (!vid_handle)
4430 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004431 video_supported = TPACPI_VIDEO_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004432 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
4433 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004434 video_supported = TPACPI_VIDEO_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004435 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
4436 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004437 video_supported = TPACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004438 else
4439 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004440 video_supported = TPACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004442 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
4443 str_supported(video_supported != TPACPI_VIDEO_NONE),
4444 video_supported);
4445
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004446 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447}
4448
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004449static void video_exit(void)
4450{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004451 dbg_printk(TPACPI_DBG_EXIT,
4452 "restoring original video autoswitch mode\n");
4453 if (video_autosw_set(video_orig_autosw))
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004454 printk(TPACPI_ERR "error while trying to restore original "
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004455 "video autoswitch mode\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004456}
4457
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004458static int video_outputsw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459{
4460 int status = 0;
4461 int i;
4462
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004463 switch (video_supported) {
4464 case TPACPI_VIDEO_570:
4465 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
4466 TP_ACPI_VIDEO_570_PHSCMD))
4467 return -EIO;
4468 status = i & TP_ACPI_VIDEO_570_PHSMASK;
4469 break;
4470 case TPACPI_VIDEO_770:
4471 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4472 return -EIO;
4473 if (i)
4474 status |= TP_ACPI_VIDEO_S_LCD;
4475 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4476 return -EIO;
4477 if (i)
4478 status |= TP_ACPI_VIDEO_S_CRT;
4479 break;
4480 case TPACPI_VIDEO_NEW:
4481 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4482 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4483 return -EIO;
4484 if (i)
4485 status |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004487 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4488 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4489 return -EIO;
4490 if (i)
4491 status |= TP_ACPI_VIDEO_S_LCD;
4492 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4493 return -EIO;
4494 if (i)
4495 status |= TP_ACPI_VIDEO_S_DVI;
4496 break;
4497 default:
4498 return -ENOSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004499 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500
4501 return status;
4502}
4503
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004504static int video_outputsw_set(int status)
4505{
4506 int autosw;
4507 int res = 0;
4508
4509 switch (video_supported) {
4510 case TPACPI_VIDEO_570:
4511 res = acpi_evalf(NULL, NULL,
4512 "\\_SB.PHS2", "vdd",
4513 TP_ACPI_VIDEO_570_PHS2CMD,
4514 status | TP_ACPI_VIDEO_570_PHS2SET);
4515 break;
4516 case TPACPI_VIDEO_770:
4517 autosw = video_autosw_get();
4518 if (autosw < 0)
4519 return autosw;
4520
4521 res = video_autosw_set(1);
4522 if (res)
4523 return res;
4524 res = acpi_evalf(vid_handle, NULL,
4525 "ASWT", "vdd", status * 0x100, 0);
4526 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004527 printk(TPACPI_ERR
4528 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004529 return -EIO;
4530 }
4531 break;
4532 case TPACPI_VIDEO_NEW:
4533 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004534 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004535 break;
4536 default:
4537 return -ENOSYS;
4538 }
4539
4540 return (res)? 0 : -EIO;
4541}
4542
4543static int video_autosw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004545 int autosw = 0;
4546
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004547 switch (video_supported) {
4548 case TPACPI_VIDEO_570:
4549 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
4550 return -EIO;
4551 break;
4552 case TPACPI_VIDEO_770:
4553 case TPACPI_VIDEO_NEW:
4554 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4555 return -EIO;
4556 break;
4557 default:
4558 return -ENOSYS;
4559 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004560
4561 return autosw & 1;
4562}
4563
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004564static int video_autosw_set(int enable)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004565{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004566 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004567 return -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004568 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004569}
4570
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004571static int video_outputsw_cycle(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004572{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004573 int autosw = video_autosw_get();
4574 int res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004575
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004576 if (autosw < 0)
4577 return autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004578
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004579 switch (video_supported) {
4580 case TPACPI_VIDEO_570:
4581 res = video_autosw_set(1);
4582 if (res)
4583 return res;
4584 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4585 break;
4586 case TPACPI_VIDEO_770:
4587 case TPACPI_VIDEO_NEW:
4588 res = video_autosw_set(1);
4589 if (res)
4590 return res;
4591 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4592 break;
4593 default:
4594 return -ENOSYS;
4595 }
4596 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004597 printk(TPACPI_ERR
4598 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004599 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004600 }
4601
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004602 return (res)? 0 : -EIO;
4603}
4604
4605static int video_expand_toggle(void)
4606{
4607 switch (video_supported) {
4608 case TPACPI_VIDEO_570:
4609 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
4610 0 : -EIO;
4611 case TPACPI_VIDEO_770:
4612 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
4613 0 : -EIO;
4614 case TPACPI_VIDEO_NEW:
4615 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
4616 0 : -EIO;
4617 default:
4618 return -ENOSYS;
4619 }
4620 /* not reached */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004621}
4622
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004623static int video_read(struct seq_file *m)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004624{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004625 int status, autosw;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004626
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004627 if (video_supported == TPACPI_VIDEO_NONE) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004628 seq_printf(m, "status:\t\tnot supported\n");
4629 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004630 }
4631
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004632 status = video_outputsw_get();
4633 if (status < 0)
4634 return status;
4635
4636 autosw = video_autosw_get();
4637 if (autosw < 0)
4638 return autosw;
4639
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004640 seq_printf(m, "status:\t\tsupported\n");
4641 seq_printf(m, "lcd:\t\t%s\n", enabled(status, 0));
4642 seq_printf(m, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004643 if (video_supported == TPACPI_VIDEO_NEW)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004644 seq_printf(m, "dvi:\t\t%s\n", enabled(status, 3));
4645 seq_printf(m, "auto:\t\t%s\n", enabled(autosw, 0));
4646 seq_printf(m, "commands:\tlcd_enable, lcd_disable\n");
4647 seq_printf(m, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004648 if (video_supported == TPACPI_VIDEO_NEW)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004649 seq_printf(m, "commands:\tdvi_enable, dvi_disable\n");
4650 seq_printf(m, "commands:\tauto_enable, auto_disable\n");
4651 seq_printf(m, "commands:\tvideo_switch, expand_toggle\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004652
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004653 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004654}
4655
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004656static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657{
4658 char *cmd;
4659 int enable, disable, status;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004660 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004662 if (video_supported == TPACPI_VIDEO_NONE)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004663 return -ENODEV;
4664
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004665 enable = 0;
4666 disable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667
4668 while ((cmd = next_cmd(&buf))) {
4669 if (strlencmp(cmd, "lcd_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004670 enable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004671 } else if (strlencmp(cmd, "lcd_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004672 disable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673 } else if (strlencmp(cmd, "crt_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004674 enable |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004675 } else if (strlencmp(cmd, "crt_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004676 disable |= TP_ACPI_VIDEO_S_CRT;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004677 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004678 strlencmp(cmd, "dvi_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004679 enable |= TP_ACPI_VIDEO_S_DVI;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004680 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004681 strlencmp(cmd, "dvi_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004682 disable |= TP_ACPI_VIDEO_S_DVI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683 } else if (strlencmp(cmd, "auto_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004684 res = video_autosw_set(1);
4685 if (res)
4686 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687 } else if (strlencmp(cmd, "auto_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004688 res = video_autosw_set(0);
4689 if (res)
4690 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004691 } else if (strlencmp(cmd, "video_switch") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004692 res = video_outputsw_cycle();
4693 if (res)
4694 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004696 res = video_expand_toggle();
4697 if (res)
4698 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004699 } else
4700 return -EINVAL;
4701 }
4702
4703 if (enable || disable) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004704 status = video_outputsw_get();
4705 if (status < 0)
4706 return status;
4707 res = video_outputsw_set((status & ~disable) | enable);
4708 if (res)
4709 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710 }
4711
4712 return 0;
4713}
4714
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004715static struct ibm_struct video_driver_data = {
4716 .name = "video",
4717 .read = video_read,
4718 .write = video_write,
4719 .exit = video_exit,
4720};
4721
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02004722#endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4723
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004724/*************************************************************************
4725 * Light (thinklight) subdriver
4726 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004727
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004728TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4729TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004730
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004731static int light_get_status(void)
4732{
4733 int status = 0;
4734
4735 if (tp_features.light_status) {
4736 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4737 return -EIO;
4738 return (!!status);
4739 }
4740
4741 return -ENXIO;
4742}
4743
4744static int light_set_status(int status)
4745{
4746 int rc;
4747
4748 if (tp_features.light) {
4749 if (cmos_handle) {
4750 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4751 (status)?
4752 TP_CMOS_THINKLIGHT_ON :
4753 TP_CMOS_THINKLIGHT_OFF);
4754 } else {
4755 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4756 (status)? 1 : 0);
4757 }
4758 return (rc)? 0 : -EIO;
4759 }
4760
4761 return -ENXIO;
4762}
4763
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004764static void light_set_status_worker(struct work_struct *work)
4765{
4766 struct tpacpi_led_classdev *data =
4767 container_of(work, struct tpacpi_led_classdev, work);
4768
4769 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00004770 light_set_status((data->new_state != TPACPI_LED_OFF));
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004771}
4772
4773static void light_sysfs_set(struct led_classdev *led_cdev,
4774 enum led_brightness brightness)
4775{
4776 struct tpacpi_led_classdev *data =
4777 container_of(led_cdev,
4778 struct tpacpi_led_classdev,
4779 led_classdev);
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00004780 data->new_state = (brightness != LED_OFF) ?
4781 TPACPI_LED_ON : TPACPI_LED_OFF;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004782 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004783}
4784
4785static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4786{
4787 return (light_get_status() == 1)? LED_FULL : LED_OFF;
4788}
4789
4790static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4791 .led_classdev = {
4792 .name = "tpacpi::thinklight",
4793 .brightness_set = &light_sysfs_set,
4794 .brightness_get = &light_sysfs_get,
4795 }
4796};
4797
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004798static int __init light_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004799{
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004800 int rc;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004801
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004802 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4803
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004804 TPACPI_ACPIHANDLE_INIT(ledb);
4805 TPACPI_ACPIHANDLE_INIT(lght);
4806 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004807 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004808
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004809 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004810 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004811
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004812 if (tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004813 /* light status not supported on
4814 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004815 tp_features.light_status =
4816 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004817
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004818 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4819 str_supported(tp_features.light),
4820 str_supported(tp_features.light_status));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004821
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004822 if (!tp_features.light)
4823 return 1;
4824
4825 rc = led_classdev_register(&tpacpi_pdev->dev,
4826 &tpacpi_led_thinklight.led_classdev);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004827
4828 if (rc < 0) {
4829 tp_features.light = 0;
4830 tp_features.light_status = 0;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004831 } else {
4832 rc = 0;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004833 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004834
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004835 return rc;
4836}
4837
4838static void light_exit(void)
4839{
4840 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
4841 if (work_pending(&tpacpi_led_thinklight.work))
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004842 flush_workqueue(tpacpi_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843}
4844
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004845static int light_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846{
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004847 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004848
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004849 if (!tp_features.light) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004850 seq_printf(m, "status:\t\tnot supported\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004851 } else if (!tp_features.light_status) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004852 seq_printf(m, "status:\t\tunknown\n");
4853 seq_printf(m, "commands:\ton, off\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004854 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004855 status = light_get_status();
4856 if (status < 0)
4857 return status;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004858 seq_printf(m, "status:\t\t%s\n", onoff(status, 0));
4859 seq_printf(m, "commands:\ton, off\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004860 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004861
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004862 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863}
4864
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004865static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004866{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004868 int newstatus = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004869
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004870 if (!tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004871 return -ENODEV;
4872
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873 while ((cmd = next_cmd(&buf))) {
4874 if (strlencmp(cmd, "on") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004875 newstatus = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876 } else if (strlencmp(cmd, "off") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004877 newstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004878 } else
4879 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880 }
4881
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004882 return light_set_status(newstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004883}
4884
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004885static struct ibm_struct light_driver_data = {
4886 .name = "light",
4887 .read = light_read,
4888 .write = light_write,
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004889 .exit = light_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004890};
4891
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004892/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004893 * CMOS subdriver
4894 */
4895
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004896/* sysfs cmos_command -------------------------------------------------- */
4897static ssize_t cmos_command_store(struct device *dev,
4898 struct device_attribute *attr,
4899 const char *buf, size_t count)
4900{
4901 unsigned long cmos_cmd;
4902 int res;
4903
4904 if (parse_strtoul(buf, 21, &cmos_cmd))
4905 return -EINVAL;
4906
4907 res = issue_thinkpad_cmos_command(cmos_cmd);
4908 return (res)? res : count;
4909}
4910
4911static struct device_attribute dev_attr_cmos_command =
4912 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4913
4914/* --------------------------------------------------------------------- */
4915
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004916static int __init cmos_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004917{
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004918 int res;
4919
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004920 vdbg_printk(TPACPI_DBG_INIT,
4921 "initializing cmos commands subdriver\n");
4922
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004923 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004924
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004925 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4926 str_supported(cmos_handle != NULL));
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004927
4928 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4929 if (res)
4930 return res;
4931
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004932 return (cmos_handle)? 0 : 1;
4933}
4934
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004935static void cmos_exit(void)
4936{
4937 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4938}
4939
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004940static int cmos_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004941{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004942 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4943 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944 if (!cmos_handle)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004945 seq_printf(m, "status:\t\tnot supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946 else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004947 seq_printf(m, "status:\t\tsupported\n");
4948 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949 }
4950
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004951 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004952}
4953
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004954static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955{
4956 char *cmd;
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004957 int cmos_cmd, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004958
4959 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004960 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
4961 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962 /* cmos_cmd set */
4963 } else
4964 return -EINVAL;
4965
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004966 res = issue_thinkpad_cmos_command(cmos_cmd);
4967 if (res)
4968 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969 }
4970
4971 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004972}
4973
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004974static struct ibm_struct cmos_driver_data = {
4975 .name = "cmos",
4976 .read = cmos_read,
4977 .write = cmos_write,
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004978 .exit = cmos_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004979};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004980
4981/*************************************************************************
4982 * LED subdriver
4983 */
4984
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004985enum led_access_mode {
4986 TPACPI_LED_NONE = 0,
4987 TPACPI_LED_570, /* 570 */
4988 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4989 TPACPI_LED_NEW, /* all others */
4990};
4991
4992enum { /* For TPACPI_LED_OLD */
4993 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
4994 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
4995 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
4996};
4997
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02004998static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004999
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005000TPACPI_HANDLE(led, ec, "SLED", /* 570 */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005001 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
5002 /* T20-22, X20-21 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005003 "LED", /* all others */
5004 ); /* R30, R31 */
5005
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005006#define TPACPI_LED_NUMLEDS 16
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005007static struct tpacpi_led_classdev *tpacpi_leds;
5008static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
Harvey Harrisone3aa51f2008-05-29 17:51:57 -07005009static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005010 /* there's a limit of 19 chars + NULL before 2.6.26 */
5011 "tpacpi::power",
5012 "tpacpi:orange:batt",
5013 "tpacpi:green:batt",
5014 "tpacpi::dock_active",
5015 "tpacpi::bay_active",
5016 "tpacpi::dock_batt",
5017 "tpacpi::unknown_led",
5018 "tpacpi::standby",
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005019 "tpacpi::dock_status1",
5020 "tpacpi::dock_status2",
5021 "tpacpi::unknown_led2",
5022 "tpacpi::unknown_led3",
5023 "tpacpi::thinkvantage",
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005024};
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005025#define TPACPI_SAFE_LEDS 0x1081U
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005026
5027static inline bool tpacpi_is_led_restricted(const unsigned int led)
5028{
5029#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5030 return false;
5031#else
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005032 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005033#endif
5034}
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005035
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005036static int led_get_status(const unsigned int led)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005037{
5038 int status;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005039 enum led_status_t led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005040
5041 switch (led_supported) {
5042 case TPACPI_LED_570:
5043 if (!acpi_evalf(ec_handle,
5044 &status, "GLED", "dd", 1 << led))
5045 return -EIO;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005046 led_s = (status == 0)?
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005047 TPACPI_LED_OFF :
5048 ((status == 1)?
5049 TPACPI_LED_ON :
5050 TPACPI_LED_BLINK);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005051 tpacpi_led_state_cache[led] = led_s;
5052 return led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005053 default:
5054 return -ENXIO;
5055 }
5056
5057 /* not reached */
5058}
5059
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005060static int led_set_status(const unsigned int led,
5061 const enum led_status_t ledstatus)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005062{
5063 /* off, on, blink. Index is led_status_t */
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005064 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
5065 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005066
5067 int rc = 0;
5068
5069 switch (led_supported) {
5070 case TPACPI_LED_570:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005071 /* 570 */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005072 if (unlikely(led > 7))
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005073 return -EINVAL;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005074 if (unlikely(tpacpi_is_led_restricted(led)))
5075 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005076 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5077 (1 << led), led_sled_arg1[ledstatus]))
5078 rc = -EIO;
5079 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005080 case TPACPI_LED_OLD:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005081 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005082 if (unlikely(led > 7))
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005083 return -EINVAL;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005084 if (unlikely(tpacpi_is_led_restricted(led)))
5085 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005086 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
5087 if (rc >= 0)
5088 rc = ec_write(TPACPI_LED_EC_HLBL,
5089 (ledstatus == TPACPI_LED_BLINK) << led);
5090 if (rc >= 0)
5091 rc = ec_write(TPACPI_LED_EC_HLCL,
5092 (ledstatus != TPACPI_LED_OFF) << led);
5093 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005094 case TPACPI_LED_NEW:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005095 /* all others */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005096 if (unlikely(led >= TPACPI_LED_NUMLEDS))
5097 return -EINVAL;
5098 if (unlikely(tpacpi_is_led_restricted(led)))
5099 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005100 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5101 led, led_led_arg1[ledstatus]))
5102 rc = -EIO;
5103 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005104 default:
5105 rc = -ENXIO;
5106 }
5107
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005108 if (!rc)
5109 tpacpi_led_state_cache[led] = ledstatus;
5110
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005111 return rc;
5112}
5113
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005114static void led_set_status_worker(struct work_struct *work)
5115{
5116 struct tpacpi_led_classdev *data =
5117 container_of(work, struct tpacpi_led_classdev, work);
5118
5119 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005120 led_set_status(data->led, data->new_state);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005121}
5122
5123static void led_sysfs_set(struct led_classdev *led_cdev,
5124 enum led_brightness brightness)
5125{
5126 struct tpacpi_led_classdev *data = container_of(led_cdev,
5127 struct tpacpi_led_classdev, led_classdev);
5128
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005129 if (brightness == LED_OFF)
5130 data->new_state = TPACPI_LED_OFF;
5131 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
5132 data->new_state = TPACPI_LED_ON;
5133 else
5134 data->new_state = TPACPI_LED_BLINK;
5135
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03005136 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005137}
5138
5139static int led_sysfs_blink_set(struct led_classdev *led_cdev,
5140 unsigned long *delay_on, unsigned long *delay_off)
5141{
5142 struct tpacpi_led_classdev *data = container_of(led_cdev,
5143 struct tpacpi_led_classdev, led_classdev);
5144
5145 /* Can we choose the flash rate? */
5146 if (*delay_on == 0 && *delay_off == 0) {
5147 /* yes. set them to the hardware blink rate (1 Hz) */
5148 *delay_on = 500; /* ms */
5149 *delay_off = 500; /* ms */
5150 } else if ((*delay_on != 500) || (*delay_off != 500))
5151 return -EINVAL;
5152
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005153 data->new_state = TPACPI_LED_BLINK;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03005154 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005155
5156 return 0;
5157}
5158
5159static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
5160{
5161 int rc;
5162
5163 struct tpacpi_led_classdev *data = container_of(led_cdev,
5164 struct tpacpi_led_classdev, led_classdev);
5165
5166 rc = led_get_status(data->led);
5167
5168 if (rc == TPACPI_LED_OFF || rc < 0)
5169 rc = LED_OFF; /* no error handling in led class :( */
5170 else
5171 rc = LED_FULL;
5172
5173 return rc;
5174}
5175
5176static void led_exit(void)
5177{
5178 unsigned int i;
5179
5180 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5181 if (tpacpi_leds[i].led_classdev.name)
5182 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
5183 }
5184
5185 kfree(tpacpi_leds);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005186}
5187
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005188static int __init tpacpi_init_led(unsigned int led)
5189{
5190 int rc;
5191
5192 tpacpi_leds[led].led = led;
5193
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005194 /* LEDs with no name don't get registered */
5195 if (!tpacpi_led_names[led])
5196 return 0;
5197
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005198 tpacpi_leds[led].led_classdev.brightness_set = &led_sysfs_set;
5199 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5200 if (led_supported == TPACPI_LED_570)
5201 tpacpi_leds[led].led_classdev.brightness_get =
5202 &led_sysfs_get;
5203
5204 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5205
5206 INIT_WORK(&tpacpi_leds[led].work, led_set_status_worker);
5207
5208 rc = led_classdev_register(&tpacpi_pdev->dev,
5209 &tpacpi_leds[led].led_classdev);
5210 if (rc < 0)
5211 tpacpi_leds[led].led_classdev.name = NULL;
5212
5213 return rc;
5214}
5215
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005216static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
5217 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5218 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5219 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5220
5221 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5222 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5223 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5224 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5225 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5226 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5227 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5228 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5229
5230 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5231 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5232 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5233 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5234 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5235
5236 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5237 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5238 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5239 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5240
5241 /* (1) - may have excess leds enabled on MSB */
5242
5243 /* Defaults (order matters, keep last, don't reorder!) */
5244 { /* Lenovo */
5245 .vendor = PCI_VENDOR_ID_LENOVO,
5246 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5247 .quirks = 0x1fffU,
5248 },
5249 { /* IBM ThinkPads with no EC version string */
5250 .vendor = PCI_VENDOR_ID_IBM,
5251 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
5252 .quirks = 0x00ffU,
5253 },
5254 { /* IBM ThinkPads with EC version string */
5255 .vendor = PCI_VENDOR_ID_IBM,
5256 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5257 .quirks = 0x00bfU,
5258 },
5259};
5260
5261#undef TPACPI_LEDQ_IBM
5262#undef TPACPI_LEDQ_LNV
5263
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005264static int __init led_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005265{
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005266 unsigned int i;
5267 int rc;
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005268 unsigned long useful_leds;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005269
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005270 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
5271
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005272 TPACPI_ACPIHANDLE_INIT(led);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005273
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005274 if (!led_handle)
5275 /* led not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005276 led_supported = TPACPI_LED_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005277 else if (strlencmp(led_path, "SLED") == 0)
5278 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005279 led_supported = TPACPI_LED_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005280 else if (strlencmp(led_path, "SYSL") == 0)
5281 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005282 led_supported = TPACPI_LED_OLD;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005283 else
5284 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005285 led_supported = TPACPI_LED_NEW;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005286
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005287 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
5288 str_supported(led_supported), led_supported);
5289
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005290 if (led_supported == TPACPI_LED_NONE)
5291 return 1;
5292
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005293 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
5294 GFP_KERNEL);
5295 if (!tpacpi_leds) {
5296 printk(TPACPI_ERR "Out of memory for LED data\n");
5297 return -ENOMEM;
5298 }
5299
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005300 useful_leds = tpacpi_check_quirks(led_useful_qtable,
5301 ARRAY_SIZE(led_useful_qtable));
5302
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005303 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005304 if (!tpacpi_is_led_restricted(i) &&
5305 test_bit(i, &useful_leds)) {
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005306 rc = tpacpi_init_led(i);
5307 if (rc < 0) {
5308 led_exit();
5309 return rc;
5310 }
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005311 }
5312 }
5313
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005314#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005315 printk(TPACPI_NOTICE
5316 "warning: userspace override of important "
5317 "firmware LEDs is enabled\n");
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005318#endif
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005319 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005320}
5321
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005322#define str_led_status(s) \
5323 ((s) == TPACPI_LED_OFF ? "off" : \
5324 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005325
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005326static int led_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005327{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005328 if (!led_supported) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005329 seq_printf(m, "status:\t\tnot supported\n");
5330 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005331 }
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005332 seq_printf(m, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005333
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005334 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005335 /* 570 */
5336 int i, status;
5337 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005338 status = led_get_status(i);
5339 if (status < 0)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005340 return -EIO;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005341 seq_printf(m, "%d:\t\t%s\n",
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005342 i, str_led_status(status));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005343 }
5344 }
5345
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005346 seq_printf(m, "commands:\t"
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005347 "<led> on, <led> off, <led> blink (<led> is 0-15)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005348
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005349 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005350}
5351
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005352static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005353{
5354 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005355 int led, rc;
5356 enum led_status_t s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005357
5358 if (!led_supported)
5359 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005360
5361 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005362 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 15)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005363 return -EINVAL;
5364
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005365 if (strstr(cmd, "off")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005366 s = TPACPI_LED_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005367 } else if (strstr(cmd, "on")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005368 s = TPACPI_LED_ON;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005369 } else if (strstr(cmd, "blink")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005370 s = TPACPI_LED_BLINK;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005371 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005372 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005373 }
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005374
5375 rc = led_set_status(led, s);
5376 if (rc < 0)
5377 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005378 }
5379
5380 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005381}
5382
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005383static struct ibm_struct led_driver_data = {
5384 .name = "led",
5385 .read = led_read,
5386 .write = led_write,
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005387 .exit = led_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005388};
5389
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005390/*************************************************************************
5391 * Beep subdriver
5392 */
5393
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005394TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005395
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005396#define TPACPI_BEEP_Q1 0x0001
5397
5398static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
5399 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
5400 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
5401};
5402
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005403static int __init beep_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005404{
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005405 unsigned long quirks;
5406
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005407 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
5408
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005409 TPACPI_ACPIHANDLE_INIT(beep);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005410
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005411 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
5412 str_supported(beep_handle != NULL));
5413
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005414 quirks = tpacpi_check_quirks(beep_quirk_table,
5415 ARRAY_SIZE(beep_quirk_table));
5416
5417 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
5418
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005419 return (beep_handle)? 0 : 1;
5420}
5421
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005422static int beep_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005423{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005424 if (!beep_handle)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005425 seq_printf(m, "status:\t\tnot supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005426 else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005427 seq_printf(m, "status:\t\tsupported\n");
5428 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-17)\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005429 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005430
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005431 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005432}
5433
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005434static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005435{
5436 char *cmd;
5437 int beep_cmd;
5438
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005439 if (!beep_handle)
5440 return -ENODEV;
5441
Linus Torvalds1da177e2005-04-16 15:20:36 -07005442 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005443 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
5444 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005445 /* beep_cmd set */
5446 } else
5447 return -EINVAL;
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005448 if (tp_features.beep_needs_two_args) {
5449 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
5450 beep_cmd, 0))
5451 return -EIO;
5452 } else {
5453 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
5454 beep_cmd))
5455 return -EIO;
5456 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005457 }
5458
5459 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005460}
5461
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005462static struct ibm_struct beep_driver_data = {
5463 .name = "beep",
5464 .read = beep_read,
5465 .write = beep_write,
5466};
5467
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005468/*************************************************************************
5469 * Thermal subdriver
5470 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005471
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005472enum thermal_access_mode {
5473 TPACPI_THERMAL_NONE = 0, /* No thermal support */
5474 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
5475 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
5476 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
5477 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
5478};
5479
5480enum { /* TPACPI_THERMAL_TPEC_* */
5481 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
5482 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
5483 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005484
5485 TPACPI_THERMAL_SENSOR_NA = -128000, /* Sensor not available */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005486};
5487
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005488
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005489#define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5490struct ibm_thermal_sensors_struct {
5491 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
5492};
5493
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005494static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005495
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005496/* idx is zero-based */
5497static int thermal_get_sensor(int idx, s32 *value)
5498{
5499 int t;
5500 s8 tmp;
5501 char tmpi[5];
5502
5503 t = TP_EC_THERMAL_TMP0;
5504
5505 switch (thermal_read_mode) {
5506#if TPACPI_MAX_THERMAL_SENSORS >= 16
5507 case TPACPI_THERMAL_TPEC_16:
5508 if (idx >= 8 && idx <= 15) {
5509 t = TP_EC_THERMAL_TMP8;
5510 idx -= 8;
5511 }
5512 /* fallthrough */
5513#endif
5514 case TPACPI_THERMAL_TPEC_8:
5515 if (idx <= 7) {
5516 if (!acpi_ec_read(t + idx, &tmp))
5517 return -EIO;
5518 *value = tmp * 1000;
5519 return 0;
5520 }
5521 break;
5522
5523 case TPACPI_THERMAL_ACPI_UPDT:
5524 if (idx <= 7) {
5525 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5526 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
5527 return -EIO;
5528 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5529 return -EIO;
5530 *value = (t - 2732) * 100;
5531 return 0;
5532 }
5533 break;
5534
5535 case TPACPI_THERMAL_ACPI_TMP07:
5536 if (idx <= 7) {
5537 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5538 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5539 return -EIO;
5540 if (t > 127 || t < -127)
5541 t = TP_EC_THERMAL_TMP_NA;
5542 *value = t * 1000;
5543 return 0;
5544 }
5545 break;
5546
5547 case TPACPI_THERMAL_NONE:
5548 default:
5549 return -ENOSYS;
5550 }
5551
5552 return -EINVAL;
5553}
5554
5555static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5556{
5557 int res, i;
5558 int n;
5559
5560 n = 8;
5561 i = 0;
5562
5563 if (!s)
5564 return -EINVAL;
5565
5566 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5567 n = 16;
5568
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005569 for (i = 0 ; i < n; i++) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005570 res = thermal_get_sensor(i, &s->temp[i]);
5571 if (res)
5572 return res;
5573 }
5574
5575 return n;
5576}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005577
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005578static void thermal_dump_all_sensors(void)
5579{
5580 int n, i;
5581 struct ibm_thermal_sensors_struct t;
5582
5583 n = thermal_get_sensors(&t);
5584 if (n <= 0)
5585 return;
5586
5587 printk(TPACPI_NOTICE
5588 "temperatures (Celsius):");
5589
5590 for (i = 0; i < n; i++) {
5591 if (t.temp[i] != TPACPI_THERMAL_SENSOR_NA)
5592 printk(KERN_CONT " %d", (int)(t.temp[i] / 1000));
5593 else
5594 printk(KERN_CONT " N/A");
5595 }
5596
5597 printk(KERN_CONT "\n");
5598}
5599
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005600/* sysfs temp##_input -------------------------------------------------- */
5601
5602static ssize_t thermal_temp_input_show(struct device *dev,
5603 struct device_attribute *attr,
5604 char *buf)
5605{
5606 struct sensor_device_attribute *sensor_attr =
5607 to_sensor_dev_attr(attr);
5608 int idx = sensor_attr->index;
5609 s32 value;
5610 int res;
5611
5612 res = thermal_get_sensor(idx, &value);
5613 if (res)
5614 return res;
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005615 if (value == TPACPI_THERMAL_SENSOR_NA)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005616 return -ENXIO;
5617
5618 return snprintf(buf, PAGE_SIZE, "%d\n", value);
5619}
5620
5621#define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005622 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5623 thermal_temp_input_show, NULL, _idxB)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005624
5625static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5626 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5627 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5628 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5629 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5630 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5631 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5632 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5633 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5634 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5635 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5636 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5637 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5638 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5639 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5640 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5641 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5642};
5643
5644#define THERMAL_ATTRS(X) \
5645 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5646
5647static struct attribute *thermal_temp_input_attr[] = {
5648 THERMAL_ATTRS(8),
5649 THERMAL_ATTRS(9),
5650 THERMAL_ATTRS(10),
5651 THERMAL_ATTRS(11),
5652 THERMAL_ATTRS(12),
5653 THERMAL_ATTRS(13),
5654 THERMAL_ATTRS(14),
5655 THERMAL_ATTRS(15),
5656 THERMAL_ATTRS(0),
5657 THERMAL_ATTRS(1),
5658 THERMAL_ATTRS(2),
5659 THERMAL_ATTRS(3),
5660 THERMAL_ATTRS(4),
5661 THERMAL_ATTRS(5),
5662 THERMAL_ATTRS(6),
5663 THERMAL_ATTRS(7),
5664 NULL
5665};
5666
5667static const struct attribute_group thermal_temp_input16_group = {
5668 .attrs = thermal_temp_input_attr
5669};
5670
5671static const struct attribute_group thermal_temp_input8_group = {
5672 .attrs = &thermal_temp_input_attr[8]
5673};
5674
5675#undef THERMAL_SENSOR_ATTR_TEMP
5676#undef THERMAL_ATTRS
5677
5678/* --------------------------------------------------------------------- */
5679
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005680static int __init thermal_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005681{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005682 u8 t, ta1, ta2;
5683 int i;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005684 int acpi_tmp7;
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005685 int res;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005686
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005687 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5688
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005689 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005690
Henrique de Moraes Holschuh3d6f99c2007-07-18 23:45:46 -03005691 if (thinkpad_id.ec_model) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005692 /*
5693 * Direct EC access mode: sensors at registers
5694 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5695 * non-implemented, thermal sensors return 0x80 when
5696 * not available
5697 */
5698
5699 ta1 = ta2 = 0;
5700 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03005701 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005702 ta1 |= t;
5703 } else {
5704 ta1 = 0;
5705 break;
5706 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03005707 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005708 ta2 |= t;
5709 } else {
5710 ta1 = 0;
5711 break;
5712 }
5713 }
5714 if (ta1 == 0) {
5715 /* This is sheer paranoia, but we handle it anyway */
5716 if (acpi_tmp7) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005717 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005718 "ThinkPad ACPI EC access misbehaving, "
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005719 "falling back to ACPI TMPx access "
5720 "mode\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005721 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005722 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005723 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005724 "ThinkPad ACPI EC access misbehaving, "
5725 "disabling thermal sensors access\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005726 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005727 }
5728 } else {
5729 thermal_read_mode =
5730 (ta2 != 0) ?
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005731 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005732 }
5733 } else if (acpi_tmp7) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005734 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
5735 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005736 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005737 } else {
5738 /* Standard ACPI TMPx access, max 8 sensors */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005739 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005740 }
5741 } else {
5742 /* temperatures not supported on 570, G4x, R30, R31, R32 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005743 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005744 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005745
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005746 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5747 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5748 thermal_read_mode);
5749
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005750 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005751 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005752 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005753 &thermal_temp_input16_group);
5754 if (res)
5755 return res;
5756 break;
5757 case TPACPI_THERMAL_TPEC_8:
5758 case TPACPI_THERMAL_ACPI_TMP07:
5759 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005760 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005761 &thermal_temp_input8_group);
5762 if (res)
5763 return res;
5764 break;
5765 case TPACPI_THERMAL_NONE:
5766 default:
5767 return 1;
5768 }
5769
5770 return 0;
5771}
5772
5773static void thermal_exit(void)
5774{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005775 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005776 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005777 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005778 &thermal_temp_input16_group);
5779 break;
5780 case TPACPI_THERMAL_TPEC_8:
5781 case TPACPI_THERMAL_ACPI_TMP07:
5782 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005783 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Roel Kluinf04d5e02010-02-02 14:37:58 -08005784 &thermal_temp_input8_group);
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005785 break;
5786 case TPACPI_THERMAL_NONE:
5787 default:
5788 break;
5789 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005790}
5791
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005792static int thermal_read(struct seq_file *m)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005793{
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005794 int n, i;
5795 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005796
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005797 n = thermal_get_sensors(&t);
5798 if (unlikely(n < 0))
5799 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005800
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005801 seq_printf(m, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005802
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005803 if (n > 0) {
5804 for (i = 0; i < (n - 1); i++)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005805 seq_printf(m, "%d ", t.temp[i] / 1000);
5806 seq_printf(m, "%d\n", t.temp[i] / 1000);
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005807 } else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005808 seq_printf(m, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005809
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005810 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005811}
5812
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005813static struct ibm_struct thermal_driver_data = {
5814 .name = "thermal",
5815 .read = thermal_read,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005816 .exit = thermal_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005817};
5818
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005819/*************************************************************************
5820 * EC Dump subdriver
5821 */
5822
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005823static u8 ecdump_regs[256];
5824
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005825static int ecdump_read(struct seq_file *m)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005826{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005827 int i, j;
5828 u8 v;
5829
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005830 seq_printf(m, "EC "
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005831 " +00 +01 +02 +03 +04 +05 +06 +07"
5832 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
5833 for (i = 0; i < 256; i += 16) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005834 seq_printf(m, "EC 0x%02x:", i);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005835 for (j = 0; j < 16; j++) {
5836 if (!acpi_ec_read(i + j, &v))
5837 break;
5838 if (v != ecdump_regs[i + j])
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005839 seq_printf(m, " *%02x", v);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005840 else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005841 seq_printf(m, " %02x", v);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005842 ecdump_regs[i + j] = v;
5843 }
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005844 seq_putc(m, '\n');
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005845 if (j != 16)
5846 break;
5847 }
5848
5849 /* These are way too dangerous to advertise openly... */
5850#if 0
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005851 seq_printf(m, "commands:\t0x<offset> 0x<value>"
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005852 " (<offset> is 00-ff, <value> is 00-ff)\n");
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005853 seq_printf(m, "commands:\t0x<offset> <value> "
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005854 " (<offset> is 00-ff, <value> is 0-255)\n");
5855#endif
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005856 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005857}
5858
5859static int ecdump_write(char *buf)
5860{
5861 char *cmd;
5862 int i, v;
5863
5864 while ((cmd = next_cmd(&buf))) {
5865 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
5866 /* i and v set */
5867 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
5868 /* i and v set */
5869 } else
5870 return -EINVAL;
5871 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
5872 if (!acpi_ec_write(i, v))
5873 return -EIO;
5874 } else
5875 return -EINVAL;
5876 }
5877
5878 return 0;
5879}
5880
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005881static struct ibm_struct ecdump_driver_data = {
5882 .name = "ecdump",
5883 .read = ecdump_read,
5884 .write = ecdump_write,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03005885 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005886};
5887
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005888/*************************************************************************
5889 * Backlight/brightness subdriver
5890 */
Holger Macht8acb0252006-10-20 14:30:28 -07005891
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005892#define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5893
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005894/*
5895 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
5896 * CMOS NVRAM byte 0x5E, bits 0-3.
5897 *
5898 * EC HBRV (0x31) has the following layout
5899 * Bit 7: unknown function
5900 * Bit 6: unknown function
5901 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5902 * Bit 4: must be set to zero to avoid problems
5903 * Bit 3-0: backlight brightness level
5904 *
5905 * brightness_get_raw returns status data in the HBRV layout
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03005906 *
5907 * WARNING: The X61 has been verified to use HBRV for something else, so
5908 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
5909 * testing on the very early *60 Lenovo models...
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005910 */
5911
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005912enum {
5913 TP_EC_BACKLIGHT = 0x31,
5914
5915 /* TP_EC_BACKLIGHT bitmasks */
5916 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5917 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5918 TP_EC_BACKLIGHT_MAPSW = 0x20,
5919};
5920
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005921enum tpacpi_brightness_access_mode {
5922 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
5923 TPACPI_BRGHT_MODE_EC, /* EC control */
5924 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
5925 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
5926 TPACPI_BRGHT_MODE_MAX
5927};
5928
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03005929static struct backlight_device *ibm_backlight_device;
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005930
5931static enum tpacpi_brightness_access_mode brightness_mode =
5932 TPACPI_BRGHT_MODE_MAX;
5933
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005934static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5935
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005936static struct mutex brightness_mutex;
5937
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005938/* NVRAM brightness access,
5939 * call with brightness_mutex held! */
5940static unsigned int tpacpi_brightness_nvram_get(void)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005941{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005942 u8 lnvram;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005943
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005944 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5945 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5946 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
5947 lnvram &= (tp_features.bright_16levels) ? 0x0f : 0x07;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005948
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005949 return lnvram;
5950}
5951
5952static void tpacpi_brightness_checkpoint_nvram(void)
5953{
5954 u8 lec = 0;
5955 u8 b_nvram;
5956
5957 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
5958 return;
5959
5960 vdbg_printk(TPACPI_DBG_BRGHT,
5961 "trying to checkpoint backlight level to NVRAM...\n");
5962
5963 if (mutex_lock_killable(&brightness_mutex) < 0)
5964 return;
5965
5966 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5967 goto unlock;
5968 lec &= TP_EC_BACKLIGHT_LVLMSK;
5969 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
5970
5971 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5972 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
5973 /* NVRAM needs update */
5974 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
5975 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
5976 b_nvram |= lec;
5977 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
5978 dbg_printk(TPACPI_DBG_BRGHT,
5979 "updated NVRAM backlight level to %u (0x%02x)\n",
5980 (unsigned int) lec, (unsigned int) b_nvram);
5981 } else
5982 vdbg_printk(TPACPI_DBG_BRGHT,
5983 "NVRAM backlight level already is %u (0x%02x)\n",
5984 (unsigned int) lec, (unsigned int) b_nvram);
5985
5986unlock:
5987 mutex_unlock(&brightness_mutex);
5988}
5989
5990
5991/* call with brightness_mutex held! */
5992static int tpacpi_brightness_get_raw(int *status)
5993{
5994 u8 lec = 0;
5995
5996 switch (brightness_mode) {
5997 case TPACPI_BRGHT_MODE_UCMS_STEP:
5998 *status = tpacpi_brightness_nvram_get();
5999 return 0;
6000 case TPACPI_BRGHT_MODE_EC:
6001 case TPACPI_BRGHT_MODE_ECNVRAM:
6002 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
Henrique de Moraes Holschuh2d5e94d2008-04-26 01:02:20 -03006003 return -EIO;
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006004 *status = lec;
6005 return 0;
6006 default:
6007 return -ENXIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006008 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006009}
6010
6011/* call with brightness_mutex held! */
6012/* do NOT call with illegal backlight level value */
6013static int tpacpi_brightness_set_ec(unsigned int value)
6014{
6015 u8 lec = 0;
6016
6017 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6018 return -EIO;
6019
6020 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
6021 (lec & TP_EC_BACKLIGHT_CMDMSK) |
6022 (value & TP_EC_BACKLIGHT_LVLMSK))))
6023 return -EIO;
6024
6025 return 0;
6026}
6027
6028/* call with brightness_mutex held! */
6029static int tpacpi_brightness_set_ucmsstep(unsigned int value)
6030{
6031 int cmos_cmd, inc;
6032 unsigned int current_value, i;
6033
6034 current_value = tpacpi_brightness_nvram_get();
6035
6036 if (value == current_value)
6037 return 0;
6038
6039 cmos_cmd = (value > current_value) ?
6040 TP_CMOS_BRIGHTNESS_UP :
6041 TP_CMOS_BRIGHTNESS_DOWN;
6042 inc = (value > current_value) ? 1 : -1;
6043
6044 for (i = current_value; i != value; i += inc)
6045 if (issue_thinkpad_cmos_command(cmos_cmd))
6046 return -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006047
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006048 return 0;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006049}
6050
6051/* May return EINTR which can always be mapped to ERESTARTSYS */
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006052static int brightness_set(unsigned int value)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006053{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006054 int res;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006055
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006056 if (value > ((tp_features.bright_16levels)? 15 : 7) ||
6057 value < 0)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006058 return -EINVAL;
6059
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006060 vdbg_printk(TPACPI_DBG_BRGHT,
6061 "set backlight level to %d\n", value);
6062
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006063 res = mutex_lock_killable(&brightness_mutex);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006064 if (res < 0)
6065 return res;
6066
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006067 switch (brightness_mode) {
6068 case TPACPI_BRGHT_MODE_EC:
6069 case TPACPI_BRGHT_MODE_ECNVRAM:
6070 res = tpacpi_brightness_set_ec(value);
6071 break;
6072 case TPACPI_BRGHT_MODE_UCMS_STEP:
6073 res = tpacpi_brightness_set_ucmsstep(value);
6074 break;
6075 default:
6076 res = -ENXIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006077 }
6078
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006079 mutex_unlock(&brightness_mutex);
6080 return res;
6081}
6082
6083/* sysfs backlight class ----------------------------------------------- */
6084
6085static int brightness_update_status(struct backlight_device *bd)
6086{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006087 unsigned int level =
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006088 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
6089 bd->props.power == FB_BLANK_UNBLANK) ?
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006090 bd->props.brightness : 0;
6091
6092 dbg_printk(TPACPI_DBG_BRGHT,
6093 "backlight: attempt to set level to %d\n",
6094 level);
6095
6096 /* it is the backlight class's job (caller) to handle
6097 * EINTR and other errors properly */
6098 return brightness_set(level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006099}
Holger Macht8acb0252006-10-20 14:30:28 -07006100
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006101static int brightness_get(struct backlight_device *bd)
6102{
6103 int status, res;
6104
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006105 res = mutex_lock_killable(&brightness_mutex);
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006106 if (res < 0)
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006107 return 0;
6108
6109 res = tpacpi_brightness_get_raw(&status);
6110
6111 mutex_unlock(&brightness_mutex);
6112
6113 if (res < 0)
6114 return 0;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006115
6116 return status & TP_EC_BACKLIGHT_LVLMSK;
6117}
6118
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00006119static void tpacpi_brightness_notify_change(void)
6120{
6121 backlight_force_update(ibm_backlight_device,
6122 BACKLIGHT_UPDATE_HOTKEY);
6123}
6124
Richard Purdie599a52d2007-02-10 23:07:48 +00006125static struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006126 .get_brightness = brightness_get,
6127 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006128};
6129
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006130/* --------------------------------------------------------------------- */
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03006131
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006132/*
6133 * These are only useful for models that have only one possibility
6134 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6135 * these quirks.
6136 */
6137#define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6138#define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6139#define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6140
6141static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
6142 /* Models with ATI GPUs known to require ECNVRAM mode */
6143 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
6144
Henrique de Moraes Holschuh6da25bf2009-09-12 15:22:11 -03006145 /* Models with ATI GPUs that can use ECNVRAM */
Henrique de Moraes Holschuh7d1894d2010-02-25 21:28:56 -03006146 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC), /* R50,51 T40-42 */
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006147 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
Henrique de Moraes Holschuh7d1894d2010-02-25 21:28:56 -03006148 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_EC), /* R52 */
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006149 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6150
Henrique de Moraes Holschuh6da25bf2009-09-12 15:22:11 -03006151 /* Models with Intel Extreme Graphics 2 */
Henrique de Moraes Holschuh7d1894d2010-02-25 21:28:56 -03006152 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC), /* X40 */
Henrique de Moraes Holschuha9f8eac2009-12-09 01:36:21 +00006153 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6154 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006155
6156 /* Models with Intel GMA900 */
6157 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
6158 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
6159 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
6160};
6161
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006162static int __init brightness_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006163{
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006164 int b;
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006165 unsigned long quirks;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006166
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006167 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
6168
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03006169 mutex_init(&brightness_mutex);
6170
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006171 quirks = tpacpi_check_quirks(brightness_quirk_table,
6172 ARRAY_SIZE(brightness_quirk_table));
6173
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03006174 /*
6175 * We always attempt to detect acpi support, so as to switch
6176 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6177 * going to publish a backlight interface
6178 */
6179 b = tpacpi_check_std_acpi_brightness_support();
6180 if (b > 0) {
Thomas Renninger2dba1b52008-08-01 17:38:03 +02006181
6182 if (acpi_video_backlight_support()) {
6183 if (brightness_enable > 1) {
6184 printk(TPACPI_NOTICE
6185 "Standard ACPI backlight interface "
6186 "available, not loading native one.\n");
6187 return 1;
6188 } else if (brightness_enable == 1) {
6189 printk(TPACPI_NOTICE
6190 "Backlight control force enabled, even if standard "
6191 "ACPI backlight interface is available\n");
6192 }
6193 } else {
6194 if (brightness_enable > 1) {
6195 printk(TPACPI_NOTICE
6196 "Standard ACPI backlight interface not "
6197 "available, thinkpad_acpi native "
6198 "brightness control enabled\n");
6199 }
Henrique de Moraes Holschuhe11e2112007-10-30 17:46:22 -02006200 }
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02006201 }
6202
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03006203 if (!brightness_enable) {
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006204 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03006205 "brightness support disabled by "
6206 "module parameter\n");
6207 return 1;
6208 }
6209
6210 if (b > 16) {
6211 printk(TPACPI_ERR
6212 "Unsupported brightness interface, "
6213 "please contact %s\n", TPACPI_MAIL);
6214 return 1;
6215 }
6216 if (b == 16)
6217 tp_features.bright_16levels = 1;
6218
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006219 /*
6220 * Check for module parameter bogosity, note that we
6221 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
6222 * able to detect "unspecified"
6223 */
6224 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03006225 return -EINVAL;
6226
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006227 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
6228 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
6229 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006230 if (quirks & TPACPI_BRGHT_Q_EC)
6231 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
6232 else
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006233 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
6234
6235 dbg_printk(TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006236 "driver auto-selected brightness_mode=%d\n",
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006237 brightness_mode);
6238 }
6239
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03006240 /* Safety */
6241 if (thinkpad_id.vendor != PCI_VENDOR_ID_IBM &&
6242 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
6243 brightness_mode == TPACPI_BRGHT_MODE_EC))
6244 return -EINVAL;
6245
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006246 if (tpacpi_brightness_get_raw(&b) < 0)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03006247 return 1;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006248
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02006249 if (tp_features.bright_16levels)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006250 printk(TPACPI_INFO
6251 "detected a 16-level brightness capable ThinkPad\n");
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02006252
Henrique de Moraes Holschuh7d5a0152007-04-24 11:48:20 -03006253 ibm_backlight_device = backlight_device_register(
6254 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
6255 &ibm_backlight_data);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006256 if (IS_ERR(ibm_backlight_device)) {
Henrique de Moraes Holschuh435c47e2009-09-20 14:09:22 -03006257 int rc = PTR_ERR(ibm_backlight_device);
6258 ibm_backlight_device = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006259 printk(TPACPI_ERR "Could not register backlight device\n");
Henrique de Moraes Holschuh435c47e2009-09-20 14:09:22 -03006260 return rc;
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006261 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006262 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6263 "brightness is supported\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006264
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006265 if (quirks & TPACPI_BRGHT_Q_ASK) {
6266 printk(TPACPI_NOTICE
6267 "brightness: will use unverified default: "
6268 "brightness_mode=%d\n", brightness_mode);
6269 printk(TPACPI_NOTICE
6270 "brightness: please report to %s whether it works well "
6271 "or not on your ThinkPad\n", TPACPI_MAIL);
6272 }
6273
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02006274 ibm_backlight_device->props.max_brightness =
6275 (tp_features.bright_16levels)? 15 : 7;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006276 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006277 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00006278
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00006279 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6280 "brightness: registering brightness hotkeys "
6281 "as change notification\n");
6282 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6283 | TP_ACPI_HKEY_BRGHTUP_MASK
6284 | TP_ACPI_HKEY_BRGHTDWN_MASK);;
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006285 return 0;
6286}
6287
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006288static void brightness_suspend(pm_message_t state)
6289{
6290 tpacpi_brightness_checkpoint_nvram();
6291}
6292
6293static void brightness_shutdown(void)
6294{
6295 tpacpi_brightness_checkpoint_nvram();
6296}
6297
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006298static void brightness_exit(void)
6299{
6300 if (ibm_backlight_device) {
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006301 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006302 "calling backlight_device_unregister()\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006303 backlight_device_unregister(ibm_backlight_device);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006304 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006305
6306 tpacpi_brightness_checkpoint_nvram();
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006307}
6308
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006309static int brightness_read(struct seq_file *m)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006310{
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006311 int level;
6312
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006313 level = brightness_get(NULL);
6314 if (level < 0) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006315 seq_printf(m, "level:\t\tunreadable\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006316 } else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006317 seq_printf(m, "level:\t\t%d\n", level);
6318 seq_printf(m, "commands:\tup, down\n");
6319 seq_printf(m, "commands:\tlevel <level>"
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02006320 " (<level> is 0-%d)\n",
6321 (tp_features.bright_16levels) ? 15 : 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006322 }
6323
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006324 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006325}
6326
6327static int brightness_write(char *buf)
6328{
6329 int level;
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006330 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006331 char *cmd;
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02006332 int max_level = (tp_features.bright_16levels) ? 15 : 7;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006333
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006334 level = brightness_get(NULL);
6335 if (level < 0)
6336 return level;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006337
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006338 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006339 if (strlencmp(cmd, "up") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006340 if (level < max_level)
6341 level++;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006342 } else if (strlencmp(cmd, "down") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006343 if (level > 0)
6344 level--;
6345 } else if (sscanf(cmd, "level %d", &level) == 1 &&
6346 level >= 0 && level <= max_level) {
6347 /* new level set */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006348 } else
6349 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006350 }
6351
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006352 tpacpi_disclose_usertask("procfs brightness",
6353 "set level to %d\n", level);
6354
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006355 /*
6356 * Now we know what the final level should be, so we try to set it.
6357 * Doing it this way makes the syscall restartable in case of EINTR
6358 */
6359 rc = brightness_set(level);
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00006360 if (!rc && ibm_backlight_device)
6361 backlight_force_update(ibm_backlight_device,
6362 BACKLIGHT_UPDATE_SYSFS);
Roel Kluin80a8d122009-11-20 19:48:23 +01006363 return (rc == -EINTR)? -ERESTARTSYS : rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006364}
6365
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006366static struct ibm_struct brightness_driver_data = {
6367 .name = "brightness",
6368 .read = brightness_read,
6369 .write = brightness_write,
6370 .exit = brightness_exit,
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006371 .suspend = brightness_suspend,
6372 .shutdown = brightness_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006373};
6374
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006375/*************************************************************************
6376 * Volume subdriver
6377 */
6378
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006379/*
6380 * IBM ThinkPads have a simple volume controller with MUTE gating.
6381 * Very early Lenovo ThinkPads follow the IBM ThinkPad spec.
6382 *
6383 * Since the *61 series (and probably also the later *60 series), Lenovo
6384 * ThinkPads only implement the MUTE gate.
6385 *
6386 * EC register 0x30
6387 * Bit 6: MUTE (1 mutes sound)
6388 * Bit 3-0: Volume
6389 * Other bits should be zero as far as we know.
6390 *
6391 * This is also stored in CMOS NVRAM, byte 0x60, bit 6 (MUTE), and
6392 * bits 3-0 (volume). Other bits in NVRAM may have other functions,
6393 * such as bit 7 which is used to detect repeated presses of MUTE,
6394 * and we leave them unchanged.
6395 */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006396
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02006397#ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
6398
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006399#define TPACPI_ALSA_DRVNAME "ThinkPad EC"
6400#define TPACPI_ALSA_SHRTNAME "ThinkPad Console Audio Control"
6401#define TPACPI_ALSA_MIXERNAME TPACPI_ALSA_SHRTNAME
6402
Henrique de Moraes Holschuhead510c2009-12-26 22:52:13 -02006403static int alsa_index = ~((1 << (SNDRV_CARDS - 3)) - 1); /* last three slots */
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006404static char *alsa_id = "ThinkPadEC";
6405static int alsa_enable = SNDRV_DEFAULT_ENABLE1;
6406
6407struct tpacpi_alsa_data {
6408 struct snd_card *card;
6409 struct snd_ctl_elem_id *ctl_mute_id;
6410 struct snd_ctl_elem_id *ctl_vol_id;
6411};
6412
6413static struct snd_card *alsa_card;
6414
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006415enum {
6416 TP_EC_AUDIO = 0x30,
6417
6418 /* TP_EC_AUDIO bits */
6419 TP_EC_AUDIO_MUTESW = 6,
6420
6421 /* TP_EC_AUDIO bitmasks */
6422 TP_EC_AUDIO_LVL_MSK = 0x0F,
6423 TP_EC_AUDIO_MUTESW_MSK = (1 << TP_EC_AUDIO_MUTESW),
6424
6425 /* Maximum volume */
6426 TP_EC_VOLUME_MAX = 14,
6427};
6428
6429enum tpacpi_volume_access_mode {
6430 TPACPI_VOL_MODE_AUTO = 0, /* Not implemented yet */
6431 TPACPI_VOL_MODE_EC, /* Pure EC control */
6432 TPACPI_VOL_MODE_UCMS_STEP, /* UCMS step-based control: N/A */
6433 TPACPI_VOL_MODE_ECNVRAM, /* EC control w/ NVRAM store */
6434 TPACPI_VOL_MODE_MAX
6435};
6436
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006437enum tpacpi_volume_capabilities {
6438 TPACPI_VOL_CAP_AUTO = 0, /* Use white/blacklist */
6439 TPACPI_VOL_CAP_VOLMUTE, /* Output vol and mute */
6440 TPACPI_VOL_CAP_MUTEONLY, /* Output mute only */
6441 TPACPI_VOL_CAP_MAX
6442};
6443
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006444static enum tpacpi_volume_access_mode volume_mode =
6445 TPACPI_VOL_MODE_MAX;
6446
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006447static enum tpacpi_volume_capabilities volume_capabilities;
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006448static int volume_control_allowed;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006449
6450/*
6451 * Used to syncronize writers to TP_EC_AUDIO and
6452 * TP_NVRAM_ADDR_MIXER, as we need to do read-modify-write
6453 */
6454static struct mutex volume_mutex;
6455
6456static void tpacpi_volume_checkpoint_nvram(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006457{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006458 u8 lec = 0;
6459 u8 b_nvram;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006460 u8 ec_mask;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006461
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006462 if (volume_mode != TPACPI_VOL_MODE_ECNVRAM)
6463 return;
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006464 if (!volume_control_allowed)
6465 return;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006466
6467 vdbg_printk(TPACPI_DBG_MIXER,
6468 "trying to checkpoint mixer state to NVRAM...\n");
6469
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006470 if (tp_features.mixer_no_level_control)
6471 ec_mask = TP_EC_AUDIO_MUTESW_MSK;
6472 else
6473 ec_mask = TP_EC_AUDIO_MUTESW_MSK | TP_EC_AUDIO_LVL_MSK;
6474
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006475 if (mutex_lock_killable(&volume_mutex) < 0)
6476 return;
6477
6478 if (unlikely(!acpi_ec_read(TP_EC_AUDIO, &lec)))
6479 goto unlock;
6480 lec &= ec_mask;
6481 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
6482
6483 if (lec != (b_nvram & ec_mask)) {
6484 /* NVRAM needs update */
6485 b_nvram &= ~ec_mask;
6486 b_nvram |= lec;
6487 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_MIXER);
6488 dbg_printk(TPACPI_DBG_MIXER,
6489 "updated NVRAM mixer status to 0x%02x (0x%02x)\n",
6490 (unsigned int) lec, (unsigned int) b_nvram);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006491 } else {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006492 vdbg_printk(TPACPI_DBG_MIXER,
6493 "NVRAM mixer status already is 0x%02x (0x%02x)\n",
6494 (unsigned int) lec, (unsigned int) b_nvram);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006495 }
6496
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006497unlock:
6498 mutex_unlock(&volume_mutex);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006499}
6500
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006501static int volume_get_status_ec(u8 *status)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006502{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006503 u8 s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006504
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006505 if (!acpi_ec_read(TP_EC_AUDIO, &s))
6506 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006507
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006508 *status = s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006509
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006510 dbg_printk(TPACPI_DBG_MIXER, "status 0x%02x\n", s);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006511
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006512 return 0;
6513}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006514
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006515static int volume_get_status(u8 *status)
6516{
6517 return volume_get_status_ec(status);
6518}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006519
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006520static int volume_set_status_ec(const u8 status)
6521{
6522 if (!acpi_ec_write(TP_EC_AUDIO, status))
6523 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006524
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006525 dbg_printk(TPACPI_DBG_MIXER, "set EC mixer to 0x%02x\n", status);
6526
6527 return 0;
6528}
6529
6530static int volume_set_status(const u8 status)
6531{
6532 return volume_set_status_ec(status);
6533}
6534
6535static int volume_set_mute_ec(const bool mute)
6536{
6537 int rc;
6538 u8 s, n;
6539
6540 if (mutex_lock_killable(&volume_mutex) < 0)
6541 return -EINTR;
6542
6543 rc = volume_get_status_ec(&s);
6544 if (rc)
6545 goto unlock;
6546
6547 n = (mute) ? s | TP_EC_AUDIO_MUTESW_MSK :
6548 s & ~TP_EC_AUDIO_MUTESW_MSK;
6549
6550 if (n != s)
6551 rc = volume_set_status_ec(n);
6552
6553unlock:
6554 mutex_unlock(&volume_mutex);
6555 return rc;
6556}
6557
6558static int volume_set_mute(const bool mute)
6559{
6560 dbg_printk(TPACPI_DBG_MIXER, "trying to %smute\n",
6561 (mute) ? "" : "un");
6562 return volume_set_mute_ec(mute);
6563}
6564
6565static int volume_set_volume_ec(const u8 vol)
6566{
6567 int rc;
6568 u8 s, n;
6569
6570 if (vol > TP_EC_VOLUME_MAX)
6571 return -EINVAL;
6572
6573 if (mutex_lock_killable(&volume_mutex) < 0)
6574 return -EINTR;
6575
6576 rc = volume_get_status_ec(&s);
6577 if (rc)
6578 goto unlock;
6579
6580 n = (s & ~TP_EC_AUDIO_LVL_MSK) | vol;
6581
6582 if (n != s)
6583 rc = volume_set_status_ec(n);
6584
6585unlock:
6586 mutex_unlock(&volume_mutex);
6587 return rc;
6588}
6589
6590static int volume_set_volume(const u8 vol)
6591{
6592 dbg_printk(TPACPI_DBG_MIXER,
6593 "trying to set volume level to %hu\n", vol);
6594 return volume_set_volume_ec(vol);
6595}
6596
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006597static void volume_alsa_notify_change(void)
6598{
6599 struct tpacpi_alsa_data *d;
6600
6601 if (alsa_card && alsa_card->private_data) {
6602 d = alsa_card->private_data;
6603 if (d->ctl_mute_id)
6604 snd_ctl_notify(alsa_card,
6605 SNDRV_CTL_EVENT_MASK_VALUE,
6606 d->ctl_mute_id);
6607 if (d->ctl_vol_id)
6608 snd_ctl_notify(alsa_card,
6609 SNDRV_CTL_EVENT_MASK_VALUE,
6610 d->ctl_vol_id);
6611 }
6612}
6613
6614static int volume_alsa_vol_info(struct snd_kcontrol *kcontrol,
6615 struct snd_ctl_elem_info *uinfo)
6616{
6617 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
6618 uinfo->count = 1;
6619 uinfo->value.integer.min = 0;
6620 uinfo->value.integer.max = TP_EC_VOLUME_MAX;
6621 return 0;
6622}
6623
6624static int volume_alsa_vol_get(struct snd_kcontrol *kcontrol,
6625 struct snd_ctl_elem_value *ucontrol)
6626{
6627 u8 s;
6628 int rc;
6629
6630 rc = volume_get_status(&s);
6631 if (rc < 0)
6632 return rc;
6633
6634 ucontrol->value.integer.value[0] = s & TP_EC_AUDIO_LVL_MSK;
6635 return 0;
6636}
6637
6638static int volume_alsa_vol_put(struct snd_kcontrol *kcontrol,
6639 struct snd_ctl_elem_value *ucontrol)
6640{
6641 return volume_set_volume(ucontrol->value.integer.value[0]);
6642}
6643
6644#define volume_alsa_mute_info snd_ctl_boolean_mono_info
6645
6646static int volume_alsa_mute_get(struct snd_kcontrol *kcontrol,
6647 struct snd_ctl_elem_value *ucontrol)
6648{
6649 u8 s;
6650 int rc;
6651
6652 rc = volume_get_status(&s);
6653 if (rc < 0)
6654 return rc;
6655
6656 ucontrol->value.integer.value[0] =
6657 (s & TP_EC_AUDIO_MUTESW_MSK) ? 0 : 1;
6658 return 0;
6659}
6660
6661static int volume_alsa_mute_put(struct snd_kcontrol *kcontrol,
6662 struct snd_ctl_elem_value *ucontrol)
6663{
6664 return volume_set_mute(!ucontrol->value.integer.value[0]);
6665}
6666
6667static struct snd_kcontrol_new volume_alsa_control_vol __devinitdata = {
6668 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6669 .name = "Console Playback Volume",
6670 .index = 0,
6671 .access = SNDRV_CTL_ELEM_ACCESS_READ,
6672 .info = volume_alsa_vol_info,
6673 .get = volume_alsa_vol_get,
6674};
6675
6676static struct snd_kcontrol_new volume_alsa_control_mute __devinitdata = {
6677 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6678 .name = "Console Playback Switch",
6679 .index = 0,
6680 .access = SNDRV_CTL_ELEM_ACCESS_READ,
6681 .info = volume_alsa_mute_info,
6682 .get = volume_alsa_mute_get,
6683};
6684
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006685static void volume_suspend(pm_message_t state)
6686{
6687 tpacpi_volume_checkpoint_nvram();
6688}
6689
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006690static void volume_resume(void)
6691{
6692 volume_alsa_notify_change();
6693}
6694
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006695static void volume_shutdown(void)
6696{
6697 tpacpi_volume_checkpoint_nvram();
6698}
6699
6700static void volume_exit(void)
6701{
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006702 if (alsa_card) {
6703 snd_card_free(alsa_card);
6704 alsa_card = NULL;
6705 }
6706
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006707 tpacpi_volume_checkpoint_nvram();
6708}
6709
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006710static int __init volume_create_alsa_mixer(void)
6711{
6712 struct snd_card *card;
6713 struct tpacpi_alsa_data *data;
6714 struct snd_kcontrol *ctl_vol;
6715 struct snd_kcontrol *ctl_mute;
6716 int rc;
6717
6718 rc = snd_card_create(alsa_index, alsa_id, THIS_MODULE,
6719 sizeof(struct tpacpi_alsa_data), &card);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02006720 if (rc < 0 || !card) {
6721 printk(TPACPI_ERR
6722 "Failed to create ALSA card structures: %d\n", rc);
6723 return 1;
6724 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006725
6726 BUG_ON(!card->private_data);
6727 data = card->private_data;
6728 data->card = card;
6729
6730 strlcpy(card->driver, TPACPI_ALSA_DRVNAME,
6731 sizeof(card->driver));
6732 strlcpy(card->shortname, TPACPI_ALSA_SHRTNAME,
6733 sizeof(card->shortname));
6734 snprintf(card->mixername, sizeof(card->mixername), "ThinkPad EC %s",
6735 (thinkpad_id.ec_version_str) ?
6736 thinkpad_id.ec_version_str : "(unknown)");
6737 snprintf(card->longname, sizeof(card->longname),
6738 "%s at EC reg 0x%02x, fw %s", card->shortname, TP_EC_AUDIO,
6739 (thinkpad_id.ec_version_str) ?
6740 thinkpad_id.ec_version_str : "unknown");
6741
6742 if (volume_control_allowed) {
6743 volume_alsa_control_vol.put = volume_alsa_vol_put;
6744 volume_alsa_control_vol.access =
6745 SNDRV_CTL_ELEM_ACCESS_READWRITE;
6746
6747 volume_alsa_control_mute.put = volume_alsa_mute_put;
6748 volume_alsa_control_mute.access =
6749 SNDRV_CTL_ELEM_ACCESS_READWRITE;
6750 }
6751
6752 if (!tp_features.mixer_no_level_control) {
6753 ctl_vol = snd_ctl_new1(&volume_alsa_control_vol, NULL);
6754 rc = snd_ctl_add(card, ctl_vol);
6755 if (rc < 0) {
6756 printk(TPACPI_ERR
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02006757 "Failed to create ALSA volume control: %d\n",
6758 rc);
6759 goto err_exit;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006760 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006761 data->ctl_vol_id = &ctl_vol->id;
6762 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006763
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006764 ctl_mute = snd_ctl_new1(&volume_alsa_control_mute, NULL);
6765 rc = snd_ctl_add(card, ctl_mute);
6766 if (rc < 0) {
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02006767 printk(TPACPI_ERR "Failed to create ALSA mute control: %d\n",
6768 rc);
6769 goto err_exit;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006770 }
6771 data->ctl_mute_id = &ctl_mute->id;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006772
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006773 snd_card_set_dev(card, &tpacpi_pdev->dev);
6774 rc = snd_card_register(card);
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006775 if (rc < 0) {
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02006776 printk(TPACPI_ERR "Failed to register ALSA card: %d\n", rc);
6777 goto err_exit;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006778 }
6779
6780 alsa_card = card;
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02006781 return 0;
6782
6783err_exit:
6784 snd_card_free(card);
6785 return 1;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006786}
6787
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006788#define TPACPI_VOL_Q_MUTEONLY 0x0001 /* Mute-only control available */
6789#define TPACPI_VOL_Q_LEVEL 0x0002 /* Volume control available */
6790
6791static const struct tpacpi_quirk volume_quirk_table[] __initconst = {
6792 /* Whitelist volume level on all IBM by default */
6793 { .vendor = PCI_VENDOR_ID_IBM,
6794 .bios = TPACPI_MATCH_ANY,
6795 .ec = TPACPI_MATCH_ANY,
6796 .quirks = TPACPI_VOL_Q_LEVEL },
6797
6798 /* Lenovo models with volume control (needs confirmation) */
6799 TPACPI_QEC_LNV('7', 'C', TPACPI_VOL_Q_LEVEL), /* R60/i */
6800 TPACPI_QEC_LNV('7', 'E', TPACPI_VOL_Q_LEVEL), /* R60e/i */
6801 TPACPI_QEC_LNV('7', '9', TPACPI_VOL_Q_LEVEL), /* T60/p */
6802 TPACPI_QEC_LNV('7', 'B', TPACPI_VOL_Q_LEVEL), /* X60/s */
6803 TPACPI_QEC_LNV('7', 'J', TPACPI_VOL_Q_LEVEL), /* X60t */
6804 TPACPI_QEC_LNV('7', '7', TPACPI_VOL_Q_LEVEL), /* Z60 */
6805 TPACPI_QEC_LNV('7', 'F', TPACPI_VOL_Q_LEVEL), /* Z61 */
6806
6807 /* Whitelist mute-only on all Lenovo by default */
6808 { .vendor = PCI_VENDOR_ID_LENOVO,
6809 .bios = TPACPI_MATCH_ANY,
6810 .ec = TPACPI_MATCH_ANY,
6811 .quirks = TPACPI_VOL_Q_MUTEONLY }
6812};
6813
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006814static int __init volume_init(struct ibm_init_struct *iibm)
6815{
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006816 unsigned long quirks;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006817 int rc;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006818
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006819 vdbg_printk(TPACPI_DBG_INIT, "initializing volume subdriver\n");
6820
6821 mutex_init(&volume_mutex);
6822
6823 /*
6824 * Check for module parameter bogosity, note that we
6825 * init volume_mode to TPACPI_VOL_MODE_MAX in order to be
6826 * able to detect "unspecified"
6827 */
6828 if (volume_mode > TPACPI_VOL_MODE_MAX)
6829 return -EINVAL;
6830
6831 if (volume_mode == TPACPI_VOL_MODE_UCMS_STEP) {
6832 printk(TPACPI_ERR
6833 "UCMS step volume mode not implemented, "
6834 "please contact %s\n", TPACPI_MAIL);
6835 return 1;
6836 }
6837
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006838 if (volume_capabilities >= TPACPI_VOL_CAP_MAX)
6839 return -EINVAL;
6840
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006841 /*
6842 * The ALSA mixer is our primary interface.
6843 * When disabled, don't install the subdriver at all
6844 */
6845 if (!alsa_enable) {
6846 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6847 "ALSA mixer disabled by parameter, "
6848 "not loading volume subdriver...\n");
6849 return 1;
6850 }
6851
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006852 quirks = tpacpi_check_quirks(volume_quirk_table,
6853 ARRAY_SIZE(volume_quirk_table));
6854
6855 switch (volume_capabilities) {
6856 case TPACPI_VOL_CAP_AUTO:
6857 if (quirks & TPACPI_VOL_Q_MUTEONLY)
6858 tp_features.mixer_no_level_control = 1;
6859 else if (quirks & TPACPI_VOL_Q_LEVEL)
6860 tp_features.mixer_no_level_control = 0;
6861 else
6862 return 1; /* no mixer */
6863 break;
6864 case TPACPI_VOL_CAP_VOLMUTE:
6865 tp_features.mixer_no_level_control = 0;
6866 break;
6867 case TPACPI_VOL_CAP_MUTEONLY:
6868 tp_features.mixer_no_level_control = 1;
6869 break;
6870 default:
6871 return 1;
6872 }
6873
6874 if (volume_capabilities != TPACPI_VOL_CAP_AUTO)
6875 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6876 "using user-supplied volume_capabilities=%d\n",
6877 volume_capabilities);
6878
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006879 if (volume_mode == TPACPI_VOL_MODE_AUTO ||
6880 volume_mode == TPACPI_VOL_MODE_MAX) {
6881 volume_mode = TPACPI_VOL_MODE_ECNVRAM;
6882
6883 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6884 "driver auto-selected volume_mode=%d\n",
6885 volume_mode);
6886 } else {
6887 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6888 "using user-supplied volume_mode=%d\n",
6889 volume_mode);
6890 }
6891
6892 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006893 "mute is supported, volume control is %s\n",
6894 str_supported(!tp_features.mixer_no_level_control));
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006895
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006896 rc = volume_create_alsa_mixer();
6897 if (rc) {
6898 printk(TPACPI_ERR
6899 "Could not create the ALSA mixer interface\n");
6900 return rc;
6901 }
6902
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006903 printk(TPACPI_INFO
6904 "Console audio control enabled, mode: %s\n",
6905 (volume_control_allowed) ?
6906 "override (read/write)" :
6907 "monitor (read only)");
6908
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006909 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6910 "registering volume hotkeys as change notification\n");
6911 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6912 | TP_ACPI_HKEY_VOLUP_MASK
6913 | TP_ACPI_HKEY_VOLDWN_MASK
6914 | TP_ACPI_HKEY_MUTE_MASK);
6915
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006916 return 0;
6917}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006918
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006919static int volume_read(struct seq_file *m)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006920{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006921 u8 status;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006922
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006923 if (volume_get_status(&status) < 0) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006924 seq_printf(m, "level:\t\tunreadable\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006925 } else {
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006926 if (tp_features.mixer_no_level_control)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006927 seq_printf(m, "level:\t\tunsupported\n");
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006928 else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006929 seq_printf(m, "level:\t\t%d\n",
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006930 status & TP_EC_AUDIO_LVL_MSK);
6931
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006932 seq_printf(m, "mute:\t\t%s\n",
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006933 onoff(status, TP_EC_AUDIO_MUTESW));
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006934
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006935 if (volume_control_allowed) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006936 seq_printf(m, "commands:\tunmute, mute\n");
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006937 if (!tp_features.mixer_no_level_control) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006938 seq_printf(m,
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006939 "commands:\tup, down\n");
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006940 seq_printf(m,
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006941 "commands:\tlevel <level>"
6942 " (<level> is 0-%d)\n",
6943 TP_EC_VOLUME_MAX);
6944 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006945 }
6946 }
6947
6948 return 0;
6949}
6950
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006951static int volume_write(char *buf)
6952{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006953 u8 s;
6954 u8 new_level, new_mute;
6955 int l;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006956 char *cmd;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006957 int rc;
6958
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006959 /*
6960 * We do allow volume control at driver startup, so that the
6961 * user can set initial state through the volume=... parameter hack.
6962 */
6963 if (!volume_control_allowed && tpacpi_lifecycle != TPACPI_LIFE_INIT) {
6964 if (unlikely(!tp_warned.volume_ctrl_forbidden)) {
6965 tp_warned.volume_ctrl_forbidden = 1;
6966 printk(TPACPI_NOTICE
6967 "Console audio control in monitor mode, "
6968 "changes are not allowed.\n");
6969 printk(TPACPI_NOTICE
6970 "Use the volume_control=1 module parameter "
6971 "to enable volume control\n");
6972 }
6973 return -EPERM;
6974 }
6975
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006976 rc = volume_get_status(&s);
6977 if (rc < 0)
6978 return rc;
6979
6980 new_level = s & TP_EC_AUDIO_LVL_MSK;
6981 new_mute = s & TP_EC_AUDIO_MUTESW_MSK;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006982
6983 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006984 if (!tp_features.mixer_no_level_control) {
6985 if (strlencmp(cmd, "up") == 0) {
6986 if (new_mute)
6987 new_mute = 0;
6988 else if (new_level < TP_EC_VOLUME_MAX)
6989 new_level++;
6990 continue;
6991 } else if (strlencmp(cmd, "down") == 0) {
6992 if (new_mute)
6993 new_mute = 0;
6994 else if (new_level > 0)
6995 new_level--;
6996 continue;
6997 } else if (sscanf(cmd, "level %u", &l) == 1 &&
6998 l >= 0 && l <= TP_EC_VOLUME_MAX) {
6999 new_level = l;
7000 continue;
7001 }
7002 }
7003 if (strlencmp(cmd, "mute") == 0)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007004 new_mute = TP_EC_AUDIO_MUTESW_MSK;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007005 else if (strlencmp(cmd, "unmute") == 0)
7006 new_mute = 0;
7007 else
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007008 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007009 }
7010
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007011 if (tp_features.mixer_no_level_control) {
7012 tpacpi_disclose_usertask("procfs volume", "%smute\n",
7013 new_mute ? "" : "un");
7014 rc = volume_set_mute(!!new_mute);
7015 } else {
7016 tpacpi_disclose_usertask("procfs volume",
7017 "%smute and set level to %d\n",
7018 new_mute ? "" : "un", new_level);
7019 rc = volume_set_status(new_mute | new_level);
7020 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007021 volume_alsa_notify_change();
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007022
7023 return (rc == -EINTR) ? -ERESTARTSYS : rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007024}
7025
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007026static struct ibm_struct volume_driver_data = {
7027 .name = "volume",
7028 .read = volume_read,
7029 .write = volume_write,
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007030 .exit = volume_exit,
7031 .suspend = volume_suspend,
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007032 .resume = volume_resume,
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007033 .shutdown = volume_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007034};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007035
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02007036#else /* !CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7037
7038#define alsa_card NULL
7039
7040static void inline volume_alsa_notify_change(void)
7041{
7042}
7043
7044static int __init volume_init(struct ibm_init_struct *iibm)
7045{
7046 printk(TPACPI_INFO
7047 "volume: disabled as there is no ALSA support in this kernel\n");
7048
7049 return 1;
7050}
7051
7052static struct ibm_struct volume_driver_data = {
7053 .name = "volume",
7054};
7055
7056#endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7057
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007058/*************************************************************************
7059 * Fan subdriver
7060 */
7061
7062/*
7063 * FAN ACCESS MODES
7064 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007065 * TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007066 * ACPI GFAN method: returns fan level
7067 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007068 * see TPACPI_FAN_WR_ACPI_SFAN
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007069 * EC 0x2f (HFSP) not available if GFAN exists
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007070 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007071 * TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007072 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
7073 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007074 * EC 0x2f (HFSP) might be available *for reading*, but do not use
7075 * it for writing.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007076 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007077 * TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007078 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
7079 * Supported on almost all ThinkPads
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007080 *
7081 * Fan speed changes of any sort (including those caused by the
7082 * disengaged mode) are usually done slowly by the firmware as the
7083 * maximum ammount of fan duty cycle change per second seems to be
7084 * limited.
7085 *
7086 * Reading is not available if GFAN exists.
7087 * Writing is not available if SFAN exists.
7088 *
7089 * Bits
7090 * 7 automatic mode engaged;
7091 * (default operation mode of the ThinkPad)
7092 * fan level is ignored in this mode.
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007093 * 6 full speed mode (takes precedence over bit 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007094 * not available on all thinkpads. May disable
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007095 * the tachometer while the fan controller ramps up
7096 * the speed (which can take up to a few *minutes*).
7097 * Speeds up fan to 100% duty-cycle, which is far above
7098 * the standard RPM levels. It is not impossible that
7099 * it could cause hardware damage.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007100 * 5-3 unused in some models. Extra bits for fan level
7101 * in others, but still useless as all values above
7102 * 7 map to the same speed as level 7 in these models.
7103 * 2-0 fan level (0..7 usually)
7104 * 0x00 = stop
7105 * 0x07 = max (set when temperatures critical)
7106 * Some ThinkPads may have other levels, see
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007107 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007108 *
7109 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
7110 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
7111 * does so, its initial value is meaningless (0x07).
7112 *
7113 * For firmware bugs, refer to:
7114 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7115 *
7116 * ----
7117 *
7118 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
7119 * Main fan tachometer reading (in RPM)
7120 *
7121 * This register is present on all ThinkPads with a new-style EC, and
7122 * it is known not to be present on the A21m/e, and T22, as there is
7123 * something else in offset 0x84 according to the ACPI DSDT. Other
7124 * ThinkPads from this same time period (and earlier) probably lack the
7125 * tachometer as well.
7126 *
Nick Andrew877d0312009-01-26 11:06:57 +01007127 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007128 * was never fixed by IBM to report the EC firmware version string
7129 * probably support the tachometer (like the early X models), so
7130 * detecting it is quite hard. We need more data to know for sure.
7131 *
7132 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
7133 * might result.
7134 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007135 * FIRMWARE BUG: may go stale while the EC is switching to full speed
7136 * mode.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007137 *
7138 * For firmware bugs, refer to:
7139 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7140 *
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007141 * ----
7142 *
7143 * ThinkPad EC register 0x31 bit 0 (only on select models)
7144 *
7145 * When bit 0 of EC register 0x31 is zero, the tachometer registers
7146 * show the speed of the main fan. When bit 0 of EC register 0x31
7147 * is one, the tachometer registers show the speed of the auxiliary
7148 * fan.
7149 *
7150 * Fan control seems to affect both fans, regardless of the state
7151 * of this bit.
7152 *
7153 * So far, only the firmware for the X60/X61 non-tablet versions
7154 * seem to support this (firmware TP-7M).
7155 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007156 * TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007157 * ThinkPad X31, X40, X41. Not available in the X60.
7158 *
7159 * FANS ACPI handle: takes three arguments: low speed, medium speed,
7160 * high speed. ACPI DSDT seems to map these three speeds to levels
7161 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
7162 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
7163 *
7164 * The speeds are stored on handles
7165 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
7166 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007167 * There are three default speed sets, accessible as handles:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007168 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
7169 *
7170 * ACPI DSDT switches which set is in use depending on various
7171 * factors.
7172 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007173 * TPACPI_FAN_WR_TPEC is also available and should be used to
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007174 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
7175 * but the ACPI tables just mention level 7.
7176 */
7177
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007178enum { /* Fan control constants */
7179 fan_status_offset = 0x2f, /* EC register 0x2f */
7180 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
7181 * 0x84 must be read before 0x85 */
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007182 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
7183 bit 0 selects which fan is active */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007184
7185 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
7186 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
7187
7188 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
7189};
7190
7191enum fan_status_access_mode {
7192 TPACPI_FAN_NONE = 0, /* No fan status or control */
7193 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
7194 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
7195};
7196
7197enum fan_control_access_mode {
7198 TPACPI_FAN_WR_NONE = 0, /* No fan control */
7199 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
7200 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
7201 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
7202};
7203
7204enum fan_control_commands {
7205 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
7206 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
7207 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
7208 * and also watchdog cmd */
7209};
7210
7211static int fan_control_allowed;
7212
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007213static enum fan_status_access_mode fan_status_access_mode;
7214static enum fan_control_access_mode fan_control_access_mode;
7215static enum fan_control_commands fan_control_commands;
7216
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02007217static u8 fan_control_initial_status;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007218static u8 fan_control_desired_level;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007219static u8 fan_control_resume_level;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007220static int fan_watchdog_maxinterval;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007221
7222static struct mutex fan_mutex;
7223
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007224static void fan_watchdog_fire(struct work_struct *ignored);
Len Brown25c68a32006-12-08 04:43:41 -05007225static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007226
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007227TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
7228TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007229 "\\FSPD", /* 600e/x, 770e, 770x */
7230 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007231TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007232 "JFNS", /* 770x-JL */
7233 ); /* all others */
7234
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007235/*
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007236 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
7237 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
7238 * be in auto mode (0x80).
7239 *
7240 * This is corrected by any write to HFSP either by the driver, or
7241 * by the firmware.
7242 *
7243 * We assume 0x07 really means auto mode while this quirk is active,
7244 * as this is far more likely than the ThinkPad being in level 7,
7245 * which is only used by the firmware during thermal emergencies.
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007246 *
7247 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
7248 * TP-70 (T43, R52), which are known to be buggy.
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007249 */
7250
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007251static void fan_quirk1_setup(void)
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007252{
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007253 if (fan_control_initial_status == 0x07) {
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007254 printk(TPACPI_NOTICE
7255 "fan_init: initial fan status is unknown, "
7256 "assuming it is in auto mode\n");
7257 tp_features.fan_ctrl_status_undef = 1;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007258 }
7259}
7260
7261static void fan_quirk1_handle(u8 *fan_status)
7262{
7263 if (unlikely(tp_features.fan_ctrl_status_undef)) {
7264 if (*fan_status != fan_control_initial_status) {
7265 /* something changed the HFSP regisnter since
7266 * driver init time, so it is not undefined
7267 * anymore */
7268 tp_features.fan_ctrl_status_undef = 0;
7269 } else {
7270 /* Return most likely status. In fact, it
7271 * might be the only possible status */
7272 *fan_status = TP_EC_FAN_AUTO;
7273 }
7274 }
7275}
7276
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007277/* Select main fan on X60/X61, NOOP on others */
7278static bool fan_select_fan1(void)
7279{
7280 if (tp_features.second_fan) {
7281 u8 val;
7282
7283 if (ec_read(fan_select_offset, &val) < 0)
7284 return false;
7285 val &= 0xFEU;
7286 if (ec_write(fan_select_offset, val) < 0)
7287 return false;
7288 }
7289 return true;
7290}
7291
7292/* Select secondary fan on X60/X61 */
7293static bool fan_select_fan2(void)
7294{
7295 u8 val;
7296
7297 if (!tp_features.second_fan)
7298 return false;
7299
7300 if (ec_read(fan_select_offset, &val) < 0)
7301 return false;
7302 val |= 0x01U;
7303 if (ec_write(fan_select_offset, val) < 0)
7304 return false;
7305
7306 return true;
7307}
7308
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007309/*
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007310 * Call with fan_mutex held
7311 */
7312static void fan_update_desired_level(u8 status)
7313{
7314 if ((status &
7315 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7316 if (status > 7)
7317 fan_control_desired_level = 7;
7318 else
7319 fan_control_desired_level = status;
7320 }
7321}
7322
7323static int fan_get_status(u8 *status)
7324{
7325 u8 s;
7326
7327 /* TODO:
7328 * Add TPACPI_FAN_RD_ACPI_FANS ? */
7329
7330 switch (fan_status_access_mode) {
7331 case TPACPI_FAN_RD_ACPI_GFAN:
7332 /* 570, 600e/x, 770e, 770x */
7333
7334 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
7335 return -EIO;
7336
7337 if (likely(status))
7338 *status = s & 0x07;
7339
7340 break;
7341
7342 case TPACPI_FAN_RD_TPEC:
7343 /* all except 570, 600e/x, 770e, 770x */
7344 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
7345 return -EIO;
7346
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007347 if (likely(status)) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007348 *status = s;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007349 fan_quirk1_handle(status);
7350 }
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007351
7352 break;
7353
7354 default:
7355 return -ENXIO;
7356 }
7357
7358 return 0;
7359}
7360
7361static int fan_get_status_safe(u8 *status)
7362{
7363 int rc;
7364 u8 s;
7365
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007366 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007367 return -ERESTARTSYS;
7368 rc = fan_get_status(&s);
7369 if (!rc)
7370 fan_update_desired_level(s);
7371 mutex_unlock(&fan_mutex);
7372
7373 if (status)
7374 *status = s;
7375
7376 return rc;
7377}
7378
7379static int fan_get_speed(unsigned int *speed)
7380{
7381 u8 hi, lo;
7382
7383 switch (fan_status_access_mode) {
7384 case TPACPI_FAN_RD_TPEC:
7385 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007386 if (unlikely(!fan_select_fan1()))
7387 return -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007388 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
7389 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
7390 return -EIO;
7391
7392 if (likely(speed))
7393 *speed = (hi << 8) | lo;
7394
7395 break;
7396
7397 default:
7398 return -ENXIO;
7399 }
7400
7401 return 0;
7402}
7403
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007404static int fan2_get_speed(unsigned int *speed)
7405{
7406 u8 hi, lo;
7407 bool rc;
7408
7409 switch (fan_status_access_mode) {
7410 case TPACPI_FAN_RD_TPEC:
7411 /* all except 570, 600e/x, 770e, 770x */
7412 if (unlikely(!fan_select_fan2()))
7413 return -EIO;
7414 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
7415 !acpi_ec_read(fan_rpm_offset + 1, &hi);
7416 fan_select_fan1(); /* play it safe */
7417 if (rc)
7418 return -EIO;
7419
7420 if (likely(speed))
7421 *speed = (hi << 8) | lo;
7422
7423 break;
7424
7425 default:
7426 return -ENXIO;
7427 }
7428
7429 return 0;
7430}
7431
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007432static int fan_set_level(int level)
7433{
7434 if (!fan_control_allowed)
7435 return -EPERM;
7436
7437 switch (fan_control_access_mode) {
7438 case TPACPI_FAN_WR_ACPI_SFAN:
7439 if (level >= 0 && level <= 7) {
7440 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
7441 return -EIO;
7442 } else
7443 return -EINVAL;
7444 break;
7445
7446 case TPACPI_FAN_WR_ACPI_FANS:
7447 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007448 if (!(level & TP_EC_FAN_AUTO) &&
7449 !(level & TP_EC_FAN_FULLSPEED) &&
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007450 ((level < 0) || (level > 7)))
7451 return -EINVAL;
7452
7453 /* safety net should the EC not support AUTO
7454 * or FULLSPEED mode bits and just ignore them */
7455 if (level & TP_EC_FAN_FULLSPEED)
7456 level |= 7; /* safety min speed 7 */
Roel Kluin547266e2008-02-05 00:24:56 +01007457 else if (level & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007458 level |= 4; /* safety min speed 4 */
7459
7460 if (!acpi_ec_write(fan_status_offset, level))
7461 return -EIO;
7462 else
7463 tp_features.fan_ctrl_status_undef = 0;
7464 break;
7465
7466 default:
7467 return -ENXIO;
7468 }
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007469
7470 vdbg_printk(TPACPI_DBG_FAN,
7471 "fan control: set fan control register to 0x%02x\n", level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007472 return 0;
7473}
7474
7475static int fan_set_level_safe(int level)
7476{
7477 int rc;
7478
7479 if (!fan_control_allowed)
7480 return -EPERM;
7481
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007482 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007483 return -ERESTARTSYS;
7484
7485 if (level == TPACPI_FAN_LAST_LEVEL)
7486 level = fan_control_desired_level;
7487
7488 rc = fan_set_level(level);
7489 if (!rc)
7490 fan_update_desired_level(level);
7491
7492 mutex_unlock(&fan_mutex);
7493 return rc;
7494}
7495
7496static int fan_set_enable(void)
7497{
7498 u8 s;
7499 int rc;
7500
7501 if (!fan_control_allowed)
7502 return -EPERM;
7503
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007504 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007505 return -ERESTARTSYS;
7506
7507 switch (fan_control_access_mode) {
7508 case TPACPI_FAN_WR_ACPI_FANS:
7509 case TPACPI_FAN_WR_TPEC:
7510 rc = fan_get_status(&s);
7511 if (rc < 0)
7512 break;
7513
7514 /* Don't go out of emergency fan mode */
7515 if (s != 7) {
7516 s &= 0x07;
7517 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
7518 }
7519
7520 if (!acpi_ec_write(fan_status_offset, s))
7521 rc = -EIO;
7522 else {
7523 tp_features.fan_ctrl_status_undef = 0;
7524 rc = 0;
7525 }
7526 break;
7527
7528 case TPACPI_FAN_WR_ACPI_SFAN:
7529 rc = fan_get_status(&s);
7530 if (rc < 0)
7531 break;
7532
7533 s &= 0x07;
7534
7535 /* Set fan to at least level 4 */
7536 s |= 4;
7537
7538 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007539 rc = -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007540 else
7541 rc = 0;
7542 break;
7543
7544 default:
7545 rc = -ENXIO;
7546 }
7547
7548 mutex_unlock(&fan_mutex);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007549
7550 if (!rc)
7551 vdbg_printk(TPACPI_DBG_FAN,
7552 "fan control: set fan control register to 0x%02x\n",
7553 s);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007554 return rc;
7555}
7556
7557static int fan_set_disable(void)
7558{
7559 int rc;
7560
7561 if (!fan_control_allowed)
7562 return -EPERM;
7563
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007564 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007565 return -ERESTARTSYS;
7566
7567 rc = 0;
7568 switch (fan_control_access_mode) {
7569 case TPACPI_FAN_WR_ACPI_FANS:
7570 case TPACPI_FAN_WR_TPEC:
7571 if (!acpi_ec_write(fan_status_offset, 0x00))
7572 rc = -EIO;
7573 else {
7574 fan_control_desired_level = 0;
7575 tp_features.fan_ctrl_status_undef = 0;
7576 }
7577 break;
7578
7579 case TPACPI_FAN_WR_ACPI_SFAN:
7580 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
7581 rc = -EIO;
7582 else
7583 fan_control_desired_level = 0;
7584 break;
7585
7586 default:
7587 rc = -ENXIO;
7588 }
7589
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007590 if (!rc)
7591 vdbg_printk(TPACPI_DBG_FAN,
7592 "fan control: set fan control register to 0\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007593
7594 mutex_unlock(&fan_mutex);
7595 return rc;
7596}
7597
7598static int fan_set_speed(int speed)
7599{
7600 int rc;
7601
7602 if (!fan_control_allowed)
7603 return -EPERM;
7604
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007605 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007606 return -ERESTARTSYS;
7607
7608 rc = 0;
7609 switch (fan_control_access_mode) {
7610 case TPACPI_FAN_WR_ACPI_FANS:
7611 if (speed >= 0 && speed <= 65535) {
7612 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
7613 speed, speed, speed))
7614 rc = -EIO;
7615 } else
7616 rc = -EINVAL;
7617 break;
7618
7619 default:
7620 rc = -ENXIO;
7621 }
7622
7623 mutex_unlock(&fan_mutex);
7624 return rc;
7625}
7626
7627static void fan_watchdog_reset(void)
7628{
7629 static int fan_watchdog_active;
7630
7631 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
7632 return;
7633
7634 if (fan_watchdog_active)
7635 cancel_delayed_work(&fan_watchdog_task);
7636
7637 if (fan_watchdog_maxinterval > 0 &&
7638 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
7639 fan_watchdog_active = 1;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03007640 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007641 msecs_to_jiffies(fan_watchdog_maxinterval
7642 * 1000))) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007643 printk(TPACPI_ERR
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03007644 "failed to queue the fan watchdog, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007645 "watchdog will not trigger\n");
7646 }
7647 } else
7648 fan_watchdog_active = 0;
7649}
7650
7651static void fan_watchdog_fire(struct work_struct *ignored)
7652{
7653 int rc;
7654
7655 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
7656 return;
7657
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007658 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007659 rc = fan_set_enable();
7660 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007661 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007662 "will try again later...\n", -rc);
7663 /* reschedule for later */
7664 fan_watchdog_reset();
7665 }
7666}
7667
7668/*
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007669 * SYSFS fan layout: hwmon compatible (device)
7670 *
7671 * pwm*_enable:
7672 * 0: "disengaged" mode
7673 * 1: manual mode
7674 * 2: native EC "auto" mode (recommended, hardware default)
7675 *
7676 * pwm*: set speed in manual mode, ignored otherwise.
7677 * 0 is level 0; 255 is level 7. Intermediate points done with linear
7678 * interpolation.
7679 *
7680 * fan*_input: tachometer reading, RPM
7681 *
7682 *
7683 * SYSFS fan layout: extensions
7684 *
7685 * fan_watchdog (driver):
7686 * fan watchdog interval in seconds, 0 disables (default), max 120
7687 */
7688
7689/* sysfs fan pwm1_enable ----------------------------------------------- */
7690static ssize_t fan_pwm1_enable_show(struct device *dev,
7691 struct device_attribute *attr,
7692 char *buf)
7693{
7694 int res, mode;
7695 u8 status;
7696
7697 res = fan_get_status_safe(&status);
7698 if (res)
7699 return res;
7700
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007701 if (status & TP_EC_FAN_FULLSPEED) {
7702 mode = 0;
7703 } else if (status & TP_EC_FAN_AUTO) {
7704 mode = 2;
7705 } else
7706 mode = 1;
7707
7708 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
7709}
7710
7711static ssize_t fan_pwm1_enable_store(struct device *dev,
7712 struct device_attribute *attr,
7713 const char *buf, size_t count)
7714{
7715 unsigned long t;
7716 int res, level;
7717
7718 if (parse_strtoul(buf, 2, &t))
7719 return -EINVAL;
7720
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007721 tpacpi_disclose_usertask("hwmon pwm1_enable",
7722 "set fan mode to %lu\n", t);
7723
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007724 switch (t) {
7725 case 0:
7726 level = TP_EC_FAN_FULLSPEED;
7727 break;
7728 case 1:
7729 level = TPACPI_FAN_LAST_LEVEL;
7730 break;
7731 case 2:
7732 level = TP_EC_FAN_AUTO;
7733 break;
7734 case 3:
7735 /* reserved for software-controlled auto mode */
7736 return -ENOSYS;
7737 default:
7738 return -EINVAL;
7739 }
7740
7741 res = fan_set_level_safe(level);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03007742 if (res == -ENXIO)
7743 return -EINVAL;
7744 else if (res < 0)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007745 return res;
7746
7747 fan_watchdog_reset();
7748
7749 return count;
7750}
7751
7752static struct device_attribute dev_attr_fan_pwm1_enable =
7753 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
7754 fan_pwm1_enable_show, fan_pwm1_enable_store);
7755
7756/* sysfs fan pwm1 ------------------------------------------------------ */
7757static ssize_t fan_pwm1_show(struct device *dev,
7758 struct device_attribute *attr,
7759 char *buf)
7760{
7761 int res;
7762 u8 status;
7763
7764 res = fan_get_status_safe(&status);
7765 if (res)
7766 return res;
7767
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007768 if ((status &
7769 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
7770 status = fan_control_desired_level;
7771
7772 if (status > 7)
7773 status = 7;
7774
7775 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
7776}
7777
7778static ssize_t fan_pwm1_store(struct device *dev,
7779 struct device_attribute *attr,
7780 const char *buf, size_t count)
7781{
7782 unsigned long s;
7783 int rc;
7784 u8 status, newlevel;
7785
7786 if (parse_strtoul(buf, 255, &s))
7787 return -EINVAL;
7788
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007789 tpacpi_disclose_usertask("hwmon pwm1",
7790 "set fan speed to %lu\n", s);
7791
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007792 /* scale down from 0-255 to 0-7 */
7793 newlevel = (s >> 5) & 0x07;
7794
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007795 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02007796 return -ERESTARTSYS;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007797
7798 rc = fan_get_status(&status);
7799 if (!rc && (status &
7800 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7801 rc = fan_set_level(newlevel);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03007802 if (rc == -ENXIO)
7803 rc = -EINVAL;
7804 else if (!rc) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007805 fan_update_desired_level(newlevel);
Henrique de Moraes Holschuhca4ac2f2007-04-27 22:00:11 -03007806 fan_watchdog_reset();
7807 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007808 }
7809
7810 mutex_unlock(&fan_mutex);
7811 return (rc)? rc : count;
7812}
7813
7814static struct device_attribute dev_attr_fan_pwm1 =
7815 __ATTR(pwm1, S_IWUSR | S_IRUGO,
7816 fan_pwm1_show, fan_pwm1_store);
7817
7818/* sysfs fan fan1_input ------------------------------------------------ */
7819static ssize_t fan_fan1_input_show(struct device *dev,
7820 struct device_attribute *attr,
7821 char *buf)
7822{
7823 int res;
7824 unsigned int speed;
7825
7826 res = fan_get_speed(&speed);
7827 if (res < 0)
7828 return res;
7829
7830 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7831}
7832
7833static struct device_attribute dev_attr_fan_fan1_input =
7834 __ATTR(fan1_input, S_IRUGO,
7835 fan_fan1_input_show, NULL);
7836
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007837/* sysfs fan fan2_input ------------------------------------------------ */
7838static ssize_t fan_fan2_input_show(struct device *dev,
7839 struct device_attribute *attr,
7840 char *buf)
7841{
7842 int res;
7843 unsigned int speed;
7844
7845 res = fan2_get_speed(&speed);
7846 if (res < 0)
7847 return res;
7848
7849 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7850}
7851
7852static struct device_attribute dev_attr_fan_fan2_input =
7853 __ATTR(fan2_input, S_IRUGO,
7854 fan_fan2_input_show, NULL);
7855
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007856/* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007857static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
7858 char *buf)
7859{
7860 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
7861}
7862
7863static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
7864 const char *buf, size_t count)
7865{
7866 unsigned long t;
7867
7868 if (parse_strtoul(buf, 120, &t))
7869 return -EINVAL;
7870
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03007871 if (!fan_control_allowed)
7872 return -EPERM;
7873
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007874 fan_watchdog_maxinterval = t;
7875 fan_watchdog_reset();
7876
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007877 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
7878
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007879 return count;
7880}
7881
7882static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
7883 fan_fan_watchdog_show, fan_fan_watchdog_store);
7884
7885/* --------------------------------------------------------------------- */
7886static struct attribute *fan_attributes[] = {
7887 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
7888 &dev_attr_fan_fan1_input.attr,
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007889 NULL, /* for fan2_input */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007890 NULL
7891};
7892
7893static const struct attribute_group fan_attr_group = {
7894 .attrs = fan_attributes,
7895};
7896
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007897#define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
7898#define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007899
7900#define TPACPI_FAN_QI(__id1, __id2, __quirks) \
7901 { .vendor = PCI_VENDOR_ID_IBM, \
7902 .bios = TPACPI_MATCH_ANY, \
7903 .ec = TPID(__id1, __id2), \
7904 .quirks = __quirks }
7905
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007906#define TPACPI_FAN_QL(__id1, __id2, __quirks) \
7907 { .vendor = PCI_VENDOR_ID_LENOVO, \
7908 .bios = TPACPI_MATCH_ANY, \
7909 .ec = TPID(__id1, __id2), \
7910 .quirks = __quirks }
7911
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007912static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
7913 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
7914 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
7915 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
7916 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007917 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007918};
7919
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007920#undef TPACPI_FAN_QL
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007921#undef TPACPI_FAN_QI
7922
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007923static int __init fan_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007924{
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007925 int rc;
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007926 unsigned long quirks;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007927
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007928 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7929 "initializing fan subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007930
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03007931 mutex_init(&fan_mutex);
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007932 fan_status_access_mode = TPACPI_FAN_NONE;
7933 fan_control_access_mode = TPACPI_FAN_WR_NONE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007934 fan_control_commands = 0;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007935 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03007936 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007937 tp_features.second_fan = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007938 fan_control_desired_level = 7;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007939
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007940 TPACPI_ACPIHANDLE_INIT(fans);
7941 TPACPI_ACPIHANDLE_INIT(gfan);
7942 TPACPI_ACPIHANDLE_INIT(sfan);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007943
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007944 quirks = tpacpi_check_quirks(fan_quirk_table,
7945 ARRAY_SIZE(fan_quirk_table));
7946
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007947 if (gfan_handle) {
7948 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007949 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007950 } else {
7951 /* all other ThinkPads: note that even old-style
7952 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02007953 if (likely(acpi_ec_read(fan_status_offset,
7954 &fan_control_initial_status))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007955 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007956 if (quirks & TPACPI_FAN_Q1)
7957 fan_quirk1_setup();
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007958 if (quirks & TPACPI_FAN_2FAN) {
7959 tp_features.second_fan = 1;
7960 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7961 "secondary fan support enabled\n");
7962 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007963 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007964 printk(TPACPI_ERR
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007965 "ThinkPad ACPI EC access misbehaving, "
7966 "fan status and control unavailable\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007967 return 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007968 }
7969 }
7970
7971 if (sfan_handle) {
7972 /* 570, 770x-JL */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007973 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02007974 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007975 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007976 } else {
7977 if (!gfan_handle) {
7978 /* gfan without sfan means no fan control */
7979 /* all other models implement TP EC 0x2f control */
7980
7981 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02007982 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007983 fan_control_access_mode =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007984 TPACPI_FAN_WR_ACPI_FANS;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007985 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007986 TPACPI_FAN_CMD_SPEED |
7987 TPACPI_FAN_CMD_LEVEL |
7988 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007989 } else {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007990 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02007991 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007992 TPACPI_FAN_CMD_LEVEL |
7993 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007994 }
7995 }
7996 }
7997
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007998 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7999 "fan is %s, modes %d, %d\n",
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008000 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
8001 fan_control_access_mode != TPACPI_FAN_WR_NONE),
8002 fan_status_access_mode, fan_control_access_mode);
8003
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008004 /* fan control master switch */
8005 if (!fan_control_allowed) {
8006 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8007 fan_control_commands = 0;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008008 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008009 "fan control features disabled by parameter\n");
8010 }
8011
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008012 /* update fan_control_desired_level */
8013 if (fan_status_access_mode != TPACPI_FAN_NONE)
8014 fan_get_status_safe(NULL);
8015
8016 if (fan_status_access_mode != TPACPI_FAN_NONE ||
8017 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008018 if (tp_features.second_fan) {
8019 /* attach second fan tachometer */
8020 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
8021 &dev_attr_fan_fan2_input.attr;
8022 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008023 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008024 &fan_attr_group);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008025 if (rc < 0)
8026 return rc;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03008027
8028 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
8029 &driver_attr_fan_watchdog);
8030 if (rc < 0) {
8031 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
8032 &fan_attr_group);
8033 return rc;
8034 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008035 return 0;
8036 } else
8037 return 1;
8038}
8039
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008040static void fan_exit(void)
8041{
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008042 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008043 "cancelling any pending fan watchdog tasks\n");
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008044
8045 /* FIXME: can we really do this unconditionally? */
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008046 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008047 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
8048 &driver_attr_fan_watchdog);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008049
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008050 cancel_delayed_work(&fan_watchdog_task);
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03008051 flush_workqueue(tpacpi_wq);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008052}
8053
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008054static void fan_suspend(pm_message_t state)
8055{
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008056 int rc;
8057
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008058 if (!fan_control_allowed)
8059 return;
8060
8061 /* Store fan status in cache */
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008062 fan_control_resume_level = 0;
8063 rc = fan_get_status_safe(&fan_control_resume_level);
8064 if (rc < 0)
8065 printk(TPACPI_NOTICE
8066 "failed to read fan level for later "
8067 "restore during resume: %d\n", rc);
8068
8069 /* if it is undefined, don't attempt to restore it.
8070 * KEEP THIS LAST */
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008071 if (tp_features.fan_ctrl_status_undef)
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008072 fan_control_resume_level = 0;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008073}
8074
8075static void fan_resume(void)
8076{
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008077 u8 current_level = 7;
8078 bool do_set = false;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008079 int rc;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008080
8081 /* DSDT *always* updates status on resume */
8082 tp_features.fan_ctrl_status_undef = 0;
8083
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008084 if (!fan_control_allowed ||
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008085 !fan_control_resume_level ||
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008086 (fan_get_status_safe(&current_level) < 0))
8087 return;
8088
8089 switch (fan_control_access_mode) {
8090 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008091 /* never decrease fan level */
8092 do_set = (fan_control_resume_level > current_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008093 break;
8094 case TPACPI_FAN_WR_ACPI_FANS:
8095 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008096 /* never decrease fan level, scale is:
8097 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
8098 *
8099 * We expect the firmware to set either 7 or AUTO, but we
8100 * handle FULLSPEED out of paranoia.
8101 *
8102 * So, we can safely only restore FULLSPEED or 7, anything
8103 * else could slow the fan. Restoring AUTO is useless, at
8104 * best that's exactly what the DSDT already set (it is the
8105 * slower it uses).
8106 *
8107 * Always keep in mind that the DSDT *will* have set the
8108 * fans to what the vendor supposes is the best level. We
8109 * muck with it only to speed the fan up.
8110 */
8111 if (fan_control_resume_level != 7 &&
8112 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
8113 return;
8114 else
8115 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
8116 (current_level != fan_control_resume_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008117 break;
8118 default:
8119 return;
8120 }
8121 if (do_set) {
8122 printk(TPACPI_NOTICE
8123 "restoring fan level to 0x%02x\n",
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008124 fan_control_resume_level);
8125 rc = fan_set_level_safe(fan_control_resume_level);
8126 if (rc < 0)
8127 printk(TPACPI_NOTICE
8128 "failed to restore fan level: %d\n", rc);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008129 }
8130}
8131
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008132static int fan_read(struct seq_file *m)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008133{
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008134 int rc;
8135 u8 status;
8136 unsigned int speed = 0;
8137
8138 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008139 case TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008140 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008141 rc = fan_get_status_safe(&status);
8142 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008143 return rc;
8144
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008145 seq_printf(m, "status:\t\t%s\n"
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008146 "level:\t\t%d\n",
8147 (status != 0) ? "enabled" : "disabled", status);
8148 break;
8149
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008150 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008151 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008152 rc = fan_get_status_safe(&status);
8153 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008154 return rc;
8155
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008156 seq_printf(m, "status:\t\t%s\n",
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008157 (status != 0) ? "enabled" : "disabled");
8158
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008159 rc = fan_get_speed(&speed);
8160 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008161 return rc;
8162
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008163 seq_printf(m, "speed:\t\t%d\n", speed);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008164
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008165 if (status & TP_EC_FAN_FULLSPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008166 /* Disengaged mode takes precedence */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008167 seq_printf(m, "level:\t\tdisengaged\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008168 else if (status & TP_EC_FAN_AUTO)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008169 seq_printf(m, "level:\t\tauto\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008170 else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008171 seq_printf(m, "level:\t\t%d\n", status);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008172 break;
8173
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008174 case TPACPI_FAN_NONE:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008175 default:
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008176 seq_printf(m, "status:\t\tnot supported\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008177 }
8178
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008179 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008180 seq_printf(m, "commands:\tlevel <level>");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008181
8182 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008183 case TPACPI_FAN_WR_ACPI_SFAN:
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008184 seq_printf(m, " (<level> is 0-7)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008185 break;
8186
8187 default:
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008188 seq_printf(m, " (<level> is 0-7, "
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008189 "auto, disengaged, full-speed)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008190 break;
8191 }
8192 }
8193
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008194 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008195 seq_printf(m, "commands:\tenable, disable\n"
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008196 "commands:\twatchdog <timeout> (<timeout> "
8197 "is 0 (off), 1-120 (seconds))\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008198
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008199 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008200 seq_printf(m, "commands:\tspeed <speed>"
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008201 " (<speed> is 0-65535)\n");
8202
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008203 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008204}
8205
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008206static int fan_write_cmd_level(const char *cmd, int *rc)
8207{
8208 int level;
8209
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02008210 if (strlencmp(cmd, "level auto") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008211 level = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008212 else if ((strlencmp(cmd, "level disengaged") == 0) |
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008213 (strlencmp(cmd, "level full-speed") == 0))
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008214 level = TP_EC_FAN_FULLSPEED;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02008215 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008216 return 0;
8217
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008218 *rc = fan_set_level_safe(level);
8219 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008220 printk(TPACPI_ERR "level command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008221 "access mode %d", fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008222 else if (!*rc)
8223 tpacpi_disclose_usertask("procfs fan",
8224 "set level to %d\n", level);
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008225
8226 return 1;
8227}
8228
8229static int fan_write_cmd_enable(const char *cmd, int *rc)
8230{
8231 if (strlencmp(cmd, "enable") != 0)
8232 return 0;
8233
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008234 *rc = fan_set_enable();
8235 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008236 printk(TPACPI_ERR "enable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008237 "access mode %d", fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008238 else if (!*rc)
8239 tpacpi_disclose_usertask("procfs fan", "enable\n");
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008240
8241 return 1;
8242}
8243
8244static int fan_write_cmd_disable(const char *cmd, int *rc)
8245{
8246 if (strlencmp(cmd, "disable") != 0)
8247 return 0;
8248
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008249 *rc = fan_set_disable();
8250 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008251 printk(TPACPI_ERR "disable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008252 "access mode %d", fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008253 else if (!*rc)
8254 tpacpi_disclose_usertask("procfs fan", "disable\n");
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008255
8256 return 1;
8257}
8258
8259static int fan_write_cmd_speed(const char *cmd, int *rc)
8260{
8261 int speed;
8262
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02008263 /* TODO:
8264 * Support speed <low> <medium> <high> ? */
8265
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008266 if (sscanf(cmd, "speed %d", &speed) != 1)
8267 return 0;
8268
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008269 *rc = fan_set_speed(speed);
8270 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008271 printk(TPACPI_ERR "speed command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008272 "access mode %d", fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008273 else if (!*rc)
8274 tpacpi_disclose_usertask("procfs fan",
8275 "set speed to %d\n", speed);
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008276
8277 return 1;
8278}
8279
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008280static int fan_write_cmd_watchdog(const char *cmd, int *rc)
8281{
8282 int interval;
8283
8284 if (sscanf(cmd, "watchdog %d", &interval) != 1)
8285 return 0;
8286
8287 if (interval < 0 || interval > 120)
8288 *rc = -EINVAL;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008289 else {
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008290 fan_watchdog_maxinterval = interval;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008291 tpacpi_disclose_usertask("procfs fan",
8292 "set watchdog timer to %d\n",
8293 interval);
8294 }
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008295
8296 return 1;
8297}
8298
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008299static int fan_write(char *buf)
8300{
8301 char *cmd;
8302 int rc = 0;
8303
8304 while (!rc && (cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008305 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008306 fan_write_cmd_level(cmd, &rc)) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008307 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008308 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008309 fan_write_cmd_disable(cmd, &rc) ||
8310 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008311 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008312 fan_write_cmd_speed(cmd, &rc))
8313 )
8314 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008315 else if (!rc)
8316 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008317 }
8318
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008319 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008320}
8321
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008322static struct ibm_struct fan_driver_data = {
8323 .name = "fan",
8324 .read = fan_read,
8325 .write = fan_write,
8326 .exit = fan_exit,
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008327 .suspend = fan_suspend,
8328 .resume = fan_resume,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008329};
8330
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008331/****************************************************************************
8332 ****************************************************************************
8333 *
8334 * Infrastructure
8335 *
8336 ****************************************************************************
8337 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07008338
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03008339/*
8340 * HKEY event callout for other subdrivers go here
8341 * (yes, it is ugly, but it is quick, safe, and gets the job done
8342 */
8343static void tpacpi_driver_event(const unsigned int hkey_event)
8344{
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00008345 if (ibm_backlight_device) {
8346 switch (hkey_event) {
8347 case TP_HKEY_EV_BRGHT_UP:
8348 case TP_HKEY_EV_BRGHT_DOWN:
8349 tpacpi_brightness_notify_change();
8350 }
8351 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02008352 if (alsa_card) {
8353 switch (hkey_event) {
8354 case TP_HKEY_EV_VOL_UP:
8355 case TP_HKEY_EV_VOL_DOWN:
8356 case TP_HKEY_EV_VOL_MUTE:
8357 volume_alsa_notify_change();
8358 }
8359 }
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03008360}
8361
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03008362static void hotkey_driver_event(const unsigned int scancode)
8363{
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03008364 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode);
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03008365}
8366
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008367/* sysfs name ---------------------------------------------------------- */
8368static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
8369 struct device_attribute *attr,
8370 char *buf)
8371{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008372 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008373}
8374
8375static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
8376 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
8377
8378/* --------------------------------------------------------------------- */
8379
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008380/* /proc support */
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03008381static struct proc_dir_entry *proc_dir;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008382
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008383/*
8384 * Module and infrastructure proble, init and exit handling
8385 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008386
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008387static int force_load;
8388
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008389#ifdef CONFIG_THINKPAD_ACPI_DEBUG
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008390static const char * __init str_supported(int is_supported)
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008391{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008392 static char text_unsupported[] __initdata = "not supported";
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008393
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008394 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008395}
8396#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
8397
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008398static void ibm_exit(struct ibm_struct *ibm)
8399{
8400 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
8401
8402 list_del_init(&ibm->all_drivers);
8403
8404 if (ibm->flags.acpi_notify_installed) {
8405 dbg_printk(TPACPI_DBG_EXIT,
8406 "%s: acpi_remove_notify_handler\n", ibm->name);
8407 BUG_ON(!ibm->acpi);
8408 acpi_remove_notify_handler(*ibm->acpi->handle,
8409 ibm->acpi->type,
8410 dispatch_acpi_notify);
8411 ibm->flags.acpi_notify_installed = 0;
8412 ibm->flags.acpi_notify_installed = 0;
8413 }
8414
8415 if (ibm->flags.proc_created) {
8416 dbg_printk(TPACPI_DBG_EXIT,
8417 "%s: remove_proc_entry\n", ibm->name);
8418 remove_proc_entry(ibm->name, proc_dir);
8419 ibm->flags.proc_created = 0;
8420 }
8421
8422 if (ibm->flags.acpi_driver_registered) {
8423 dbg_printk(TPACPI_DBG_EXIT,
8424 "%s: acpi_bus_unregister_driver\n", ibm->name);
8425 BUG_ON(!ibm->acpi);
8426 acpi_bus_unregister_driver(ibm->acpi->driver);
8427 kfree(ibm->acpi->driver);
8428 ibm->acpi->driver = NULL;
8429 ibm->flags.acpi_driver_registered = 0;
8430 }
8431
8432 if (ibm->flags.init_called && ibm->exit) {
8433 ibm->exit();
8434 ibm->flags.init_called = 0;
8435 }
8436
8437 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
8438}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02008439
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008440static int __init ibm_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008441{
8442 int ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008443 struct ibm_struct *ibm = iibm->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008444 struct proc_dir_entry *entry;
8445
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008446 BUG_ON(ibm == NULL);
8447
8448 INIT_LIST_HEAD(&ibm->all_drivers);
8449
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03008450 if (ibm->flags.experimental && !experimental)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008451 return 0;
8452
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008453 dbg_printk(TPACPI_DBG_INIT,
8454 "probing for %s\n", ibm->name);
8455
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008456 if (iibm->init) {
8457 ret = iibm->init(iibm);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008458 if (ret > 0)
8459 return 0; /* probe failed */
8460 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008461 return ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008462
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03008463 ibm->flags.init_called = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008464 }
8465
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03008466 if (ibm->acpi) {
8467 if (ibm->acpi->hid) {
8468 ret = register_tpacpi_subdriver(ibm);
8469 if (ret)
8470 goto err_out;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008471 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03008472
8473 if (ibm->acpi->notify) {
8474 ret = setup_acpi_notify(ibm);
8475 if (ret == -ENODEV) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008476 printk(TPACPI_NOTICE "disabling subdriver %s\n",
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03008477 ibm->name);
8478 ret = 0;
8479 goto err_out;
8480 }
8481 if (ret < 0)
8482 goto err_out;
8483 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008484 }
8485
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008486 dbg_printk(TPACPI_DBG_INIT,
8487 "%s installed\n", ibm->name);
8488
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008489 if (ibm->read) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008490 mode_t mode;
8491
8492 mode = S_IRUGO;
8493 if (ibm->write)
8494 mode |= S_IWUSR;
8495 entry = proc_create_data(ibm->name, mode, proc_dir,
8496 &dispatch_proc_fops, ibm);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008497 if (!entry) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008498 printk(TPACPI_ERR "unable to create proc entry %s\n",
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008499 ibm->name);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008500 ret = -ENODEV;
8501 goto err_out;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008502 }
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03008503 ibm->flags.proc_created = 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008504 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008505
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008506 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
8507
Linus Torvalds1da177e2005-04-16 15:20:36 -07008508 return 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008509
8510err_out:
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008511 dbg_printk(TPACPI_DBG_INIT,
8512 "%s: at error exit path with result %d\n",
8513 ibm->name, ret);
8514
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008515 ibm_exit(ibm);
8516 return (ret < 0)? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008517}
8518
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008519/* Probing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008520
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008521static bool __pure __init tpacpi_is_fw_digit(const char c)
8522{
8523 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
8524}
8525
8526/* Most models: xxyTkkWW (#.##c); Ancient 570/600 and -SL lacks (#.##c) */
8527static bool __pure __init tpacpi_is_valid_fw_id(const char* const s,
8528 const char t)
8529{
8530 return s && strlen(s) >= 8 &&
8531 tpacpi_is_fw_digit(s[0]) &&
8532 tpacpi_is_fw_digit(s[1]) &&
8533 s[2] == t && s[3] == 'T' &&
8534 tpacpi_is_fw_digit(s[4]) &&
8535 tpacpi_is_fw_digit(s[5]) &&
8536 s[6] == 'W' && s[7] == 'W';
8537}
8538
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008539/* returns 0 - probe ok, or < 0 - probe error.
8540 * Probe ok doesn't mean thinkpad found.
8541 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
8542static int __must_check __init get_thinkpad_model_data(
8543 struct thinkpad_id_data *tp)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02008544{
Jeff Garzik18552562007-10-03 15:15:40 -04008545 const struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02008546 char ec_fw_string[18];
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008547 char const *s;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02008548
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008549 if (!tp)
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008550 return -EINVAL;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008551
8552 memset(tp, 0, sizeof(*tp));
8553
8554 if (dmi_name_in_vendors("IBM"))
8555 tp->vendor = PCI_VENDOR_ID_IBM;
8556 else if (dmi_name_in_vendors("LENOVO"))
8557 tp->vendor = PCI_VENDOR_ID_LENOVO;
8558 else
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008559 return 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008560
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008561 s = dmi_get_system_info(DMI_BIOS_VERSION);
8562 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
8563 if (s && !tp->bios_version_str)
8564 return -ENOMEM;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008565
8566 /* Really ancient ThinkPad 240X will fail this, which is fine */
8567 if (!tpacpi_is_valid_fw_id(tp->bios_version_str, 'E'))
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008568 return 0;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008569
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008570 tp->bios_model = tp->bios_version_str[0]
8571 | (tp->bios_version_str[1] << 8);
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008572 tp->bios_release = (tp->bios_version_str[4] << 8)
8573 | tp->bios_version_str[5];
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008574
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02008575 /*
8576 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
8577 * X32 or newer, all Z series; Some models must have an
8578 * up-to-date BIOS or they will not be detected.
8579 *
8580 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
8581 */
8582 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02008583 if (sscanf(dev->name,
8584 "IBM ThinkPad Embedded Controller -[%17c",
8585 ec_fw_string) == 1) {
8586 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
8587 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008588
8589 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008590 if (!tp->ec_version_str)
8591 return -ENOMEM;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008592
8593 if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) {
8594 tp->ec_model = ec_fw_string[0]
8595 | (ec_fw_string[1] << 8);
8596 tp->ec_release = (ec_fw_string[4] << 8)
8597 | ec_fw_string[5];
8598 } else {
8599 printk(TPACPI_NOTICE
8600 "ThinkPad firmware release %s "
8601 "doesn't match the known patterns\n",
8602 ec_fw_string);
8603 printk(TPACPI_NOTICE
8604 "please report this to %s\n",
8605 TPACPI_MAIL);
8606 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008607 break;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02008608 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02008609 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008610
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008611 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
8612 if (s && !strnicmp(s, "ThinkPad", 8)) {
8613 tp->model_str = kstrdup(s, GFP_KERNEL);
8614 if (!tp->model_str)
8615 return -ENOMEM;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008616 }
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03008617
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008618 s = dmi_get_system_info(DMI_PRODUCT_NAME);
8619 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
8620 if (s && !tp->nummodel_str)
8621 return -ENOMEM;
8622
8623 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008624}
8625
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008626static int __init probe_for_thinkpad(void)
8627{
8628 int is_thinkpad;
8629
8630 if (acpi_disabled)
8631 return -ENODEV;
8632
8633 /*
8634 * Non-ancient models have better DMI tagging, but very old models
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03008635 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008636 */
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03008637 is_thinkpad = (thinkpad_id.model_str != NULL) ||
8638 (thinkpad_id.ec_model != 0) ||
8639 tpacpi_is_fw_known();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008640
8641 /* ec is required because many other handles are relative to it */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008642 TPACPI_ACPIHANDLE_INIT(ec);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008643 if (!ec_handle) {
8644 if (is_thinkpad)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008645 printk(TPACPI_ERR
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008646 "Not yet supported ThinkPad detected!\n");
8647 return -ENODEV;
8648 }
8649
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03008650 if (!is_thinkpad && !force_load)
8651 return -ENODEV;
8652
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008653 return 0;
8654}
8655
8656
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008657/* Module init, exit, parameters */
8658
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008659static struct ibm_init_struct ibms_init[] __initdata = {
8660 {
8661 .init = thinkpad_acpi_driver_init,
8662 .data = &thinkpad_acpi_driver_data,
8663 },
8664 {
8665 .init = hotkey_init,
8666 .data = &hotkey_driver_data,
8667 },
8668 {
8669 .init = bluetooth_init,
8670 .data = &bluetooth_driver_data,
8671 },
8672 {
8673 .init = wan_init,
8674 .data = &wan_driver_data,
8675 },
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02008676 {
8677 .init = uwb_init,
8678 .data = &uwb_driver_data,
8679 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02008680#ifdef CONFIG_THINKPAD_ACPI_VIDEO
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008681 {
8682 .init = video_init,
8683 .data = &video_driver_data,
8684 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02008685#endif
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008686 {
8687 .init = light_init,
8688 .data = &light_driver_data,
8689 },
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008690 {
8691 .init = cmos_init,
8692 .data = &cmos_driver_data,
8693 },
8694 {
8695 .init = led_init,
8696 .data = &led_driver_data,
8697 },
8698 {
8699 .init = beep_init,
8700 .data = &beep_driver_data,
8701 },
8702 {
8703 .init = thermal_init,
8704 .data = &thermal_driver_data,
8705 },
8706 {
8707 .data = &ecdump_driver_data,
8708 },
8709 {
8710 .init = brightness_init,
8711 .data = &brightness_driver_data,
8712 },
8713 {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02008714 .init = volume_init,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008715 .data = &volume_driver_data,
8716 },
8717 {
8718 .init = fan_init,
8719 .data = &fan_driver_data,
8720 },
8721};
8722
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008723static int __init set_ibm_param(const char *val, struct kernel_param *kp)
8724{
8725 unsigned int i;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008726 struct ibm_struct *ibm;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008727
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02008728 if (!kp || !kp->name || !val)
8729 return -EINVAL;
8730
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008731 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
8732 ibm = ibms_init[i].data;
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02008733 WARN_ON(ibm == NULL);
8734
8735 if (!ibm || !ibm->name)
8736 continue;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008737
8738 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
8739 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008740 return -ENOSPC;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008741 strcpy(ibms_init[i].param, val);
8742 strcat(ibms_init[i].param, ",");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008743 return 0;
8744 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008745 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008746
8747 return -EINVAL;
8748}
8749
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008750module_param(experimental, int, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008751MODULE_PARM_DESC(experimental,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008752 "Enables experimental features when non-zero");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008753
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03008754module_param_named(debug, dbg_level, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008755MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03008756
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008757module_param(force_load, bool, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008758MODULE_PARM_DESC(force_load,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008759 "Attempts to load the driver even on a "
8760 "mis-identified ThinkPad when true");
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03008761
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008762module_param_named(fan_control, fan_control_allowed, bool, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008763MODULE_PARM_DESC(fan_control,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008764 "Enables setting fan parameters features when true");
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008765
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008766module_param_named(brightness_mode, brightness_mode, uint, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008767MODULE_PARM_DESC(brightness_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008768 "Selects brightness control strategy: "
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00008769 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03008770
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008771module_param(brightness_enable, uint, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008772MODULE_PARM_DESC(brightness_enable,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008773 "Enables backlight control when 1, disables when 0");
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02008774
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008775module_param(hotkey_report_mode, uint, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008776MODULE_PARM_DESC(hotkey_report_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008777 "used for backwards compatibility with userspace, "
8778 "see documentation");
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03008779
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02008780#ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02008781module_param_named(volume_mode, volume_mode, uint, 0444);
8782MODULE_PARM_DESC(volume_mode,
8783 "Selects volume control strategy: "
8784 "0=auto, 1=EC, 2=N/A, 3=EC+NVRAM");
8785
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02008786module_param_named(volume_capabilities, volume_capabilities, uint, 0444);
8787MODULE_PARM_DESC(volume_capabilities,
8788 "Selects the mixer capabilites: "
8789 "0=auto, 1=volume and mute, 2=mute only");
8790
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02008791module_param_named(volume_control, volume_control_allowed, bool, 0444);
8792MODULE_PARM_DESC(volume_control,
8793 "Enables software override for the console audio "
8794 "control when true");
8795
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02008796/* ALSA module API parameters */
8797module_param_named(index, alsa_index, int, 0444);
8798MODULE_PARM_DESC(index, "ALSA index for the ACPI EC Mixer");
8799module_param_named(id, alsa_id, charp, 0444);
8800MODULE_PARM_DESC(id, "ALSA id for the ACPI EC Mixer");
8801module_param_named(enable, alsa_enable, bool, 0444);
8802MODULE_PARM_DESC(enable, "Enable the ALSA interface for the ACPI EC Mixer");
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02008803#endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02008804
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008805#define TPACPI_PARAM(feature) \
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008806 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
Henrique de Moraes Holschuhcbb14842008-02-16 02:17:50 -02008807 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008808 "at module load, see documentation")
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008809
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008810TPACPI_PARAM(hotkey);
8811TPACPI_PARAM(bluetooth);
8812TPACPI_PARAM(video);
8813TPACPI_PARAM(light);
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008814TPACPI_PARAM(cmos);
8815TPACPI_PARAM(led);
8816TPACPI_PARAM(beep);
8817TPACPI_PARAM(ecdump);
8818TPACPI_PARAM(brightness);
8819TPACPI_PARAM(volume);
8820TPACPI_PARAM(fan);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008821
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02008822#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008823module_param(dbg_wlswemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02008824MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
8825module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
8826MODULE_PARM_DESC(wlsw_state,
8827 "Initial state of the emulated WLSW switch");
8828
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008829module_param(dbg_bluetoothemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02008830MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
8831module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
8832MODULE_PARM_DESC(bluetooth_state,
8833 "Initial state of the emulated bluetooth switch");
8834
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008835module_param(dbg_wwanemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02008836MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
8837module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
8838MODULE_PARM_DESC(wwan_state,
8839 "Initial state of the emulated WWAN switch");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02008840
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008841module_param(dbg_uwbemul, uint, 0444);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02008842MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
8843module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
8844MODULE_PARM_DESC(uwb_state,
8845 "Initial state of the emulated UWB switch");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02008846#endif
8847
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008848static void thinkpad_acpi_module_exit(void)
8849{
8850 struct ibm_struct *ibm, *itmp;
8851
8852 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
8853
8854 list_for_each_entry_safe_reverse(ibm, itmp,
8855 &tpacpi_all_drivers,
8856 all_drivers) {
8857 ibm_exit(ibm);
8858 }
8859
8860 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
8861
8862 if (tpacpi_inputdev) {
8863 if (tp_features.input_device_registered)
8864 input_unregister_device(tpacpi_inputdev);
8865 else
8866 input_free_device(tpacpi_inputdev);
8867 }
8868
8869 if (tpacpi_hwmon)
8870 hwmon_device_unregister(tpacpi_hwmon);
8871
8872 if (tp_features.sensors_pdev_attrs_registered)
8873 device_remove_file(&tpacpi_sensors_pdev->dev,
8874 &dev_attr_thinkpad_acpi_pdev_name);
8875 if (tpacpi_sensors_pdev)
8876 platform_device_unregister(tpacpi_sensors_pdev);
8877 if (tpacpi_pdev)
8878 platform_device_unregister(tpacpi_pdev);
8879
8880 if (tp_features.sensors_pdrv_attrs_registered)
8881 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
8882 if (tp_features.platform_drv_attrs_registered)
8883 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
8884
8885 if (tp_features.sensors_pdrv_registered)
8886 platform_driver_unregister(&tpacpi_hwmon_pdriver);
8887
8888 if (tp_features.platform_drv_registered)
8889 platform_driver_unregister(&tpacpi_pdriver);
8890
8891 if (proc_dir)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008892 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008893
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03008894 if (tpacpi_wq)
8895 destroy_workqueue(tpacpi_wq);
8896
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008897 kfree(thinkpad_id.bios_version_str);
8898 kfree(thinkpad_id.ec_version_str);
8899 kfree(thinkpad_id.model_str);
8900}
8901
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02008902
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03008903static int __init thinkpad_acpi_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008904{
8905 int ret, i;
8906
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03008907 tpacpi_lifecycle = TPACPI_LIFE_INIT;
8908
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03008909 /* Parameter checking */
8910 if (hotkey_report_mode > 2)
8911 return -EINVAL;
8912
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008913 /* Driver-level probe */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008914
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008915 ret = get_thinkpad_model_data(&thinkpad_id);
8916 if (ret) {
8917 printk(TPACPI_ERR
8918 "unable to get DMI data: %d\n", ret);
8919 thinkpad_acpi_module_exit();
8920 return ret;
8921 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008922 ret = probe_for_thinkpad();
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008923 if (ret) {
8924 thinkpad_acpi_module_exit();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008925 return ret;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008926 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008927
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008928 /* Driver initialization */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008929
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008930 TPACPI_ACPIHANDLE_INIT(ecrd);
8931 TPACPI_ACPIHANDLE_INIT(ecwr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008932
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03008933 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
8934 if (!tpacpi_wq) {
8935 thinkpad_acpi_module_exit();
8936 return -ENOMEM;
8937 }
8938
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008939 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008940 if (!proc_dir) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008941 printk(TPACPI_ERR
8942 "unable to create proc dir " TPACPI_PROC_DIR);
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03008943 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008944 return -ENODEV;
8945 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008946
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008947 ret = platform_driver_register(&tpacpi_pdriver);
8948 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008949 printk(TPACPI_ERR
8950 "unable to register main platform driver\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008951 thinkpad_acpi_module_exit();
8952 return ret;
8953 }
Henrique de Moraes Holschuhac363932007-07-27 17:04:40 -03008954 tp_features.platform_drv_registered = 1;
8955
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008956 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
8957 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008958 printk(TPACPI_ERR
8959 "unable to register hwmon platform driver\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008960 thinkpad_acpi_module_exit();
8961 return ret;
8962 }
8963 tp_features.sensors_pdrv_registered = 1;
8964
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03008965 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03008966 if (!ret) {
8967 tp_features.platform_drv_attrs_registered = 1;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008968 ret = tpacpi_create_driver_attributes(
8969 &tpacpi_hwmon_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03008970 }
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03008971 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008972 printk(TPACPI_ERR
8973 "unable to create sysfs driver attributes\n");
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03008974 thinkpad_acpi_module_exit();
8975 return ret;
8976 }
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03008977 tp_features.sensors_pdrv_attrs_registered = 1;
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03008978
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008979
8980 /* Device initialization */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008981 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008982 NULL, 0);
8983 if (IS_ERR(tpacpi_pdev)) {
8984 ret = PTR_ERR(tpacpi_pdev);
8985 tpacpi_pdev = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008986 printk(TPACPI_ERR "unable to register platform device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008987 thinkpad_acpi_module_exit();
8988 return ret;
8989 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008990 tpacpi_sensors_pdev = platform_device_register_simple(
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008991 TPACPI_HWMON_DRVR_NAME,
8992 -1, NULL, 0);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008993 if (IS_ERR(tpacpi_sensors_pdev)) {
8994 ret = PTR_ERR(tpacpi_sensors_pdev);
8995 tpacpi_sensors_pdev = NULL;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008996 printk(TPACPI_ERR
8997 "unable to register hwmon platform device\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008998 thinkpad_acpi_module_exit();
8999 return ret;
9000 }
9001 ret = device_create_file(&tpacpi_sensors_pdev->dev,
9002 &dev_attr_thinkpad_acpi_pdev_name);
9003 if (ret) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009004 printk(TPACPI_ERR
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009005 "unable to create sysfs hwmon device attributes\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009006 thinkpad_acpi_module_exit();
9007 return ret;
9008 }
9009 tp_features.sensors_pdev_attrs_registered = 1;
9010 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009011 if (IS_ERR(tpacpi_hwmon)) {
9012 ret = PTR_ERR(tpacpi_hwmon);
9013 tpacpi_hwmon = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009014 printk(TPACPI_ERR "unable to register hwmon device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009015 thinkpad_acpi_module_exit();
9016 return ret;
9017 }
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -03009018 mutex_init(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009019 tpacpi_inputdev = input_allocate_device();
9020 if (!tpacpi_inputdev) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009021 printk(TPACPI_ERR "unable to allocate input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009022 thinkpad_acpi_module_exit();
9023 return -ENOMEM;
9024 } else {
9025 /* Prepare input device, but don't register */
9026 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009027 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009028 tpacpi_inputdev->id.bustype = BUS_HOST;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03009029 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
9030 thinkpad_id.vendor :
9031 PCI_VENDOR_ID_IBM;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009032 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
9033 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
Henrique de Moraes Holschuhd112ef92009-12-09 01:36:26 +00009034 tpacpi_inputdev->dev.parent = &tpacpi_pdev->dev;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009035 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009036 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
9037 ret = ibm_init(&ibms_init[i]);
9038 if (ret >= 0 && *ibms_init[i].param)
9039 ret = ibms_init[i].data->write(ibms_init[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009040 if (ret < 0) {
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03009041 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07009042 return ret;
9043 }
9044 }
Henrique de Moraes Holschuhb589ea42010-02-25 21:28:58 -03009045
9046 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
9047
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009048 ret = input_register_device(tpacpi_inputdev);
9049 if (ret < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009050 printk(TPACPI_ERR "unable to register input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009051 thinkpad_acpi_module_exit();
9052 return ret;
9053 } else {
9054 tp_features.input_device_registered = 1;
9055 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009056
9057 return 0;
9058}
9059
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03009060MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
9061
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009062/*
Henrique de Moraes Holschuh922fe092009-04-14 02:44:13 +00009063 * This will autoload the driver in almost every ThinkPad
9064 * in widespread use.
9065 *
9066 * Only _VERY_ old models, like the 240, 240x and 570 lack
9067 * the HKEY event interface.
9068 */
9069MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
9070
9071/*
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009072 * DMI matching for module autoloading
9073 *
9074 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
9075 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
9076 *
9077 * Only models listed in thinkwiki will be supported, so add yours
9078 * if it is not there yet.
9079 */
9080#define IBM_BIOS_MODULE_ALIAS(__type) \
Mathieu Chouquet-Stringerb36a50f2009-03-14 16:35:26 +01009081 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009082
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009083/* Ancient thinkpad BIOSes have to be identified by
9084 * BIOS type or model number, and there are far less
9085 * BIOS types than model numbers... */
Henrique de Moraes Holschuh922fe092009-04-14 02:44:13 +00009086IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009087
Henrique de Moraes Holschuhf68f53a2009-04-14 02:44:12 +00009088MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
9089MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009090MODULE_DESCRIPTION(TPACPI_DESC);
9091MODULE_VERSION(TPACPI_VERSION);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009092MODULE_LICENSE("GPL");
9093
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03009094module_init(thinkpad_acpi_module_init);
9095module_exit(thinkpad_acpi_module_exit);