blob: af2352517c99011fc15970b4f5fd88246164a55e [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 Holschuh94954cc2007-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 Holschuh94954cc2007-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
Henrique de Moraes Holschuh32afbf02007-10-08 10:12:56 -03001021 while (*buf && isspace(*buf))
1022 buf++;
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001023 *value = simple_strtoul(buf, &endp, 0);
1024 while (*endp && isspace(*endp))
1025 endp++;
1026 if (*endp || *value > max)
1027 return -EINVAL;
1028
1029 return 0;
1030}
1031
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03001032static void tpacpi_disable_brightness_delay(void)
1033{
1034 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
1035 printk(TPACPI_NOTICE
1036 "ACPI backlight control delay disabled\n");
1037}
1038
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03001039static int __init tpacpi_query_bcl_levels(acpi_handle handle)
1040{
1041 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1042 union acpi_object *obj;
1043 int rc;
1044
1045 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
1046 obj = (union acpi_object *)buffer.pointer;
1047 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
1048 printk(TPACPI_ERR "Unknown _BCL data, "
1049 "please report this to %s\n", TPACPI_MAIL);
1050 rc = 0;
1051 } else {
1052 rc = obj->package.count;
1053 }
1054 } else {
1055 return 0;
1056 }
1057
1058 kfree(buffer.pointer);
1059 return rc;
1060}
1061
1062static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
1063 u32 lvl, void *context, void **rv)
1064{
1065 char name[ACPI_PATH_SEGMENT_LENGTH];
1066 struct acpi_buffer buffer = { sizeof(name), &name };
1067
1068 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
1069 !strncmp("_BCL", name, sizeof(name) - 1)) {
1070 BUG_ON(!rv || !*rv);
1071 **(int **)rv = tpacpi_query_bcl_levels(handle);
1072 return AE_CTRL_TERMINATE;
1073 } else {
1074 return AE_OK;
1075 }
1076}
1077
1078/*
1079 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
1080 */
1081static int __init tpacpi_check_std_acpi_brightness_support(void)
1082{
1083 int status;
1084 int bcl_levels = 0;
1085 void *bcl_ptr = &bcl_levels;
1086
1087 if (!vid_handle) {
1088 TPACPI_ACPIHANDLE_INIT(vid);
1089 }
1090 if (!vid_handle)
1091 return 0;
1092
1093 /*
1094 * Search for a _BCL method, and execute it. This is safe on all
1095 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
1096 * BIOS in ACPI backlight control mode. We do NOT have to care
1097 * about calling the _BCL method in an enabled video device, any
1098 * will do for our purposes.
1099 */
1100
1101 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
1102 tpacpi_acpi_walk_find_bcl, NULL,
1103 &bcl_ptr);
1104
1105 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
1106 tp_features.bright_acpimode = 1;
1107 return (bcl_levels - 2);
1108 }
1109
1110 return 0;
1111}
1112
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00001113static void printk_deprecated_attribute(const char * const what,
1114 const char * const details)
1115{
1116 tpacpi_log_usertask("deprecated sysfs attribute");
1117 printk(TPACPI_WARN "WARNING: sysfs attribute %s is deprecated and "
1118 "will be removed. %s\n",
1119 what, details);
1120}
1121
Johannes Berg19d337d2009-06-02 13:01:37 +02001122/*************************************************************************
1123 * rfkill and radio control support helpers
1124 */
1125
1126/*
1127 * ThinkPad-ACPI firmware handling model:
1128 *
1129 * WLSW (master wireless switch) is event-driven, and is common to all
1130 * firmware-controlled radios. It cannot be controlled, just monitored,
1131 * as expected. It overrides all radio state in firmware
1132 *
1133 * The kernel, a masked-off hotkey, and WLSW can change the radio state
1134 * (TODO: verify how WLSW interacts with the returned radio state).
1135 *
1136 * The only time there are shadow radio state changes, is when
1137 * masked-off hotkeys are used.
1138 */
1139
1140/*
1141 * Internal driver API for radio state:
1142 *
1143 * int: < 0 = error, otherwise enum tpacpi_rfkill_state
1144 * bool: true means radio blocked (off)
1145 */
1146enum tpacpi_rfkill_state {
1147 TPACPI_RFK_RADIO_OFF = 0,
1148 TPACPI_RFK_RADIO_ON
1149};
1150
1151/* rfkill switches */
1152enum tpacpi_rfk_id {
1153 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
1154 TPACPI_RFK_WWAN_SW_ID,
1155 TPACPI_RFK_UWB_SW_ID,
1156 TPACPI_RFK_SW_MAX
1157};
1158
1159static const char *tpacpi_rfkill_names[] = {
1160 [TPACPI_RFK_BLUETOOTH_SW_ID] = "bluetooth",
1161 [TPACPI_RFK_WWAN_SW_ID] = "wwan",
1162 [TPACPI_RFK_UWB_SW_ID] = "uwb",
1163 [TPACPI_RFK_SW_MAX] = NULL
1164};
1165
1166/* ThinkPad-ACPI rfkill subdriver */
1167struct tpacpi_rfk {
1168 struct rfkill *rfkill;
1169 enum tpacpi_rfk_id id;
1170 const struct tpacpi_rfk_ops *ops;
1171};
1172
1173struct tpacpi_rfk_ops {
1174 /* firmware interface */
1175 int (*get_status)(void);
1176 int (*set_status)(const enum tpacpi_rfkill_state);
1177};
1178
1179static struct tpacpi_rfk *tpacpi_rfkill_switches[TPACPI_RFK_SW_MAX];
1180
1181/* Query FW and update rfkill sw state for a given rfkill switch */
1182static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk *tp_rfk)
1183{
1184 int status;
1185
1186 if (!tp_rfk)
1187 return -ENODEV;
1188
1189 status = (tp_rfk->ops->get_status)();
1190 if (status < 0)
1191 return status;
1192
1193 rfkill_set_sw_state(tp_rfk->rfkill,
1194 (status == TPACPI_RFK_RADIO_OFF));
1195
1196 return status;
1197}
1198
1199/* Query FW and update rfkill sw state for all rfkill switches */
1200static void tpacpi_rfk_update_swstate_all(void)
1201{
1202 unsigned int i;
1203
1204 for (i = 0; i < TPACPI_RFK_SW_MAX; i++)
1205 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[i]);
1206}
1207
1208/*
1209 * Sync the HW-blocking state of all rfkill switches,
1210 * do notice it causes the rfkill core to schedule uevents
1211 */
1212static void tpacpi_rfk_update_hwblock_state(bool blocked)
1213{
1214 unsigned int i;
1215 struct tpacpi_rfk *tp_rfk;
1216
1217 for (i = 0; i < TPACPI_RFK_SW_MAX; i++) {
1218 tp_rfk = tpacpi_rfkill_switches[i];
1219 if (tp_rfk) {
1220 if (rfkill_set_hw_state(tp_rfk->rfkill,
1221 blocked)) {
1222 /* ignore -- we track sw block */
1223 }
1224 }
1225 }
1226}
1227
1228/* Call to get the WLSW state from the firmware */
1229static int hotkey_get_wlsw(void);
1230
1231/* Call to query WLSW state and update all rfkill switches */
1232static bool tpacpi_rfk_check_hwblock_state(void)
1233{
1234 int res = hotkey_get_wlsw();
1235 int hw_blocked;
1236
1237 /* When unknown or unsupported, we have to assume it is unblocked */
1238 if (res < 0)
1239 return false;
1240
1241 hw_blocked = (res == TPACPI_RFK_RADIO_OFF);
1242 tpacpi_rfk_update_hwblock_state(hw_blocked);
1243
1244 return hw_blocked;
1245}
1246
1247static int tpacpi_rfk_hook_set_block(void *data, bool blocked)
1248{
1249 struct tpacpi_rfk *tp_rfk = data;
1250 int res;
1251
1252 dbg_printk(TPACPI_DBG_RFKILL,
1253 "request to change radio state to %s\n",
1254 blocked ? "blocked" : "unblocked");
1255
1256 /* try to set radio state */
1257 res = (tp_rfk->ops->set_status)(blocked ?
1258 TPACPI_RFK_RADIO_OFF : TPACPI_RFK_RADIO_ON);
1259
1260 /* and update the rfkill core with whatever the FW really did */
1261 tpacpi_rfk_update_swstate(tp_rfk);
1262
1263 return (res < 0) ? res : 0;
1264}
1265
1266static const struct rfkill_ops tpacpi_rfk_rfkill_ops = {
1267 .set_block = tpacpi_rfk_hook_set_block,
1268};
1269
1270static int __init tpacpi_new_rfkill(const enum tpacpi_rfk_id id,
1271 const struct tpacpi_rfk_ops *tp_rfkops,
1272 const enum rfkill_type rfktype,
1273 const char *name,
1274 const bool set_default)
1275{
1276 struct tpacpi_rfk *atp_rfk;
1277 int res;
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001278 bool sw_state = false;
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001279 bool hw_state;
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001280 int sw_status;
Johannes Berg19d337d2009-06-02 13:01:37 +02001281
1282 BUG_ON(id >= TPACPI_RFK_SW_MAX || tpacpi_rfkill_switches[id]);
1283
Johannes Berg19d337d2009-06-02 13:01:37 +02001284 atp_rfk = kzalloc(sizeof(struct tpacpi_rfk), GFP_KERNEL);
1285 if (atp_rfk)
1286 atp_rfk->rfkill = rfkill_alloc(name,
1287 &tpacpi_pdev->dev,
1288 rfktype,
1289 &tpacpi_rfk_rfkill_ops,
1290 atp_rfk);
1291 if (!atp_rfk || !atp_rfk->rfkill) {
1292 printk(TPACPI_ERR
1293 "failed to allocate memory for rfkill class\n");
1294 kfree(atp_rfk);
1295 return -ENOMEM;
1296 }
1297
1298 atp_rfk->id = id;
1299 atp_rfk->ops = tp_rfkops;
1300
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001301 sw_status = (tp_rfkops->get_status)();
1302 if (sw_status < 0) {
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001303 printk(TPACPI_ERR
1304 "failed to read initial state for %s, error %d\n",
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001305 name, sw_status);
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001306 } else {
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001307 sw_state = (sw_status == TPACPI_RFK_RADIO_OFF);
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001308 if (set_default) {
1309 /* try to keep the initial state, since we ask the
1310 * firmware to preserve it across S5 in NVRAM */
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001311 rfkill_init_sw_state(atp_rfk->rfkill, sw_state);
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001312 }
1313 }
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001314 hw_state = tpacpi_rfk_check_hwblock_state();
1315 rfkill_set_hw_state(atp_rfk->rfkill, hw_state);
Johannes Berg19d337d2009-06-02 13:01:37 +02001316
1317 res = rfkill_register(atp_rfk->rfkill);
1318 if (res < 0) {
1319 printk(TPACPI_ERR
1320 "failed to register %s rfkill switch: %d\n",
1321 name, res);
1322 rfkill_destroy(atp_rfk->rfkill);
1323 kfree(atp_rfk);
1324 return res;
1325 }
1326
1327 tpacpi_rfkill_switches[id] = atp_rfk;
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001328
1329 printk(TPACPI_INFO "rfkill switch %s: radio is %sblocked\n",
1330 name, (sw_state || hw_state) ? "" : "un");
Johannes Berg19d337d2009-06-02 13:01:37 +02001331 return 0;
1332}
1333
1334static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id)
1335{
1336 struct tpacpi_rfk *tp_rfk;
1337
1338 BUG_ON(id >= TPACPI_RFK_SW_MAX);
1339
1340 tp_rfk = tpacpi_rfkill_switches[id];
1341 if (tp_rfk) {
1342 rfkill_unregister(tp_rfk->rfkill);
Corentin Chary5f0dadb2009-09-14 12:43:52 +02001343 rfkill_destroy(tp_rfk->rfkill);
Johannes Berg19d337d2009-06-02 13:01:37 +02001344 tpacpi_rfkill_switches[id] = NULL;
1345 kfree(tp_rfk);
1346 }
1347}
1348
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00001349static void printk_deprecated_rfkill_attribute(const char * const what)
1350{
1351 printk_deprecated_attribute(what,
1352 "Please switch to generic rfkill before year 2010");
1353}
1354
Johannes Berg19d337d2009-06-02 13:01:37 +02001355/* sysfs <radio> enable ------------------------------------------------ */
1356static ssize_t tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id,
1357 struct device_attribute *attr,
1358 char *buf)
1359{
1360 int status;
1361
1362 printk_deprecated_rfkill_attribute(attr->attr.name);
1363
1364 /* This is in the ABI... */
1365 if (tpacpi_rfk_check_hwblock_state()) {
1366 status = TPACPI_RFK_RADIO_OFF;
1367 } else {
1368 status = tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1369 if (status < 0)
1370 return status;
1371 }
1372
1373 return snprintf(buf, PAGE_SIZE, "%d\n",
1374 (status == TPACPI_RFK_RADIO_ON) ? 1 : 0);
1375}
1376
1377static ssize_t tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id,
1378 struct device_attribute *attr,
1379 const char *buf, size_t count)
1380{
1381 unsigned long t;
1382 int res;
1383
1384 printk_deprecated_rfkill_attribute(attr->attr.name);
1385
1386 if (parse_strtoul(buf, 1, &t))
1387 return -EINVAL;
1388
1389 tpacpi_disclose_usertask(attr->attr.name, "set to %ld\n", t);
1390
1391 /* This is in the ABI... */
1392 if (tpacpi_rfk_check_hwblock_state() && !!t)
1393 return -EPERM;
1394
1395 res = tpacpi_rfkill_switches[id]->ops->set_status((!!t) ?
1396 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF);
1397 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1398
1399 return (res < 0) ? res : count;
1400}
1401
1402/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001403static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id, struct seq_file *m)
Johannes Berg19d337d2009-06-02 13:01:37 +02001404{
Johannes Berg19d337d2009-06-02 13:01:37 +02001405 if (id >= TPACPI_RFK_SW_MAX)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001406 seq_printf(m, "status:\t\tnot supported\n");
Johannes Berg19d337d2009-06-02 13:01:37 +02001407 else {
1408 int status;
1409
1410 /* This is in the ABI... */
1411 if (tpacpi_rfk_check_hwblock_state()) {
1412 status = TPACPI_RFK_RADIO_OFF;
1413 } else {
1414 status = tpacpi_rfk_update_swstate(
1415 tpacpi_rfkill_switches[id]);
1416 if (status < 0)
1417 return status;
1418 }
1419
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001420 seq_printf(m, "status:\t\t%s\n",
Johannes Berg19d337d2009-06-02 13:01:37 +02001421 (status == TPACPI_RFK_RADIO_ON) ?
1422 "enabled" : "disabled");
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001423 seq_printf(m, "commands:\tenable, disable\n");
Johannes Berg19d337d2009-06-02 13:01:37 +02001424 }
1425
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001426 return 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02001427}
1428
1429static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id, char *buf)
1430{
1431 char *cmd;
1432 int status = -1;
1433 int res = 0;
1434
1435 if (id >= TPACPI_RFK_SW_MAX)
1436 return -ENODEV;
1437
1438 while ((cmd = next_cmd(&buf))) {
1439 if (strlencmp(cmd, "enable") == 0)
1440 status = TPACPI_RFK_RADIO_ON;
1441 else if (strlencmp(cmd, "disable") == 0)
1442 status = TPACPI_RFK_RADIO_OFF;
1443 else
1444 return -EINVAL;
1445 }
1446
1447 if (status != -1) {
1448 tpacpi_disclose_usertask("procfs", "attempt to %s %s\n",
1449 (status == TPACPI_RFK_RADIO_ON) ?
1450 "enable" : "disable",
1451 tpacpi_rfkill_names[id]);
1452 res = (tpacpi_rfkill_switches[id]->ops->set_status)(status);
1453 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1454 }
1455
1456 return res;
1457}
1458
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001459/*************************************************************************
1460 * thinkpad-acpi driver attributes
1461 */
1462
1463/* interface_version --------------------------------------------------- */
1464static ssize_t tpacpi_driver_interface_version_show(
1465 struct device_driver *drv,
1466 char *buf)
1467{
1468 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1469}
1470
1471static DRIVER_ATTR(interface_version, S_IRUGO,
1472 tpacpi_driver_interface_version_show, NULL);
1473
1474/* debug_level --------------------------------------------------------- */
1475static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1476 char *buf)
1477{
1478 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1479}
1480
1481static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1482 const char *buf, size_t count)
1483{
1484 unsigned long t;
1485
1486 if (parse_strtoul(buf, 0xffff, &t))
1487 return -EINVAL;
1488
1489 dbg_level = t;
1490
1491 return count;
1492}
1493
1494static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1495 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1496
1497/* version ------------------------------------------------------------- */
1498static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1499 char *buf)
1500{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001501 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1502 TPACPI_DESC, TPACPI_VERSION);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001503}
1504
1505static DRIVER_ATTR(version, S_IRUGO,
1506 tpacpi_driver_version_show, NULL);
1507
1508/* --------------------------------------------------------------------- */
1509
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001510#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1511
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001512/* wlsw_emulstate ------------------------------------------------------ */
1513static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1514 char *buf)
1515{
1516 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1517}
1518
1519static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1520 const char *buf, size_t count)
1521{
1522 unsigned long t;
1523
1524 if (parse_strtoul(buf, 1, &t))
1525 return -EINVAL;
1526
Johannes Berg19d337d2009-06-02 13:01:37 +02001527 if (tpacpi_wlsw_emulstate != !!t) {
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001528 tpacpi_wlsw_emulstate = !!t;
Johannes Berg19d337d2009-06-02 13:01:37 +02001529 tpacpi_rfk_update_hwblock_state(!t); /* negative logic */
1530 }
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001531
1532 return count;
1533}
1534
1535static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1536 tpacpi_driver_wlsw_emulstate_show,
1537 tpacpi_driver_wlsw_emulstate_store);
1538
1539/* bluetooth_emulstate ------------------------------------------------- */
1540static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1541 struct device_driver *drv,
1542 char *buf)
1543{
1544 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1545}
1546
1547static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1548 struct device_driver *drv,
1549 const char *buf, size_t count)
1550{
1551 unsigned long t;
1552
1553 if (parse_strtoul(buf, 1, &t))
1554 return -EINVAL;
1555
1556 tpacpi_bluetooth_emulstate = !!t;
1557
1558 return count;
1559}
1560
1561static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1562 tpacpi_driver_bluetooth_emulstate_show,
1563 tpacpi_driver_bluetooth_emulstate_store);
1564
1565/* wwan_emulstate ------------------------------------------------- */
1566static ssize_t tpacpi_driver_wwan_emulstate_show(
1567 struct device_driver *drv,
1568 char *buf)
1569{
1570 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1571}
1572
1573static ssize_t tpacpi_driver_wwan_emulstate_store(
1574 struct device_driver *drv,
1575 const char *buf, size_t count)
1576{
1577 unsigned long t;
1578
1579 if (parse_strtoul(buf, 1, &t))
1580 return -EINVAL;
1581
1582 tpacpi_wwan_emulstate = !!t;
1583
1584 return count;
1585}
1586
1587static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1588 tpacpi_driver_wwan_emulstate_show,
1589 tpacpi_driver_wwan_emulstate_store);
1590
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001591/* uwb_emulstate ------------------------------------------------- */
1592static ssize_t tpacpi_driver_uwb_emulstate_show(
1593 struct device_driver *drv,
1594 char *buf)
1595{
1596 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1597}
1598
1599static ssize_t tpacpi_driver_uwb_emulstate_store(
1600 struct device_driver *drv,
1601 const char *buf, size_t count)
1602{
1603 unsigned long t;
1604
1605 if (parse_strtoul(buf, 1, &t))
1606 return -EINVAL;
1607
1608 tpacpi_uwb_emulstate = !!t;
1609
1610 return count;
1611}
1612
1613static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
1614 tpacpi_driver_uwb_emulstate_show,
1615 tpacpi_driver_uwb_emulstate_store);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001616#endif
1617
1618/* --------------------------------------------------------------------- */
1619
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001620static struct driver_attribute *tpacpi_driver_attributes[] = {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001621 &driver_attr_debug_level, &driver_attr_version,
1622 &driver_attr_interface_version,
1623};
1624
1625static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1626{
1627 int i, res;
1628
1629 i = 0;
1630 res = 0;
1631 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1632 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1633 i++;
1634 }
1635
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001636#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1637 if (!res && dbg_wlswemul)
1638 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1639 if (!res && dbg_bluetoothemul)
1640 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1641 if (!res && dbg_wwanemul)
1642 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001643 if (!res && dbg_uwbemul)
1644 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001645#endif
1646
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001647 return res;
1648}
1649
1650static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1651{
1652 int i;
1653
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001654 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001655 driver_remove_file(drv, tpacpi_driver_attributes[i]);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001656
1657#ifdef THINKPAD_ACPI_DEBUGFACILITIES
1658 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1659 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1660 driver_remove_file(drv, &driver_attr_wwan_emulstate);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001661 driver_remove_file(drv, &driver_attr_uwb_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001662#endif
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001663}
1664
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001665/*************************************************************************
1666 * Firmware Data
1667 */
1668
1669/*
1670 * Table of recommended minimum BIOS versions
1671 *
1672 * Reasons for listing:
1673 * 1. Stable BIOS, listed because the unknown ammount of
1674 * bugs and bad ACPI behaviour on older versions
1675 *
1676 * 2. BIOS or EC fw with known bugs that trigger on Linux
1677 *
1678 * 3. BIOS with known reduced functionality in older versions
1679 *
1680 * We recommend the latest BIOS and EC version.
1681 * We only support the latest BIOS and EC fw version as a rule.
1682 *
1683 * Sources: IBM ThinkPad Public Web Documents (update changelogs),
1684 * Information from users in ThinkWiki
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03001685 *
1686 * WARNING: we use this table also to detect that the machine is
1687 * a ThinkPad in some cases, so don't remove entries lightly.
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001688 */
1689
1690#define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1691 { .vendor = (__v), \
1692 .bios = TPID(__id1, __id2), \
1693 .ec = TPACPI_MATCH_ANY, \
1694 .quirks = TPACPI_MATCH_ANY << 16 \
1695 | (__bv1) << 8 | (__bv2) }
1696
1697#define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001698 __eid, __ev1, __ev2) \
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001699 { .vendor = (__v), \
1700 .bios = TPID(__bid1, __bid2), \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001701 .ec = __eid, \
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001702 .quirks = (__ev1) << 24 | (__ev2) << 16 \
1703 | (__bv1) << 8 | (__bv2) }
1704
1705#define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1706 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1707
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001708/* Outdated IBM BIOSes often lack the EC id string */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001709#define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1710 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001711 __bv1, __bv2, TPID(__id1, __id2), \
1712 __ev1, __ev2), \
1713 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1714 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1715 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001716
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001717/* Outdated IBM BIOSes often lack the EC id string */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001718#define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1719 __eid1, __eid2, __ev1, __ev2) \
1720 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001721 __bv1, __bv2, TPID(__eid1, __eid2), \
1722 __ev1, __ev2), \
1723 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1724 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1725 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001726
1727#define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1728 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1729
1730#define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1731 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001732 __bv1, __bv2, TPID(__id1, __id2), \
1733 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001734
1735#define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1736 __eid1, __eid2, __ev1, __ev2) \
1737 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001738 __bv1, __bv2, TPID(__eid1, __eid2), \
1739 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001740
1741static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
1742 /* Numeric models ------------------ */
1743 /* FW MODEL BIOS VERS */
1744 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1745 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1746 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1747 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1748 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1749 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1750 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1751 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1752 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1753
1754 /* A-series ------------------------- */
1755 /* FW MODEL BIOS VERS EC VERS */
1756 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1757 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1758 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1759 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1760 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1761 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1762 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1763 TPV_QI0('1', '3', '2', '0'), /* A22m */
1764 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1765 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1766 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1767
1768 /* G-series ------------------------- */
1769 /* FW MODEL BIOS VERS */
1770 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1771 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1772
1773 /* R-series, T-series --------------- */
1774 /* FW MODEL BIOS VERS EC VERS */
1775 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1776 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1777 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1778 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1779 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1780 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1781 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1782 T40/p, T41/p, T42/p (1) */
1783 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1784 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1785 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1786 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1787
1788 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1789 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1790 TPV_QI0('1', '6', '3', '2'), /* T22 */
1791 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1792 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1793 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1794
1795 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1796 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
Henrique de Moraes Holschuh90765c62009-12-09 01:36:23 +00001797 TPV_QL1('7', 'E', 'D', '0', '1', '5'), /* R60e, R60i */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001798
1799 /* BIOS FW BIOS VERS EC FW EC VERS */
1800 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1801 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1802
1803 /* X-series ------------------------- */
1804 /* FW MODEL BIOS VERS EC VERS */
1805 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1806 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1807 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1808 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1809 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1810 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1811 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1812
Henrique de Moraes Holschuh90765c62009-12-09 01:36:23 +00001813 TPV_QL1('7', 'B', 'D', '7', '4', '0'), /* X60/s */
1814 TPV_QL1('7', 'J', '3', '0', '1', '3'), /* X60t */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001815
1816 /* (0) - older versions lack DMI EC fw string and functionality */
1817 /* (1) - older versions known to lack functionality */
1818};
1819
1820#undef TPV_QL1
1821#undef TPV_QL0
1822#undef TPV_QI2
1823#undef TPV_QI1
1824#undef TPV_QI0
1825#undef TPV_Q_X
1826#undef TPV_Q
1827
1828static void __init tpacpi_check_outdated_fw(void)
1829{
1830 unsigned long fwvers;
1831 u16 ec_version, bios_version;
1832
1833 fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
1834 ARRAY_SIZE(tpacpi_bios_version_qtable));
1835
1836 if (!fwvers)
1837 return;
1838
1839 bios_version = fwvers & 0xffffU;
1840 ec_version = (fwvers >> 16) & 0xffffU;
1841
1842 /* note that unknown versions are set to 0x0000 and we use that */
1843 if ((bios_version > thinkpad_id.bios_release) ||
1844 (ec_version > thinkpad_id.ec_release &&
1845 ec_version != TPACPI_MATCH_ANY)) {
1846 /*
1847 * The changelogs would let us track down the exact
1848 * reason, but it is just too much of a pain to track
1849 * it. We only list BIOSes that are either really
1850 * broken, or really stable to begin with, so it is
1851 * best if the user upgrades the firmware anyway.
1852 */
1853 printk(TPACPI_WARN
1854 "WARNING: Outdated ThinkPad BIOS/EC firmware\n");
1855 printk(TPACPI_WARN
1856 "WARNING: This firmware may be missing critical bug "
1857 "fixes and/or important features\n");
1858 }
1859}
1860
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03001861static bool __init tpacpi_is_fw_known(void)
1862{
1863 return tpacpi_check_quirks(tpacpi_bios_version_qtable,
1864 ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
1865}
1866
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03001867/****************************************************************************
1868 ****************************************************************************
1869 *
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001870 * Subdrivers
1871 *
1872 ****************************************************************************
1873 ****************************************************************************/
1874
1875/*************************************************************************
Henrique de Moraes Holschuh142cfc92007-04-21 11:08:26 -03001876 * thinkpad-acpi init subdriver
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001877 */
1878
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001879static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001881 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
1882 printk(TPACPI_INFO "%s\n", TPACPI_URL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001884 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03001885 (thinkpad_id.bios_version_str) ?
1886 thinkpad_id.bios_version_str : "unknown",
1887 (thinkpad_id.ec_version_str) ?
1888 thinkpad_id.ec_version_str : "unknown");
1889
1890 if (thinkpad_id.vendor && thinkpad_id.model_str)
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03001891 printk(TPACPI_INFO "%s %s, model %s\n",
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03001892 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
1893 "IBM" : ((thinkpad_id.vendor ==
1894 PCI_VENDOR_ID_LENOVO) ?
1895 "Lenovo" : "Unknown vendor"),
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03001896 thinkpad_id.model_str,
1897 (thinkpad_id.nummodel_str) ?
1898 thinkpad_id.nummodel_str : "unknown");
Henrique de Moraes Holschuh3945ac32007-02-06 19:13:44 -02001899
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001900 tpacpi_check_outdated_fw();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 return 0;
1902}
1903
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001904static int thinkpad_acpi_driver_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905{
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001906 seq_printf(m, "driver:\t\t%s\n", TPACPI_DESC);
1907 seq_printf(m, "version:\t%s\n", TPACPI_VERSION);
1908 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909}
1910
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001911static struct ibm_struct thinkpad_acpi_driver_data = {
1912 .name = "driver",
1913 .read = thinkpad_acpi_driver_read,
1914};
1915
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001916/*************************************************************************
1917 * Hotkey subdriver
1918 */
1919
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001920/*
1921 * ThinkPad firmware event model
1922 *
1923 * The ThinkPad firmware has two main event interfaces: normal ACPI
1924 * notifications (which follow the ACPI standard), and a private event
1925 * interface.
1926 *
1927 * The private event interface also issues events for the hotkeys. As
1928 * the driver gained features, the event handling code ended up being
1929 * built around the hotkey subdriver. This will need to be refactored
1930 * to a more formal event API eventually.
1931 *
1932 * Some "hotkeys" are actually supposed to be used as event reports,
1933 * such as "brightness has changed", "volume has changed", depending on
1934 * the ThinkPad model and how the firmware is operating.
1935 *
1936 * Unlike other classes, hotkey-class events have mask/unmask control on
1937 * non-ancient firmware. However, how it behaves changes a lot with the
1938 * firmware model and version.
1939 */
1940
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001941enum { /* hot key scan codes (derived from ACPI DSDT) */
1942 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1943 TP_ACPI_HOTKEYSCAN_FNF2,
1944 TP_ACPI_HOTKEYSCAN_FNF3,
1945 TP_ACPI_HOTKEYSCAN_FNF4,
1946 TP_ACPI_HOTKEYSCAN_FNF5,
1947 TP_ACPI_HOTKEYSCAN_FNF6,
1948 TP_ACPI_HOTKEYSCAN_FNF7,
1949 TP_ACPI_HOTKEYSCAN_FNF8,
1950 TP_ACPI_HOTKEYSCAN_FNF9,
1951 TP_ACPI_HOTKEYSCAN_FNF10,
1952 TP_ACPI_HOTKEYSCAN_FNF11,
1953 TP_ACPI_HOTKEYSCAN_FNF12,
1954 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1955 TP_ACPI_HOTKEYSCAN_FNINSERT,
1956 TP_ACPI_HOTKEYSCAN_FNDELETE,
1957 TP_ACPI_HOTKEYSCAN_FNHOME,
1958 TP_ACPI_HOTKEYSCAN_FNEND,
1959 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1960 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1961 TP_ACPI_HOTKEYSCAN_FNSPACE,
1962 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1963 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1964 TP_ACPI_HOTKEYSCAN_MUTE,
1965 TP_ACPI_HOTKEYSCAN_THINKPAD,
1966};
1967
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001968enum { /* Keys/events available through NVRAM polling */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001969 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1970 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1971};
1972
1973enum { /* Positions of some of the keys in hotkey masks */
1974 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1975 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1976 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1977 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1978 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1979 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1980 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1981 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1982 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1983 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1984 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1985};
1986
1987enum { /* NVRAM to ACPI HKEY group map */
1988 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1989 TP_ACPI_HKEY_ZOOM_MASK |
1990 TP_ACPI_HKEY_DISPSWTCH_MASK |
1991 TP_ACPI_HKEY_HIBERNATE_MASK,
1992 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1993 TP_ACPI_HKEY_BRGHTDWN_MASK,
1994 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1995 TP_ACPI_HKEY_VOLDWN_MASK |
1996 TP_ACPI_HKEY_MUTE_MASK,
1997};
1998
1999#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2000struct tp_nvram_state {
2001 u16 thinkpad_toggle:1;
2002 u16 zoom_toggle:1;
2003 u16 display_toggle:1;
2004 u16 thinklight_toggle:1;
2005 u16 hibernate_toggle:1;
2006 u16 displayexp_toggle:1;
2007 u16 display_state:1;
2008 u16 brightness_toggle:1;
2009 u16 volume_toggle:1;
2010 u16 mute:1;
2011
2012 u8 brightness_level;
2013 u8 volume_level;
2014};
2015
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002016/* kthread for the hotkey poller */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002017static struct task_struct *tpacpi_hotkey_task;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002018
2019/* Acquired while the poller kthread is running, use to sync start/stop */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002020static struct mutex hotkey_thread_mutex;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002021
2022/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002023 * Acquire mutex to write poller control variables as an
2024 * atomic block.
2025 *
2026 * Increment hotkey_config_change when changing them if you
2027 * want the kthread to forget old state.
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002028 *
2029 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
2030 */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002031static struct mutex hotkey_thread_data_mutex;
2032static unsigned int hotkey_config_change;
2033
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002034/*
2035 * hotkey poller control variables
2036 *
2037 * Must be atomic or readers will also need to acquire mutex
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002038 *
2039 * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
2040 * should be used only when the changes need to be taken as
2041 * a block, OR when one needs to force the kthread to forget
2042 * old state.
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002043 */
2044static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
2045static unsigned int hotkey_poll_freq = 10; /* Hz */
2046
2047#define HOTKEY_CONFIG_CRITICAL_START \
2048 do { \
2049 mutex_lock(&hotkey_thread_data_mutex); \
2050 hotkey_config_change++; \
2051 } while (0);
2052#define HOTKEY_CONFIG_CRITICAL_END \
2053 mutex_unlock(&hotkey_thread_data_mutex);
2054
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002055#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2056
2057#define hotkey_source_mask 0U
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002058#define HOTKEY_CONFIG_CRITICAL_START
2059#define HOTKEY_CONFIG_CRITICAL_END
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002060
2061#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2062
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002063static struct mutex hotkey_mutex;
2064
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002065static enum { /* Reasons for waking up */
2066 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
2067 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
2068 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
2069} hotkey_wakeup_reason;
2070
2071static int hotkey_autosleep_ack;
2072
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002073static u32 hotkey_orig_mask; /* events the BIOS had enabled */
2074static u32 hotkey_all_mask; /* all events supported in fw */
2075static u32 hotkey_reserved_mask; /* events better left disabled */
2076static u32 hotkey_driver_mask; /* events needed by the driver */
2077static u32 hotkey_user_mask; /* events visible to userspace */
2078static u32 hotkey_acpi_mask; /* events enabled in firmware */
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002079
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02002080static unsigned int hotkey_report_mode;
2081
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002082static u16 *hotkey_keycode_map;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002083
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -03002084static struct attribute_set *hotkey_dev_attributes;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002085
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002086static void tpacpi_driver_event(const unsigned int hkey_event);
2087static void hotkey_driver_event(const unsigned int scancode);
2088
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002089/* HKEY.MHKG() return bits */
2090#define TP_HOTKEY_TABLET_MASK (1 << 3)
2091
Johannes Berg19d337d2009-06-02 13:01:37 +02002092static int hotkey_get_wlsw(void)
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002093{
Johannes Berg19d337d2009-06-02 13:01:37 +02002094 int status;
2095
2096 if (!tp_features.hotkey_wlsw)
2097 return -ENODEV;
2098
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02002099#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
Johannes Berg19d337d2009-06-02 13:01:37 +02002100 if (dbg_wlswemul)
2101 return (tpacpi_wlsw_emulstate) ?
2102 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02002103#endif
Johannes Berg19d337d2009-06-02 13:01:37 +02002104
2105 if (!acpi_evalf(hkey_handle, &status, "WLSW", "d"))
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002106 return -EIO;
Johannes Berg19d337d2009-06-02 13:01:37 +02002107
2108 return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002109}
2110
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002111static int hotkey_get_tablet_mode(int *status)
2112{
2113 int s;
2114
2115 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
2116 return -EIO;
2117
Henrique de Moraes Holschuhcee47f52008-03-04 14:29:21 -08002118 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
2119 return 0;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002120}
2121
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002122/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002123 * Reads current event mask from firmware, and updates
2124 * hotkey_acpi_mask accordingly. Also resets any bits
2125 * from hotkey_user_mask that are unavailable to be
2126 * delivered (shadow requirement of the userspace ABI).
2127 *
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002128 * Call with hotkey_mutex held
2129 */
2130static int hotkey_mask_get(void)
2131{
2132 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002133 u32 m = 0;
2134
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002135 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002136 return -EIO;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002137
2138 hotkey_acpi_mask = m;
2139 } else {
2140 /* no mask support doesn't mean no event support... */
2141 hotkey_acpi_mask = hotkey_all_mask;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002142 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002143
2144 /* sync userspace-visible mask */
2145 hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002146
2147 return 0;
2148}
2149
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002150void static hotkey_mask_warn_incomplete_mask(void)
2151{
2152 /* log only what the user can fix... */
2153 const u32 wantedmask = hotkey_driver_mask &
2154 ~(hotkey_acpi_mask | hotkey_source_mask) &
2155 (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
2156
2157 if (wantedmask)
2158 printk(TPACPI_NOTICE
2159 "required events 0x%08x not enabled!\n",
2160 wantedmask);
2161}
2162
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002163/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002164 * Set the firmware mask when supported
2165 *
2166 * Also calls hotkey_mask_get to update hotkey_acpi_mask.
2167 *
2168 * NOTE: does not set bits in hotkey_user_mask, but may reset them.
2169 *
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002170 * Call with hotkey_mutex held
2171 */
2172static int hotkey_mask_set(u32 mask)
2173{
2174 int i;
2175 int rc = 0;
2176
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002177 const u32 fwmask = mask & ~hotkey_source_mask;
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -03002178
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002179 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002180 for (i = 0; i < 32; i++) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002181 if (!acpi_evalf(hkey_handle,
2182 NULL, "MHKM", "vdd", i + 1,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002183 !!(mask & (1 << i)))) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002184 rc = -EIO;
2185 break;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002186 }
2187 }
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002188 }
2189
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002190 /*
2191 * We *must* make an inconditional call to hotkey_mask_get to
2192 * refresh hotkey_acpi_mask and update hotkey_user_mask
2193 *
2194 * Take the opportunity to also log when we cannot _enable_
2195 * a given event.
2196 */
2197 if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
2198 printk(TPACPI_NOTICE
2199 "asked for hotkey mask 0x%08x, but "
2200 "firmware forced it to 0x%08x\n",
2201 fwmask, hotkey_acpi_mask);
2202 }
2203
Henrique de Moraes Holschuh6b30eb72009-12-09 01:36:25 +00002204 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING)
2205 hotkey_mask_warn_incomplete_mask();
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002206
2207 return rc;
2208}
2209
2210/*
2211 * Sets hotkey_user_mask and tries to set the firmware mask
2212 *
2213 * Call with hotkey_mutex held
2214 */
2215static int hotkey_user_mask_set(const u32 mask)
2216{
2217 int rc;
2218
2219 /* Give people a chance to notice they are doing something that
2220 * is bound to go boom on their users sooner or later */
2221 if (!tp_warned.hotkey_mask_ff &&
2222 (mask == 0xffff || mask == 0xffffff ||
2223 mask == 0xffffffff)) {
2224 tp_warned.hotkey_mask_ff = 1;
2225 printk(TPACPI_NOTICE
2226 "setting the hotkey mask to 0x%08x is likely "
2227 "not the best way to go about it\n", mask);
2228 printk(TPACPI_NOTICE
2229 "please consider using the driver defaults, "
2230 "and refer to up-to-date thinkpad-acpi "
2231 "documentation\n");
2232 }
2233
2234 /* Try to enable what the user asked for, plus whatever we need.
2235 * this syncs everything but won't enable bits in hotkey_user_mask */
2236 rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
2237
2238 /* Enable the available bits in hotkey_user_mask */
2239 hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
2240
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002241 return rc;
2242}
2243
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002244/*
2245 * Sets the driver hotkey mask.
2246 *
2247 * Can be called even if the hotkey subdriver is inactive
2248 */
2249static int tpacpi_hotkey_driver_mask_set(const u32 mask)
2250{
2251 int rc;
2252
2253 /* Do the right thing if hotkey_init has not been called yet */
2254 if (!tp_features.hotkey) {
2255 hotkey_driver_mask = mask;
2256 return 0;
2257 }
2258
2259 mutex_lock(&hotkey_mutex);
2260
2261 HOTKEY_CONFIG_CRITICAL_START
2262 hotkey_driver_mask = mask;
Henrique de Moraes Holschuhb684a362009-09-26 21:42:49 -03002263#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002264 hotkey_source_mask |= (mask & ~hotkey_all_mask);
Henrique de Moraes Holschuhb684a362009-09-26 21:42:49 -03002265#endif
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002266 HOTKEY_CONFIG_CRITICAL_END
2267
2268 rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) &
2269 ~hotkey_source_mask);
2270 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 Holschuhdb25f162009-09-12 15:22:14 -03002554static void hotkey_poll_setup(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 Holschuhdb25f162009-09-12 15:22:14 -03002585static void hotkey_poll_setup_safe(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 Holschuhdb25f162009-09-12 15:22:14 -03002603static void hotkey_poll_setup_safe(bool __unused)
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02002604{
2605}
2606
2607#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2608
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002609static int hotkey_inputdev_open(struct input_dev *dev)
2610{
2611 switch (tpacpi_lifecycle) {
2612 case TPACPI_LIFE_INIT:
2613 /*
2614 * hotkey_init will call hotkey_poll_setup_safe
2615 * at the appropriate moment
2616 */
2617 return 0;
2618 case TPACPI_LIFE_EXITING:
2619 return -EBUSY;
2620 case TPACPI_LIFE_RUNNING:
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002621 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002622 return 0;
2623 }
2624
2625 /* Should only happen if tpacpi_lifecycle is corrupt */
2626 BUG();
2627 return -EBUSY;
2628}
2629
2630static void hotkey_inputdev_close(struct input_dev *dev)
2631{
2632 /* disable hotkey polling when possible */
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002633 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING &&
2634 !(hotkey_source_mask & hotkey_driver_mask))
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002635 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002636}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002637
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002638/* sysfs hotkey enable ------------------------------------------------- */
2639static ssize_t hotkey_enable_show(struct device *dev,
2640 struct device_attribute *attr,
2641 char *buf)
2642{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002643 int res, status;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002644
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002645 printk_deprecated_attribute("hotkey_enable",
2646 "Hotkey reporting is always enabled");
2647
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002648 res = hotkey_status_get(&status);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002649 if (res)
2650 return res;
2651
2652 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2653}
2654
2655static ssize_t hotkey_enable_store(struct device *dev,
2656 struct device_attribute *attr,
2657 const char *buf, size_t count)
2658{
2659 unsigned long t;
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002660
2661 printk_deprecated_attribute("hotkey_enable",
2662 "Hotkeys can be disabled through hotkey_mask");
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002663
2664 if (parse_strtoul(buf, 1, &t))
2665 return -EINVAL;
2666
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002667 if (t == 0)
2668 return -EPERM;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002669
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002670 return count;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002671}
2672
2673static struct device_attribute dev_attr_hotkey_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002674 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002675 hotkey_enable_show, hotkey_enable_store);
2676
2677/* sysfs hotkey mask --------------------------------------------------- */
2678static ssize_t hotkey_mask_show(struct device *dev,
2679 struct device_attribute *attr,
2680 char *buf)
2681{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002682 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_user_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002683}
2684
2685static ssize_t hotkey_mask_store(struct device *dev,
2686 struct device_attribute *attr,
2687 const char *buf, size_t count)
2688{
2689 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002690 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002691
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002692 if (parse_strtoul(buf, 0xffffffffUL, &t))
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002693 return -EINVAL;
2694
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002695 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002696 return -ERESTARTSYS;
2697
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002698 res = hotkey_user_mask_set(t);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002699
2700#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002701 hotkey_poll_setup(true);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002702#endif
2703
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002704 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002705
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002706 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2707
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002708 return (res) ? res : count;
2709}
2710
2711static struct device_attribute dev_attr_hotkey_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002712 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002713 hotkey_mask_show, hotkey_mask_store);
2714
2715/* sysfs hotkey bios_enabled ------------------------------------------- */
2716static ssize_t hotkey_bios_enabled_show(struct device *dev,
2717 struct device_attribute *attr,
2718 char *buf)
2719{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002720 return sprintf(buf, "0\n");
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002721}
2722
2723static struct device_attribute dev_attr_hotkey_bios_enabled =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002724 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002725
2726/* sysfs hotkey bios_mask ---------------------------------------------- */
2727static ssize_t hotkey_bios_mask_show(struct device *dev,
2728 struct device_attribute *attr,
2729 char *buf)
2730{
Henrique de Moraes Holschuh06777be2009-09-12 15:22:15 -03002731 printk_deprecated_attribute("hotkey_bios_mask",
2732 "This attribute is useless.");
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002733 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002734}
2735
2736static struct device_attribute dev_attr_hotkey_bios_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002737 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002738
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002739/* sysfs hotkey all_mask ----------------------------------------------- */
2740static ssize_t hotkey_all_mask_show(struct device *dev,
2741 struct device_attribute *attr,
2742 char *buf)
2743{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002744 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2745 hotkey_all_mask | hotkey_source_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002746}
2747
2748static struct device_attribute dev_attr_hotkey_all_mask =
2749 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
2750
2751/* sysfs hotkey recommended_mask --------------------------------------- */
2752static ssize_t hotkey_recommended_mask_show(struct device *dev,
2753 struct device_attribute *attr,
2754 char *buf)
2755{
2756 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002757 (hotkey_all_mask | hotkey_source_mask)
2758 & ~hotkey_reserved_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002759}
2760
2761static struct device_attribute dev_attr_hotkey_recommended_mask =
2762 __ATTR(hotkey_recommended_mask, S_IRUGO,
2763 hotkey_recommended_mask_show, NULL);
2764
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002765#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2766
2767/* sysfs hotkey hotkey_source_mask ------------------------------------- */
2768static ssize_t hotkey_source_mask_show(struct device *dev,
2769 struct device_attribute *attr,
2770 char *buf)
2771{
2772 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2773}
2774
2775static ssize_t hotkey_source_mask_store(struct device *dev,
2776 struct device_attribute *attr,
2777 const char *buf, size_t count)
2778{
2779 unsigned long t;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002780 u32 r_ev;
2781 int rc;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002782
2783 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2784 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2785 return -EINVAL;
2786
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002787 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002788 return -ERESTARTSYS;
2789
2790 HOTKEY_CONFIG_CRITICAL_START
2791 hotkey_source_mask = t;
2792 HOTKEY_CONFIG_CRITICAL_END
2793
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002794 rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
2795 ~hotkey_source_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002796 hotkey_poll_setup(true);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002797
2798 /* check if events needed by the driver got disabled */
2799 r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
2800 & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002801
2802 mutex_unlock(&hotkey_mutex);
2803
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002804 if (rc < 0)
2805 printk(TPACPI_ERR "hotkey_source_mask: failed to update the"
2806 "firmware event mask!\n");
2807
2808 if (r_ev)
2809 printk(TPACPI_NOTICE "hotkey_source_mask: "
2810 "some important events were disabled: "
2811 "0x%04x\n", r_ev);
2812
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002813 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2814
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002815 return (rc < 0) ? rc : count;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002816}
2817
2818static struct device_attribute dev_attr_hotkey_source_mask =
2819 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2820 hotkey_source_mask_show, hotkey_source_mask_store);
2821
2822/* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2823static ssize_t hotkey_poll_freq_show(struct device *dev,
2824 struct device_attribute *attr,
2825 char *buf)
2826{
2827 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2828}
2829
2830static ssize_t hotkey_poll_freq_store(struct device *dev,
2831 struct device_attribute *attr,
2832 const char *buf, size_t count)
2833{
2834 unsigned long t;
2835
2836 if (parse_strtoul(buf, 25, &t))
2837 return -EINVAL;
2838
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002839 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002840 return -ERESTARTSYS;
2841
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002842 hotkey_poll_set_freq(t);
2843 hotkey_poll_setup(true);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002844
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002845 mutex_unlock(&hotkey_mutex);
2846
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002847 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2848
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002849 return count;
2850}
2851
2852static struct device_attribute dev_attr_hotkey_poll_freq =
2853 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2854 hotkey_poll_freq_show, hotkey_poll_freq_store);
2855
2856#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2857
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002858/* sysfs hotkey radio_sw (pollable) ------------------------------------ */
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002859static ssize_t hotkey_radio_sw_show(struct device *dev,
2860 struct device_attribute *attr,
2861 char *buf)
2862{
Johannes Berg19d337d2009-06-02 13:01:37 +02002863 int res;
2864 res = hotkey_get_wlsw();
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002865 if (res < 0)
2866 return res;
2867
Johannes Berg19d337d2009-06-02 13:01:37 +02002868 /* Opportunistic update */
2869 tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
2870
2871 return snprintf(buf, PAGE_SIZE, "%d\n",
2872 (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002873}
2874
2875static struct device_attribute dev_attr_hotkey_radio_sw =
2876 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2877
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002878static void hotkey_radio_sw_notify_change(void)
2879{
2880 if (tp_features.hotkey_wlsw)
2881 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2882 "hotkey_radio_sw");
2883}
2884
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002885/* sysfs hotkey tablet mode (pollable) --------------------------------- */
2886static ssize_t hotkey_tablet_mode_show(struct device *dev,
2887 struct device_attribute *attr,
2888 char *buf)
2889{
2890 int res, s;
2891 res = hotkey_get_tablet_mode(&s);
2892 if (res < 0)
2893 return res;
2894
2895 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2896}
2897
2898static struct device_attribute dev_attr_hotkey_tablet_mode =
2899 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2900
2901static void hotkey_tablet_mode_notify_change(void)
2902{
2903 if (tp_features.hotkey_tablet)
2904 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2905 "hotkey_tablet_mode");
2906}
2907
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002908/* sysfs hotkey report_mode -------------------------------------------- */
2909static ssize_t hotkey_report_mode_show(struct device *dev,
2910 struct device_attribute *attr,
2911 char *buf)
2912{
2913 return snprintf(buf, PAGE_SIZE, "%d\n",
2914 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2915}
2916
2917static struct device_attribute dev_attr_hotkey_report_mode =
2918 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2919
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002920/* sysfs wakeup reason (pollable) -------------------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002921static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2922 struct device_attribute *attr,
2923 char *buf)
2924{
2925 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2926}
2927
2928static struct device_attribute dev_attr_hotkey_wakeup_reason =
2929 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2930
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02002931static void hotkey_wakeup_reason_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002932{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002933 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2934 "wakeup_reason");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002935}
2936
2937/* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002938static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2939 struct device_attribute *attr,
2940 char *buf)
2941{
2942 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2943}
2944
2945static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2946 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2947 hotkey_wakeup_hotunplug_complete_show, NULL);
2948
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02002949static void hotkey_wakeup_hotunplug_complete_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002950{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002951 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2952 "wakeup_hotunplug_complete");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002953}
2954
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002955/* --------------------------------------------------------------------- */
2956
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002957static struct attribute *hotkey_attributes[] __initdata = {
2958 &dev_attr_hotkey_enable.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002959 &dev_attr_hotkey_bios_enabled.attr,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002960 &dev_attr_hotkey_bios_mask.attr,
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002961 &dev_attr_hotkey_report_mode.attr,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002962 &dev_attr_hotkey_wakeup_reason.attr,
2963 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002964 &dev_attr_hotkey_mask.attr,
2965 &dev_attr_hotkey_all_mask.attr,
2966 &dev_attr_hotkey_recommended_mask.attr,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002967#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002968 &dev_attr_hotkey_source_mask.attr,
2969 &dev_attr_hotkey_poll_freq.attr,
2970#endif
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002971};
2972
Johannes Berg19d337d2009-06-02 13:01:37 +02002973/*
2974 * Sync both the hw and sw blocking state of all switches
2975 */
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002976static void tpacpi_send_radiosw_update(void)
2977{
2978 int wlsw;
2979
Johannes Berg19d337d2009-06-02 13:01:37 +02002980 /*
2981 * We must sync all rfkill controllers *before* issuing any
2982 * rfkill input events, or we will race the rfkill core input
2983 * handler.
2984 *
2985 * tpacpi_inputdev_send_mutex works as a syncronization point
2986 * for the above.
2987 *
2988 * We optimize to avoid numerous calls to hotkey_get_wlsw.
2989 */
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002990
Johannes Berg19d337d2009-06-02 13:01:37 +02002991 wlsw = hotkey_get_wlsw();
2992
2993 /* Sync hw blocking state first if it is hw-blocked */
2994 if (wlsw == TPACPI_RFK_RADIO_OFF)
2995 tpacpi_rfk_update_hwblock_state(true);
2996
2997 /* Sync sw blocking state */
2998 tpacpi_rfk_update_swstate_all();
2999
3000 /* Sync hw blocking state last if it is hw-unblocked */
3001 if (wlsw == TPACPI_RFK_RADIO_ON)
3002 tpacpi_rfk_update_hwblock_state(false);
3003
3004 /* Issue rfkill input event for WLSW switch */
3005 if (!(wlsw < 0)) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003006 mutex_lock(&tpacpi_inputdev_send_mutex);
3007
3008 input_report_switch(tpacpi_inputdev,
Johannes Berg19d337d2009-06-02 13:01:37 +02003009 SW_RFKILL_ALL, (wlsw > 0));
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003010 input_sync(tpacpi_inputdev);
3011
3012 mutex_unlock(&tpacpi_inputdev_send_mutex);
3013 }
Johannes Berg19d337d2009-06-02 13:01:37 +02003014
3015 /*
3016 * this can be unconditional, as we will poll state again
3017 * if userspace uses the notify to read data
3018 */
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003019 hotkey_radio_sw_notify_change();
3020}
3021
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003022static void hotkey_exit(void)
3023{
3024#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003025 mutex_lock(&hotkey_mutex);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003026 hotkey_poll_stop_sync();
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003027 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003028#endif
3029
3030 if (hotkey_dev_attributes)
3031 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3032
3033 kfree(hotkey_keycode_map);
3034
Henrique de Moraes Holschuh4be73002009-09-20 14:09:23 -03003035 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003036 "restoring original HKEY status and mask\n");
3037 /* yes, there is a bitwise or below, we want the
3038 * functions to be called even if one of them fail */
3039 if (((tp_features.hotkey_mask &&
3040 hotkey_mask_set(hotkey_orig_mask)) |
3041 hotkey_status_set(false)) != 0)
Henrique de Moraes Holschuh4be73002009-09-20 14:09:23 -03003042 printk(TPACPI_ERR
3043 "failed to restore hot key mask "
3044 "to BIOS defaults\n");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003045}
3046
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003047static void __init hotkey_unmap(const unsigned int scancode)
3048{
3049 if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
3050 clear_bit(hotkey_keycode_map[scancode],
3051 tpacpi_inputdev->keybit);
3052 hotkey_keycode_map[scancode] = KEY_RESERVED;
3053 }
3054}
3055
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003056/*
3057 * HKEY quirks:
3058 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
3059 */
3060
3061#define TPACPI_HK_Q_INIMASK 0x0001
3062
3063static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
3064 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
3065 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
3066 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
3067 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
3068 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
3069 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
3070 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
3071 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
3072 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
3073 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
3074 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
3075 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
3076 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
3077 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
3078 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
3079 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
3080 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
3081 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
3082 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
3083};
3084
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003085static int __init hotkey_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003086{
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003087 /* Requirements for changing the default keymaps:
3088 *
3089 * 1. Many of the keys are mapped to KEY_RESERVED for very
3090 * good reasons. Do not change them unless you have deep
3091 * knowledge on the IBM and Lenovo ThinkPad firmware for
3092 * the various ThinkPad models. The driver behaves
3093 * differently for KEY_RESERVED: such keys have their
3094 * hot key mask *unset* in mask_recommended, and also
3095 * in the initial hot key mask programmed into the
3096 * firmware at driver load time, which means the firm-
3097 * ware may react very differently if you change them to
3098 * something else;
3099 *
3100 * 2. You must be subscribed to the linux-thinkpad and
3101 * ibm-acpi-devel mailing lists, and you should read the
3102 * list archives since 2007 if you want to change the
3103 * keymaps. This requirement exists so that you will
3104 * know the past history of problems with the thinkpad-
3105 * acpi driver keymaps, and also that you will be
3106 * listening to any bug reports;
3107 *
3108 * 3. Do not send thinkpad-acpi specific patches directly to
3109 * for merging, *ever*. Send them to the linux-acpi
3110 * mailinglist for comments. Merging is to be done only
3111 * through acpi-test and the ACPI maintainer.
3112 *
3113 * If the above is too much to ask, don't change the keymap.
3114 * Ask the thinkpad-acpi maintainer to do it, instead.
3115 */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003116 static u16 ibm_keycode_map[] __initdata = {
3117 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3118 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
3119 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3120 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003121
3122 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003123 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3124 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3125 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003126
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003127 /* brightness: firmware always reacts to them */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003128 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003129 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003130
3131 /* Thinklight: firmware always react to it */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003132 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003133
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003134 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3135 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003136
3137 /* Volume: firmware always react to it and reprograms
3138 * the built-in *extra* mixer. Never map it to control
3139 * another mixer by default. */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003140 KEY_RESERVED, /* 0x14: VOLUME UP */
3141 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3142 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003143
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003144 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003145
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003146 /* (assignments unknown, please report if found) */
3147 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3148 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3149 };
3150 static u16 lenovo_keycode_map[] __initdata = {
3151 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3152 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
3153 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3154 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003155
3156 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003157 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3158 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3159 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003160
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003161 /* These should be enabled --only-- when ACPI video
3162 * is disabled (i.e. in "vendor" mode), and are handled
3163 * in a special way by the init code */
3164 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
3165 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003166
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003167 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003168
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003169 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3170 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003171
3172 /* Volume: z60/z61, T60 (BIOS version?): firmware always
3173 * react to it and reprograms the built-in *extra* mixer.
3174 * Never map it to control another mixer by default.
3175 *
3176 * T60?, T61, R60?, R61: firmware and EC tries to send
3177 * these over the regular keyboard, so these are no-ops,
3178 * but there are still weird bugs re. MUTE, so do not
3179 * change unless you get test reports from all Lenovo
3180 * models. May cause the BIOS to interfere with the
3181 * HDA mixer.
3182 */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003183 KEY_RESERVED, /* 0x14: VOLUME UP */
3184 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3185 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003186
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003187 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003188
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003189 /* (assignments unknown, please report if found) */
3190 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3191 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3192 };
3193
3194#define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
3195#define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
3196#define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
3197
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003198 int res, i;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003199 int status;
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03003200 int hkeyv;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003201 bool radiosw_state = false;
3202 bool tabletsw_state = false;
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03003203
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003204 unsigned long quirks;
3205
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003206 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3207 "initializing hotkey subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003208
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003209 BUG_ON(!tpacpi_inputdev);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003210 BUG_ON(tpacpi_inputdev->open != NULL ||
3211 tpacpi_inputdev->close != NULL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003212
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003213 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003214 mutex_init(&hotkey_mutex);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003215
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003216#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3217 mutex_init(&hotkey_thread_mutex);
3218 mutex_init(&hotkey_thread_data_mutex);
3219#endif
3220
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003221 /* hotkey not supported on 570 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003222 tp_features.hotkey = hkey_handle != NULL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003223
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003224 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3225 "hotkeys are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003226 str_supported(tp_features.hotkey));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003227
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003228 if (!tp_features.hotkey)
3229 return 1;
3230
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003231 quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
3232 ARRAY_SIZE(tpacpi_hotkey_qtable));
3233
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03003234 tpacpi_disable_brightness_delay();
3235
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003236 /* MUST have enough space for all attributes to be added to
3237 * hotkey_dev_attributes */
3238 hotkey_dev_attributes = create_attr_set(
3239 ARRAY_SIZE(hotkey_attributes) + 2,
3240 NULL);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003241 if (!hotkey_dev_attributes)
3242 return -ENOMEM;
3243 res = add_many_to_attr_set(hotkey_dev_attributes,
3244 hotkey_attributes,
3245 ARRAY_SIZE(hotkey_attributes));
3246 if (res)
3247 goto err_exit;
3248
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003249 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003250 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
3251 for HKEY interface version 0x100 */
3252 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
3253 if ((hkeyv >> 8) != 1) {
3254 printk(TPACPI_ERR "unknown version of the "
3255 "HKEY interface: 0x%x\n", hkeyv);
3256 printk(TPACPI_ERR "please report this to %s\n",
3257 TPACPI_MAIL);
3258 } else {
3259 /*
3260 * MHKV 0x100 in A31, R40, R40e,
3261 * T4x, X31, and later
3262 */
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003263 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3264 "firmware HKEY interface version: 0x%x\n",
3265 hkeyv);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003266
3267 /* Paranoia check AND init hotkey_all_mask */
3268 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3269 "MHKA", "qd")) {
3270 printk(TPACPI_ERR
3271 "missing MHKA handler, "
3272 "please report this to %s\n",
3273 TPACPI_MAIL);
3274 /* Fallback: pre-init for FN+F3,F4,F12 */
3275 hotkey_all_mask = 0x080cU;
3276 } else {
3277 tp_features.hotkey_mask = 1;
3278 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003279 }
3280 }
3281
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003282 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3283 "hotkey masks are %s\n",
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003284 str_supported(tp_features.hotkey_mask));
3285
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003286 /* Init hotkey_all_mask if not initialized yet */
3287 if (!tp_features.hotkey_mask && !hotkey_all_mask &&
3288 (quirks & TPACPI_HK_Q_INIMASK))
3289 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003290
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003291 /* Init hotkey_acpi_mask and hotkey_orig_mask */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003292 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003293 /* hotkey_source_mask *must* be zero for
3294 * the first hotkey_mask_get to return hotkey_orig_mask */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003295 res = hotkey_mask_get();
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003296 if (res)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003297 goto err_exit;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003298
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003299 hotkey_orig_mask = hotkey_acpi_mask;
3300 } else {
3301 hotkey_orig_mask = hotkey_all_mask;
3302 hotkey_acpi_mask = hotkey_all_mask;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003303 }
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003304
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003305#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3306 if (dbg_wlswemul) {
3307 tp_features.hotkey_wlsw = 1;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003308 radiosw_state = !!tpacpi_wlsw_emulstate;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003309 printk(TPACPI_INFO
3310 "radio switch emulation enabled\n");
3311 } else
3312#endif
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003313 /* Not all thinkpads have a hardware radio switch */
3314 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
3315 tp_features.hotkey_wlsw = 1;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003316 radiosw_state = !!status;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003317 printk(TPACPI_INFO
3318 "radio switch found; radios are %s\n",
3319 enabled(status, 0));
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003320 }
3321 if (tp_features.hotkey_wlsw)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003322 res = add_to_attr_set(hotkey_dev_attributes,
3323 &dev_attr_hotkey_radio_sw.attr);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003324
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003325 /* For X41t, X60t, X61t Tablets... */
3326 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
3327 tp_features.hotkey_tablet = 1;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003328 tabletsw_state = !!(status & TP_HOTKEY_TABLET_MASK);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003329 printk(TPACPI_INFO
3330 "possible tablet mode switch found; "
3331 "ThinkPad in %s mode\n",
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003332 (tabletsw_state) ? "tablet" : "laptop");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003333 res = add_to_attr_set(hotkey_dev_attributes,
3334 &dev_attr_hotkey_tablet_mode.attr);
3335 }
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02003336
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003337 if (!res)
3338 res = register_attr_set_with_sysfs(
3339 hotkey_dev_attributes,
3340 &tpacpi_pdev->dev.kobj);
3341 if (res)
3342 goto err_exit;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003343
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003344 /* Set up key map */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003345
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003346 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
3347 GFP_KERNEL);
3348 if (!hotkey_keycode_map) {
3349 printk(TPACPI_ERR
3350 "failed to allocate memory for key map\n");
3351 res = -ENOMEM;
3352 goto err_exit;
3353 }
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003354
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003355 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003356 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003357 "using Lenovo default hot key map\n");
3358 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
3359 TPACPI_HOTKEY_MAP_SIZE);
3360 } else {
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003361 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003362 "using IBM default hot key map\n");
3363 memcpy(hotkey_keycode_map, &ibm_keycode_map,
3364 TPACPI_HOTKEY_MAP_SIZE);
3365 }
3366
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003367 input_set_capability(tpacpi_inputdev, EV_MSC, MSC_SCAN);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003368 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
3369 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3370 tpacpi_inputdev->keycode = hotkey_keycode_map;
3371 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3372 if (hotkey_keycode_map[i] != KEY_RESERVED) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003373 input_set_capability(tpacpi_inputdev, EV_KEY,
3374 hotkey_keycode_map[i]);
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003375 } else {
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003376 if (i < sizeof(hotkey_reserved_mask)*8)
3377 hotkey_reserved_mask |= 1 << i;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003378 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003379 }
3380
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003381 if (tp_features.hotkey_wlsw) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003382 input_set_capability(tpacpi_inputdev, EV_SW, SW_RFKILL_ALL);
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003383 input_report_switch(tpacpi_inputdev,
3384 SW_RFKILL_ALL, radiosw_state);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003385 }
3386 if (tp_features.hotkey_tablet) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003387 input_set_capability(tpacpi_inputdev, EV_SW, SW_TABLET_MODE);
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003388 input_report_switch(tpacpi_inputdev,
3389 SW_TABLET_MODE, tabletsw_state);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003390 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003391
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003392 /* Do not issue duplicate brightness change events to
3393 * userspace */
3394 if (!tp_features.bright_acpimode)
3395 /* update bright_acpimode... */
3396 tpacpi_check_std_acpi_brightness_support();
3397
Henrique de Moraes Holschuh8bf3d4c2009-05-30 13:25:09 -03003398 if (tp_features.bright_acpimode && acpi_video_backlight_support()) {
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003399 printk(TPACPI_INFO
3400 "This ThinkPad has standard ACPI backlight "
3401 "brightness control, supported by the ACPI "
3402 "video driver\n");
3403 printk(TPACPI_NOTICE
3404 "Disabling thinkpad-acpi brightness events "
3405 "by default...\n");
3406
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003407 /* Disable brightness up/down on Lenovo thinkpads when
3408 * ACPI is handling them, otherwise it is plain impossible
3409 * for userspace to do something even remotely sane */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003410 hotkey_reserved_mask |=
3411 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3412 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003413 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
3414 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003415 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003416
Henrique de Moraes Holschuh230d8cf252009-09-12 15:22:17 -03003417#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003418 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3419 & ~hotkey_all_mask
3420 & ~hotkey_reserved_mask;
Henrique de Moraes Holschuh230d8cf252009-09-12 15:22:17 -03003421
3422 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3423 "hotkey source mask 0x%08x, polling freq %u\n",
3424 hotkey_source_mask, hotkey_poll_freq);
3425#endif
3426
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003427 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3428 "enabling firmware HKEY event interface...\n");
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003429 res = hotkey_status_set(true);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003430 if (res) {
3431 hotkey_exit();
3432 return res;
3433 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003434 res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
3435 | hotkey_driver_mask)
3436 & ~hotkey_source_mask);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003437 if (res < 0 && res != -ENXIO) {
3438 hotkey_exit();
3439 return res;
3440 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003441 hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
3442 & ~hotkey_reserved_mask;
3443 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3444 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3445 hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003446
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003447 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3448 "legacy ibm/hotkey event reporting over procfs %s\n",
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003449 (hotkey_report_mode < 2) ?
3450 "enabled" : "disabled");
3451
3452 tpacpi_inputdev->open = &hotkey_inputdev_open;
3453 tpacpi_inputdev->close = &hotkey_inputdev_close;
3454
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003455 hotkey_poll_setup_safe(true);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003456
3457 return 0;
3458
3459err_exit:
3460 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3461 hotkey_dev_attributes = NULL;
3462
3463 return (res < 0)? res : 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003464}
3465
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003466static bool hotkey_notify_hotkey(const u32 hkey,
3467 bool *send_acpi_ev,
3468 bool *ignore_acpi_ev)
3469{
3470 /* 0x1000-0x1FFF: key presses */
3471 unsigned int scancode = hkey & 0xfff;
3472 *send_acpi_ev = true;
3473 *ignore_acpi_ev = false;
3474
3475 if (scancode > 0 && scancode < 0x21) {
3476 scancode--;
3477 if (!(hotkey_source_mask & (1 << scancode))) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003478 tpacpi_input_send_key_masked(scancode);
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003479 *send_acpi_ev = false;
3480 } else {
3481 *ignore_acpi_ev = true;
3482 }
3483 return true;
3484 }
3485 return false;
3486}
3487
3488static bool hotkey_notify_wakeup(const u32 hkey,
3489 bool *send_acpi_ev,
3490 bool *ignore_acpi_ev)
3491{
3492 /* 0x2000-0x2FFF: Wakeup reason */
3493 *send_acpi_ev = true;
3494 *ignore_acpi_ev = false;
3495
3496 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003497 case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */
3498 case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003499 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3500 *ignore_acpi_ev = true;
3501 break;
3502
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003503 case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */
3504 case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003505 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3506 *ignore_acpi_ev = true;
3507 break;
3508
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003509 case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */
3510 case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003511 printk(TPACPI_ALERT
3512 "EMERGENCY WAKEUP: battery almost empty\n");
3513 /* how to auto-heal: */
3514 /* 2313: woke up from S3, go to S4/S5 */
3515 /* 2413: woke up from S4, go to S5 */
3516 break;
3517
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003518 default:
3519 return false;
3520 }
3521
3522 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
3523 printk(TPACPI_INFO
3524 "woke up due to a hot-unplug "
3525 "request...\n");
3526 hotkey_wakeup_reason_notify_change();
3527 }
3528 return true;
3529}
3530
3531static bool hotkey_notify_usrevent(const u32 hkey,
3532 bool *send_acpi_ev,
3533 bool *ignore_acpi_ev)
3534{
3535 /* 0x5000-0x5FFF: human interface helpers */
3536 *send_acpi_ev = true;
3537 *ignore_acpi_ev = false;
3538
3539 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003540 case TP_HKEY_EV_PEN_INSERTED: /* X61t: tablet pen inserted into bay */
3541 case TP_HKEY_EV_PEN_REMOVED: /* X61t: tablet pen removed from bay */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003542 return true;
3543
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003544 case TP_HKEY_EV_TABLET_TABLET: /* X41t-X61t: tablet mode */
3545 case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003546 tpacpi_input_send_tabletsw();
3547 hotkey_tablet_mode_notify_change();
3548 *send_acpi_ev = false;
3549 return true;
3550
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003551 case TP_HKEY_EV_LID_CLOSE: /* Lid closed */
3552 case TP_HKEY_EV_LID_OPEN: /* Lid opened */
3553 case TP_HKEY_EV_BRGHT_CHANGED: /* brightness changed */
Henrique de Moraes Holschuh176dd982009-09-20 14:09:24 -03003554 /* do not propagate these events */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003555 *ignore_acpi_ev = true;
3556 return true;
3557
3558 default:
3559 return false;
3560 }
3561}
3562
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003563static void thermal_dump_all_sensors(void);
3564
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003565static bool hotkey_notify_thermal(const u32 hkey,
3566 bool *send_acpi_ev,
3567 bool *ignore_acpi_ev)
3568{
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003569 bool known = true;
3570
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003571 /* 0x6000-0x6FFF: thermal alarms */
3572 *send_acpi_ev = true;
3573 *ignore_acpi_ev = false;
3574
3575 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003576 case TP_HKEY_EV_THM_TABLE_CHANGED:
Henrique de Moraes Holschuh54926ce2009-01-11 03:01:09 -02003577 printk(TPACPI_INFO
3578 "EC reports that Thermal Table has changed\n");
3579 /* recommended action: do nothing, we don't have
3580 * Lenovo ATM information */
3581 return true;
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003582 case TP_HKEY_EV_ALARM_BAT_HOT:
3583 printk(TPACPI_CRIT
3584 "THERMAL ALARM: battery is too hot!\n");
3585 /* recommended action: warn user through gui */
3586 break;
3587 case TP_HKEY_EV_ALARM_BAT_XHOT:
3588 printk(TPACPI_ALERT
3589 "THERMAL EMERGENCY: battery is extremely hot!\n");
3590 /* recommended action: immediate sleep/hibernate */
3591 break;
3592 case TP_HKEY_EV_ALARM_SENSOR_HOT:
3593 printk(TPACPI_CRIT
3594 "THERMAL ALARM: "
3595 "a sensor reports something is too hot!\n");
3596 /* recommended action: warn user through gui, that */
3597 /* some internal component is too hot */
3598 break;
3599 case TP_HKEY_EV_ALARM_SENSOR_XHOT:
3600 printk(TPACPI_ALERT
3601 "THERMAL EMERGENCY: "
3602 "a sensor reports something is extremely hot!\n");
3603 /* recommended action: immediate sleep/hibernate */
3604 break;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003605 default:
3606 printk(TPACPI_ALERT
3607 "THERMAL ALERT: unknown thermal alarm received\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003608 known = false;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003609 }
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003610
3611 thermal_dump_all_sensors();
3612
3613 return known;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003614}
3615
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003616static void hotkey_notify(struct ibm_struct *ibm, u32 event)
3617{
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003618 u32 hkey;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003619 bool send_acpi_ev;
3620 bool ignore_acpi_ev;
3621 bool known_ev;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003622
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003623 if (event != 0x80) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003624 printk(TPACPI_ERR
3625 "unknown HKEY notification event %d\n", event);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003626 /* forward it to userspace, maybe it knows how to handle it */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003627 acpi_bus_generate_netlink_event(
3628 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02003629 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003630 event, 0);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003631 return;
3632 }
3633
3634 while (1) {
3635 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003636 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003637 return;
3638 }
3639
3640 if (hkey == 0) {
3641 /* queue empty */
3642 return;
3643 }
3644
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003645 send_acpi_ev = true;
3646 ignore_acpi_ev = false;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003647
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003648 switch (hkey >> 12) {
3649 case 1:
3650 /* 0x1000-0x1FFF: key presses */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003651 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
3652 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003653 break;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003654 case 2:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003655 /* 0x2000-0x2FFF: Wakeup reason */
3656 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
3657 &ignore_acpi_ev);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003658 break;
3659 case 3:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003660 /* 0x3000-0x3FFF: bay-related wakeups */
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003661 if (hkey == 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 Holschuha713b4d2008-01-08 13:02:52 -02003667 } else {
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003668 known_ev = false;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003669 }
3670 break;
3671 case 4:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003672 /* 0x4000-0x4FFF: dock-related wakeups */
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003673 if (hkey == TP_HKEY_EV_UNDOCK_ACK) {
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003674 hotkey_autosleep_ack = 1;
3675 printk(TPACPI_INFO
3676 "undocked\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003677 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003678 known_ev = true;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003679 } else {
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003680 known_ev = false;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003681 }
3682 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003683 case 5:
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02003684 /* 0x5000-0x5FFF: human interface helpers */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003685 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
3686 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003687 break;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003688 case 6:
3689 /* 0x6000-0x6FFF: thermal alarms */
3690 known_ev = hotkey_notify_thermal(hkey, &send_acpi_ev,
3691 &ignore_acpi_ev);
3692 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003693 case 7:
3694 /* 0x7000-0x7FFF: misc */
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003695 if (tp_features.hotkey_wlsw &&
3696 hkey == TP_HKEY_EV_RFKILL_CHANGED) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003697 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02003698 send_acpi_ev = 0;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003699 known_ev = true;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003700 break;
3701 }
3702 /* fallthrough to default */
3703 default:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003704 known_ev = false;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02003705 }
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003706 if (!known_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003707 printk(TPACPI_NOTICE
3708 "unhandled HKEY event 0x%04x\n", hkey);
Henrique de Moraes Holschuhcb429352009-01-11 03:01:07 -02003709 printk(TPACPI_NOTICE
3710 "please report the conditions when this "
3711 "event happened to %s\n", TPACPI_MAIL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003712 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003713
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003714 /* Legacy events */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003715 if (!ignore_acpi_ev &&
3716 (send_acpi_ev || hotkey_report_mode < 2)) {
3717 acpi_bus_generate_proc_event(ibm->acpi->device,
3718 event, hkey);
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03003719 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003720
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003721 /* netlink events */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03003722 if (!ignore_acpi_ev && send_acpi_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003723 acpi_bus_generate_netlink_event(
3724 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02003725 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003726 event, hkey);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003727 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003728 }
3729}
3730
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003731static void hotkey_suspend(pm_message_t state)
3732{
3733 /* Do these on suspend, we get the events on early resume! */
3734 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
3735 hotkey_autosleep_ack = 0;
3736}
3737
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03003738static void hotkey_resume(void)
3739{
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03003740 tpacpi_disable_brightness_delay();
3741
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003742 if (hotkey_status_set(true) < 0 ||
3743 hotkey_mask_set(hotkey_acpi_mask) < 0)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003744 printk(TPACPI_ERR
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003745 "error while attempting to reset the event "
3746 "firmware interface\n");
3747
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003748 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02003749 hotkey_tablet_mode_notify_change();
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003750 hotkey_wakeup_reason_notify_change();
3751 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003752 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03003753}
3754
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003755/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003756static int hotkey_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03003758 int res, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003760 if (!tp_features.hotkey) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003761 seq_printf(m, "status:\t\tnot supported\n");
3762 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003763 }
3764
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02003765 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02003766 return -ERESTARTSYS;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02003767 res = hotkey_status_get(&status);
3768 if (!res)
3769 res = hotkey_mask_get();
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003770 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03003771 if (res)
3772 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003774 seq_printf(m, "status:\t\t%s\n", enabled(status, 0));
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003775 if (hotkey_all_mask) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003776 seq_printf(m, "mask:\t\t0x%08x\n", hotkey_user_mask);
3777 seq_printf(m, "commands:\tenable, disable, reset, <mask>\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778 } else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003779 seq_printf(m, "mask:\t\tnot supported\n");
3780 seq_printf(m, "commands:\tenable, disable, reset\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781 }
3782
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003783 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784}
3785
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003786static void hotkey_enabledisable_warn(bool enable)
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003787{
3788 tpacpi_log_usertask("procfs hotkey enable/disable");
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003789 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
3790 TPACPI_WARN
3791 "hotkey enable/disable functionality has been "
3792 "removed from the driver. Hotkeys are always "
3793 "enabled\n"))
3794 printk(TPACPI_ERR
3795 "Please remove the hotkey=enable module "
3796 "parameter, it is deprecated. Hotkeys are always "
3797 "enabled\n");
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003798}
3799
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003800static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003802 int res;
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03003803 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003806 if (!tp_features.hotkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807 return -ENODEV;
3808
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02003809 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02003810 return -ERESTARTSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003811
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003812 mask = hotkey_user_mask;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003813
3814 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815 while ((cmd = next_cmd(&buf))) {
3816 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003817 hotkey_enabledisable_warn(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003819 hotkey_enabledisable_warn(0);
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003820 res = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821 } else if (strlencmp(cmd, "reset") == 0) {
Henrique de Moraes Holschuh20c9aa42009-09-12 15:22:16 -03003822 mask = (hotkey_all_mask | hotkey_source_mask)
3823 & ~hotkey_reserved_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
3825 /* mask set */
3826 } else if (sscanf(cmd, "%x", &mask) == 1) {
3827 /* mask set */
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003828 } else {
3829 res = -EINVAL;
3830 goto errexit;
3831 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832 }
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003833
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003834 if (!res) {
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003835 tpacpi_disclose_usertask("procfs hotkey",
3836 "set mask to 0x%08x\n", mask);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003837 res = hotkey_user_mask_set(mask);
3838 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003840errexit:
3841 mutex_unlock(&hotkey_mutex);
3842 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003843}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003845static const struct acpi_device_id ibm_htk_device_ids[] = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003846 {TPACPI_ACPI_HKEY_HID, 0},
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003847 {"", 0},
3848};
3849
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003850static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003851 .hid = ibm_htk_device_ids,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003852 .notify = hotkey_notify,
3853 .handle = &hkey_handle,
3854 .type = ACPI_DEVICE_NOTIFY,
3855};
3856
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003857static struct ibm_struct hotkey_driver_data = {
3858 .name = "hotkey",
3859 .read = hotkey_read,
3860 .write = hotkey_write,
3861 .exit = hotkey_exit,
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03003862 .resume = hotkey_resume,
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003863 .suspend = hotkey_suspend,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003864 .acpi = &ibm_hotkey_acpidriver,
3865};
3866
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003867/*************************************************************************
3868 * Bluetooth subdriver
3869 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003871enum {
3872 /* ACPI GBDC/SBDC bits */
3873 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
3874 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003875 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
3876 off / last state */
3877};
3878
3879enum {
3880 /* ACPI \BLTH commands */
3881 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
3882 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
3883 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
3884 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
3885 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003886};
3887
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003888#define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
3889
Johannes Berg19d337d2009-06-02 13:01:37 +02003890static int bluetooth_get_status(void)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003891{
3892 int status;
3893
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003894#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3895 if (dbg_bluetoothemul)
3896 return (tpacpi_bluetooth_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02003897 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003898#endif
3899
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003900 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
3901 return -EIO;
3902
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003903 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02003904 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003905}
3906
Johannes Berg19d337d2009-06-02 13:01:37 +02003907static int bluetooth_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003908{
3909 int status;
3910
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003911 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02003912 "will attempt to %s bluetooth\n",
3913 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003914
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003915#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3916 if (dbg_bluetoothemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02003917 tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003918 return 0;
3919 }
3920#endif
3921
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003922 /* We make sure to keep TP_ACPI_BLUETOOTH_RESUMECTRL off */
Henrique de Moraes Holschuh208b9962009-12-09 01:36:22 +00003923 status = TP_ACPI_BLUETOOTH_RESUMECTRL;
Johannes Berg19d337d2009-06-02 13:01:37 +02003924 if (state == TPACPI_RFK_RADIO_ON)
Henrique de Moraes Holschuh208b9962009-12-09 01:36:22 +00003925 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
Johannes Berg19d337d2009-06-02 13:01:37 +02003926
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003927 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
3928 return -EIO;
3929
3930 return 0;
3931}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003932
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003933/* sysfs bluetooth enable ---------------------------------------------- */
3934static ssize_t bluetooth_enable_show(struct device *dev,
3935 struct device_attribute *attr,
3936 char *buf)
3937{
Johannes Berg19d337d2009-06-02 13:01:37 +02003938 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID,
3939 attr, buf);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003940}
3941
3942static ssize_t bluetooth_enable_store(struct device *dev,
3943 struct device_attribute *attr,
3944 const char *buf, size_t count)
3945{
Johannes Berg19d337d2009-06-02 13:01:37 +02003946 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID,
3947 attr, buf, count);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003948}
3949
3950static struct device_attribute dev_attr_bluetooth_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03003951 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003952 bluetooth_enable_show, bluetooth_enable_store);
3953
3954/* --------------------------------------------------------------------- */
3955
3956static struct attribute *bluetooth_attributes[] = {
3957 &dev_attr_bluetooth_enable.attr,
3958 NULL
3959};
3960
3961static const struct attribute_group bluetooth_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003962 .attrs = bluetooth_attributes,
3963};
3964
Johannes Berg19d337d2009-06-02 13:01:37 +02003965static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = {
3966 .get_status = bluetooth_get_status,
3967 .set_status = bluetooth_set_status,
3968};
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003969
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003970static void bluetooth_shutdown(void)
3971{
3972 /* Order firmware to save current state to NVRAM */
3973 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3974 TP_ACPI_BLTH_SAVE_STATE))
3975 printk(TPACPI_NOTICE
3976 "failed to save bluetooth state to NVRAM\n");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003977 else
3978 vdbg_printk(TPACPI_DBG_RFKILL,
3979 "bluestooth state saved to NVRAM\n");
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003980}
3981
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003982static void bluetooth_exit(void)
3983{
3984 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3985 &bluetooth_attr_group);
Johannes Berg19d337d2009-06-02 13:01:37 +02003986
3987 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
3988
3989 bluetooth_shutdown();
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003990}
3991
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003992static int __init bluetooth_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003994 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003995 int status = 0;
3996
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003997 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3998 "initializing bluetooth subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003999
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004000 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004001
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004002 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4003 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004004 tp_features.bluetooth = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004005 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004007 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4008 "bluetooth is %s, status 0x%02x\n",
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004009 str_supported(tp_features.bluetooth),
4010 status);
4011
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004012#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4013 if (dbg_bluetoothemul) {
4014 tp_features.bluetooth = 1;
4015 printk(TPACPI_INFO
4016 "bluetooth switch emulation enabled\n");
4017 } else
4018#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004019 if (tp_features.bluetooth &&
4020 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
4021 /* no bluetooth hardware present in system */
4022 tp_features.bluetooth = 0;
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004023 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004024 "bluetooth hardware not installed\n");
4025 }
4026
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004027 if (!tp_features.bluetooth)
4028 return 1;
4029
Johannes Berg19d337d2009-06-02 13:01:37 +02004030 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
4031 &bluetooth_tprfk_ops,
4032 RFKILL_TYPE_BLUETOOTH,
4033 TPACPI_RFK_BLUETOOTH_SW_NAME,
4034 true);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004035 if (res)
4036 return res;
4037
Johannes Berg19d337d2009-06-02 13:01:37 +02004038 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4039 &bluetooth_attr_group);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004040 if (res) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004041 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004042 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004043 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004044
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004045 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046}
4047
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004048/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004049static int bluetooth_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050{
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004051 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052}
4053
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004054static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055{
Johannes Berg19d337d2009-06-02 13:01:37 +02004056 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057}
4058
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004059static struct ibm_struct bluetooth_driver_data = {
4060 .name = "bluetooth",
4061 .read = bluetooth_read,
4062 .write = bluetooth_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004063 .exit = bluetooth_exit,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004064 .shutdown = bluetooth_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004065};
4066
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004067/*************************************************************************
4068 * Wan subdriver
4069 */
4070
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004071enum {
4072 /* ACPI GWAN/SWAN bits */
4073 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
4074 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02004075 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
4076 off / last state */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004077};
4078
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004079#define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
4080
Johannes Berg19d337d2009-06-02 13:01:37 +02004081static int wan_get_status(void)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004082{
4083 int status;
4084
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004085#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4086 if (dbg_wwanemul)
4087 return (tpacpi_wwan_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004088 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004089#endif
4090
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004091 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
4092 return -EIO;
4093
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004094 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004095 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004096}
4097
Johannes Berg19d337d2009-06-02 13:01:37 +02004098static int wan_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004099{
4100 int status;
4101
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004102 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02004103 "will attempt to %s wwan\n",
4104 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004105
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004106#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4107 if (dbg_wwanemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004108 tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004109 return 0;
4110 }
4111#endif
4112
Henrique de Moraes Holschuh208b9962009-12-09 01:36:22 +00004113 /* We make sure to set TP_ACPI_WANCARD_RESUMECTRL */
4114 status = TP_ACPI_WANCARD_RESUMECTRL;
Johannes Berg19d337d2009-06-02 13:01:37 +02004115 if (state == TPACPI_RFK_RADIO_ON)
Henrique de Moraes Holschuh208b9962009-12-09 01:36:22 +00004116 status |= TP_ACPI_WANCARD_RADIOSSW;
Johannes Berg19d337d2009-06-02 13:01:37 +02004117
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004118 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
4119 return -EIO;
4120
4121 return 0;
4122}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004123
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004124/* sysfs wan enable ---------------------------------------------------- */
4125static ssize_t wan_enable_show(struct device *dev,
4126 struct device_attribute *attr,
4127 char *buf)
4128{
Johannes Berg19d337d2009-06-02 13:01:37 +02004129 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
4130 attr, buf);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004131}
4132
4133static ssize_t wan_enable_store(struct device *dev,
4134 struct device_attribute *attr,
4135 const char *buf, size_t count)
4136{
Johannes Berg19d337d2009-06-02 13:01:37 +02004137 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
4138 attr, buf, count);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004139}
4140
4141static struct device_attribute dev_attr_wan_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03004142 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004143 wan_enable_show, wan_enable_store);
4144
4145/* --------------------------------------------------------------------- */
4146
4147static struct attribute *wan_attributes[] = {
4148 &dev_attr_wan_enable.attr,
4149 NULL
4150};
4151
4152static const struct attribute_group wan_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004153 .attrs = wan_attributes,
4154};
4155
Johannes Berg19d337d2009-06-02 13:01:37 +02004156static const struct tpacpi_rfk_ops wan_tprfk_ops = {
4157 .get_status = wan_get_status,
4158 .set_status = wan_set_status,
4159};
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004160
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004161static void wan_shutdown(void)
4162{
4163 /* Order firmware to save current state to NVRAM */
4164 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
4165 TP_ACPI_WGSV_SAVE_STATE))
4166 printk(TPACPI_NOTICE
4167 "failed to save WWAN state to NVRAM\n");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004168 else
4169 vdbg_printk(TPACPI_DBG_RFKILL,
4170 "WWAN state saved to NVRAM\n");
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004171}
4172
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004173static void wan_exit(void)
4174{
4175 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4176 &wan_attr_group);
Johannes Berg19d337d2009-06-02 13:01:37 +02004177
4178 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4179
4180 wan_shutdown();
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004181}
4182
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004183static int __init wan_init(struct ibm_init_struct *iibm)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004184{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004185 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004186 int status = 0;
4187
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004188 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4189 "initializing wan subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004190
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004191 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004192
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004193 tp_features.wan = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004194 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004195
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004196 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4197 "wan is %s, status 0x%02x\n",
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004198 str_supported(tp_features.wan),
4199 status);
4200
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004201#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4202 if (dbg_wwanemul) {
4203 tp_features.wan = 1;
4204 printk(TPACPI_INFO
4205 "wwan switch emulation enabled\n");
4206 } else
4207#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004208 if (tp_features.wan &&
4209 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
4210 /* no wan hardware present in system */
4211 tp_features.wan = 0;
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004212 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004213 "wan hardware not installed\n");
4214 }
4215
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004216 if (!tp_features.wan)
4217 return 1;
4218
Johannes Berg19d337d2009-06-02 13:01:37 +02004219 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
4220 &wan_tprfk_ops,
4221 RFKILL_TYPE_WWAN,
4222 TPACPI_RFK_WWAN_SW_NAME,
4223 true);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004224 if (res)
4225 return res;
4226
Johannes Berg19d337d2009-06-02 13:01:37 +02004227 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4228 &wan_attr_group);
4229
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004230 if (res) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004231 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004232 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004233 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004234
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004235 return 0;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004236}
4237
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004238/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004239static int wan_read(struct seq_file *m)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004240{
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004241 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, m);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004242}
4243
4244static int wan_write(char *buf)
4245{
Johannes Berg19d337d2009-06-02 13:01:37 +02004246 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004247}
4248
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004249static struct ibm_struct wan_driver_data = {
4250 .name = "wan",
4251 .read = wan_read,
4252 .write = wan_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004253 .exit = wan_exit,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004254 .shutdown = wan_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004255};
4256
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004257/*************************************************************************
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004258 * UWB subdriver
4259 */
4260
4261enum {
4262 /* ACPI GUWB/SUWB bits */
4263 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4264 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4265};
4266
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004267#define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4268
Johannes Berg19d337d2009-06-02 13:01:37 +02004269static int uwb_get_status(void)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004270{
4271 int status;
4272
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004273#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4274 if (dbg_uwbemul)
4275 return (tpacpi_uwb_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004276 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004277#endif
4278
4279 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4280 return -EIO;
4281
4282 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
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}
4285
Johannes Berg19d337d2009-06-02 13:01:37 +02004286static int uwb_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004287{
4288 int status;
4289
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004290 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02004291 "will attempt to %s UWB\n",
4292 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004293
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004294#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4295 if (dbg_uwbemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004296 tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004297 return 0;
4298 }
4299#endif
4300
Johannes Berg19d337d2009-06-02 13:01:37 +02004301 if (state == TPACPI_RFK_RADIO_ON)
4302 status = TP_ACPI_UWB_RADIOSSW;
4303 else
4304 status = 0;
4305
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004306 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4307 return -EIO;
4308
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004309 return 0;
4310}
4311
4312/* --------------------------------------------------------------------- */
4313
Johannes Berg19d337d2009-06-02 13:01:37 +02004314static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
4315 .get_status = uwb_get_status,
4316 .set_status = uwb_set_status,
4317};
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004318
4319static void uwb_exit(void)
4320{
Johannes Berg19d337d2009-06-02 13:01:37 +02004321 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004322}
4323
4324static int __init uwb_init(struct ibm_init_struct *iibm)
4325{
4326 int res;
4327 int status = 0;
4328
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004329 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4330 "initializing uwb subdriver\n");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004331
4332 TPACPI_ACPIHANDLE_INIT(hkey);
4333
4334 tp_features.uwb = hkey_handle &&
4335 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4336
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004337 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4338 "uwb is %s, status 0x%02x\n",
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004339 str_supported(tp_features.uwb),
4340 status);
4341
4342#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4343 if (dbg_uwbemul) {
4344 tp_features.uwb = 1;
4345 printk(TPACPI_INFO
4346 "uwb switch emulation enabled\n");
4347 } else
4348#endif
4349 if (tp_features.uwb &&
4350 !(status & TP_ACPI_UWB_HWPRESENT)) {
4351 /* no uwb hardware present in system */
4352 tp_features.uwb = 0;
4353 dbg_printk(TPACPI_DBG_INIT,
4354 "uwb hardware not installed\n");
4355 }
4356
4357 if (!tp_features.uwb)
4358 return 1;
4359
4360 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
Johannes Berg19d337d2009-06-02 13:01:37 +02004361 &uwb_tprfk_ops,
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004362 RFKILL_TYPE_UWB,
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004363 TPACPI_RFK_UWB_SW_NAME,
Johannes Berg19d337d2009-06-02 13:01:37 +02004364 false);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004365 return res;
4366}
4367
4368static struct ibm_struct uwb_driver_data = {
4369 .name = "uwb",
4370 .exit = uwb_exit,
4371 .flags.experimental = 1,
4372};
4373
4374/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004375 * Video subdriver
4376 */
4377
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02004378#ifdef CONFIG_THINKPAD_ACPI_VIDEO
4379
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004380enum video_access_mode {
4381 TPACPI_VIDEO_NONE = 0,
4382 TPACPI_VIDEO_570, /* 570 */
4383 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4384 TPACPI_VIDEO_NEW, /* all others */
4385};
4386
4387enum { /* video status flags, based on VIDEO_570 */
4388 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4389 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4390 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4391};
4392
4393enum { /* TPACPI_VIDEO_570 constants */
4394 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4395 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4396 * video_status_flags */
4397 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4398 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4399};
4400
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02004401static enum video_access_mode video_supported;
4402static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004403
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004404static int video_autosw_get(void);
4405static int video_autosw_set(int enable);
4406
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004407TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004408
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004409static int __init video_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004411 int ivga;
4412
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004413 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4414
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004415 TPACPI_ACPIHANDLE_INIT(vid);
4416 TPACPI_ACPIHANDLE_INIT(vid2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004417
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004418 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4419 /* G41, assume IVGA doesn't change */
4420 vid_handle = vid2_handle;
4421
4422 if (!vid_handle)
4423 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004424 video_supported = TPACPI_VIDEO_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004425 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
4426 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004427 video_supported = TPACPI_VIDEO_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004428 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
4429 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004430 video_supported = TPACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004431 else
4432 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004433 video_supported = TPACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004435 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
4436 str_supported(video_supported != TPACPI_VIDEO_NONE),
4437 video_supported);
4438
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004439 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440}
4441
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004442static void video_exit(void)
4443{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004444 dbg_printk(TPACPI_DBG_EXIT,
4445 "restoring original video autoswitch mode\n");
4446 if (video_autosw_set(video_orig_autosw))
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004447 printk(TPACPI_ERR "error while trying to restore original "
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004448 "video autoswitch mode\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004449}
4450
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004451static int video_outputsw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004452{
4453 int status = 0;
4454 int i;
4455
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004456 switch (video_supported) {
4457 case TPACPI_VIDEO_570:
4458 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
4459 TP_ACPI_VIDEO_570_PHSCMD))
4460 return -EIO;
4461 status = i & TP_ACPI_VIDEO_570_PHSMASK;
4462 break;
4463 case TPACPI_VIDEO_770:
4464 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4465 return -EIO;
4466 if (i)
4467 status |= TP_ACPI_VIDEO_S_LCD;
4468 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4469 return -EIO;
4470 if (i)
4471 status |= TP_ACPI_VIDEO_S_CRT;
4472 break;
4473 case TPACPI_VIDEO_NEW:
4474 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4475 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4476 return -EIO;
4477 if (i)
4478 status |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004480 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4481 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4482 return -EIO;
4483 if (i)
4484 status |= TP_ACPI_VIDEO_S_LCD;
4485 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4486 return -EIO;
4487 if (i)
4488 status |= TP_ACPI_VIDEO_S_DVI;
4489 break;
4490 default:
4491 return -ENOSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004492 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493
4494 return status;
4495}
4496
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004497static int video_outputsw_set(int status)
4498{
4499 int autosw;
4500 int res = 0;
4501
4502 switch (video_supported) {
4503 case TPACPI_VIDEO_570:
4504 res = acpi_evalf(NULL, NULL,
4505 "\\_SB.PHS2", "vdd",
4506 TP_ACPI_VIDEO_570_PHS2CMD,
4507 status | TP_ACPI_VIDEO_570_PHS2SET);
4508 break;
4509 case TPACPI_VIDEO_770:
4510 autosw = video_autosw_get();
4511 if (autosw < 0)
4512 return autosw;
4513
4514 res = video_autosw_set(1);
4515 if (res)
4516 return res;
4517 res = acpi_evalf(vid_handle, NULL,
4518 "ASWT", "vdd", status * 0x100, 0);
4519 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004520 printk(TPACPI_ERR
4521 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004522 return -EIO;
4523 }
4524 break;
4525 case TPACPI_VIDEO_NEW:
4526 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004527 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004528 break;
4529 default:
4530 return -ENOSYS;
4531 }
4532
4533 return (res)? 0 : -EIO;
4534}
4535
4536static int video_autosw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004538 int autosw = 0;
4539
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004540 switch (video_supported) {
4541 case TPACPI_VIDEO_570:
4542 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
4543 return -EIO;
4544 break;
4545 case TPACPI_VIDEO_770:
4546 case TPACPI_VIDEO_NEW:
4547 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4548 return -EIO;
4549 break;
4550 default:
4551 return -ENOSYS;
4552 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004553
4554 return autosw & 1;
4555}
4556
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004557static int video_autosw_set(int enable)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004558{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004559 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004560 return -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004561 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004562}
4563
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004564static int video_outputsw_cycle(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004565{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004566 int autosw = video_autosw_get();
4567 int res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004568
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004569 if (autosw < 0)
4570 return autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004571
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004572 switch (video_supported) {
4573 case TPACPI_VIDEO_570:
4574 res = video_autosw_set(1);
4575 if (res)
4576 return res;
4577 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4578 break;
4579 case TPACPI_VIDEO_770:
4580 case TPACPI_VIDEO_NEW:
4581 res = video_autosw_set(1);
4582 if (res)
4583 return res;
4584 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4585 break;
4586 default:
4587 return -ENOSYS;
4588 }
4589 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004590 printk(TPACPI_ERR
4591 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004592 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004593 }
4594
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004595 return (res)? 0 : -EIO;
4596}
4597
4598static int video_expand_toggle(void)
4599{
4600 switch (video_supported) {
4601 case TPACPI_VIDEO_570:
4602 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
4603 0 : -EIO;
4604 case TPACPI_VIDEO_770:
4605 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
4606 0 : -EIO;
4607 case TPACPI_VIDEO_NEW:
4608 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
4609 0 : -EIO;
4610 default:
4611 return -ENOSYS;
4612 }
4613 /* not reached */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004614}
4615
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004616static int video_read(struct seq_file *m)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004617{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004618 int status, autosw;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004619
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004620 if (video_supported == TPACPI_VIDEO_NONE) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004621 seq_printf(m, "status:\t\tnot supported\n");
4622 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004623 }
4624
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004625 status = video_outputsw_get();
4626 if (status < 0)
4627 return status;
4628
4629 autosw = video_autosw_get();
4630 if (autosw < 0)
4631 return autosw;
4632
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004633 seq_printf(m, "status:\t\tsupported\n");
4634 seq_printf(m, "lcd:\t\t%s\n", enabled(status, 0));
4635 seq_printf(m, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004636 if (video_supported == TPACPI_VIDEO_NEW)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004637 seq_printf(m, "dvi:\t\t%s\n", enabled(status, 3));
4638 seq_printf(m, "auto:\t\t%s\n", enabled(autosw, 0));
4639 seq_printf(m, "commands:\tlcd_enable, lcd_disable\n");
4640 seq_printf(m, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004641 if (video_supported == TPACPI_VIDEO_NEW)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004642 seq_printf(m, "commands:\tdvi_enable, dvi_disable\n");
4643 seq_printf(m, "commands:\tauto_enable, auto_disable\n");
4644 seq_printf(m, "commands:\tvideo_switch, expand_toggle\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004645
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004646 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004647}
4648
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004649static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650{
4651 char *cmd;
4652 int enable, disable, status;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004653 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004655 if (video_supported == TPACPI_VIDEO_NONE)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004656 return -ENODEV;
4657
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004658 enable = 0;
4659 disable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004660
4661 while ((cmd = next_cmd(&buf))) {
4662 if (strlencmp(cmd, "lcd_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004663 enable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664 } else if (strlencmp(cmd, "lcd_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004665 disable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666 } else if (strlencmp(cmd, "crt_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004667 enable |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668 } else if (strlencmp(cmd, "crt_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004669 disable |= TP_ACPI_VIDEO_S_CRT;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004670 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004671 strlencmp(cmd, "dvi_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004672 enable |= TP_ACPI_VIDEO_S_DVI;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004673 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004674 strlencmp(cmd, "dvi_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004675 disable |= TP_ACPI_VIDEO_S_DVI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676 } else if (strlencmp(cmd, "auto_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004677 res = video_autosw_set(1);
4678 if (res)
4679 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680 } else if (strlencmp(cmd, "auto_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004681 res = video_autosw_set(0);
4682 if (res)
4683 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684 } else if (strlencmp(cmd, "video_switch") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004685 res = video_outputsw_cycle();
4686 if (res)
4687 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004689 res = video_expand_toggle();
4690 if (res)
4691 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004692 } else
4693 return -EINVAL;
4694 }
4695
4696 if (enable || disable) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004697 status = video_outputsw_get();
4698 if (status < 0)
4699 return status;
4700 res = video_outputsw_set((status & ~disable) | enable);
4701 if (res)
4702 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703 }
4704
4705 return 0;
4706}
4707
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004708static struct ibm_struct video_driver_data = {
4709 .name = "video",
4710 .read = video_read,
4711 .write = video_write,
4712 .exit = video_exit,
4713};
4714
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02004715#endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4716
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004717/*************************************************************************
4718 * Light (thinklight) subdriver
4719 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004721TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4722TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004723
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004724static int light_get_status(void)
4725{
4726 int status = 0;
4727
4728 if (tp_features.light_status) {
4729 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4730 return -EIO;
4731 return (!!status);
4732 }
4733
4734 return -ENXIO;
4735}
4736
4737static int light_set_status(int status)
4738{
4739 int rc;
4740
4741 if (tp_features.light) {
4742 if (cmos_handle) {
4743 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4744 (status)?
4745 TP_CMOS_THINKLIGHT_ON :
4746 TP_CMOS_THINKLIGHT_OFF);
4747 } else {
4748 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4749 (status)? 1 : 0);
4750 }
4751 return (rc)? 0 : -EIO;
4752 }
4753
4754 return -ENXIO;
4755}
4756
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004757static void light_set_status_worker(struct work_struct *work)
4758{
4759 struct tpacpi_led_classdev *data =
4760 container_of(work, struct tpacpi_led_classdev, work);
4761
4762 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00004763 light_set_status((data->new_state != TPACPI_LED_OFF));
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004764}
4765
4766static void light_sysfs_set(struct led_classdev *led_cdev,
4767 enum led_brightness brightness)
4768{
4769 struct tpacpi_led_classdev *data =
4770 container_of(led_cdev,
4771 struct tpacpi_led_classdev,
4772 led_classdev);
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00004773 data->new_state = (brightness != LED_OFF) ?
4774 TPACPI_LED_ON : TPACPI_LED_OFF;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004775 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004776}
4777
4778static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4779{
4780 return (light_get_status() == 1)? LED_FULL : LED_OFF;
4781}
4782
4783static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4784 .led_classdev = {
4785 .name = "tpacpi::thinklight",
4786 .brightness_set = &light_sysfs_set,
4787 .brightness_get = &light_sysfs_get,
4788 }
4789};
4790
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004791static int __init light_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792{
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004793 int rc;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004794
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004795 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4796
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004797 TPACPI_ACPIHANDLE_INIT(ledb);
4798 TPACPI_ACPIHANDLE_INIT(lght);
4799 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004800 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004801
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004802 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004803 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004804
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004805 if (tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004806 /* light status not supported on
4807 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004808 tp_features.light_status =
4809 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004810
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004811 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4812 str_supported(tp_features.light),
4813 str_supported(tp_features.light_status));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004814
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004815 if (!tp_features.light)
4816 return 1;
4817
4818 rc = led_classdev_register(&tpacpi_pdev->dev,
4819 &tpacpi_led_thinklight.led_classdev);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004820
4821 if (rc < 0) {
4822 tp_features.light = 0;
4823 tp_features.light_status = 0;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004824 } else {
4825 rc = 0;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004826 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004827
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004828 return rc;
4829}
4830
4831static void light_exit(void)
4832{
4833 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
4834 if (work_pending(&tpacpi_led_thinklight.work))
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004835 flush_workqueue(tpacpi_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836}
4837
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004838static int light_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004839{
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004840 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004842 if (!tp_features.light) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004843 seq_printf(m, "status:\t\tnot supported\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004844 } else if (!tp_features.light_status) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004845 seq_printf(m, "status:\t\tunknown\n");
4846 seq_printf(m, "commands:\ton, off\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004847 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004848 status = light_get_status();
4849 if (status < 0)
4850 return status;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004851 seq_printf(m, "status:\t\t%s\n", onoff(status, 0));
4852 seq_printf(m, "commands:\ton, off\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004853 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004855 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856}
4857
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004858static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004859{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004861 int newstatus = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004862
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004863 if (!tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004864 return -ENODEV;
4865
Linus Torvalds1da177e2005-04-16 15:20:36 -07004866 while ((cmd = next_cmd(&buf))) {
4867 if (strlencmp(cmd, "on") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004868 newstatus = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004869 } else if (strlencmp(cmd, "off") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004870 newstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871 } else
4872 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873 }
4874
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004875 return light_set_status(newstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876}
4877
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004878static struct ibm_struct light_driver_data = {
4879 .name = "light",
4880 .read = light_read,
4881 .write = light_write,
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004882 .exit = light_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004883};
4884
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004885/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004886 * CMOS subdriver
4887 */
4888
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004889/* sysfs cmos_command -------------------------------------------------- */
4890static ssize_t cmos_command_store(struct device *dev,
4891 struct device_attribute *attr,
4892 const char *buf, size_t count)
4893{
4894 unsigned long cmos_cmd;
4895 int res;
4896
4897 if (parse_strtoul(buf, 21, &cmos_cmd))
4898 return -EINVAL;
4899
4900 res = issue_thinkpad_cmos_command(cmos_cmd);
4901 return (res)? res : count;
4902}
4903
4904static struct device_attribute dev_attr_cmos_command =
4905 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4906
4907/* --------------------------------------------------------------------- */
4908
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004909static int __init cmos_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004910{
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004911 int res;
4912
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004913 vdbg_printk(TPACPI_DBG_INIT,
4914 "initializing cmos commands subdriver\n");
4915
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004916 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004917
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004918 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4919 str_supported(cmos_handle != NULL));
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004920
4921 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4922 if (res)
4923 return res;
4924
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004925 return (cmos_handle)? 0 : 1;
4926}
4927
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004928static void cmos_exit(void)
4929{
4930 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4931}
4932
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004933static int cmos_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004934{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004935 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4936 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937 if (!cmos_handle)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004938 seq_printf(m, "status:\t\tnot supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004939 else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004940 seq_printf(m, "status:\t\tsupported\n");
4941 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942 }
4943
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004944 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945}
4946
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004947static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004948{
4949 char *cmd;
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004950 int cmos_cmd, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951
4952 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004953 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
4954 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955 /* cmos_cmd set */
4956 } else
4957 return -EINVAL;
4958
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004959 res = issue_thinkpad_cmos_command(cmos_cmd);
4960 if (res)
4961 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962 }
4963
4964 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004965}
4966
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004967static struct ibm_struct cmos_driver_data = {
4968 .name = "cmos",
4969 .read = cmos_read,
4970 .write = cmos_write,
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004971 .exit = cmos_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004972};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004973
4974/*************************************************************************
4975 * LED subdriver
4976 */
4977
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004978enum led_access_mode {
4979 TPACPI_LED_NONE = 0,
4980 TPACPI_LED_570, /* 570 */
4981 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4982 TPACPI_LED_NEW, /* all others */
4983};
4984
4985enum { /* For TPACPI_LED_OLD */
4986 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
4987 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
4988 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
4989};
4990
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02004991static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004992
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004993TPACPI_HANDLE(led, ec, "SLED", /* 570 */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004994 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
4995 /* T20-22, X20-21 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004996 "LED", /* all others */
4997 ); /* R30, R31 */
4998
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03004999#define TPACPI_LED_NUMLEDS 16
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005000static struct tpacpi_led_classdev *tpacpi_leds;
5001static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
Harvey Harrisone3aa51f2008-05-29 17:51:57 -07005002static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005003 /* there's a limit of 19 chars + NULL before 2.6.26 */
5004 "tpacpi::power",
5005 "tpacpi:orange:batt",
5006 "tpacpi:green:batt",
5007 "tpacpi::dock_active",
5008 "tpacpi::bay_active",
5009 "tpacpi::dock_batt",
5010 "tpacpi::unknown_led",
5011 "tpacpi::standby",
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005012 "tpacpi::dock_status1",
5013 "tpacpi::dock_status2",
5014 "tpacpi::unknown_led2",
5015 "tpacpi::unknown_led3",
5016 "tpacpi::thinkvantage",
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005017};
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005018#define TPACPI_SAFE_LEDS 0x1081U
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005019
5020static inline bool tpacpi_is_led_restricted(const unsigned int led)
5021{
5022#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5023 return false;
5024#else
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005025 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005026#endif
5027}
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005028
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005029static int led_get_status(const unsigned int led)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005030{
5031 int status;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005032 enum led_status_t led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005033
5034 switch (led_supported) {
5035 case TPACPI_LED_570:
5036 if (!acpi_evalf(ec_handle,
5037 &status, "GLED", "dd", 1 << led))
5038 return -EIO;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005039 led_s = (status == 0)?
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005040 TPACPI_LED_OFF :
5041 ((status == 1)?
5042 TPACPI_LED_ON :
5043 TPACPI_LED_BLINK);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005044 tpacpi_led_state_cache[led] = led_s;
5045 return led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005046 default:
5047 return -ENXIO;
5048 }
5049
5050 /* not reached */
5051}
5052
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005053static int led_set_status(const unsigned int led,
5054 const enum led_status_t ledstatus)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005055{
5056 /* off, on, blink. Index is led_status_t */
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005057 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
5058 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005059
5060 int rc = 0;
5061
5062 switch (led_supported) {
5063 case TPACPI_LED_570:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005064 /* 570 */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005065 if (unlikely(led > 7))
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005066 return -EINVAL;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005067 if (unlikely(tpacpi_is_led_restricted(led)))
5068 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005069 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5070 (1 << led), led_sled_arg1[ledstatus]))
5071 rc = -EIO;
5072 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005073 case TPACPI_LED_OLD:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005074 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005075 if (unlikely(led > 7))
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005076 return -EINVAL;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005077 if (unlikely(tpacpi_is_led_restricted(led)))
5078 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005079 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
5080 if (rc >= 0)
5081 rc = ec_write(TPACPI_LED_EC_HLBL,
5082 (ledstatus == TPACPI_LED_BLINK) << led);
5083 if (rc >= 0)
5084 rc = ec_write(TPACPI_LED_EC_HLCL,
5085 (ledstatus != TPACPI_LED_OFF) << led);
5086 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005087 case TPACPI_LED_NEW:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005088 /* all others */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005089 if (unlikely(led >= TPACPI_LED_NUMLEDS))
5090 return -EINVAL;
5091 if (unlikely(tpacpi_is_led_restricted(led)))
5092 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005093 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5094 led, led_led_arg1[ledstatus]))
5095 rc = -EIO;
5096 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005097 default:
5098 rc = -ENXIO;
5099 }
5100
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005101 if (!rc)
5102 tpacpi_led_state_cache[led] = ledstatus;
5103
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005104 return rc;
5105}
5106
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005107static void led_set_status_worker(struct work_struct *work)
5108{
5109 struct tpacpi_led_classdev *data =
5110 container_of(work, struct tpacpi_led_classdev, work);
5111
5112 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005113 led_set_status(data->led, data->new_state);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005114}
5115
5116static void led_sysfs_set(struct led_classdev *led_cdev,
5117 enum led_brightness brightness)
5118{
5119 struct tpacpi_led_classdev *data = container_of(led_cdev,
5120 struct tpacpi_led_classdev, led_classdev);
5121
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005122 if (brightness == LED_OFF)
5123 data->new_state = TPACPI_LED_OFF;
5124 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
5125 data->new_state = TPACPI_LED_ON;
5126 else
5127 data->new_state = TPACPI_LED_BLINK;
5128
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03005129 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005130}
5131
5132static int led_sysfs_blink_set(struct led_classdev *led_cdev,
5133 unsigned long *delay_on, unsigned long *delay_off)
5134{
5135 struct tpacpi_led_classdev *data = container_of(led_cdev,
5136 struct tpacpi_led_classdev, led_classdev);
5137
5138 /* Can we choose the flash rate? */
5139 if (*delay_on == 0 && *delay_off == 0) {
5140 /* yes. set them to the hardware blink rate (1 Hz) */
5141 *delay_on = 500; /* ms */
5142 *delay_off = 500; /* ms */
5143 } else if ((*delay_on != 500) || (*delay_off != 500))
5144 return -EINVAL;
5145
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005146 data->new_state = TPACPI_LED_BLINK;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03005147 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005148
5149 return 0;
5150}
5151
5152static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
5153{
5154 int rc;
5155
5156 struct tpacpi_led_classdev *data = container_of(led_cdev,
5157 struct tpacpi_led_classdev, led_classdev);
5158
5159 rc = led_get_status(data->led);
5160
5161 if (rc == TPACPI_LED_OFF || rc < 0)
5162 rc = LED_OFF; /* no error handling in led class :( */
5163 else
5164 rc = LED_FULL;
5165
5166 return rc;
5167}
5168
5169static void led_exit(void)
5170{
5171 unsigned int i;
5172
5173 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5174 if (tpacpi_leds[i].led_classdev.name)
5175 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
5176 }
5177
5178 kfree(tpacpi_leds);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005179}
5180
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005181static int __init tpacpi_init_led(unsigned int led)
5182{
5183 int rc;
5184
5185 tpacpi_leds[led].led = led;
5186
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005187 /* LEDs with no name don't get registered */
5188 if (!tpacpi_led_names[led])
5189 return 0;
5190
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005191 tpacpi_leds[led].led_classdev.brightness_set = &led_sysfs_set;
5192 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5193 if (led_supported == TPACPI_LED_570)
5194 tpacpi_leds[led].led_classdev.brightness_get =
5195 &led_sysfs_get;
5196
5197 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5198
5199 INIT_WORK(&tpacpi_leds[led].work, led_set_status_worker);
5200
5201 rc = led_classdev_register(&tpacpi_pdev->dev,
5202 &tpacpi_leds[led].led_classdev);
5203 if (rc < 0)
5204 tpacpi_leds[led].led_classdev.name = NULL;
5205
5206 return rc;
5207}
5208
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005209static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
5210 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5211 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5212 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5213
5214 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5215 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5216 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5217 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5218 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5219 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5220 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5221 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5222
5223 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5224 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5225 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5226 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5227 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5228
5229 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5230 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5231 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5232 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5233
5234 /* (1) - may have excess leds enabled on MSB */
5235
5236 /* Defaults (order matters, keep last, don't reorder!) */
5237 { /* Lenovo */
5238 .vendor = PCI_VENDOR_ID_LENOVO,
5239 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5240 .quirks = 0x1fffU,
5241 },
5242 { /* IBM ThinkPads with no EC version string */
5243 .vendor = PCI_VENDOR_ID_IBM,
5244 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
5245 .quirks = 0x00ffU,
5246 },
5247 { /* IBM ThinkPads with EC version string */
5248 .vendor = PCI_VENDOR_ID_IBM,
5249 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5250 .quirks = 0x00bfU,
5251 },
5252};
5253
5254#undef TPACPI_LEDQ_IBM
5255#undef TPACPI_LEDQ_LNV
5256
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005257static int __init led_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005258{
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005259 unsigned int i;
5260 int rc;
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005261 unsigned long useful_leds;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005262
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005263 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
5264
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005265 TPACPI_ACPIHANDLE_INIT(led);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005266
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005267 if (!led_handle)
5268 /* led not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005269 led_supported = TPACPI_LED_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005270 else if (strlencmp(led_path, "SLED") == 0)
5271 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005272 led_supported = TPACPI_LED_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005273 else if (strlencmp(led_path, "SYSL") == 0)
5274 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005275 led_supported = TPACPI_LED_OLD;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005276 else
5277 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005278 led_supported = TPACPI_LED_NEW;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005279
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005280 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
5281 str_supported(led_supported), led_supported);
5282
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005283 if (led_supported == TPACPI_LED_NONE)
5284 return 1;
5285
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005286 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
5287 GFP_KERNEL);
5288 if (!tpacpi_leds) {
5289 printk(TPACPI_ERR "Out of memory for LED data\n");
5290 return -ENOMEM;
5291 }
5292
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005293 useful_leds = tpacpi_check_quirks(led_useful_qtable,
5294 ARRAY_SIZE(led_useful_qtable));
5295
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005296 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005297 if (!tpacpi_is_led_restricted(i) &&
5298 test_bit(i, &useful_leds)) {
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005299 rc = tpacpi_init_led(i);
5300 if (rc < 0) {
5301 led_exit();
5302 return rc;
5303 }
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005304 }
5305 }
5306
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005307#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005308 printk(TPACPI_NOTICE
5309 "warning: userspace override of important "
5310 "firmware LEDs is enabled\n");
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005311#endif
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005312 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005313}
5314
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005315#define str_led_status(s) \
5316 ((s) == TPACPI_LED_OFF ? "off" : \
5317 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005318
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005319static int led_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005320{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005321 if (!led_supported) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005322 seq_printf(m, "status:\t\tnot supported\n");
5323 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005324 }
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005325 seq_printf(m, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005326
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005327 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005328 /* 570 */
5329 int i, status;
5330 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005331 status = led_get_status(i);
5332 if (status < 0)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005333 return -EIO;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005334 seq_printf(m, "%d:\t\t%s\n",
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005335 i, str_led_status(status));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005336 }
5337 }
5338
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005339 seq_printf(m, "commands:\t"
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005340 "<led> on, <led> off, <led> blink (<led> is 0-15)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005341
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005342 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005343}
5344
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005345static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005346{
5347 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005348 int led, rc;
5349 enum led_status_t s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005350
5351 if (!led_supported)
5352 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005353
5354 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005355 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 15)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005356 return -EINVAL;
5357
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005358 if (strstr(cmd, "off")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005359 s = TPACPI_LED_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005360 } else if (strstr(cmd, "on")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005361 s = TPACPI_LED_ON;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005362 } else if (strstr(cmd, "blink")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005363 s = TPACPI_LED_BLINK;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005364 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005365 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005366 }
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005367
5368 rc = led_set_status(led, s);
5369 if (rc < 0)
5370 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005371 }
5372
5373 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005374}
5375
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005376static struct ibm_struct led_driver_data = {
5377 .name = "led",
5378 .read = led_read,
5379 .write = led_write,
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005380 .exit = led_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005381};
5382
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005383/*************************************************************************
5384 * Beep subdriver
5385 */
5386
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005387TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005388
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005389#define TPACPI_BEEP_Q1 0x0001
5390
5391static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
5392 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
5393 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
5394};
5395
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005396static int __init beep_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005397{
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005398 unsigned long quirks;
5399
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005400 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
5401
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005402 TPACPI_ACPIHANDLE_INIT(beep);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005403
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005404 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
5405 str_supported(beep_handle != NULL));
5406
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005407 quirks = tpacpi_check_quirks(beep_quirk_table,
5408 ARRAY_SIZE(beep_quirk_table));
5409
5410 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
5411
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005412 return (beep_handle)? 0 : 1;
5413}
5414
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005415static int beep_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005416{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005417 if (!beep_handle)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005418 seq_printf(m, "status:\t\tnot supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005419 else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005420 seq_printf(m, "status:\t\tsupported\n");
5421 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-17)\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005422 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005423
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005424 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005425}
5426
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005427static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005428{
5429 char *cmd;
5430 int beep_cmd;
5431
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005432 if (!beep_handle)
5433 return -ENODEV;
5434
Linus Torvalds1da177e2005-04-16 15:20:36 -07005435 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005436 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
5437 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005438 /* beep_cmd set */
5439 } else
5440 return -EINVAL;
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005441 if (tp_features.beep_needs_two_args) {
5442 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
5443 beep_cmd, 0))
5444 return -EIO;
5445 } else {
5446 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
5447 beep_cmd))
5448 return -EIO;
5449 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005450 }
5451
5452 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005453}
5454
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005455static struct ibm_struct beep_driver_data = {
5456 .name = "beep",
5457 .read = beep_read,
5458 .write = beep_write,
5459};
5460
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005461/*************************************************************************
5462 * Thermal subdriver
5463 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005464
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005465enum thermal_access_mode {
5466 TPACPI_THERMAL_NONE = 0, /* No thermal support */
5467 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
5468 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
5469 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
5470 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
5471};
5472
5473enum { /* TPACPI_THERMAL_TPEC_* */
5474 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
5475 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
5476 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005477
5478 TPACPI_THERMAL_SENSOR_NA = -128000, /* Sensor not available */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005479};
5480
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005481
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005482#define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5483struct ibm_thermal_sensors_struct {
5484 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
5485};
5486
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005487static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005488
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005489/* idx is zero-based */
5490static int thermal_get_sensor(int idx, s32 *value)
5491{
5492 int t;
5493 s8 tmp;
5494 char tmpi[5];
5495
5496 t = TP_EC_THERMAL_TMP0;
5497
5498 switch (thermal_read_mode) {
5499#if TPACPI_MAX_THERMAL_SENSORS >= 16
5500 case TPACPI_THERMAL_TPEC_16:
5501 if (idx >= 8 && idx <= 15) {
5502 t = TP_EC_THERMAL_TMP8;
5503 idx -= 8;
5504 }
5505 /* fallthrough */
5506#endif
5507 case TPACPI_THERMAL_TPEC_8:
5508 if (idx <= 7) {
5509 if (!acpi_ec_read(t + idx, &tmp))
5510 return -EIO;
5511 *value = tmp * 1000;
5512 return 0;
5513 }
5514 break;
5515
5516 case TPACPI_THERMAL_ACPI_UPDT:
5517 if (idx <= 7) {
5518 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5519 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
5520 return -EIO;
5521 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5522 return -EIO;
5523 *value = (t - 2732) * 100;
5524 return 0;
5525 }
5526 break;
5527
5528 case TPACPI_THERMAL_ACPI_TMP07:
5529 if (idx <= 7) {
5530 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5531 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5532 return -EIO;
5533 if (t > 127 || t < -127)
5534 t = TP_EC_THERMAL_TMP_NA;
5535 *value = t * 1000;
5536 return 0;
5537 }
5538 break;
5539
5540 case TPACPI_THERMAL_NONE:
5541 default:
5542 return -ENOSYS;
5543 }
5544
5545 return -EINVAL;
5546}
5547
5548static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5549{
5550 int res, i;
5551 int n;
5552
5553 n = 8;
5554 i = 0;
5555
5556 if (!s)
5557 return -EINVAL;
5558
5559 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5560 n = 16;
5561
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005562 for (i = 0 ; i < n; i++) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005563 res = thermal_get_sensor(i, &s->temp[i]);
5564 if (res)
5565 return res;
5566 }
5567
5568 return n;
5569}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005570
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005571static void thermal_dump_all_sensors(void)
5572{
5573 int n, i;
5574 struct ibm_thermal_sensors_struct t;
5575
5576 n = thermal_get_sensors(&t);
5577 if (n <= 0)
5578 return;
5579
5580 printk(TPACPI_NOTICE
5581 "temperatures (Celsius):");
5582
5583 for (i = 0; i < n; i++) {
5584 if (t.temp[i] != TPACPI_THERMAL_SENSOR_NA)
5585 printk(KERN_CONT " %d", (int)(t.temp[i] / 1000));
5586 else
5587 printk(KERN_CONT " N/A");
5588 }
5589
5590 printk(KERN_CONT "\n");
5591}
5592
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005593/* sysfs temp##_input -------------------------------------------------- */
5594
5595static ssize_t thermal_temp_input_show(struct device *dev,
5596 struct device_attribute *attr,
5597 char *buf)
5598{
5599 struct sensor_device_attribute *sensor_attr =
5600 to_sensor_dev_attr(attr);
5601 int idx = sensor_attr->index;
5602 s32 value;
5603 int res;
5604
5605 res = thermal_get_sensor(idx, &value);
5606 if (res)
5607 return res;
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005608 if (value == TPACPI_THERMAL_SENSOR_NA)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005609 return -ENXIO;
5610
5611 return snprintf(buf, PAGE_SIZE, "%d\n", value);
5612}
5613
5614#define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005615 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5616 thermal_temp_input_show, NULL, _idxB)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005617
5618static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5619 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5620 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5621 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5622 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5623 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5624 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5625 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5626 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5627 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5628 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5629 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5630 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5631 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5632 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5633 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5634 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5635};
5636
5637#define THERMAL_ATTRS(X) \
5638 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5639
5640static struct attribute *thermal_temp_input_attr[] = {
5641 THERMAL_ATTRS(8),
5642 THERMAL_ATTRS(9),
5643 THERMAL_ATTRS(10),
5644 THERMAL_ATTRS(11),
5645 THERMAL_ATTRS(12),
5646 THERMAL_ATTRS(13),
5647 THERMAL_ATTRS(14),
5648 THERMAL_ATTRS(15),
5649 THERMAL_ATTRS(0),
5650 THERMAL_ATTRS(1),
5651 THERMAL_ATTRS(2),
5652 THERMAL_ATTRS(3),
5653 THERMAL_ATTRS(4),
5654 THERMAL_ATTRS(5),
5655 THERMAL_ATTRS(6),
5656 THERMAL_ATTRS(7),
5657 NULL
5658};
5659
5660static const struct attribute_group thermal_temp_input16_group = {
5661 .attrs = thermal_temp_input_attr
5662};
5663
5664static const struct attribute_group thermal_temp_input8_group = {
5665 .attrs = &thermal_temp_input_attr[8]
5666};
5667
5668#undef THERMAL_SENSOR_ATTR_TEMP
5669#undef THERMAL_ATTRS
5670
5671/* --------------------------------------------------------------------- */
5672
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005673static int __init thermal_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005674{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005675 u8 t, ta1, ta2;
5676 int i;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005677 int acpi_tmp7;
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005678 int res;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005679
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005680 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5681
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005682 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005683
Henrique de Moraes Holschuh3d6f99c2007-07-18 23:45:46 -03005684 if (thinkpad_id.ec_model) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005685 /*
5686 * Direct EC access mode: sensors at registers
5687 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5688 * non-implemented, thermal sensors return 0x80 when
5689 * not available
5690 */
5691
5692 ta1 = ta2 = 0;
5693 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03005694 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005695 ta1 |= t;
5696 } else {
5697 ta1 = 0;
5698 break;
5699 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03005700 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005701 ta2 |= t;
5702 } else {
5703 ta1 = 0;
5704 break;
5705 }
5706 }
5707 if (ta1 == 0) {
5708 /* This is sheer paranoia, but we handle it anyway */
5709 if (acpi_tmp7) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005710 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005711 "ThinkPad ACPI EC access misbehaving, "
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005712 "falling back to ACPI TMPx access "
5713 "mode\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005714 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005715 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005716 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005717 "ThinkPad ACPI EC access misbehaving, "
5718 "disabling thermal sensors access\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005719 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005720 }
5721 } else {
5722 thermal_read_mode =
5723 (ta2 != 0) ?
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005724 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005725 }
5726 } else if (acpi_tmp7) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005727 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
5728 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005729 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005730 } else {
5731 /* Standard ACPI TMPx access, max 8 sensors */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005732 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005733 }
5734 } else {
5735 /* temperatures not supported on 570, G4x, R30, R31, R32 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005736 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005737 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005738
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005739 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5740 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5741 thermal_read_mode);
5742
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005743 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005744 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005745 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005746 &thermal_temp_input16_group);
5747 if (res)
5748 return res;
5749 break;
5750 case TPACPI_THERMAL_TPEC_8:
5751 case TPACPI_THERMAL_ACPI_TMP07:
5752 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005753 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005754 &thermal_temp_input8_group);
5755 if (res)
5756 return res;
5757 break;
5758 case TPACPI_THERMAL_NONE:
5759 default:
5760 return 1;
5761 }
5762
5763 return 0;
5764}
5765
5766static void thermal_exit(void)
5767{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005768 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005769 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005770 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005771 &thermal_temp_input16_group);
5772 break;
5773 case TPACPI_THERMAL_TPEC_8:
5774 case TPACPI_THERMAL_ACPI_TMP07:
5775 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005776 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005777 &thermal_temp_input16_group);
5778 break;
5779 case TPACPI_THERMAL_NONE:
5780 default:
5781 break;
5782 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005783}
5784
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005785static int thermal_read(struct seq_file *m)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005786{
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005787 int n, i;
5788 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005789
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005790 n = thermal_get_sensors(&t);
5791 if (unlikely(n < 0))
5792 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005793
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005794 seq_printf(m, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005795
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005796 if (n > 0) {
5797 for (i = 0; i < (n - 1); i++)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005798 seq_printf(m, "%d ", t.temp[i] / 1000);
5799 seq_printf(m, "%d\n", t.temp[i] / 1000);
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005800 } else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005801 seq_printf(m, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005802
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005803 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005804}
5805
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005806static struct ibm_struct thermal_driver_data = {
5807 .name = "thermal",
5808 .read = thermal_read,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005809 .exit = thermal_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005810};
5811
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005812/*************************************************************************
5813 * EC Dump subdriver
5814 */
5815
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005816static u8 ecdump_regs[256];
5817
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005818static int ecdump_read(struct seq_file *m)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005819{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005820 int i, j;
5821 u8 v;
5822
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005823 seq_printf(m, "EC "
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005824 " +00 +01 +02 +03 +04 +05 +06 +07"
5825 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
5826 for (i = 0; i < 256; i += 16) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005827 seq_printf(m, "EC 0x%02x:", i);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005828 for (j = 0; j < 16; j++) {
5829 if (!acpi_ec_read(i + j, &v))
5830 break;
5831 if (v != ecdump_regs[i + j])
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005832 seq_printf(m, " *%02x", v);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005833 else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005834 seq_printf(m, " %02x", v);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005835 ecdump_regs[i + j] = v;
5836 }
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005837 seq_putc(m, '\n');
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005838 if (j != 16)
5839 break;
5840 }
5841
5842 /* These are way too dangerous to advertise openly... */
5843#if 0
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005844 seq_printf(m, "commands:\t0x<offset> 0x<value>"
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005845 " (<offset> is 00-ff, <value> is 00-ff)\n");
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005846 seq_printf(m, "commands:\t0x<offset> <value> "
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005847 " (<offset> is 00-ff, <value> is 0-255)\n");
5848#endif
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005849 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005850}
5851
5852static int ecdump_write(char *buf)
5853{
5854 char *cmd;
5855 int i, v;
5856
5857 while ((cmd = next_cmd(&buf))) {
5858 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
5859 /* i and v set */
5860 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
5861 /* i and v set */
5862 } else
5863 return -EINVAL;
5864 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
5865 if (!acpi_ec_write(i, v))
5866 return -EIO;
5867 } else
5868 return -EINVAL;
5869 }
5870
5871 return 0;
5872}
5873
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005874static struct ibm_struct ecdump_driver_data = {
5875 .name = "ecdump",
5876 .read = ecdump_read,
5877 .write = ecdump_write,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03005878 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005879};
5880
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005881/*************************************************************************
5882 * Backlight/brightness subdriver
5883 */
Holger Macht8acb0252006-10-20 14:30:28 -07005884
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005885#define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5886
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005887/*
5888 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
5889 * CMOS NVRAM byte 0x5E, bits 0-3.
5890 *
5891 * EC HBRV (0x31) has the following layout
5892 * Bit 7: unknown function
5893 * Bit 6: unknown function
5894 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5895 * Bit 4: must be set to zero to avoid problems
5896 * Bit 3-0: backlight brightness level
5897 *
5898 * brightness_get_raw returns status data in the HBRV layout
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03005899 *
5900 * WARNING: The X61 has been verified to use HBRV for something else, so
5901 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
5902 * testing on the very early *60 Lenovo models...
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005903 */
5904
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005905enum {
5906 TP_EC_BACKLIGHT = 0x31,
5907
5908 /* TP_EC_BACKLIGHT bitmasks */
5909 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5910 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5911 TP_EC_BACKLIGHT_MAPSW = 0x20,
5912};
5913
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005914enum tpacpi_brightness_access_mode {
5915 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
5916 TPACPI_BRGHT_MODE_EC, /* EC control */
5917 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
5918 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
5919 TPACPI_BRGHT_MODE_MAX
5920};
5921
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -03005922static struct backlight_device *ibm_backlight_device;
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005923
5924static enum tpacpi_brightness_access_mode brightness_mode =
5925 TPACPI_BRGHT_MODE_MAX;
5926
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005927static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5928
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005929static struct mutex brightness_mutex;
5930
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005931/* NVRAM brightness access,
5932 * call with brightness_mutex held! */
5933static unsigned int tpacpi_brightness_nvram_get(void)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005934{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005935 u8 lnvram;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005936
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005937 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5938 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5939 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
5940 lnvram &= (tp_features.bright_16levels) ? 0x0f : 0x07;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005941
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005942 return lnvram;
5943}
5944
5945static void tpacpi_brightness_checkpoint_nvram(void)
5946{
5947 u8 lec = 0;
5948 u8 b_nvram;
5949
5950 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
5951 return;
5952
5953 vdbg_printk(TPACPI_DBG_BRGHT,
5954 "trying to checkpoint backlight level to NVRAM...\n");
5955
5956 if (mutex_lock_killable(&brightness_mutex) < 0)
5957 return;
5958
5959 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5960 goto unlock;
5961 lec &= TP_EC_BACKLIGHT_LVLMSK;
5962 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
5963
5964 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5965 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
5966 /* NVRAM needs update */
5967 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
5968 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
5969 b_nvram |= lec;
5970 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
5971 dbg_printk(TPACPI_DBG_BRGHT,
5972 "updated NVRAM backlight level to %u (0x%02x)\n",
5973 (unsigned int) lec, (unsigned int) b_nvram);
5974 } else
5975 vdbg_printk(TPACPI_DBG_BRGHT,
5976 "NVRAM backlight level already is %u (0x%02x)\n",
5977 (unsigned int) lec, (unsigned int) b_nvram);
5978
5979unlock:
5980 mutex_unlock(&brightness_mutex);
5981}
5982
5983
5984/* call with brightness_mutex held! */
5985static int tpacpi_brightness_get_raw(int *status)
5986{
5987 u8 lec = 0;
5988
5989 switch (brightness_mode) {
5990 case TPACPI_BRGHT_MODE_UCMS_STEP:
5991 *status = tpacpi_brightness_nvram_get();
5992 return 0;
5993 case TPACPI_BRGHT_MODE_EC:
5994 case TPACPI_BRGHT_MODE_ECNVRAM:
5995 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
Henrique de Moraes Holschuh2d5e94d2008-04-26 01:02:20 -03005996 return -EIO;
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005997 *status = lec;
5998 return 0;
5999 default:
6000 return -ENXIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006001 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006002}
6003
6004/* call with brightness_mutex held! */
6005/* do NOT call with illegal backlight level value */
6006static int tpacpi_brightness_set_ec(unsigned int value)
6007{
6008 u8 lec = 0;
6009
6010 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6011 return -EIO;
6012
6013 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
6014 (lec & TP_EC_BACKLIGHT_CMDMSK) |
6015 (value & TP_EC_BACKLIGHT_LVLMSK))))
6016 return -EIO;
6017
6018 return 0;
6019}
6020
6021/* call with brightness_mutex held! */
6022static int tpacpi_brightness_set_ucmsstep(unsigned int value)
6023{
6024 int cmos_cmd, inc;
6025 unsigned int current_value, i;
6026
6027 current_value = tpacpi_brightness_nvram_get();
6028
6029 if (value == current_value)
6030 return 0;
6031
6032 cmos_cmd = (value > current_value) ?
6033 TP_CMOS_BRIGHTNESS_UP :
6034 TP_CMOS_BRIGHTNESS_DOWN;
6035 inc = (value > current_value) ? 1 : -1;
6036
6037 for (i = current_value; i != value; i += inc)
6038 if (issue_thinkpad_cmos_command(cmos_cmd))
6039 return -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006040
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006041 return 0;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006042}
6043
6044/* May return EINTR which can always be mapped to ERESTARTSYS */
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006045static int brightness_set(unsigned int value)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006046{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006047 int res;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006048
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006049 if (value > ((tp_features.bright_16levels)? 15 : 7) ||
6050 value < 0)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006051 return -EINVAL;
6052
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006053 vdbg_printk(TPACPI_DBG_BRGHT,
6054 "set backlight level to %d\n", value);
6055
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006056 res = mutex_lock_killable(&brightness_mutex);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006057 if (res < 0)
6058 return res;
6059
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006060 switch (brightness_mode) {
6061 case TPACPI_BRGHT_MODE_EC:
6062 case TPACPI_BRGHT_MODE_ECNVRAM:
6063 res = tpacpi_brightness_set_ec(value);
6064 break;
6065 case TPACPI_BRGHT_MODE_UCMS_STEP:
6066 res = tpacpi_brightness_set_ucmsstep(value);
6067 break;
6068 default:
6069 res = -ENXIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006070 }
6071
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006072 mutex_unlock(&brightness_mutex);
6073 return res;
6074}
6075
6076/* sysfs backlight class ----------------------------------------------- */
6077
6078static int brightness_update_status(struct backlight_device *bd)
6079{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006080 unsigned int level =
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006081 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
6082 bd->props.power == FB_BLANK_UNBLANK) ?
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006083 bd->props.brightness : 0;
6084
6085 dbg_printk(TPACPI_DBG_BRGHT,
6086 "backlight: attempt to set level to %d\n",
6087 level);
6088
6089 /* it is the backlight class's job (caller) to handle
6090 * EINTR and other errors properly */
6091 return brightness_set(level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006092}
Holger Macht8acb0252006-10-20 14:30:28 -07006093
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006094static int brightness_get(struct backlight_device *bd)
6095{
6096 int status, res;
6097
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006098 res = mutex_lock_killable(&brightness_mutex);
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006099 if (res < 0)
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006100 return 0;
6101
6102 res = tpacpi_brightness_get_raw(&status);
6103
6104 mutex_unlock(&brightness_mutex);
6105
6106 if (res < 0)
6107 return 0;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006108
6109 return status & TP_EC_BACKLIGHT_LVLMSK;
6110}
6111
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00006112static void tpacpi_brightness_notify_change(void)
6113{
6114 backlight_force_update(ibm_backlight_device,
6115 BACKLIGHT_UPDATE_HOTKEY);
6116}
6117
Richard Purdie599a52d2007-02-10 23:07:48 +00006118static struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006119 .get_brightness = brightness_get,
6120 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006121};
6122
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006123/* --------------------------------------------------------------------- */
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03006124
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006125/*
6126 * These are only useful for models that have only one possibility
6127 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6128 * these quirks.
6129 */
6130#define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6131#define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6132#define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6133
6134static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
6135 /* Models with ATI GPUs known to require ECNVRAM mode */
6136 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
6137
Henrique de Moraes Holschuh6da25bf2009-09-12 15:22:11 -03006138 /* Models with ATI GPUs that can use ECNVRAM */
6139 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC),
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006140 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6141 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6142 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6143
Henrique de Moraes Holschuh6da25bf2009-09-12 15:22:11 -03006144 /* Models with Intel Extreme Graphics 2 */
6145 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC),
Henrique de Moraes Holschuha9f8eac2009-12-09 01:36:21 +00006146 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6147 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006148
6149 /* Models with Intel GMA900 */
6150 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
6151 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
6152 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
6153};
6154
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006155static int __init brightness_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006156{
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006157 int b;
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006158 unsigned long quirks;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006159
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006160 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
6161
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03006162 mutex_init(&brightness_mutex);
6163
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006164 quirks = tpacpi_check_quirks(brightness_quirk_table,
6165 ARRAY_SIZE(brightness_quirk_table));
6166
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03006167 /*
6168 * We always attempt to detect acpi support, so as to switch
6169 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6170 * going to publish a backlight interface
6171 */
6172 b = tpacpi_check_std_acpi_brightness_support();
6173 if (b > 0) {
Thomas Renninger2dba1b52008-08-01 17:38:03 +02006174
6175 if (acpi_video_backlight_support()) {
6176 if (brightness_enable > 1) {
6177 printk(TPACPI_NOTICE
6178 "Standard ACPI backlight interface "
6179 "available, not loading native one.\n");
6180 return 1;
6181 } else if (brightness_enable == 1) {
6182 printk(TPACPI_NOTICE
6183 "Backlight control force enabled, even if standard "
6184 "ACPI backlight interface is available\n");
6185 }
6186 } else {
6187 if (brightness_enable > 1) {
6188 printk(TPACPI_NOTICE
6189 "Standard ACPI backlight interface not "
6190 "available, thinkpad_acpi native "
6191 "brightness control enabled\n");
6192 }
Henrique de Moraes Holschuhe11e2112007-10-30 17:46:22 -02006193 }
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02006194 }
6195
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03006196 if (!brightness_enable) {
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006197 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03006198 "brightness support disabled by "
6199 "module parameter\n");
6200 return 1;
6201 }
6202
6203 if (b > 16) {
6204 printk(TPACPI_ERR
6205 "Unsupported brightness interface, "
6206 "please contact %s\n", TPACPI_MAIL);
6207 return 1;
6208 }
6209 if (b == 16)
6210 tp_features.bright_16levels = 1;
6211
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006212 /*
6213 * Check for module parameter bogosity, note that we
6214 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
6215 * able to detect "unspecified"
6216 */
6217 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03006218 return -EINVAL;
6219
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006220 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
6221 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
6222 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006223 if (quirks & TPACPI_BRGHT_Q_EC)
6224 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
6225 else
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006226 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
6227
6228 dbg_printk(TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006229 "driver auto-selected brightness_mode=%d\n",
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006230 brightness_mode);
6231 }
6232
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03006233 /* Safety */
6234 if (thinkpad_id.vendor != PCI_VENDOR_ID_IBM &&
6235 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
6236 brightness_mode == TPACPI_BRGHT_MODE_EC))
6237 return -EINVAL;
6238
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006239 if (tpacpi_brightness_get_raw(&b) < 0)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03006240 return 1;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006241
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02006242 if (tp_features.bright_16levels)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006243 printk(TPACPI_INFO
6244 "detected a 16-level brightness capable ThinkPad\n");
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02006245
Henrique de Moraes Holschuh7d5a0152007-04-24 11:48:20 -03006246 ibm_backlight_device = backlight_device_register(
6247 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
6248 &ibm_backlight_data);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006249 if (IS_ERR(ibm_backlight_device)) {
Henrique de Moraes Holschuh435c47e2009-09-20 14:09:22 -03006250 int rc = PTR_ERR(ibm_backlight_device);
6251 ibm_backlight_device = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006252 printk(TPACPI_ERR "Could not register backlight device\n");
Henrique de Moraes Holschuh435c47e2009-09-20 14:09:22 -03006253 return rc;
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006254 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006255 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6256 "brightness is supported\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006257
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006258 if (quirks & TPACPI_BRGHT_Q_ASK) {
6259 printk(TPACPI_NOTICE
6260 "brightness: will use unverified default: "
6261 "brightness_mode=%d\n", brightness_mode);
6262 printk(TPACPI_NOTICE
6263 "brightness: please report to %s whether it works well "
6264 "or not on your ThinkPad\n", TPACPI_MAIL);
6265 }
6266
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02006267 ibm_backlight_device->props.max_brightness =
6268 (tp_features.bright_16levels)? 15 : 7;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006269 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006270 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00006271
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00006272 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6273 "brightness: registering brightness hotkeys "
6274 "as change notification\n");
6275 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6276 | TP_ACPI_HKEY_BRGHTUP_MASK
6277 | TP_ACPI_HKEY_BRGHTDWN_MASK);;
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006278 return 0;
6279}
6280
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006281static void brightness_suspend(pm_message_t state)
6282{
6283 tpacpi_brightness_checkpoint_nvram();
6284}
6285
6286static void brightness_shutdown(void)
6287{
6288 tpacpi_brightness_checkpoint_nvram();
6289}
6290
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006291static void brightness_exit(void)
6292{
6293 if (ibm_backlight_device) {
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006294 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006295 "calling backlight_device_unregister()\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006296 backlight_device_unregister(ibm_backlight_device);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006297 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006298
6299 tpacpi_brightness_checkpoint_nvram();
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006300}
6301
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006302static int brightness_read(struct seq_file *m)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006303{
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006304 int level;
6305
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006306 level = brightness_get(NULL);
6307 if (level < 0) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006308 seq_printf(m, "level:\t\tunreadable\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006309 } else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006310 seq_printf(m, "level:\t\t%d\n", level);
6311 seq_printf(m, "commands:\tup, down\n");
6312 seq_printf(m, "commands:\tlevel <level>"
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02006313 " (<level> is 0-%d)\n",
6314 (tp_features.bright_16levels) ? 15 : 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006315 }
6316
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006317 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006318}
6319
6320static int brightness_write(char *buf)
6321{
6322 int level;
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006323 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006324 char *cmd;
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02006325 int max_level = (tp_features.bright_16levels) ? 15 : 7;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006326
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006327 level = brightness_get(NULL);
6328 if (level < 0)
6329 return level;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006330
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006331 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006332 if (strlencmp(cmd, "up") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006333 if (level < max_level)
6334 level++;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006335 } else if (strlencmp(cmd, "down") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006336 if (level > 0)
6337 level--;
6338 } else if (sscanf(cmd, "level %d", &level) == 1 &&
6339 level >= 0 && level <= max_level) {
6340 /* new level set */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006341 } else
6342 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006343 }
6344
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006345 tpacpi_disclose_usertask("procfs brightness",
6346 "set level to %d\n", level);
6347
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006348 /*
6349 * Now we know what the final level should be, so we try to set it.
6350 * Doing it this way makes the syscall restartable in case of EINTR
6351 */
6352 rc = brightness_set(level);
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00006353 if (!rc && ibm_backlight_device)
6354 backlight_force_update(ibm_backlight_device,
6355 BACKLIGHT_UPDATE_SYSFS);
Roel Kluin80a8d122009-11-20 19:48:23 +01006356 return (rc == -EINTR)? -ERESTARTSYS : rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006357}
6358
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006359static struct ibm_struct brightness_driver_data = {
6360 .name = "brightness",
6361 .read = brightness_read,
6362 .write = brightness_write,
6363 .exit = brightness_exit,
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006364 .suspend = brightness_suspend,
6365 .shutdown = brightness_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006366};
6367
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006368/*************************************************************************
6369 * Volume subdriver
6370 */
6371
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006372/*
6373 * IBM ThinkPads have a simple volume controller with MUTE gating.
6374 * Very early Lenovo ThinkPads follow the IBM ThinkPad spec.
6375 *
6376 * Since the *61 series (and probably also the later *60 series), Lenovo
6377 * ThinkPads only implement the MUTE gate.
6378 *
6379 * EC register 0x30
6380 * Bit 6: MUTE (1 mutes sound)
6381 * Bit 3-0: Volume
6382 * Other bits should be zero as far as we know.
6383 *
6384 * This is also stored in CMOS NVRAM, byte 0x60, bit 6 (MUTE), and
6385 * bits 3-0 (volume). Other bits in NVRAM may have other functions,
6386 * such as bit 7 which is used to detect repeated presses of MUTE,
6387 * and we leave them unchanged.
6388 */
6389
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006390#define TPACPI_ALSA_DRVNAME "ThinkPad EC"
6391#define TPACPI_ALSA_SHRTNAME "ThinkPad Console Audio Control"
6392#define TPACPI_ALSA_MIXERNAME TPACPI_ALSA_SHRTNAME
6393
6394static int alsa_index = SNDRV_DEFAULT_IDX1;
6395static char *alsa_id = "ThinkPadEC";
6396static int alsa_enable = SNDRV_DEFAULT_ENABLE1;
6397
6398struct tpacpi_alsa_data {
6399 struct snd_card *card;
6400 struct snd_ctl_elem_id *ctl_mute_id;
6401 struct snd_ctl_elem_id *ctl_vol_id;
6402};
6403
6404static struct snd_card *alsa_card;
6405
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006406enum {
6407 TP_EC_AUDIO = 0x30,
6408
6409 /* TP_EC_AUDIO bits */
6410 TP_EC_AUDIO_MUTESW = 6,
6411
6412 /* TP_EC_AUDIO bitmasks */
6413 TP_EC_AUDIO_LVL_MSK = 0x0F,
6414 TP_EC_AUDIO_MUTESW_MSK = (1 << TP_EC_AUDIO_MUTESW),
6415
6416 /* Maximum volume */
6417 TP_EC_VOLUME_MAX = 14,
6418};
6419
6420enum tpacpi_volume_access_mode {
6421 TPACPI_VOL_MODE_AUTO = 0, /* Not implemented yet */
6422 TPACPI_VOL_MODE_EC, /* Pure EC control */
6423 TPACPI_VOL_MODE_UCMS_STEP, /* UCMS step-based control: N/A */
6424 TPACPI_VOL_MODE_ECNVRAM, /* EC control w/ NVRAM store */
6425 TPACPI_VOL_MODE_MAX
6426};
6427
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006428enum tpacpi_volume_capabilities {
6429 TPACPI_VOL_CAP_AUTO = 0, /* Use white/blacklist */
6430 TPACPI_VOL_CAP_VOLMUTE, /* Output vol and mute */
6431 TPACPI_VOL_CAP_MUTEONLY, /* Output mute only */
6432 TPACPI_VOL_CAP_MAX
6433};
6434
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006435static enum tpacpi_volume_access_mode volume_mode =
6436 TPACPI_VOL_MODE_MAX;
6437
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006438static enum tpacpi_volume_capabilities volume_capabilities;
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006439static int volume_control_allowed;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006440
6441/*
6442 * Used to syncronize writers to TP_EC_AUDIO and
6443 * TP_NVRAM_ADDR_MIXER, as we need to do read-modify-write
6444 */
6445static struct mutex volume_mutex;
6446
6447static void tpacpi_volume_checkpoint_nvram(void)
6448{
6449 u8 lec = 0;
6450 u8 b_nvram;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006451 u8 ec_mask;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006452
6453 if (volume_mode != TPACPI_VOL_MODE_ECNVRAM)
6454 return;
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006455 if (!volume_control_allowed)
6456 return;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006457
6458 vdbg_printk(TPACPI_DBG_MIXER,
6459 "trying to checkpoint mixer state to NVRAM...\n");
6460
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006461 if (tp_features.mixer_no_level_control)
6462 ec_mask = TP_EC_AUDIO_MUTESW_MSK;
6463 else
6464 ec_mask = TP_EC_AUDIO_MUTESW_MSK | TP_EC_AUDIO_LVL_MSK;
6465
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006466 if (mutex_lock_killable(&volume_mutex) < 0)
6467 return;
6468
6469 if (unlikely(!acpi_ec_read(TP_EC_AUDIO, &lec)))
6470 goto unlock;
6471 lec &= ec_mask;
6472 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
6473
6474 if (lec != (b_nvram & ec_mask)) {
6475 /* NVRAM needs update */
6476 b_nvram &= ~ec_mask;
6477 b_nvram |= lec;
6478 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_MIXER);
6479 dbg_printk(TPACPI_DBG_MIXER,
6480 "updated NVRAM mixer status to 0x%02x (0x%02x)\n",
6481 (unsigned int) lec, (unsigned int) b_nvram);
6482 } else {
6483 vdbg_printk(TPACPI_DBG_MIXER,
6484 "NVRAM mixer status already is 0x%02x (0x%02x)\n",
6485 (unsigned int) lec, (unsigned int) b_nvram);
6486 }
6487
6488unlock:
6489 mutex_unlock(&volume_mutex);
6490}
6491
6492static int volume_get_status_ec(u8 *status)
6493{
6494 u8 s;
6495
6496 if (!acpi_ec_read(TP_EC_AUDIO, &s))
6497 return -EIO;
6498
6499 *status = s;
6500
6501 dbg_printk(TPACPI_DBG_MIXER, "status 0x%02x\n", s);
6502
6503 return 0;
6504}
6505
6506static int volume_get_status(u8 *status)
6507{
6508 return volume_get_status_ec(status);
6509}
6510
6511static int volume_set_status_ec(const u8 status)
6512{
6513 if (!acpi_ec_write(TP_EC_AUDIO, status))
6514 return -EIO;
6515
6516 dbg_printk(TPACPI_DBG_MIXER, "set EC mixer to 0x%02x\n", status);
6517
6518 return 0;
6519}
6520
6521static int volume_set_status(const u8 status)
6522{
6523 return volume_set_status_ec(status);
6524}
6525
6526static int volume_set_mute_ec(const bool mute)
6527{
6528 int rc;
6529 u8 s, n;
6530
6531 if (mutex_lock_killable(&volume_mutex) < 0)
6532 return -EINTR;
6533
6534 rc = volume_get_status_ec(&s);
6535 if (rc)
6536 goto unlock;
6537
6538 n = (mute) ? s | TP_EC_AUDIO_MUTESW_MSK :
6539 s & ~TP_EC_AUDIO_MUTESW_MSK;
6540
6541 if (n != s)
6542 rc = volume_set_status_ec(n);
6543
6544unlock:
6545 mutex_unlock(&volume_mutex);
6546 return rc;
6547}
6548
6549static int volume_set_mute(const bool mute)
6550{
6551 dbg_printk(TPACPI_DBG_MIXER, "trying to %smute\n",
6552 (mute) ? "" : "un");
6553 return volume_set_mute_ec(mute);
6554}
6555
6556static int volume_set_volume_ec(const u8 vol)
6557{
6558 int rc;
6559 u8 s, n;
6560
6561 if (vol > TP_EC_VOLUME_MAX)
6562 return -EINVAL;
6563
6564 if (mutex_lock_killable(&volume_mutex) < 0)
6565 return -EINTR;
6566
6567 rc = volume_get_status_ec(&s);
6568 if (rc)
6569 goto unlock;
6570
6571 n = (s & ~TP_EC_AUDIO_LVL_MSK) | vol;
6572
6573 if (n != s)
6574 rc = volume_set_status_ec(n);
6575
6576unlock:
6577 mutex_unlock(&volume_mutex);
6578 return rc;
6579}
6580
6581static int volume_set_volume(const u8 vol)
6582{
6583 dbg_printk(TPACPI_DBG_MIXER,
6584 "trying to set volume level to %hu\n", vol);
6585 return volume_set_volume_ec(vol);
6586}
6587
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006588static void volume_alsa_notify_change(void)
6589{
6590 struct tpacpi_alsa_data *d;
6591
6592 if (alsa_card && alsa_card->private_data) {
6593 d = alsa_card->private_data;
6594 if (d->ctl_mute_id)
6595 snd_ctl_notify(alsa_card,
6596 SNDRV_CTL_EVENT_MASK_VALUE,
6597 d->ctl_mute_id);
6598 if (d->ctl_vol_id)
6599 snd_ctl_notify(alsa_card,
6600 SNDRV_CTL_EVENT_MASK_VALUE,
6601 d->ctl_vol_id);
6602 }
6603}
6604
6605static int volume_alsa_vol_info(struct snd_kcontrol *kcontrol,
6606 struct snd_ctl_elem_info *uinfo)
6607{
6608 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
6609 uinfo->count = 1;
6610 uinfo->value.integer.min = 0;
6611 uinfo->value.integer.max = TP_EC_VOLUME_MAX;
6612 return 0;
6613}
6614
6615static int volume_alsa_vol_get(struct snd_kcontrol *kcontrol,
6616 struct snd_ctl_elem_value *ucontrol)
6617{
6618 u8 s;
6619 int rc;
6620
6621 rc = volume_get_status(&s);
6622 if (rc < 0)
6623 return rc;
6624
6625 ucontrol->value.integer.value[0] = s & TP_EC_AUDIO_LVL_MSK;
6626 return 0;
6627}
6628
6629static int volume_alsa_vol_put(struct snd_kcontrol *kcontrol,
6630 struct snd_ctl_elem_value *ucontrol)
6631{
6632 return volume_set_volume(ucontrol->value.integer.value[0]);
6633}
6634
6635#define volume_alsa_mute_info snd_ctl_boolean_mono_info
6636
6637static int volume_alsa_mute_get(struct snd_kcontrol *kcontrol,
6638 struct snd_ctl_elem_value *ucontrol)
6639{
6640 u8 s;
6641 int rc;
6642
6643 rc = volume_get_status(&s);
6644 if (rc < 0)
6645 return rc;
6646
6647 ucontrol->value.integer.value[0] =
6648 (s & TP_EC_AUDIO_MUTESW_MSK) ? 0 : 1;
6649 return 0;
6650}
6651
6652static int volume_alsa_mute_put(struct snd_kcontrol *kcontrol,
6653 struct snd_ctl_elem_value *ucontrol)
6654{
6655 return volume_set_mute(!ucontrol->value.integer.value[0]);
6656}
6657
6658static struct snd_kcontrol_new volume_alsa_control_vol __devinitdata = {
6659 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6660 .name = "Console Playback Volume",
6661 .index = 0,
6662 .access = SNDRV_CTL_ELEM_ACCESS_READ,
6663 .info = volume_alsa_vol_info,
6664 .get = volume_alsa_vol_get,
6665};
6666
6667static struct snd_kcontrol_new volume_alsa_control_mute __devinitdata = {
6668 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6669 .name = "Console Playback Switch",
6670 .index = 0,
6671 .access = SNDRV_CTL_ELEM_ACCESS_READ,
6672 .info = volume_alsa_mute_info,
6673 .get = volume_alsa_mute_get,
6674};
6675
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006676static void volume_suspend(pm_message_t state)
6677{
6678 tpacpi_volume_checkpoint_nvram();
6679}
6680
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006681static void volume_resume(void)
6682{
6683 volume_alsa_notify_change();
6684}
6685
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006686static void volume_shutdown(void)
6687{
6688 tpacpi_volume_checkpoint_nvram();
6689}
6690
6691static void volume_exit(void)
6692{
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006693 if (alsa_card) {
6694 snd_card_free(alsa_card);
6695 alsa_card = NULL;
6696 }
6697
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006698 tpacpi_volume_checkpoint_nvram();
6699}
6700
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006701static int __init volume_create_alsa_mixer(void)
6702{
6703 struct snd_card *card;
6704 struct tpacpi_alsa_data *data;
6705 struct snd_kcontrol *ctl_vol;
6706 struct snd_kcontrol *ctl_mute;
6707 int rc;
6708
6709 rc = snd_card_create(alsa_index, alsa_id, THIS_MODULE,
6710 sizeof(struct tpacpi_alsa_data), &card);
6711 if (rc < 0)
6712 return rc;
6713 if (!card)
6714 return -ENOMEM;
6715
6716 BUG_ON(!card->private_data);
6717 data = card->private_data;
6718 data->card = card;
6719
6720 strlcpy(card->driver, TPACPI_ALSA_DRVNAME,
6721 sizeof(card->driver));
6722 strlcpy(card->shortname, TPACPI_ALSA_SHRTNAME,
6723 sizeof(card->shortname));
6724 snprintf(card->mixername, sizeof(card->mixername), "ThinkPad EC %s",
6725 (thinkpad_id.ec_version_str) ?
6726 thinkpad_id.ec_version_str : "(unknown)");
6727 snprintf(card->longname, sizeof(card->longname),
6728 "%s at EC reg 0x%02x, fw %s", card->shortname, TP_EC_AUDIO,
6729 (thinkpad_id.ec_version_str) ?
6730 thinkpad_id.ec_version_str : "unknown");
6731
6732 if (volume_control_allowed) {
6733 volume_alsa_control_vol.put = volume_alsa_vol_put;
6734 volume_alsa_control_vol.access =
6735 SNDRV_CTL_ELEM_ACCESS_READWRITE;
6736
6737 volume_alsa_control_mute.put = volume_alsa_mute_put;
6738 volume_alsa_control_mute.access =
6739 SNDRV_CTL_ELEM_ACCESS_READWRITE;
6740 }
6741
6742 if (!tp_features.mixer_no_level_control) {
6743 ctl_vol = snd_ctl_new1(&volume_alsa_control_vol, NULL);
6744 rc = snd_ctl_add(card, ctl_vol);
6745 if (rc < 0) {
6746 printk(TPACPI_ERR
6747 "Failed to create ALSA volume control\n");
6748 goto err_out;
6749 }
6750 data->ctl_vol_id = &ctl_vol->id;
6751 }
6752
6753 ctl_mute = snd_ctl_new1(&volume_alsa_control_mute, NULL);
6754 rc = snd_ctl_add(card, ctl_mute);
6755 if (rc < 0) {
6756 printk(TPACPI_ERR "Failed to create ALSA mute control\n");
6757 goto err_out;
6758 }
6759 data->ctl_mute_id = &ctl_mute->id;
6760
6761 snd_card_set_dev(card, &tpacpi_pdev->dev);
6762 rc = snd_card_register(card);
6763
6764err_out:
6765 if (rc < 0) {
6766 snd_card_free(card);
6767 card = NULL;
6768 }
6769
6770 alsa_card = card;
6771 return rc;
6772}
6773
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006774#define TPACPI_VOL_Q_MUTEONLY 0x0001 /* Mute-only control available */
6775#define TPACPI_VOL_Q_LEVEL 0x0002 /* Volume control available */
6776
6777static const struct tpacpi_quirk volume_quirk_table[] __initconst = {
6778 /* Whitelist volume level on all IBM by default */
6779 { .vendor = PCI_VENDOR_ID_IBM,
6780 .bios = TPACPI_MATCH_ANY,
6781 .ec = TPACPI_MATCH_ANY,
6782 .quirks = TPACPI_VOL_Q_LEVEL },
6783
6784 /* Lenovo models with volume control (needs confirmation) */
6785 TPACPI_QEC_LNV('7', 'C', TPACPI_VOL_Q_LEVEL), /* R60/i */
6786 TPACPI_QEC_LNV('7', 'E', TPACPI_VOL_Q_LEVEL), /* R60e/i */
6787 TPACPI_QEC_LNV('7', '9', TPACPI_VOL_Q_LEVEL), /* T60/p */
6788 TPACPI_QEC_LNV('7', 'B', TPACPI_VOL_Q_LEVEL), /* X60/s */
6789 TPACPI_QEC_LNV('7', 'J', TPACPI_VOL_Q_LEVEL), /* X60t */
6790 TPACPI_QEC_LNV('7', '7', TPACPI_VOL_Q_LEVEL), /* Z60 */
6791 TPACPI_QEC_LNV('7', 'F', TPACPI_VOL_Q_LEVEL), /* Z61 */
6792
6793 /* Whitelist mute-only on all Lenovo by default */
6794 { .vendor = PCI_VENDOR_ID_LENOVO,
6795 .bios = TPACPI_MATCH_ANY,
6796 .ec = TPACPI_MATCH_ANY,
6797 .quirks = TPACPI_VOL_Q_MUTEONLY }
6798};
6799
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006800static int __init volume_init(struct ibm_init_struct *iibm)
6801{
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006802 unsigned long quirks;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006803 int rc;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006804
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006805 vdbg_printk(TPACPI_DBG_INIT, "initializing volume subdriver\n");
6806
6807 mutex_init(&volume_mutex);
6808
6809 /*
6810 * Check for module parameter bogosity, note that we
6811 * init volume_mode to TPACPI_VOL_MODE_MAX in order to be
6812 * able to detect "unspecified"
6813 */
6814 if (volume_mode > TPACPI_VOL_MODE_MAX)
6815 return -EINVAL;
6816
6817 if (volume_mode == TPACPI_VOL_MODE_UCMS_STEP) {
6818 printk(TPACPI_ERR
6819 "UCMS step volume mode not implemented, "
6820 "please contact %s\n", TPACPI_MAIL);
6821 return 1;
6822 }
6823
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006824 if (volume_capabilities >= TPACPI_VOL_CAP_MAX)
6825 return -EINVAL;
6826
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006827 /*
6828 * The ALSA mixer is our primary interface.
6829 * When disabled, don't install the subdriver at all
6830 */
6831 if (!alsa_enable) {
6832 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6833 "ALSA mixer disabled by parameter, "
6834 "not loading volume subdriver...\n");
6835 return 1;
6836 }
6837
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006838 quirks = tpacpi_check_quirks(volume_quirk_table,
6839 ARRAY_SIZE(volume_quirk_table));
6840
6841 switch (volume_capabilities) {
6842 case TPACPI_VOL_CAP_AUTO:
6843 if (quirks & TPACPI_VOL_Q_MUTEONLY)
6844 tp_features.mixer_no_level_control = 1;
6845 else if (quirks & TPACPI_VOL_Q_LEVEL)
6846 tp_features.mixer_no_level_control = 0;
6847 else
6848 return 1; /* no mixer */
6849 break;
6850 case TPACPI_VOL_CAP_VOLMUTE:
6851 tp_features.mixer_no_level_control = 0;
6852 break;
6853 case TPACPI_VOL_CAP_MUTEONLY:
6854 tp_features.mixer_no_level_control = 1;
6855 break;
6856 default:
6857 return 1;
6858 }
6859
6860 if (volume_capabilities != TPACPI_VOL_CAP_AUTO)
6861 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6862 "using user-supplied volume_capabilities=%d\n",
6863 volume_capabilities);
6864
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006865 if (volume_mode == TPACPI_VOL_MODE_AUTO ||
6866 volume_mode == TPACPI_VOL_MODE_MAX) {
6867 volume_mode = TPACPI_VOL_MODE_ECNVRAM;
6868
6869 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6870 "driver auto-selected volume_mode=%d\n",
6871 volume_mode);
6872 } else {
6873 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6874 "using user-supplied volume_mode=%d\n",
6875 volume_mode);
6876 }
6877
6878 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006879 "mute is supported, volume control is %s\n",
6880 str_supported(!tp_features.mixer_no_level_control));
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006881
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006882 rc = volume_create_alsa_mixer();
6883 if (rc) {
6884 printk(TPACPI_ERR
6885 "Could not create the ALSA mixer interface\n");
6886 return rc;
6887 }
6888
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006889 printk(TPACPI_INFO
6890 "Console audio control enabled, mode: %s\n",
6891 (volume_control_allowed) ?
6892 "override (read/write)" :
6893 "monitor (read only)");
6894
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006895 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6896 "registering volume hotkeys as change notification\n");
6897 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6898 | TP_ACPI_HKEY_VOLUP_MASK
6899 | TP_ACPI_HKEY_VOLDWN_MASK
6900 | TP_ACPI_HKEY_MUTE_MASK);
6901
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006902 return 0;
6903}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006904
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006905static int volume_read(struct seq_file *m)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006906{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006907 u8 status;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006908
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006909 if (volume_get_status(&status) < 0) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006910 seq_printf(m, "level:\t\tunreadable\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006911 } else {
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006912 if (tp_features.mixer_no_level_control)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006913 seq_printf(m, "level:\t\tunsupported\n");
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006914 else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006915 seq_printf(m, "level:\t\t%d\n",
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006916 status & TP_EC_AUDIO_LVL_MSK);
6917
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006918 seq_printf(m, "mute:\t\t%s\n",
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006919 onoff(status, TP_EC_AUDIO_MUTESW));
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006920
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006921 if (volume_control_allowed) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006922 seq_printf(m, "commands:\tunmute, mute\n");
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006923 if (!tp_features.mixer_no_level_control) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006924 seq_printf(m,
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006925 "commands:\tup, down\n");
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006926 seq_printf(m,
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006927 "commands:\tlevel <level>"
6928 " (<level> is 0-%d)\n",
6929 TP_EC_VOLUME_MAX);
6930 }
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006931 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006932 }
6933
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006934 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006935}
6936
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006937static int volume_write(char *buf)
6938{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006939 u8 s;
6940 u8 new_level, new_mute;
6941 int l;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006942 char *cmd;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006943 int rc;
6944
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006945 /*
6946 * We do allow volume control at driver startup, so that the
6947 * user can set initial state through the volume=... parameter hack.
6948 */
6949 if (!volume_control_allowed && tpacpi_lifecycle != TPACPI_LIFE_INIT) {
6950 if (unlikely(!tp_warned.volume_ctrl_forbidden)) {
6951 tp_warned.volume_ctrl_forbidden = 1;
6952 printk(TPACPI_NOTICE
6953 "Console audio control in monitor mode, "
6954 "changes are not allowed.\n");
6955 printk(TPACPI_NOTICE
6956 "Use the volume_control=1 module parameter "
6957 "to enable volume control\n");
6958 }
6959 return -EPERM;
6960 }
6961
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006962 rc = volume_get_status(&s);
6963 if (rc < 0)
6964 return rc;
6965
6966 new_level = s & TP_EC_AUDIO_LVL_MSK;
6967 new_mute = s & TP_EC_AUDIO_MUTESW_MSK;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006968
6969 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006970 if (!tp_features.mixer_no_level_control) {
6971 if (strlencmp(cmd, "up") == 0) {
6972 if (new_mute)
6973 new_mute = 0;
6974 else if (new_level < TP_EC_VOLUME_MAX)
6975 new_level++;
6976 continue;
6977 } else if (strlencmp(cmd, "down") == 0) {
6978 if (new_mute)
6979 new_mute = 0;
6980 else if (new_level > 0)
6981 new_level--;
6982 continue;
6983 } else if (sscanf(cmd, "level %u", &l) == 1 &&
6984 l >= 0 && l <= TP_EC_VOLUME_MAX) {
6985 new_level = l;
6986 continue;
6987 }
6988 }
6989 if (strlencmp(cmd, "mute") == 0)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006990 new_mute = TP_EC_AUDIO_MUTESW_MSK;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006991 else if (strlencmp(cmd, "unmute") == 0)
6992 new_mute = 0;
6993 else
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006994 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006995 }
6996
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006997 if (tp_features.mixer_no_level_control) {
6998 tpacpi_disclose_usertask("procfs volume", "%smute\n",
6999 new_mute ? "" : "un");
7000 rc = volume_set_mute(!!new_mute);
7001 } else {
7002 tpacpi_disclose_usertask("procfs volume",
7003 "%smute and set level to %d\n",
7004 new_mute ? "" : "un", new_level);
7005 rc = volume_set_status(new_mute | new_level);
7006 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007007 volume_alsa_notify_change();
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007008
7009 return (rc == -EINTR) ? -ERESTARTSYS : rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007010}
7011
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007012static struct ibm_struct volume_driver_data = {
7013 .name = "volume",
7014 .read = volume_read,
7015 .write = volume_write,
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007016 .exit = volume_exit,
7017 .suspend = volume_suspend,
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007018 .resume = volume_resume,
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007019 .shutdown = volume_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007020};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007021
7022/*************************************************************************
7023 * Fan subdriver
7024 */
7025
7026/*
7027 * FAN ACCESS MODES
7028 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007029 * TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007030 * ACPI GFAN method: returns fan level
7031 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007032 * see TPACPI_FAN_WR_ACPI_SFAN
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007033 * EC 0x2f (HFSP) not available if GFAN exists
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007034 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007035 * TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007036 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
7037 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007038 * EC 0x2f (HFSP) might be available *for reading*, but do not use
7039 * it for writing.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007040 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007041 * TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007042 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
7043 * Supported on almost all ThinkPads
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007044 *
7045 * Fan speed changes of any sort (including those caused by the
7046 * disengaged mode) are usually done slowly by the firmware as the
7047 * maximum ammount of fan duty cycle change per second seems to be
7048 * limited.
7049 *
7050 * Reading is not available if GFAN exists.
7051 * Writing is not available if SFAN exists.
7052 *
7053 * Bits
7054 * 7 automatic mode engaged;
7055 * (default operation mode of the ThinkPad)
7056 * fan level is ignored in this mode.
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007057 * 6 full speed mode (takes precedence over bit 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007058 * not available on all thinkpads. May disable
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007059 * the tachometer while the fan controller ramps up
7060 * the speed (which can take up to a few *minutes*).
7061 * Speeds up fan to 100% duty-cycle, which is far above
7062 * the standard RPM levels. It is not impossible that
7063 * it could cause hardware damage.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007064 * 5-3 unused in some models. Extra bits for fan level
7065 * in others, but still useless as all values above
7066 * 7 map to the same speed as level 7 in these models.
7067 * 2-0 fan level (0..7 usually)
7068 * 0x00 = stop
7069 * 0x07 = max (set when temperatures critical)
7070 * Some ThinkPads may have other levels, see
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007071 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007072 *
7073 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
7074 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
7075 * does so, its initial value is meaningless (0x07).
7076 *
7077 * For firmware bugs, refer to:
7078 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7079 *
7080 * ----
7081 *
7082 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
7083 * Main fan tachometer reading (in RPM)
7084 *
7085 * This register is present on all ThinkPads with a new-style EC, and
7086 * it is known not to be present on the A21m/e, and T22, as there is
7087 * something else in offset 0x84 according to the ACPI DSDT. Other
7088 * ThinkPads from this same time period (and earlier) probably lack the
7089 * tachometer as well.
7090 *
Nick Andrew877d0312009-01-26 11:06:57 +01007091 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007092 * was never fixed by IBM to report the EC firmware version string
7093 * probably support the tachometer (like the early X models), so
7094 * detecting it is quite hard. We need more data to know for sure.
7095 *
7096 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
7097 * might result.
7098 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007099 * FIRMWARE BUG: may go stale while the EC is switching to full speed
7100 * mode.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007101 *
7102 * For firmware bugs, refer to:
7103 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7104 *
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007105 * ----
7106 *
7107 * ThinkPad EC register 0x31 bit 0 (only on select models)
7108 *
7109 * When bit 0 of EC register 0x31 is zero, the tachometer registers
7110 * show the speed of the main fan. When bit 0 of EC register 0x31
7111 * is one, the tachometer registers show the speed of the auxiliary
7112 * fan.
7113 *
7114 * Fan control seems to affect both fans, regardless of the state
7115 * of this bit.
7116 *
7117 * So far, only the firmware for the X60/X61 non-tablet versions
7118 * seem to support this (firmware TP-7M).
7119 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007120 * TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007121 * ThinkPad X31, X40, X41. Not available in the X60.
7122 *
7123 * FANS ACPI handle: takes three arguments: low speed, medium speed,
7124 * high speed. ACPI DSDT seems to map these three speeds to levels
7125 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
7126 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
7127 *
7128 * The speeds are stored on handles
7129 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
7130 *
7131 * There are three default speed sets, acessible as handles:
7132 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
7133 *
7134 * ACPI DSDT switches which set is in use depending on various
7135 * factors.
7136 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007137 * TPACPI_FAN_WR_TPEC is also available and should be used to
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007138 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
7139 * but the ACPI tables just mention level 7.
7140 */
7141
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007142enum { /* Fan control constants */
7143 fan_status_offset = 0x2f, /* EC register 0x2f */
7144 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
7145 * 0x84 must be read before 0x85 */
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007146 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
7147 bit 0 selects which fan is active */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007148
7149 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
7150 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
7151
7152 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
7153};
7154
7155enum fan_status_access_mode {
7156 TPACPI_FAN_NONE = 0, /* No fan status or control */
7157 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
7158 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
7159};
7160
7161enum fan_control_access_mode {
7162 TPACPI_FAN_WR_NONE = 0, /* No fan control */
7163 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
7164 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
7165 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
7166};
7167
7168enum fan_control_commands {
7169 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
7170 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
7171 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
7172 * and also watchdog cmd */
7173};
7174
7175static int fan_control_allowed;
7176
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007177static enum fan_status_access_mode fan_status_access_mode;
7178static enum fan_control_access_mode fan_control_access_mode;
7179static enum fan_control_commands fan_control_commands;
7180
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02007181static u8 fan_control_initial_status;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007182static u8 fan_control_desired_level;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007183static u8 fan_control_resume_level;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007184static int fan_watchdog_maxinterval;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007185
7186static struct mutex fan_mutex;
7187
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007188static void fan_watchdog_fire(struct work_struct *ignored);
Len Brown25c68a32006-12-08 04:43:41 -05007189static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007190
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007191TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
7192TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007193 "\\FSPD", /* 600e/x, 770e, 770x */
7194 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007195TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007196 "JFNS", /* 770x-JL */
7197 ); /* all others */
7198
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007199/*
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007200 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
7201 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
7202 * be in auto mode (0x80).
7203 *
7204 * This is corrected by any write to HFSP either by the driver, or
7205 * by the firmware.
7206 *
7207 * We assume 0x07 really means auto mode while this quirk is active,
7208 * as this is far more likely than the ThinkPad being in level 7,
7209 * which is only used by the firmware during thermal emergencies.
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007210 *
7211 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
7212 * TP-70 (T43, R52), which are known to be buggy.
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007213 */
7214
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007215static void fan_quirk1_setup(void)
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007216{
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007217 if (fan_control_initial_status == 0x07) {
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007218 printk(TPACPI_NOTICE
7219 "fan_init: initial fan status is unknown, "
7220 "assuming it is in auto mode\n");
7221 tp_features.fan_ctrl_status_undef = 1;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007222 }
7223}
7224
7225static void fan_quirk1_handle(u8 *fan_status)
7226{
7227 if (unlikely(tp_features.fan_ctrl_status_undef)) {
7228 if (*fan_status != fan_control_initial_status) {
7229 /* something changed the HFSP regisnter since
7230 * driver init time, so it is not undefined
7231 * anymore */
7232 tp_features.fan_ctrl_status_undef = 0;
7233 } else {
7234 /* Return most likely status. In fact, it
7235 * might be the only possible status */
7236 *fan_status = TP_EC_FAN_AUTO;
7237 }
7238 }
7239}
7240
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007241/* Select main fan on X60/X61, NOOP on others */
7242static bool fan_select_fan1(void)
7243{
7244 if (tp_features.second_fan) {
7245 u8 val;
7246
7247 if (ec_read(fan_select_offset, &val) < 0)
7248 return false;
7249 val &= 0xFEU;
7250 if (ec_write(fan_select_offset, val) < 0)
7251 return false;
7252 }
7253 return true;
7254}
7255
7256/* Select secondary fan on X60/X61 */
7257static bool fan_select_fan2(void)
7258{
7259 u8 val;
7260
7261 if (!tp_features.second_fan)
7262 return false;
7263
7264 if (ec_read(fan_select_offset, &val) < 0)
7265 return false;
7266 val |= 0x01U;
7267 if (ec_write(fan_select_offset, val) < 0)
7268 return false;
7269
7270 return true;
7271}
7272
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007273/*
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007274 * Call with fan_mutex held
7275 */
7276static void fan_update_desired_level(u8 status)
7277{
7278 if ((status &
7279 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7280 if (status > 7)
7281 fan_control_desired_level = 7;
7282 else
7283 fan_control_desired_level = status;
7284 }
7285}
7286
7287static int fan_get_status(u8 *status)
7288{
7289 u8 s;
7290
7291 /* TODO:
7292 * Add TPACPI_FAN_RD_ACPI_FANS ? */
7293
7294 switch (fan_status_access_mode) {
7295 case TPACPI_FAN_RD_ACPI_GFAN:
7296 /* 570, 600e/x, 770e, 770x */
7297
7298 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
7299 return -EIO;
7300
7301 if (likely(status))
7302 *status = s & 0x07;
7303
7304 break;
7305
7306 case TPACPI_FAN_RD_TPEC:
7307 /* all except 570, 600e/x, 770e, 770x */
7308 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
7309 return -EIO;
7310
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007311 if (likely(status)) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007312 *status = s;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007313 fan_quirk1_handle(status);
7314 }
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007315
7316 break;
7317
7318 default:
7319 return -ENXIO;
7320 }
7321
7322 return 0;
7323}
7324
7325static int fan_get_status_safe(u8 *status)
7326{
7327 int rc;
7328 u8 s;
7329
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007330 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007331 return -ERESTARTSYS;
7332 rc = fan_get_status(&s);
7333 if (!rc)
7334 fan_update_desired_level(s);
7335 mutex_unlock(&fan_mutex);
7336
7337 if (status)
7338 *status = s;
7339
7340 return rc;
7341}
7342
7343static int fan_get_speed(unsigned int *speed)
7344{
7345 u8 hi, lo;
7346
7347 switch (fan_status_access_mode) {
7348 case TPACPI_FAN_RD_TPEC:
7349 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007350 if (unlikely(!fan_select_fan1()))
7351 return -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007352 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
7353 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
7354 return -EIO;
7355
7356 if (likely(speed))
7357 *speed = (hi << 8) | lo;
7358
7359 break;
7360
7361 default:
7362 return -ENXIO;
7363 }
7364
7365 return 0;
7366}
7367
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007368static int fan2_get_speed(unsigned int *speed)
7369{
7370 u8 hi, lo;
7371 bool rc;
7372
7373 switch (fan_status_access_mode) {
7374 case TPACPI_FAN_RD_TPEC:
7375 /* all except 570, 600e/x, 770e, 770x */
7376 if (unlikely(!fan_select_fan2()))
7377 return -EIO;
7378 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
7379 !acpi_ec_read(fan_rpm_offset + 1, &hi);
7380 fan_select_fan1(); /* play it safe */
7381 if (rc)
7382 return -EIO;
7383
7384 if (likely(speed))
7385 *speed = (hi << 8) | lo;
7386
7387 break;
7388
7389 default:
7390 return -ENXIO;
7391 }
7392
7393 return 0;
7394}
7395
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007396static int fan_set_level(int level)
7397{
7398 if (!fan_control_allowed)
7399 return -EPERM;
7400
7401 switch (fan_control_access_mode) {
7402 case TPACPI_FAN_WR_ACPI_SFAN:
7403 if (level >= 0 && level <= 7) {
7404 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
7405 return -EIO;
7406 } else
7407 return -EINVAL;
7408 break;
7409
7410 case TPACPI_FAN_WR_ACPI_FANS:
7411 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007412 if (!(level & TP_EC_FAN_AUTO) &&
7413 !(level & TP_EC_FAN_FULLSPEED) &&
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007414 ((level < 0) || (level > 7)))
7415 return -EINVAL;
7416
7417 /* safety net should the EC not support AUTO
7418 * or FULLSPEED mode bits and just ignore them */
7419 if (level & TP_EC_FAN_FULLSPEED)
7420 level |= 7; /* safety min speed 7 */
Roel Kluin547266e2008-02-05 00:24:56 +01007421 else if (level & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007422 level |= 4; /* safety min speed 4 */
7423
7424 if (!acpi_ec_write(fan_status_offset, level))
7425 return -EIO;
7426 else
7427 tp_features.fan_ctrl_status_undef = 0;
7428 break;
7429
7430 default:
7431 return -ENXIO;
7432 }
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007433
7434 vdbg_printk(TPACPI_DBG_FAN,
7435 "fan control: set fan control register to 0x%02x\n", level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007436 return 0;
7437}
7438
7439static int fan_set_level_safe(int level)
7440{
7441 int rc;
7442
7443 if (!fan_control_allowed)
7444 return -EPERM;
7445
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007446 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007447 return -ERESTARTSYS;
7448
7449 if (level == TPACPI_FAN_LAST_LEVEL)
7450 level = fan_control_desired_level;
7451
7452 rc = fan_set_level(level);
7453 if (!rc)
7454 fan_update_desired_level(level);
7455
7456 mutex_unlock(&fan_mutex);
7457 return rc;
7458}
7459
7460static int fan_set_enable(void)
7461{
7462 u8 s;
7463 int rc;
7464
7465 if (!fan_control_allowed)
7466 return -EPERM;
7467
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007468 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007469 return -ERESTARTSYS;
7470
7471 switch (fan_control_access_mode) {
7472 case TPACPI_FAN_WR_ACPI_FANS:
7473 case TPACPI_FAN_WR_TPEC:
7474 rc = fan_get_status(&s);
7475 if (rc < 0)
7476 break;
7477
7478 /* Don't go out of emergency fan mode */
7479 if (s != 7) {
7480 s &= 0x07;
7481 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
7482 }
7483
7484 if (!acpi_ec_write(fan_status_offset, s))
7485 rc = -EIO;
7486 else {
7487 tp_features.fan_ctrl_status_undef = 0;
7488 rc = 0;
7489 }
7490 break;
7491
7492 case TPACPI_FAN_WR_ACPI_SFAN:
7493 rc = fan_get_status(&s);
7494 if (rc < 0)
7495 break;
7496
7497 s &= 0x07;
7498
7499 /* Set fan to at least level 4 */
7500 s |= 4;
7501
7502 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007503 rc = -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007504 else
7505 rc = 0;
7506 break;
7507
7508 default:
7509 rc = -ENXIO;
7510 }
7511
7512 mutex_unlock(&fan_mutex);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007513
7514 if (!rc)
7515 vdbg_printk(TPACPI_DBG_FAN,
7516 "fan control: set fan control register to 0x%02x\n",
7517 s);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007518 return rc;
7519}
7520
7521static int fan_set_disable(void)
7522{
7523 int rc;
7524
7525 if (!fan_control_allowed)
7526 return -EPERM;
7527
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007528 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007529 return -ERESTARTSYS;
7530
7531 rc = 0;
7532 switch (fan_control_access_mode) {
7533 case TPACPI_FAN_WR_ACPI_FANS:
7534 case TPACPI_FAN_WR_TPEC:
7535 if (!acpi_ec_write(fan_status_offset, 0x00))
7536 rc = -EIO;
7537 else {
7538 fan_control_desired_level = 0;
7539 tp_features.fan_ctrl_status_undef = 0;
7540 }
7541 break;
7542
7543 case TPACPI_FAN_WR_ACPI_SFAN:
7544 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
7545 rc = -EIO;
7546 else
7547 fan_control_desired_level = 0;
7548 break;
7549
7550 default:
7551 rc = -ENXIO;
7552 }
7553
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007554 if (!rc)
7555 vdbg_printk(TPACPI_DBG_FAN,
7556 "fan control: set fan control register to 0\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007557
7558 mutex_unlock(&fan_mutex);
7559 return rc;
7560}
7561
7562static int fan_set_speed(int speed)
7563{
7564 int rc;
7565
7566 if (!fan_control_allowed)
7567 return -EPERM;
7568
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007569 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007570 return -ERESTARTSYS;
7571
7572 rc = 0;
7573 switch (fan_control_access_mode) {
7574 case TPACPI_FAN_WR_ACPI_FANS:
7575 if (speed >= 0 && speed <= 65535) {
7576 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
7577 speed, speed, speed))
7578 rc = -EIO;
7579 } else
7580 rc = -EINVAL;
7581 break;
7582
7583 default:
7584 rc = -ENXIO;
7585 }
7586
7587 mutex_unlock(&fan_mutex);
7588 return rc;
7589}
7590
7591static void fan_watchdog_reset(void)
7592{
7593 static int fan_watchdog_active;
7594
7595 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
7596 return;
7597
7598 if (fan_watchdog_active)
7599 cancel_delayed_work(&fan_watchdog_task);
7600
7601 if (fan_watchdog_maxinterval > 0 &&
7602 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
7603 fan_watchdog_active = 1;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03007604 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007605 msecs_to_jiffies(fan_watchdog_maxinterval
7606 * 1000))) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007607 printk(TPACPI_ERR
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03007608 "failed to queue the fan watchdog, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007609 "watchdog will not trigger\n");
7610 }
7611 } else
7612 fan_watchdog_active = 0;
7613}
7614
7615static void fan_watchdog_fire(struct work_struct *ignored)
7616{
7617 int rc;
7618
7619 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
7620 return;
7621
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007622 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007623 rc = fan_set_enable();
7624 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007625 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007626 "will try again later...\n", -rc);
7627 /* reschedule for later */
7628 fan_watchdog_reset();
7629 }
7630}
7631
7632/*
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007633 * SYSFS fan layout: hwmon compatible (device)
7634 *
7635 * pwm*_enable:
7636 * 0: "disengaged" mode
7637 * 1: manual mode
7638 * 2: native EC "auto" mode (recommended, hardware default)
7639 *
7640 * pwm*: set speed in manual mode, ignored otherwise.
7641 * 0 is level 0; 255 is level 7. Intermediate points done with linear
7642 * interpolation.
7643 *
7644 * fan*_input: tachometer reading, RPM
7645 *
7646 *
7647 * SYSFS fan layout: extensions
7648 *
7649 * fan_watchdog (driver):
7650 * fan watchdog interval in seconds, 0 disables (default), max 120
7651 */
7652
7653/* sysfs fan pwm1_enable ----------------------------------------------- */
7654static ssize_t fan_pwm1_enable_show(struct device *dev,
7655 struct device_attribute *attr,
7656 char *buf)
7657{
7658 int res, mode;
7659 u8 status;
7660
7661 res = fan_get_status_safe(&status);
7662 if (res)
7663 return res;
7664
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007665 if (status & TP_EC_FAN_FULLSPEED) {
7666 mode = 0;
7667 } else if (status & TP_EC_FAN_AUTO) {
7668 mode = 2;
7669 } else
7670 mode = 1;
7671
7672 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
7673}
7674
7675static ssize_t fan_pwm1_enable_store(struct device *dev,
7676 struct device_attribute *attr,
7677 const char *buf, size_t count)
7678{
7679 unsigned long t;
7680 int res, level;
7681
7682 if (parse_strtoul(buf, 2, &t))
7683 return -EINVAL;
7684
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007685 tpacpi_disclose_usertask("hwmon pwm1_enable",
7686 "set fan mode to %lu\n", t);
7687
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007688 switch (t) {
7689 case 0:
7690 level = TP_EC_FAN_FULLSPEED;
7691 break;
7692 case 1:
7693 level = TPACPI_FAN_LAST_LEVEL;
7694 break;
7695 case 2:
7696 level = TP_EC_FAN_AUTO;
7697 break;
7698 case 3:
7699 /* reserved for software-controlled auto mode */
7700 return -ENOSYS;
7701 default:
7702 return -EINVAL;
7703 }
7704
7705 res = fan_set_level_safe(level);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03007706 if (res == -ENXIO)
7707 return -EINVAL;
7708 else if (res < 0)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007709 return res;
7710
7711 fan_watchdog_reset();
7712
7713 return count;
7714}
7715
7716static struct device_attribute dev_attr_fan_pwm1_enable =
7717 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
7718 fan_pwm1_enable_show, fan_pwm1_enable_store);
7719
7720/* sysfs fan pwm1 ------------------------------------------------------ */
7721static ssize_t fan_pwm1_show(struct device *dev,
7722 struct device_attribute *attr,
7723 char *buf)
7724{
7725 int res;
7726 u8 status;
7727
7728 res = fan_get_status_safe(&status);
7729 if (res)
7730 return res;
7731
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007732 if ((status &
7733 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
7734 status = fan_control_desired_level;
7735
7736 if (status > 7)
7737 status = 7;
7738
7739 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
7740}
7741
7742static ssize_t fan_pwm1_store(struct device *dev,
7743 struct device_attribute *attr,
7744 const char *buf, size_t count)
7745{
7746 unsigned long s;
7747 int rc;
7748 u8 status, newlevel;
7749
7750 if (parse_strtoul(buf, 255, &s))
7751 return -EINVAL;
7752
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007753 tpacpi_disclose_usertask("hwmon pwm1",
7754 "set fan speed to %lu\n", s);
7755
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007756 /* scale down from 0-255 to 0-7 */
7757 newlevel = (s >> 5) & 0x07;
7758
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007759 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02007760 return -ERESTARTSYS;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007761
7762 rc = fan_get_status(&status);
7763 if (!rc && (status &
7764 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7765 rc = fan_set_level(newlevel);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03007766 if (rc == -ENXIO)
7767 rc = -EINVAL;
7768 else if (!rc) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007769 fan_update_desired_level(newlevel);
Henrique de Moraes Holschuhca4ac2f2007-04-27 22:00:11 -03007770 fan_watchdog_reset();
7771 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007772 }
7773
7774 mutex_unlock(&fan_mutex);
7775 return (rc)? rc : count;
7776}
7777
7778static struct device_attribute dev_attr_fan_pwm1 =
7779 __ATTR(pwm1, S_IWUSR | S_IRUGO,
7780 fan_pwm1_show, fan_pwm1_store);
7781
7782/* sysfs fan fan1_input ------------------------------------------------ */
7783static ssize_t fan_fan1_input_show(struct device *dev,
7784 struct device_attribute *attr,
7785 char *buf)
7786{
7787 int res;
7788 unsigned int speed;
7789
7790 res = fan_get_speed(&speed);
7791 if (res < 0)
7792 return res;
7793
7794 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7795}
7796
7797static struct device_attribute dev_attr_fan_fan1_input =
7798 __ATTR(fan1_input, S_IRUGO,
7799 fan_fan1_input_show, NULL);
7800
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007801/* sysfs fan fan2_input ------------------------------------------------ */
7802static ssize_t fan_fan2_input_show(struct device *dev,
7803 struct device_attribute *attr,
7804 char *buf)
7805{
7806 int res;
7807 unsigned int speed;
7808
7809 res = fan2_get_speed(&speed);
7810 if (res < 0)
7811 return res;
7812
7813 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7814}
7815
7816static struct device_attribute dev_attr_fan_fan2_input =
7817 __ATTR(fan2_input, S_IRUGO,
7818 fan_fan2_input_show, NULL);
7819
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007820/* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007821static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
7822 char *buf)
7823{
7824 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
7825}
7826
7827static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
7828 const char *buf, size_t count)
7829{
7830 unsigned long t;
7831
7832 if (parse_strtoul(buf, 120, &t))
7833 return -EINVAL;
7834
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03007835 if (!fan_control_allowed)
7836 return -EPERM;
7837
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007838 fan_watchdog_maxinterval = t;
7839 fan_watchdog_reset();
7840
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007841 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
7842
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007843 return count;
7844}
7845
7846static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
7847 fan_fan_watchdog_show, fan_fan_watchdog_store);
7848
7849/* --------------------------------------------------------------------- */
7850static struct attribute *fan_attributes[] = {
7851 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
7852 &dev_attr_fan_fan1_input.attr,
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007853 NULL, /* for fan2_input */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007854 NULL
7855};
7856
7857static const struct attribute_group fan_attr_group = {
7858 .attrs = fan_attributes,
7859};
7860
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007861#define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
7862#define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007863
7864#define TPACPI_FAN_QI(__id1, __id2, __quirks) \
7865 { .vendor = PCI_VENDOR_ID_IBM, \
7866 .bios = TPACPI_MATCH_ANY, \
7867 .ec = TPID(__id1, __id2), \
7868 .quirks = __quirks }
7869
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007870#define TPACPI_FAN_QL(__id1, __id2, __quirks) \
7871 { .vendor = PCI_VENDOR_ID_LENOVO, \
7872 .bios = TPACPI_MATCH_ANY, \
7873 .ec = TPID(__id1, __id2), \
7874 .quirks = __quirks }
7875
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007876static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
7877 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
7878 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
7879 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
7880 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007881 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007882};
7883
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007884#undef TPACPI_FAN_QL
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007885#undef TPACPI_FAN_QI
7886
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007887static int __init fan_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007888{
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007889 int rc;
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007890 unsigned long quirks;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007891
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007892 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7893 "initializing fan subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007894
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03007895 mutex_init(&fan_mutex);
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007896 fan_status_access_mode = TPACPI_FAN_NONE;
7897 fan_control_access_mode = TPACPI_FAN_WR_NONE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007898 fan_control_commands = 0;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007899 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03007900 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007901 tp_features.second_fan = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007902 fan_control_desired_level = 7;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007903
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007904 TPACPI_ACPIHANDLE_INIT(fans);
7905 TPACPI_ACPIHANDLE_INIT(gfan);
7906 TPACPI_ACPIHANDLE_INIT(sfan);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007907
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007908 quirks = tpacpi_check_quirks(fan_quirk_table,
7909 ARRAY_SIZE(fan_quirk_table));
7910
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007911 if (gfan_handle) {
7912 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007913 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007914 } else {
7915 /* all other ThinkPads: note that even old-style
7916 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02007917 if (likely(acpi_ec_read(fan_status_offset,
7918 &fan_control_initial_status))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007919 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007920 if (quirks & TPACPI_FAN_Q1)
7921 fan_quirk1_setup();
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007922 if (quirks & TPACPI_FAN_2FAN) {
7923 tp_features.second_fan = 1;
7924 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7925 "secondary fan support enabled\n");
7926 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007927 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007928 printk(TPACPI_ERR
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007929 "ThinkPad ACPI EC access misbehaving, "
7930 "fan status and control unavailable\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007931 return 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007932 }
7933 }
7934
7935 if (sfan_handle) {
7936 /* 570, 770x-JL */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007937 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02007938 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007939 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007940 } else {
7941 if (!gfan_handle) {
7942 /* gfan without sfan means no fan control */
7943 /* all other models implement TP EC 0x2f control */
7944
7945 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02007946 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007947 fan_control_access_mode =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007948 TPACPI_FAN_WR_ACPI_FANS;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007949 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007950 TPACPI_FAN_CMD_SPEED |
7951 TPACPI_FAN_CMD_LEVEL |
7952 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007953 } else {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007954 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02007955 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007956 TPACPI_FAN_CMD_LEVEL |
7957 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007958 }
7959 }
7960 }
7961
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007962 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7963 "fan is %s, modes %d, %d\n",
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007964 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
7965 fan_control_access_mode != TPACPI_FAN_WR_NONE),
7966 fan_status_access_mode, fan_control_access_mode);
7967
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03007968 /* fan control master switch */
7969 if (!fan_control_allowed) {
7970 fan_control_access_mode = TPACPI_FAN_WR_NONE;
7971 fan_control_commands = 0;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007972 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03007973 "fan control features disabled by parameter\n");
7974 }
7975
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007976 /* update fan_control_desired_level */
7977 if (fan_status_access_mode != TPACPI_FAN_NONE)
7978 fan_get_status_safe(NULL);
7979
7980 if (fan_status_access_mode != TPACPI_FAN_NONE ||
7981 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007982 if (tp_features.second_fan) {
7983 /* attach second fan tachometer */
7984 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
7985 &dev_attr_fan_fan2_input.attr;
7986 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007987 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007988 &fan_attr_group);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007989 if (rc < 0)
7990 return rc;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03007991
7992 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
7993 &driver_attr_fan_watchdog);
7994 if (rc < 0) {
7995 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
7996 &fan_attr_group);
7997 return rc;
7998 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007999 return 0;
8000 } else
8001 return 1;
8002}
8003
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008004static void fan_exit(void)
8005{
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008006 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008007 "cancelling any pending fan watchdog tasks\n");
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008008
8009 /* FIXME: can we really do this unconditionally? */
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008010 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008011 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
8012 &driver_attr_fan_watchdog);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008013
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008014 cancel_delayed_work(&fan_watchdog_task);
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03008015 flush_workqueue(tpacpi_wq);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008016}
8017
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008018static void fan_suspend(pm_message_t state)
8019{
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008020 int rc;
8021
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008022 if (!fan_control_allowed)
8023 return;
8024
8025 /* Store fan status in cache */
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008026 fan_control_resume_level = 0;
8027 rc = fan_get_status_safe(&fan_control_resume_level);
8028 if (rc < 0)
8029 printk(TPACPI_NOTICE
8030 "failed to read fan level for later "
8031 "restore during resume: %d\n", rc);
8032
8033 /* if it is undefined, don't attempt to restore it.
8034 * KEEP THIS LAST */
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008035 if (tp_features.fan_ctrl_status_undef)
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008036 fan_control_resume_level = 0;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008037}
8038
8039static void fan_resume(void)
8040{
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008041 u8 current_level = 7;
8042 bool do_set = false;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008043 int rc;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008044
8045 /* DSDT *always* updates status on resume */
8046 tp_features.fan_ctrl_status_undef = 0;
8047
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008048 if (!fan_control_allowed ||
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008049 !fan_control_resume_level ||
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008050 (fan_get_status_safe(&current_level) < 0))
8051 return;
8052
8053 switch (fan_control_access_mode) {
8054 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008055 /* never decrease fan level */
8056 do_set = (fan_control_resume_level > current_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008057 break;
8058 case TPACPI_FAN_WR_ACPI_FANS:
8059 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008060 /* never decrease fan level, scale is:
8061 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
8062 *
8063 * We expect the firmware to set either 7 or AUTO, but we
8064 * handle FULLSPEED out of paranoia.
8065 *
8066 * So, we can safely only restore FULLSPEED or 7, anything
8067 * else could slow the fan. Restoring AUTO is useless, at
8068 * best that's exactly what the DSDT already set (it is the
8069 * slower it uses).
8070 *
8071 * Always keep in mind that the DSDT *will* have set the
8072 * fans to what the vendor supposes is the best level. We
8073 * muck with it only to speed the fan up.
8074 */
8075 if (fan_control_resume_level != 7 &&
8076 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
8077 return;
8078 else
8079 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
8080 (current_level != fan_control_resume_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008081 break;
8082 default:
8083 return;
8084 }
8085 if (do_set) {
8086 printk(TPACPI_NOTICE
8087 "restoring fan level to 0x%02x\n",
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008088 fan_control_resume_level);
8089 rc = fan_set_level_safe(fan_control_resume_level);
8090 if (rc < 0)
8091 printk(TPACPI_NOTICE
8092 "failed to restore fan level: %d\n", rc);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008093 }
8094}
8095
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008096static int fan_read(struct seq_file *m)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008097{
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008098 int rc;
8099 u8 status;
8100 unsigned int speed = 0;
8101
8102 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008103 case TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008104 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008105 rc = fan_get_status_safe(&status);
8106 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008107 return rc;
8108
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008109 seq_printf(m, "status:\t\t%s\n"
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008110 "level:\t\t%d\n",
8111 (status != 0) ? "enabled" : "disabled", status);
8112 break;
8113
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008114 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008115 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008116 rc = fan_get_status_safe(&status);
8117 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008118 return rc;
8119
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008120 seq_printf(m, "status:\t\t%s\n",
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008121 (status != 0) ? "enabled" : "disabled");
8122
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008123 rc = fan_get_speed(&speed);
8124 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008125 return rc;
8126
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008127 seq_printf(m, "speed:\t\t%d\n", speed);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008128
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008129 if (status & TP_EC_FAN_FULLSPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008130 /* Disengaged mode takes precedence */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008131 seq_printf(m, "level:\t\tdisengaged\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008132 else if (status & TP_EC_FAN_AUTO)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008133 seq_printf(m, "level:\t\tauto\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008134 else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008135 seq_printf(m, "level:\t\t%d\n", status);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008136 break;
8137
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008138 case TPACPI_FAN_NONE:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008139 default:
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008140 seq_printf(m, "status:\t\tnot supported\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008141 }
8142
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008143 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008144 seq_printf(m, "commands:\tlevel <level>");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008145
8146 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008147 case TPACPI_FAN_WR_ACPI_SFAN:
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008148 seq_printf(m, " (<level> is 0-7)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008149 break;
8150
8151 default:
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008152 seq_printf(m, " (<level> is 0-7, "
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008153 "auto, disengaged, full-speed)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008154 break;
8155 }
8156 }
8157
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008158 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008159 seq_printf(m, "commands:\tenable, disable\n"
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008160 "commands:\twatchdog <timeout> (<timeout> "
8161 "is 0 (off), 1-120 (seconds))\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008162
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008163 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008164 seq_printf(m, "commands:\tspeed <speed>"
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008165 " (<speed> is 0-65535)\n");
8166
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008167 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008168}
8169
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008170static int fan_write_cmd_level(const char *cmd, int *rc)
8171{
8172 int level;
8173
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02008174 if (strlencmp(cmd, "level auto") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008175 level = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008176 else if ((strlencmp(cmd, "level disengaged") == 0) |
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008177 (strlencmp(cmd, "level full-speed") == 0))
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008178 level = TP_EC_FAN_FULLSPEED;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02008179 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008180 return 0;
8181
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008182 *rc = fan_set_level_safe(level);
8183 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008184 printk(TPACPI_ERR "level command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008185 "access mode %d", fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008186 else if (!*rc)
8187 tpacpi_disclose_usertask("procfs fan",
8188 "set level to %d\n", level);
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008189
8190 return 1;
8191}
8192
8193static int fan_write_cmd_enable(const char *cmd, int *rc)
8194{
8195 if (strlencmp(cmd, "enable") != 0)
8196 return 0;
8197
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008198 *rc = fan_set_enable();
8199 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008200 printk(TPACPI_ERR "enable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008201 "access mode %d", fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008202 else if (!*rc)
8203 tpacpi_disclose_usertask("procfs fan", "enable\n");
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008204
8205 return 1;
8206}
8207
8208static int fan_write_cmd_disable(const char *cmd, int *rc)
8209{
8210 if (strlencmp(cmd, "disable") != 0)
8211 return 0;
8212
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008213 *rc = fan_set_disable();
8214 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008215 printk(TPACPI_ERR "disable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008216 "access mode %d", fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008217 else if (!*rc)
8218 tpacpi_disclose_usertask("procfs fan", "disable\n");
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008219
8220 return 1;
8221}
8222
8223static int fan_write_cmd_speed(const char *cmd, int *rc)
8224{
8225 int speed;
8226
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02008227 /* TODO:
8228 * Support speed <low> <medium> <high> ? */
8229
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008230 if (sscanf(cmd, "speed %d", &speed) != 1)
8231 return 0;
8232
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008233 *rc = fan_set_speed(speed);
8234 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008235 printk(TPACPI_ERR "speed command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008236 "access mode %d", fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008237 else if (!*rc)
8238 tpacpi_disclose_usertask("procfs fan",
8239 "set speed to %d\n", speed);
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008240
8241 return 1;
8242}
8243
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008244static int fan_write_cmd_watchdog(const char *cmd, int *rc)
8245{
8246 int interval;
8247
8248 if (sscanf(cmd, "watchdog %d", &interval) != 1)
8249 return 0;
8250
8251 if (interval < 0 || interval > 120)
8252 *rc = -EINVAL;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008253 else {
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008254 fan_watchdog_maxinterval = interval;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008255 tpacpi_disclose_usertask("procfs fan",
8256 "set watchdog timer to %d\n",
8257 interval);
8258 }
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008259
8260 return 1;
8261}
8262
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008263static int fan_write(char *buf)
8264{
8265 char *cmd;
8266 int rc = 0;
8267
8268 while (!rc && (cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008269 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008270 fan_write_cmd_level(cmd, &rc)) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008271 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008272 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008273 fan_write_cmd_disable(cmd, &rc) ||
8274 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008275 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008276 fan_write_cmd_speed(cmd, &rc))
8277 )
8278 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008279 else if (!rc)
8280 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008281 }
8282
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008283 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008284}
8285
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008286static struct ibm_struct fan_driver_data = {
8287 .name = "fan",
8288 .read = fan_read,
8289 .write = fan_write,
8290 .exit = fan_exit,
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008291 .suspend = fan_suspend,
8292 .resume = fan_resume,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008293};
8294
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008295/****************************************************************************
8296 ****************************************************************************
8297 *
8298 * Infrastructure
8299 *
8300 ****************************************************************************
8301 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07008302
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03008303/*
8304 * HKEY event callout for other subdrivers go here
8305 * (yes, it is ugly, but it is quick, safe, and gets the job done
8306 */
8307static void tpacpi_driver_event(const unsigned int hkey_event)
8308{
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00008309 if (ibm_backlight_device) {
8310 switch (hkey_event) {
8311 case TP_HKEY_EV_BRGHT_UP:
8312 case TP_HKEY_EV_BRGHT_DOWN:
8313 tpacpi_brightness_notify_change();
8314 }
8315 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02008316 if (alsa_card) {
8317 switch (hkey_event) {
8318 case TP_HKEY_EV_VOL_UP:
8319 case TP_HKEY_EV_VOL_DOWN:
8320 case TP_HKEY_EV_VOL_MUTE:
8321 volume_alsa_notify_change();
8322 }
8323 }
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03008324}
8325
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03008326static void hotkey_driver_event(const unsigned int scancode)
8327{
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03008328 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode);
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03008329}
8330
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008331/* sysfs name ---------------------------------------------------------- */
8332static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
8333 struct device_attribute *attr,
8334 char *buf)
8335{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008336 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008337}
8338
8339static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
8340 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
8341
8342/* --------------------------------------------------------------------- */
8343
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008344/* /proc support */
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -03008345static struct proc_dir_entry *proc_dir;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008346
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008347/*
8348 * Module and infrastructure proble, init and exit handling
8349 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008350
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008351static int force_load;
8352
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008353#ifdef CONFIG_THINKPAD_ACPI_DEBUG
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008354static const char * __init str_supported(int is_supported)
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008355{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008356 static char text_unsupported[] __initdata = "not supported";
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008357
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008358 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008359}
8360#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
8361
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008362static void ibm_exit(struct ibm_struct *ibm)
8363{
8364 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
8365
8366 list_del_init(&ibm->all_drivers);
8367
8368 if (ibm->flags.acpi_notify_installed) {
8369 dbg_printk(TPACPI_DBG_EXIT,
8370 "%s: acpi_remove_notify_handler\n", ibm->name);
8371 BUG_ON(!ibm->acpi);
8372 acpi_remove_notify_handler(*ibm->acpi->handle,
8373 ibm->acpi->type,
8374 dispatch_acpi_notify);
8375 ibm->flags.acpi_notify_installed = 0;
8376 ibm->flags.acpi_notify_installed = 0;
8377 }
8378
8379 if (ibm->flags.proc_created) {
8380 dbg_printk(TPACPI_DBG_EXIT,
8381 "%s: remove_proc_entry\n", ibm->name);
8382 remove_proc_entry(ibm->name, proc_dir);
8383 ibm->flags.proc_created = 0;
8384 }
8385
8386 if (ibm->flags.acpi_driver_registered) {
8387 dbg_printk(TPACPI_DBG_EXIT,
8388 "%s: acpi_bus_unregister_driver\n", ibm->name);
8389 BUG_ON(!ibm->acpi);
8390 acpi_bus_unregister_driver(ibm->acpi->driver);
8391 kfree(ibm->acpi->driver);
8392 ibm->acpi->driver = NULL;
8393 ibm->flags.acpi_driver_registered = 0;
8394 }
8395
8396 if (ibm->flags.init_called && ibm->exit) {
8397 ibm->exit();
8398 ibm->flags.init_called = 0;
8399 }
8400
8401 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
8402}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02008403
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008404static int __init ibm_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008405{
8406 int ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008407 struct ibm_struct *ibm = iibm->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008408 struct proc_dir_entry *entry;
8409
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008410 BUG_ON(ibm == NULL);
8411
8412 INIT_LIST_HEAD(&ibm->all_drivers);
8413
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03008414 if (ibm->flags.experimental && !experimental)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008415 return 0;
8416
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008417 dbg_printk(TPACPI_DBG_INIT,
8418 "probing for %s\n", ibm->name);
8419
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008420 if (iibm->init) {
8421 ret = iibm->init(iibm);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008422 if (ret > 0)
8423 return 0; /* probe failed */
8424 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008425 return ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008426
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03008427 ibm->flags.init_called = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008428 }
8429
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03008430 if (ibm->acpi) {
8431 if (ibm->acpi->hid) {
8432 ret = register_tpacpi_subdriver(ibm);
8433 if (ret)
8434 goto err_out;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008435 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03008436
8437 if (ibm->acpi->notify) {
8438 ret = setup_acpi_notify(ibm);
8439 if (ret == -ENODEV) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008440 printk(TPACPI_NOTICE "disabling subdriver %s\n",
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03008441 ibm->name);
8442 ret = 0;
8443 goto err_out;
8444 }
8445 if (ret < 0)
8446 goto err_out;
8447 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008448 }
8449
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008450 dbg_printk(TPACPI_DBG_INIT,
8451 "%s installed\n", ibm->name);
8452
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008453 if (ibm->read) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008454 mode_t mode;
8455
8456 mode = S_IRUGO;
8457 if (ibm->write)
8458 mode |= S_IWUSR;
8459 entry = proc_create_data(ibm->name, mode, proc_dir,
8460 &dispatch_proc_fops, ibm);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008461 if (!entry) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008462 printk(TPACPI_ERR "unable to create proc entry %s\n",
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008463 ibm->name);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008464 ret = -ENODEV;
8465 goto err_out;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008466 }
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03008467 ibm->flags.proc_created = 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008468 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008469
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008470 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
8471
Linus Torvalds1da177e2005-04-16 15:20:36 -07008472 return 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008473
8474err_out:
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008475 dbg_printk(TPACPI_DBG_INIT,
8476 "%s: at error exit path with result %d\n",
8477 ibm->name, ret);
8478
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008479 ibm_exit(ibm);
8480 return (ret < 0)? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008481}
8482
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008483/* Probing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008484
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008485static bool __pure __init tpacpi_is_fw_digit(const char c)
8486{
8487 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
8488}
8489
8490/* Most models: xxyTkkWW (#.##c); Ancient 570/600 and -SL lacks (#.##c) */
8491static bool __pure __init tpacpi_is_valid_fw_id(const char* const s,
8492 const char t)
8493{
8494 return s && strlen(s) >= 8 &&
8495 tpacpi_is_fw_digit(s[0]) &&
8496 tpacpi_is_fw_digit(s[1]) &&
8497 s[2] == t && s[3] == 'T' &&
8498 tpacpi_is_fw_digit(s[4]) &&
8499 tpacpi_is_fw_digit(s[5]) &&
8500 s[6] == 'W' && s[7] == 'W';
8501}
8502
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008503/* returns 0 - probe ok, or < 0 - probe error.
8504 * Probe ok doesn't mean thinkpad found.
8505 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
8506static int __must_check __init get_thinkpad_model_data(
8507 struct thinkpad_id_data *tp)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02008508{
Jeff Garzik18552562007-10-03 15:15:40 -04008509 const struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02008510 char ec_fw_string[18];
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008511 char const *s;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02008512
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008513 if (!tp)
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008514 return -EINVAL;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008515
8516 memset(tp, 0, sizeof(*tp));
8517
8518 if (dmi_name_in_vendors("IBM"))
8519 tp->vendor = PCI_VENDOR_ID_IBM;
8520 else if (dmi_name_in_vendors("LENOVO"))
8521 tp->vendor = PCI_VENDOR_ID_LENOVO;
8522 else
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008523 return 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008524
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008525 s = dmi_get_system_info(DMI_BIOS_VERSION);
8526 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
8527 if (s && !tp->bios_version_str)
8528 return -ENOMEM;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008529
8530 /* Really ancient ThinkPad 240X will fail this, which is fine */
8531 if (!tpacpi_is_valid_fw_id(tp->bios_version_str, 'E'))
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008532 return 0;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008533
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008534 tp->bios_model = tp->bios_version_str[0]
8535 | (tp->bios_version_str[1] << 8);
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008536 tp->bios_release = (tp->bios_version_str[4] << 8)
8537 | tp->bios_version_str[5];
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008538
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02008539 /*
8540 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
8541 * X32 or newer, all Z series; Some models must have an
8542 * up-to-date BIOS or they will not be detected.
8543 *
8544 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
8545 */
8546 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02008547 if (sscanf(dev->name,
8548 "IBM ThinkPad Embedded Controller -[%17c",
8549 ec_fw_string) == 1) {
8550 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
8551 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008552
8553 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008554 if (!tp->ec_version_str)
8555 return -ENOMEM;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008556
8557 if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) {
8558 tp->ec_model = ec_fw_string[0]
8559 | (ec_fw_string[1] << 8);
8560 tp->ec_release = (ec_fw_string[4] << 8)
8561 | ec_fw_string[5];
8562 } else {
8563 printk(TPACPI_NOTICE
8564 "ThinkPad firmware release %s "
8565 "doesn't match the known patterns\n",
8566 ec_fw_string);
8567 printk(TPACPI_NOTICE
8568 "please report this to %s\n",
8569 TPACPI_MAIL);
8570 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008571 break;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02008572 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02008573 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008574
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008575 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
8576 if (s && !strnicmp(s, "ThinkPad", 8)) {
8577 tp->model_str = kstrdup(s, GFP_KERNEL);
8578 if (!tp->model_str)
8579 return -ENOMEM;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008580 }
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03008581
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008582 s = dmi_get_system_info(DMI_PRODUCT_NAME);
8583 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
8584 if (s && !tp->nummodel_str)
8585 return -ENOMEM;
8586
8587 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008588}
8589
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008590static int __init probe_for_thinkpad(void)
8591{
8592 int is_thinkpad;
8593
8594 if (acpi_disabled)
8595 return -ENODEV;
8596
8597 /*
8598 * Non-ancient models have better DMI tagging, but very old models
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03008599 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008600 */
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03008601 is_thinkpad = (thinkpad_id.model_str != NULL) ||
8602 (thinkpad_id.ec_model != 0) ||
8603 tpacpi_is_fw_known();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008604
8605 /* ec is required because many other handles are relative to it */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008606 TPACPI_ACPIHANDLE_INIT(ec);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008607 if (!ec_handle) {
8608 if (is_thinkpad)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008609 printk(TPACPI_ERR
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008610 "Not yet supported ThinkPad detected!\n");
8611 return -ENODEV;
8612 }
8613
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03008614 if (!is_thinkpad && !force_load)
8615 return -ENODEV;
8616
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008617 return 0;
8618}
8619
8620
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008621/* Module init, exit, parameters */
8622
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008623static struct ibm_init_struct ibms_init[] __initdata = {
8624 {
8625 .init = thinkpad_acpi_driver_init,
8626 .data = &thinkpad_acpi_driver_data,
8627 },
8628 {
8629 .init = hotkey_init,
8630 .data = &hotkey_driver_data,
8631 },
8632 {
8633 .init = bluetooth_init,
8634 .data = &bluetooth_driver_data,
8635 },
8636 {
8637 .init = wan_init,
8638 .data = &wan_driver_data,
8639 },
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02008640 {
8641 .init = uwb_init,
8642 .data = &uwb_driver_data,
8643 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02008644#ifdef CONFIG_THINKPAD_ACPI_VIDEO
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008645 {
8646 .init = video_init,
8647 .data = &video_driver_data,
8648 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02008649#endif
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008650 {
8651 .init = light_init,
8652 .data = &light_driver_data,
8653 },
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008654 {
8655 .init = cmos_init,
8656 .data = &cmos_driver_data,
8657 },
8658 {
8659 .init = led_init,
8660 .data = &led_driver_data,
8661 },
8662 {
8663 .init = beep_init,
8664 .data = &beep_driver_data,
8665 },
8666 {
8667 .init = thermal_init,
8668 .data = &thermal_driver_data,
8669 },
8670 {
8671 .data = &ecdump_driver_data,
8672 },
8673 {
8674 .init = brightness_init,
8675 .data = &brightness_driver_data,
8676 },
8677 {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02008678 .init = volume_init,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008679 .data = &volume_driver_data,
8680 },
8681 {
8682 .init = fan_init,
8683 .data = &fan_driver_data,
8684 },
8685};
8686
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008687static int __init set_ibm_param(const char *val, struct kernel_param *kp)
8688{
8689 unsigned int i;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008690 struct ibm_struct *ibm;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008691
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02008692 if (!kp || !kp->name || !val)
8693 return -EINVAL;
8694
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008695 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
8696 ibm = ibms_init[i].data;
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02008697 WARN_ON(ibm == NULL);
8698
8699 if (!ibm || !ibm->name)
8700 continue;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008701
8702 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
8703 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008704 return -ENOSPC;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008705 strcpy(ibms_init[i].param, val);
8706 strcat(ibms_init[i].param, ",");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008707 return 0;
8708 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008709 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008710
8711 return -EINVAL;
8712}
8713
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008714module_param(experimental, int, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008715MODULE_PARM_DESC(experimental,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008716 "Enables experimental features when non-zero");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008717
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03008718module_param_named(debug, dbg_level, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008719MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03008720
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008721module_param(force_load, bool, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008722MODULE_PARM_DESC(force_load,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008723 "Attempts to load the driver even on a "
8724 "mis-identified ThinkPad when true");
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03008725
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008726module_param_named(fan_control, fan_control_allowed, bool, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008727MODULE_PARM_DESC(fan_control,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008728 "Enables setting fan parameters features when true");
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008729
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008730module_param_named(brightness_mode, brightness_mode, uint, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008731MODULE_PARM_DESC(brightness_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008732 "Selects brightness control strategy: "
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00008733 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03008734
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008735module_param(brightness_enable, uint, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008736MODULE_PARM_DESC(brightness_enable,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008737 "Enables backlight control when 1, disables when 0");
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02008738
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008739module_param(hotkey_report_mode, uint, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008740MODULE_PARM_DESC(hotkey_report_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008741 "used for backwards compatibility with userspace, "
8742 "see documentation");
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03008743
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02008744module_param_named(volume_mode, volume_mode, uint, 0444);
8745MODULE_PARM_DESC(volume_mode,
8746 "Selects volume control strategy: "
8747 "0=auto, 1=EC, 2=N/A, 3=EC+NVRAM");
8748
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02008749module_param_named(volume_capabilities, volume_capabilities, uint, 0444);
8750MODULE_PARM_DESC(volume_capabilities,
8751 "Selects the mixer capabilites: "
8752 "0=auto, 1=volume and mute, 2=mute only");
8753
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02008754module_param_named(volume_control, volume_control_allowed, bool, 0444);
8755MODULE_PARM_DESC(volume_control,
8756 "Enables software override for the console audio "
8757 "control when true");
8758
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02008759/* ALSA module API parameters */
8760module_param_named(index, alsa_index, int, 0444);
8761MODULE_PARM_DESC(index, "ALSA index for the ACPI EC Mixer");
8762module_param_named(id, alsa_id, charp, 0444);
8763MODULE_PARM_DESC(id, "ALSA id for the ACPI EC Mixer");
8764module_param_named(enable, alsa_enable, bool, 0444);
8765MODULE_PARM_DESC(enable, "Enable the ALSA interface for the ACPI EC Mixer");
8766
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008767#define TPACPI_PARAM(feature) \
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008768 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
Henrique de Moraes Holschuhcbb14842008-02-16 02:17:50 -02008769 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008770 "at module load, see documentation")
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008771
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008772TPACPI_PARAM(hotkey);
8773TPACPI_PARAM(bluetooth);
8774TPACPI_PARAM(video);
8775TPACPI_PARAM(light);
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008776TPACPI_PARAM(cmos);
8777TPACPI_PARAM(led);
8778TPACPI_PARAM(beep);
8779TPACPI_PARAM(ecdump);
8780TPACPI_PARAM(brightness);
8781TPACPI_PARAM(volume);
8782TPACPI_PARAM(fan);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008783
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02008784#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008785module_param(dbg_wlswemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02008786MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
8787module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
8788MODULE_PARM_DESC(wlsw_state,
8789 "Initial state of the emulated WLSW switch");
8790
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008791module_param(dbg_bluetoothemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02008792MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
8793module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
8794MODULE_PARM_DESC(bluetooth_state,
8795 "Initial state of the emulated bluetooth switch");
8796
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008797module_param(dbg_wwanemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02008798MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
8799module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
8800MODULE_PARM_DESC(wwan_state,
8801 "Initial state of the emulated WWAN switch");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02008802
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008803module_param(dbg_uwbemul, uint, 0444);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02008804MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
8805module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
8806MODULE_PARM_DESC(uwb_state,
8807 "Initial state of the emulated UWB switch");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02008808#endif
8809
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008810static void thinkpad_acpi_module_exit(void)
8811{
8812 struct ibm_struct *ibm, *itmp;
8813
8814 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
8815
8816 list_for_each_entry_safe_reverse(ibm, itmp,
8817 &tpacpi_all_drivers,
8818 all_drivers) {
8819 ibm_exit(ibm);
8820 }
8821
8822 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
8823
8824 if (tpacpi_inputdev) {
8825 if (tp_features.input_device_registered)
8826 input_unregister_device(tpacpi_inputdev);
8827 else
8828 input_free_device(tpacpi_inputdev);
8829 }
8830
8831 if (tpacpi_hwmon)
8832 hwmon_device_unregister(tpacpi_hwmon);
8833
8834 if (tp_features.sensors_pdev_attrs_registered)
8835 device_remove_file(&tpacpi_sensors_pdev->dev,
8836 &dev_attr_thinkpad_acpi_pdev_name);
8837 if (tpacpi_sensors_pdev)
8838 platform_device_unregister(tpacpi_sensors_pdev);
8839 if (tpacpi_pdev)
8840 platform_device_unregister(tpacpi_pdev);
8841
8842 if (tp_features.sensors_pdrv_attrs_registered)
8843 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
8844 if (tp_features.platform_drv_attrs_registered)
8845 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
8846
8847 if (tp_features.sensors_pdrv_registered)
8848 platform_driver_unregister(&tpacpi_hwmon_pdriver);
8849
8850 if (tp_features.platform_drv_registered)
8851 platform_driver_unregister(&tpacpi_pdriver);
8852
8853 if (proc_dir)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008854 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008855
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03008856 if (tpacpi_wq)
8857 destroy_workqueue(tpacpi_wq);
8858
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008859 kfree(thinkpad_id.bios_version_str);
8860 kfree(thinkpad_id.ec_version_str);
8861 kfree(thinkpad_id.model_str);
8862}
8863
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02008864
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03008865static int __init thinkpad_acpi_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008866{
8867 int ret, i;
8868
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03008869 tpacpi_lifecycle = TPACPI_LIFE_INIT;
8870
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03008871 /* Parameter checking */
8872 if (hotkey_report_mode > 2)
8873 return -EINVAL;
8874
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008875 /* Driver-level probe */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008876
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008877 ret = get_thinkpad_model_data(&thinkpad_id);
8878 if (ret) {
8879 printk(TPACPI_ERR
8880 "unable to get DMI data: %d\n", ret);
8881 thinkpad_acpi_module_exit();
8882 return ret;
8883 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008884 ret = probe_for_thinkpad();
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008885 if (ret) {
8886 thinkpad_acpi_module_exit();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008887 return ret;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008888 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008889
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008890 /* Driver initialization */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008891
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008892 TPACPI_ACPIHANDLE_INIT(ecrd);
8893 TPACPI_ACPIHANDLE_INIT(ecwr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008894
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03008895 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
8896 if (!tpacpi_wq) {
8897 thinkpad_acpi_module_exit();
8898 return -ENOMEM;
8899 }
8900
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008901 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008902 if (!proc_dir) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008903 printk(TPACPI_ERR
8904 "unable to create proc dir " TPACPI_PROC_DIR);
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03008905 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008906 return -ENODEV;
8907 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008908
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008909 ret = platform_driver_register(&tpacpi_pdriver);
8910 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008911 printk(TPACPI_ERR
8912 "unable to register main platform driver\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008913 thinkpad_acpi_module_exit();
8914 return ret;
8915 }
Henrique de Moraes Holschuhac363932007-07-27 17:04:40 -03008916 tp_features.platform_drv_registered = 1;
8917
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008918 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
8919 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008920 printk(TPACPI_ERR
8921 "unable to register hwmon platform driver\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008922 thinkpad_acpi_module_exit();
8923 return ret;
8924 }
8925 tp_features.sensors_pdrv_registered = 1;
8926
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03008927 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03008928 if (!ret) {
8929 tp_features.platform_drv_attrs_registered = 1;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008930 ret = tpacpi_create_driver_attributes(
8931 &tpacpi_hwmon_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03008932 }
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03008933 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008934 printk(TPACPI_ERR
8935 "unable to create sysfs driver attributes\n");
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03008936 thinkpad_acpi_module_exit();
8937 return ret;
8938 }
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03008939 tp_features.sensors_pdrv_attrs_registered = 1;
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03008940
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008941
8942 /* Device initialization */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008943 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008944 NULL, 0);
8945 if (IS_ERR(tpacpi_pdev)) {
8946 ret = PTR_ERR(tpacpi_pdev);
8947 tpacpi_pdev = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008948 printk(TPACPI_ERR "unable to register platform device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008949 thinkpad_acpi_module_exit();
8950 return ret;
8951 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008952 tpacpi_sensors_pdev = platform_device_register_simple(
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008953 TPACPI_HWMON_DRVR_NAME,
8954 -1, NULL, 0);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008955 if (IS_ERR(tpacpi_sensors_pdev)) {
8956 ret = PTR_ERR(tpacpi_sensors_pdev);
8957 tpacpi_sensors_pdev = NULL;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008958 printk(TPACPI_ERR
8959 "unable to register hwmon platform device\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008960 thinkpad_acpi_module_exit();
8961 return ret;
8962 }
8963 ret = device_create_file(&tpacpi_sensors_pdev->dev,
8964 &dev_attr_thinkpad_acpi_pdev_name);
8965 if (ret) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008966 printk(TPACPI_ERR
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008967 "unable to create sysfs hwmon device attributes\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008968 thinkpad_acpi_module_exit();
8969 return ret;
8970 }
8971 tp_features.sensors_pdev_attrs_registered = 1;
8972 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008973 if (IS_ERR(tpacpi_hwmon)) {
8974 ret = PTR_ERR(tpacpi_hwmon);
8975 tpacpi_hwmon = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008976 printk(TPACPI_ERR "unable to register hwmon device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008977 thinkpad_acpi_module_exit();
8978 return ret;
8979 }
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -03008980 mutex_init(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03008981 tpacpi_inputdev = input_allocate_device();
8982 if (!tpacpi_inputdev) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008983 printk(TPACPI_ERR "unable to allocate input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03008984 thinkpad_acpi_module_exit();
8985 return -ENOMEM;
8986 } else {
8987 /* Prepare input device, but don't register */
8988 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008989 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03008990 tpacpi_inputdev->id.bustype = BUS_HOST;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03008991 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
8992 thinkpad_id.vendor :
8993 PCI_VENDOR_ID_IBM;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03008994 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
8995 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
Henrique de Moraes Holschuhd112ef92009-12-09 01:36:26 +00008996 tpacpi_inputdev->dev.parent = &tpacpi_pdev->dev;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03008997 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008998 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
8999 ret = ibm_init(&ibms_init[i]);
9000 if (ret >= 0 && *ibms_init[i].param)
9001 ret = ibms_init[i].data->write(ibms_init[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009002 if (ret < 0) {
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03009003 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07009004 return ret;
9005 }
9006 }
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009007 ret = input_register_device(tpacpi_inputdev);
9008 if (ret < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009009 printk(TPACPI_ERR "unable to register input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009010 thinkpad_acpi_module_exit();
9011 return ret;
9012 } else {
9013 tp_features.input_device_registered = 1;
9014 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009015
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03009016 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009017 return 0;
9018}
9019
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03009020MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
9021
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009022/*
Henrique de Moraes Holschuh922fe092009-04-14 02:44:13 +00009023 * This will autoload the driver in almost every ThinkPad
9024 * in widespread use.
9025 *
9026 * Only _VERY_ old models, like the 240, 240x and 570 lack
9027 * the HKEY event interface.
9028 */
9029MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
9030
9031/*
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009032 * DMI matching for module autoloading
9033 *
9034 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
9035 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
9036 *
9037 * Only models listed in thinkwiki will be supported, so add yours
9038 * if it is not there yet.
9039 */
9040#define IBM_BIOS_MODULE_ALIAS(__type) \
Mathieu Chouquet-Stringerb36a50f2009-03-14 16:35:26 +01009041 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009042
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009043/* Ancient thinkpad BIOSes have to be identified by
9044 * BIOS type or model number, and there are far less
9045 * BIOS types than model numbers... */
Henrique de Moraes Holschuh922fe092009-04-14 02:44:13 +00009046IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009047
Henrique de Moraes Holschuhf68f53a2009-04-14 02:44:12 +00009048MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
9049MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009050MODULE_DESCRIPTION(TPACPI_DESC);
9051MODULE_VERSION(TPACPI_VERSION);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009052MODULE_LICENSE("GPL");
9053
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03009054module_init(thinkpad_acpi_module_init);
9055module_exit(thinkpad_acpi_module_exit);