blob: e67e4feb35cb814f85ad2df10bc1836c00cc79b6 [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
André Goddard Rosae7d28602009-12-14 18:01:06 -08001021 *value = simple_strtoul(skip_spaces(buf), &endp, 0);
1022 endp = skip_spaces(endp);
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001023 if (*endp || *value > max)
1024 return -EINVAL;
1025
1026 return 0;
1027}
1028
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03001029static void tpacpi_disable_brightness_delay(void)
1030{
1031 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
1032 printk(TPACPI_NOTICE
1033 "ACPI backlight control delay disabled\n");
1034}
1035
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03001036static int __init tpacpi_query_bcl_levels(acpi_handle handle)
1037{
1038 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1039 union acpi_object *obj;
1040 int rc;
1041
1042 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
1043 obj = (union acpi_object *)buffer.pointer;
1044 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
1045 printk(TPACPI_ERR "Unknown _BCL data, "
1046 "please report this to %s\n", TPACPI_MAIL);
1047 rc = 0;
1048 } else {
1049 rc = obj->package.count;
1050 }
1051 } else {
1052 return 0;
1053 }
1054
1055 kfree(buffer.pointer);
1056 return rc;
1057}
1058
1059static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
1060 u32 lvl, void *context, void **rv)
1061{
1062 char name[ACPI_PATH_SEGMENT_LENGTH];
1063 struct acpi_buffer buffer = { sizeof(name), &name };
1064
1065 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
1066 !strncmp("_BCL", name, sizeof(name) - 1)) {
1067 BUG_ON(!rv || !*rv);
1068 **(int **)rv = tpacpi_query_bcl_levels(handle);
1069 return AE_CTRL_TERMINATE;
1070 } else {
1071 return AE_OK;
1072 }
1073}
1074
1075/*
1076 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
1077 */
1078static int __init tpacpi_check_std_acpi_brightness_support(void)
1079{
1080 int status;
1081 int bcl_levels = 0;
1082 void *bcl_ptr = &bcl_levels;
1083
1084 if (!vid_handle) {
1085 TPACPI_ACPIHANDLE_INIT(vid);
1086 }
1087 if (!vid_handle)
1088 return 0;
1089
1090 /*
1091 * Search for a _BCL method, and execute it. This is safe on all
1092 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
1093 * BIOS in ACPI backlight control mode. We do NOT have to care
1094 * about calling the _BCL method in an enabled video device, any
1095 * will do for our purposes.
1096 */
1097
1098 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
Lin Ming22635762009-11-13 10:06:08 +08001099 tpacpi_acpi_walk_find_bcl, NULL, NULL,
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03001100 &bcl_ptr);
1101
1102 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
1103 tp_features.bright_acpimode = 1;
1104 return (bcl_levels - 2);
1105 }
1106
1107 return 0;
1108}
1109
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00001110static void printk_deprecated_attribute(const char * const what,
1111 const char * const details)
1112{
1113 tpacpi_log_usertask("deprecated sysfs attribute");
1114 printk(TPACPI_WARN "WARNING: sysfs attribute %s is deprecated and "
1115 "will be removed. %s\n",
1116 what, details);
1117}
1118
Johannes Berg19d337d2009-06-02 13:01:37 +02001119/*************************************************************************
1120 * rfkill and radio control support helpers
1121 */
1122
1123/*
1124 * ThinkPad-ACPI firmware handling model:
1125 *
1126 * WLSW (master wireless switch) is event-driven, and is common to all
1127 * firmware-controlled radios. It cannot be controlled, just monitored,
1128 * as expected. It overrides all radio state in firmware
1129 *
1130 * The kernel, a masked-off hotkey, and WLSW can change the radio state
1131 * (TODO: verify how WLSW interacts with the returned radio state).
1132 *
1133 * The only time there are shadow radio state changes, is when
1134 * masked-off hotkeys are used.
1135 */
1136
1137/*
1138 * Internal driver API for radio state:
1139 *
1140 * int: < 0 = error, otherwise enum tpacpi_rfkill_state
1141 * bool: true means radio blocked (off)
1142 */
1143enum tpacpi_rfkill_state {
1144 TPACPI_RFK_RADIO_OFF = 0,
1145 TPACPI_RFK_RADIO_ON
1146};
1147
1148/* rfkill switches */
1149enum tpacpi_rfk_id {
1150 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
1151 TPACPI_RFK_WWAN_SW_ID,
1152 TPACPI_RFK_UWB_SW_ID,
1153 TPACPI_RFK_SW_MAX
1154};
1155
1156static const char *tpacpi_rfkill_names[] = {
1157 [TPACPI_RFK_BLUETOOTH_SW_ID] = "bluetooth",
1158 [TPACPI_RFK_WWAN_SW_ID] = "wwan",
1159 [TPACPI_RFK_UWB_SW_ID] = "uwb",
1160 [TPACPI_RFK_SW_MAX] = NULL
1161};
1162
1163/* ThinkPad-ACPI rfkill subdriver */
1164struct tpacpi_rfk {
1165 struct rfkill *rfkill;
1166 enum tpacpi_rfk_id id;
1167 const struct tpacpi_rfk_ops *ops;
1168};
1169
1170struct tpacpi_rfk_ops {
1171 /* firmware interface */
1172 int (*get_status)(void);
1173 int (*set_status)(const enum tpacpi_rfkill_state);
1174};
1175
1176static struct tpacpi_rfk *tpacpi_rfkill_switches[TPACPI_RFK_SW_MAX];
1177
1178/* Query FW and update rfkill sw state for a given rfkill switch */
1179static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk *tp_rfk)
1180{
1181 int status;
1182
1183 if (!tp_rfk)
1184 return -ENODEV;
1185
1186 status = (tp_rfk->ops->get_status)();
1187 if (status < 0)
1188 return status;
1189
1190 rfkill_set_sw_state(tp_rfk->rfkill,
1191 (status == TPACPI_RFK_RADIO_OFF));
1192
1193 return status;
1194}
1195
1196/* Query FW and update rfkill sw state for all rfkill switches */
1197static void tpacpi_rfk_update_swstate_all(void)
1198{
1199 unsigned int i;
1200
1201 for (i = 0; i < TPACPI_RFK_SW_MAX; i++)
1202 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[i]);
1203}
1204
1205/*
1206 * Sync the HW-blocking state of all rfkill switches,
1207 * do notice it causes the rfkill core to schedule uevents
1208 */
1209static void tpacpi_rfk_update_hwblock_state(bool blocked)
1210{
1211 unsigned int i;
1212 struct tpacpi_rfk *tp_rfk;
1213
1214 for (i = 0; i < TPACPI_RFK_SW_MAX; i++) {
1215 tp_rfk = tpacpi_rfkill_switches[i];
1216 if (tp_rfk) {
1217 if (rfkill_set_hw_state(tp_rfk->rfkill,
1218 blocked)) {
1219 /* ignore -- we track sw block */
1220 }
1221 }
1222 }
1223}
1224
1225/* Call to get the WLSW state from the firmware */
1226static int hotkey_get_wlsw(void);
1227
1228/* Call to query WLSW state and update all rfkill switches */
1229static bool tpacpi_rfk_check_hwblock_state(void)
1230{
1231 int res = hotkey_get_wlsw();
1232 int hw_blocked;
1233
1234 /* When unknown or unsupported, we have to assume it is unblocked */
1235 if (res < 0)
1236 return false;
1237
1238 hw_blocked = (res == TPACPI_RFK_RADIO_OFF);
1239 tpacpi_rfk_update_hwblock_state(hw_blocked);
1240
1241 return hw_blocked;
1242}
1243
1244static int tpacpi_rfk_hook_set_block(void *data, bool blocked)
1245{
1246 struct tpacpi_rfk *tp_rfk = data;
1247 int res;
1248
1249 dbg_printk(TPACPI_DBG_RFKILL,
1250 "request to change radio state to %s\n",
1251 blocked ? "blocked" : "unblocked");
1252
1253 /* try to set radio state */
1254 res = (tp_rfk->ops->set_status)(blocked ?
1255 TPACPI_RFK_RADIO_OFF : TPACPI_RFK_RADIO_ON);
1256
1257 /* and update the rfkill core with whatever the FW really did */
1258 tpacpi_rfk_update_swstate(tp_rfk);
1259
1260 return (res < 0) ? res : 0;
1261}
1262
1263static const struct rfkill_ops tpacpi_rfk_rfkill_ops = {
1264 .set_block = tpacpi_rfk_hook_set_block,
1265};
1266
1267static int __init tpacpi_new_rfkill(const enum tpacpi_rfk_id id,
1268 const struct tpacpi_rfk_ops *tp_rfkops,
1269 const enum rfkill_type rfktype,
1270 const char *name,
1271 const bool set_default)
1272{
1273 struct tpacpi_rfk *atp_rfk;
1274 int res;
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001275 bool sw_state = false;
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001276 bool hw_state;
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001277 int sw_status;
Johannes Berg19d337d2009-06-02 13:01:37 +02001278
1279 BUG_ON(id >= TPACPI_RFK_SW_MAX || tpacpi_rfkill_switches[id]);
1280
Johannes Berg19d337d2009-06-02 13:01:37 +02001281 atp_rfk = kzalloc(sizeof(struct tpacpi_rfk), GFP_KERNEL);
1282 if (atp_rfk)
1283 atp_rfk->rfkill = rfkill_alloc(name,
1284 &tpacpi_pdev->dev,
1285 rfktype,
1286 &tpacpi_rfk_rfkill_ops,
1287 atp_rfk);
1288 if (!atp_rfk || !atp_rfk->rfkill) {
1289 printk(TPACPI_ERR
1290 "failed to allocate memory for rfkill class\n");
1291 kfree(atp_rfk);
1292 return -ENOMEM;
1293 }
1294
1295 atp_rfk->id = id;
1296 atp_rfk->ops = tp_rfkops;
1297
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001298 sw_status = (tp_rfkops->get_status)();
1299 if (sw_status < 0) {
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001300 printk(TPACPI_ERR
1301 "failed to read initial state for %s, error %d\n",
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001302 name, sw_status);
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001303 } else {
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001304 sw_state = (sw_status == TPACPI_RFK_RADIO_OFF);
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001305 if (set_default) {
1306 /* try to keep the initial state, since we ask the
1307 * firmware to preserve it across S5 in NVRAM */
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001308 rfkill_init_sw_state(atp_rfk->rfkill, sw_state);
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001309 }
1310 }
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001311 hw_state = tpacpi_rfk_check_hwblock_state();
1312 rfkill_set_hw_state(atp_rfk->rfkill, hw_state);
Johannes Berg19d337d2009-06-02 13:01:37 +02001313
1314 res = rfkill_register(atp_rfk->rfkill);
1315 if (res < 0) {
1316 printk(TPACPI_ERR
1317 "failed to register %s rfkill switch: %d\n",
1318 name, res);
1319 rfkill_destroy(atp_rfk->rfkill);
1320 kfree(atp_rfk);
1321 return res;
1322 }
1323
1324 tpacpi_rfkill_switches[id] = atp_rfk;
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001325
1326 printk(TPACPI_INFO "rfkill switch %s: radio is %sblocked\n",
1327 name, (sw_state || hw_state) ? "" : "un");
Johannes Berg19d337d2009-06-02 13:01:37 +02001328 return 0;
1329}
1330
1331static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id)
1332{
1333 struct tpacpi_rfk *tp_rfk;
1334
1335 BUG_ON(id >= TPACPI_RFK_SW_MAX);
1336
1337 tp_rfk = tpacpi_rfkill_switches[id];
1338 if (tp_rfk) {
1339 rfkill_unregister(tp_rfk->rfkill);
Corentin Chary5f0dadb2009-09-14 12:43:52 +02001340 rfkill_destroy(tp_rfk->rfkill);
Johannes Berg19d337d2009-06-02 13:01:37 +02001341 tpacpi_rfkill_switches[id] = NULL;
1342 kfree(tp_rfk);
1343 }
1344}
1345
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00001346static void printk_deprecated_rfkill_attribute(const char * const what)
1347{
1348 printk_deprecated_attribute(what,
1349 "Please switch to generic rfkill before year 2010");
1350}
1351
Johannes Berg19d337d2009-06-02 13:01:37 +02001352/* sysfs <radio> enable ------------------------------------------------ */
1353static ssize_t tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id,
1354 struct device_attribute *attr,
1355 char *buf)
1356{
1357 int status;
1358
1359 printk_deprecated_rfkill_attribute(attr->attr.name);
1360
1361 /* This is in the ABI... */
1362 if (tpacpi_rfk_check_hwblock_state()) {
1363 status = TPACPI_RFK_RADIO_OFF;
1364 } else {
1365 status = tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1366 if (status < 0)
1367 return status;
1368 }
1369
1370 return snprintf(buf, PAGE_SIZE, "%d\n",
1371 (status == TPACPI_RFK_RADIO_ON) ? 1 : 0);
1372}
1373
1374static ssize_t tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id,
1375 struct device_attribute *attr,
1376 const char *buf, size_t count)
1377{
1378 unsigned long t;
1379 int res;
1380
1381 printk_deprecated_rfkill_attribute(attr->attr.name);
1382
1383 if (parse_strtoul(buf, 1, &t))
1384 return -EINVAL;
1385
1386 tpacpi_disclose_usertask(attr->attr.name, "set to %ld\n", t);
1387
1388 /* This is in the ABI... */
1389 if (tpacpi_rfk_check_hwblock_state() && !!t)
1390 return -EPERM;
1391
1392 res = tpacpi_rfkill_switches[id]->ops->set_status((!!t) ?
1393 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF);
1394 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1395
1396 return (res < 0) ? res : count;
1397}
1398
1399/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001400static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id, struct seq_file *m)
Johannes Berg19d337d2009-06-02 13:01:37 +02001401{
Johannes Berg19d337d2009-06-02 13:01:37 +02001402 if (id >= TPACPI_RFK_SW_MAX)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001403 seq_printf(m, "status:\t\tnot supported\n");
Johannes Berg19d337d2009-06-02 13:01:37 +02001404 else {
1405 int status;
1406
1407 /* This is in the ABI... */
1408 if (tpacpi_rfk_check_hwblock_state()) {
1409 status = TPACPI_RFK_RADIO_OFF;
1410 } else {
1411 status = tpacpi_rfk_update_swstate(
1412 tpacpi_rfkill_switches[id]);
1413 if (status < 0)
1414 return status;
1415 }
1416
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001417 seq_printf(m, "status:\t\t%s\n",
Johannes Berg19d337d2009-06-02 13:01:37 +02001418 (status == TPACPI_RFK_RADIO_ON) ?
1419 "enabled" : "disabled");
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001420 seq_printf(m, "commands:\tenable, disable\n");
Johannes Berg19d337d2009-06-02 13:01:37 +02001421 }
1422
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001423 return 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02001424}
1425
1426static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id, char *buf)
1427{
1428 char *cmd;
1429 int status = -1;
1430 int res = 0;
1431
1432 if (id >= TPACPI_RFK_SW_MAX)
1433 return -ENODEV;
1434
1435 while ((cmd = next_cmd(&buf))) {
1436 if (strlencmp(cmd, "enable") == 0)
1437 status = TPACPI_RFK_RADIO_ON;
1438 else if (strlencmp(cmd, "disable") == 0)
1439 status = TPACPI_RFK_RADIO_OFF;
1440 else
1441 return -EINVAL;
1442 }
1443
1444 if (status != -1) {
1445 tpacpi_disclose_usertask("procfs", "attempt to %s %s\n",
1446 (status == TPACPI_RFK_RADIO_ON) ?
1447 "enable" : "disable",
1448 tpacpi_rfkill_names[id]);
1449 res = (tpacpi_rfkill_switches[id]->ops->set_status)(status);
1450 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1451 }
1452
1453 return res;
1454}
1455
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001456/*************************************************************************
1457 * thinkpad-acpi driver attributes
1458 */
1459
1460/* interface_version --------------------------------------------------- */
1461static ssize_t tpacpi_driver_interface_version_show(
1462 struct device_driver *drv,
1463 char *buf)
1464{
1465 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1466}
1467
1468static DRIVER_ATTR(interface_version, S_IRUGO,
1469 tpacpi_driver_interface_version_show, NULL);
1470
1471/* debug_level --------------------------------------------------------- */
1472static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1473 char *buf)
1474{
1475 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1476}
1477
1478static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1479 const char *buf, size_t count)
1480{
1481 unsigned long t;
1482
1483 if (parse_strtoul(buf, 0xffff, &t))
1484 return -EINVAL;
1485
1486 dbg_level = t;
1487
1488 return count;
1489}
1490
1491static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1492 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1493
1494/* version ------------------------------------------------------------- */
1495static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1496 char *buf)
1497{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001498 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1499 TPACPI_DESC, TPACPI_VERSION);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001500}
1501
1502static DRIVER_ATTR(version, S_IRUGO,
1503 tpacpi_driver_version_show, NULL);
1504
1505/* --------------------------------------------------------------------- */
1506
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001507#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1508
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001509/* wlsw_emulstate ------------------------------------------------------ */
1510static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1511 char *buf)
1512{
1513 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1514}
1515
1516static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1517 const char *buf, size_t count)
1518{
1519 unsigned long t;
1520
1521 if (parse_strtoul(buf, 1, &t))
1522 return -EINVAL;
1523
Johannes Berg19d337d2009-06-02 13:01:37 +02001524 if (tpacpi_wlsw_emulstate != !!t) {
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001525 tpacpi_wlsw_emulstate = !!t;
Johannes Berg19d337d2009-06-02 13:01:37 +02001526 tpacpi_rfk_update_hwblock_state(!t); /* negative logic */
1527 }
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001528
1529 return count;
1530}
1531
1532static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1533 tpacpi_driver_wlsw_emulstate_show,
1534 tpacpi_driver_wlsw_emulstate_store);
1535
1536/* bluetooth_emulstate ------------------------------------------------- */
1537static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1538 struct device_driver *drv,
1539 char *buf)
1540{
1541 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1542}
1543
1544static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1545 struct device_driver *drv,
1546 const char *buf, size_t count)
1547{
1548 unsigned long t;
1549
1550 if (parse_strtoul(buf, 1, &t))
1551 return -EINVAL;
1552
1553 tpacpi_bluetooth_emulstate = !!t;
1554
1555 return count;
1556}
1557
1558static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1559 tpacpi_driver_bluetooth_emulstate_show,
1560 tpacpi_driver_bluetooth_emulstate_store);
1561
1562/* wwan_emulstate ------------------------------------------------- */
1563static ssize_t tpacpi_driver_wwan_emulstate_show(
1564 struct device_driver *drv,
1565 char *buf)
1566{
1567 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1568}
1569
1570static ssize_t tpacpi_driver_wwan_emulstate_store(
1571 struct device_driver *drv,
1572 const char *buf, size_t count)
1573{
1574 unsigned long t;
1575
1576 if (parse_strtoul(buf, 1, &t))
1577 return -EINVAL;
1578
1579 tpacpi_wwan_emulstate = !!t;
1580
1581 return count;
1582}
1583
1584static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1585 tpacpi_driver_wwan_emulstate_show,
1586 tpacpi_driver_wwan_emulstate_store);
1587
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001588/* uwb_emulstate ------------------------------------------------- */
1589static ssize_t tpacpi_driver_uwb_emulstate_show(
1590 struct device_driver *drv,
1591 char *buf)
1592{
1593 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1594}
1595
1596static ssize_t tpacpi_driver_uwb_emulstate_store(
1597 struct device_driver *drv,
1598 const char *buf, size_t count)
1599{
1600 unsigned long t;
1601
1602 if (parse_strtoul(buf, 1, &t))
1603 return -EINVAL;
1604
1605 tpacpi_uwb_emulstate = !!t;
1606
1607 return count;
1608}
1609
1610static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
1611 tpacpi_driver_uwb_emulstate_show,
1612 tpacpi_driver_uwb_emulstate_store);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001613#endif
1614
1615/* --------------------------------------------------------------------- */
1616
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001617static struct driver_attribute *tpacpi_driver_attributes[] = {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001618 &driver_attr_debug_level, &driver_attr_version,
1619 &driver_attr_interface_version,
1620};
1621
1622static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1623{
1624 int i, res;
1625
1626 i = 0;
1627 res = 0;
1628 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1629 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1630 i++;
1631 }
1632
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001633#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1634 if (!res && dbg_wlswemul)
1635 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1636 if (!res && dbg_bluetoothemul)
1637 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1638 if (!res && dbg_wwanemul)
1639 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001640 if (!res && dbg_uwbemul)
1641 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001642#endif
1643
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001644 return res;
1645}
1646
1647static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1648{
1649 int i;
1650
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001651 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001652 driver_remove_file(drv, tpacpi_driver_attributes[i]);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001653
1654#ifdef THINKPAD_ACPI_DEBUGFACILITIES
1655 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1656 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1657 driver_remove_file(drv, &driver_attr_wwan_emulstate);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001658 driver_remove_file(drv, &driver_attr_uwb_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001659#endif
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001660}
1661
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001662/*************************************************************************
1663 * Firmware Data
1664 */
1665
1666/*
1667 * Table of recommended minimum BIOS versions
1668 *
1669 * Reasons for listing:
1670 * 1. Stable BIOS, listed because the unknown ammount of
1671 * bugs and bad ACPI behaviour on older versions
1672 *
1673 * 2. BIOS or EC fw with known bugs that trigger on Linux
1674 *
1675 * 3. BIOS with known reduced functionality in older versions
1676 *
1677 * We recommend the latest BIOS and EC version.
1678 * We only support the latest BIOS and EC fw version as a rule.
1679 *
1680 * Sources: IBM ThinkPad Public Web Documents (update changelogs),
1681 * Information from users in ThinkWiki
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03001682 *
1683 * WARNING: we use this table also to detect that the machine is
1684 * a ThinkPad in some cases, so don't remove entries lightly.
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001685 */
1686
1687#define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1688 { .vendor = (__v), \
1689 .bios = TPID(__id1, __id2), \
1690 .ec = TPACPI_MATCH_ANY, \
1691 .quirks = TPACPI_MATCH_ANY << 16 \
1692 | (__bv1) << 8 | (__bv2) }
1693
1694#define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001695 __eid, __ev1, __ev2) \
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001696 { .vendor = (__v), \
1697 .bios = TPID(__bid1, __bid2), \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001698 .ec = __eid, \
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001699 .quirks = (__ev1) << 24 | (__ev2) << 16 \
1700 | (__bv1) << 8 | (__bv2) }
1701
1702#define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1703 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1704
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001705/* Outdated IBM BIOSes often lack the EC id string */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001706#define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1707 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001708 __bv1, __bv2, TPID(__id1, __id2), \
1709 __ev1, __ev2), \
1710 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1711 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1712 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001713
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001714/* Outdated IBM BIOSes often lack the EC id string */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001715#define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1716 __eid1, __eid2, __ev1, __ev2) \
1717 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001718 __bv1, __bv2, TPID(__eid1, __eid2), \
1719 __ev1, __ev2), \
1720 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1721 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1722 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001723
1724#define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1725 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1726
1727#define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1728 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001729 __bv1, __bv2, TPID(__id1, __id2), \
1730 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001731
1732#define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1733 __eid1, __eid2, __ev1, __ev2) \
1734 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001735 __bv1, __bv2, TPID(__eid1, __eid2), \
1736 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001737
1738static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
1739 /* Numeric models ------------------ */
1740 /* FW MODEL BIOS VERS */
1741 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1742 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1743 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1744 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1745 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1746 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1747 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1748 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1749 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1750
1751 /* A-series ------------------------- */
1752 /* FW MODEL BIOS VERS EC VERS */
1753 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1754 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1755 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1756 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1757 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1758 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1759 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1760 TPV_QI0('1', '3', '2', '0'), /* A22m */
1761 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1762 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1763 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1764
1765 /* G-series ------------------------- */
1766 /* FW MODEL BIOS VERS */
1767 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1768 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1769
1770 /* R-series, T-series --------------- */
1771 /* FW MODEL BIOS VERS EC VERS */
1772 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1773 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1774 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1775 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1776 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1777 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1778 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1779 T40/p, T41/p, T42/p (1) */
1780 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1781 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1782 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1783 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1784
1785 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1786 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1787 TPV_QI0('1', '6', '3', '2'), /* T22 */
1788 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1789 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1790 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1791
1792 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1793 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
Henrique de Moraes Holschuh90765c62009-12-09 01:36:23 +00001794 TPV_QL1('7', 'E', 'D', '0', '1', '5'), /* R60e, R60i */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001795
1796 /* BIOS FW BIOS VERS EC FW EC VERS */
1797 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1798 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1799
1800 /* X-series ------------------------- */
1801 /* FW MODEL BIOS VERS EC VERS */
1802 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1803 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1804 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1805 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1806 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1807 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1808 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1809
Henrique de Moraes Holschuh90765c62009-12-09 01:36:23 +00001810 TPV_QL1('7', 'B', 'D', '7', '4', '0'), /* X60/s */
1811 TPV_QL1('7', 'J', '3', '0', '1', '3'), /* X60t */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001812
1813 /* (0) - older versions lack DMI EC fw string and functionality */
1814 /* (1) - older versions known to lack functionality */
1815};
1816
1817#undef TPV_QL1
1818#undef TPV_QL0
1819#undef TPV_QI2
1820#undef TPV_QI1
1821#undef TPV_QI0
1822#undef TPV_Q_X
1823#undef TPV_Q
1824
1825static void __init tpacpi_check_outdated_fw(void)
1826{
1827 unsigned long fwvers;
1828 u16 ec_version, bios_version;
1829
1830 fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
1831 ARRAY_SIZE(tpacpi_bios_version_qtable));
1832
1833 if (!fwvers)
1834 return;
1835
1836 bios_version = fwvers & 0xffffU;
1837 ec_version = (fwvers >> 16) & 0xffffU;
1838
1839 /* note that unknown versions are set to 0x0000 and we use that */
1840 if ((bios_version > thinkpad_id.bios_release) ||
1841 (ec_version > thinkpad_id.ec_release &&
1842 ec_version != TPACPI_MATCH_ANY)) {
1843 /*
1844 * The changelogs would let us track down the exact
1845 * reason, but it is just too much of a pain to track
1846 * it. We only list BIOSes that are either really
1847 * broken, or really stable to begin with, so it is
1848 * best if the user upgrades the firmware anyway.
1849 */
1850 printk(TPACPI_WARN
1851 "WARNING: Outdated ThinkPad BIOS/EC firmware\n");
1852 printk(TPACPI_WARN
1853 "WARNING: This firmware may be missing critical bug "
1854 "fixes and/or important features\n");
1855 }
1856}
1857
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03001858static bool __init tpacpi_is_fw_known(void)
1859{
1860 return tpacpi_check_quirks(tpacpi_bios_version_qtable,
1861 ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
1862}
1863
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03001864/****************************************************************************
1865 ****************************************************************************
1866 *
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001867 * Subdrivers
1868 *
1869 ****************************************************************************
1870 ****************************************************************************/
1871
1872/*************************************************************************
Henrique de Moraes Holschuh142cfc92007-04-21 11:08:26 -03001873 * thinkpad-acpi init subdriver
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001874 */
1875
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001876static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001878 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
1879 printk(TPACPI_INFO "%s\n", TPACPI_URL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001881 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03001882 (thinkpad_id.bios_version_str) ?
1883 thinkpad_id.bios_version_str : "unknown",
1884 (thinkpad_id.ec_version_str) ?
1885 thinkpad_id.ec_version_str : "unknown");
1886
1887 if (thinkpad_id.vendor && thinkpad_id.model_str)
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03001888 printk(TPACPI_INFO "%s %s, model %s\n",
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03001889 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
1890 "IBM" : ((thinkpad_id.vendor ==
1891 PCI_VENDOR_ID_LENOVO) ?
1892 "Lenovo" : "Unknown vendor"),
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03001893 thinkpad_id.model_str,
1894 (thinkpad_id.nummodel_str) ?
1895 thinkpad_id.nummodel_str : "unknown");
Henrique de Moraes Holschuh3945ac32007-02-06 19:13:44 -02001896
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001897 tpacpi_check_outdated_fw();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 return 0;
1899}
1900
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001901static int thinkpad_acpi_driver_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902{
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001903 seq_printf(m, "driver:\t\t%s\n", TPACPI_DESC);
1904 seq_printf(m, "version:\t%s\n", TPACPI_VERSION);
1905 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906}
1907
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001908static struct ibm_struct thinkpad_acpi_driver_data = {
1909 .name = "driver",
1910 .read = thinkpad_acpi_driver_read,
1911};
1912
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001913/*************************************************************************
1914 * Hotkey subdriver
1915 */
1916
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001917/*
1918 * ThinkPad firmware event model
1919 *
1920 * The ThinkPad firmware has two main event interfaces: normal ACPI
1921 * notifications (which follow the ACPI standard), and a private event
1922 * interface.
1923 *
1924 * The private event interface also issues events for the hotkeys. As
1925 * the driver gained features, the event handling code ended up being
1926 * built around the hotkey subdriver. This will need to be refactored
1927 * to a more formal event API eventually.
1928 *
1929 * Some "hotkeys" are actually supposed to be used as event reports,
1930 * such as "brightness has changed", "volume has changed", depending on
1931 * the ThinkPad model and how the firmware is operating.
1932 *
1933 * Unlike other classes, hotkey-class events have mask/unmask control on
1934 * non-ancient firmware. However, how it behaves changes a lot with the
1935 * firmware model and version.
1936 */
1937
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001938enum { /* hot key scan codes (derived from ACPI DSDT) */
1939 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1940 TP_ACPI_HOTKEYSCAN_FNF2,
1941 TP_ACPI_HOTKEYSCAN_FNF3,
1942 TP_ACPI_HOTKEYSCAN_FNF4,
1943 TP_ACPI_HOTKEYSCAN_FNF5,
1944 TP_ACPI_HOTKEYSCAN_FNF6,
1945 TP_ACPI_HOTKEYSCAN_FNF7,
1946 TP_ACPI_HOTKEYSCAN_FNF8,
1947 TP_ACPI_HOTKEYSCAN_FNF9,
1948 TP_ACPI_HOTKEYSCAN_FNF10,
1949 TP_ACPI_HOTKEYSCAN_FNF11,
1950 TP_ACPI_HOTKEYSCAN_FNF12,
1951 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1952 TP_ACPI_HOTKEYSCAN_FNINSERT,
1953 TP_ACPI_HOTKEYSCAN_FNDELETE,
1954 TP_ACPI_HOTKEYSCAN_FNHOME,
1955 TP_ACPI_HOTKEYSCAN_FNEND,
1956 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1957 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1958 TP_ACPI_HOTKEYSCAN_FNSPACE,
1959 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1960 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1961 TP_ACPI_HOTKEYSCAN_MUTE,
1962 TP_ACPI_HOTKEYSCAN_THINKPAD,
1963};
1964
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001965enum { /* Keys/events available through NVRAM polling */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001966 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1967 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1968};
1969
1970enum { /* Positions of some of the keys in hotkey masks */
1971 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1972 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1973 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1974 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1975 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1976 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1977 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1978 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1979 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1980 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1981 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1982};
1983
1984enum { /* NVRAM to ACPI HKEY group map */
1985 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1986 TP_ACPI_HKEY_ZOOM_MASK |
1987 TP_ACPI_HKEY_DISPSWTCH_MASK |
1988 TP_ACPI_HKEY_HIBERNATE_MASK,
1989 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1990 TP_ACPI_HKEY_BRGHTDWN_MASK,
1991 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1992 TP_ACPI_HKEY_VOLDWN_MASK |
1993 TP_ACPI_HKEY_MUTE_MASK,
1994};
1995
1996#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1997struct tp_nvram_state {
1998 u16 thinkpad_toggle:1;
1999 u16 zoom_toggle:1;
2000 u16 display_toggle:1;
2001 u16 thinklight_toggle:1;
2002 u16 hibernate_toggle:1;
2003 u16 displayexp_toggle:1;
2004 u16 display_state:1;
2005 u16 brightness_toggle:1;
2006 u16 volume_toggle:1;
2007 u16 mute:1;
2008
2009 u8 brightness_level;
2010 u8 volume_level;
2011};
2012
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002013/* kthread for the hotkey poller */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002014static struct task_struct *tpacpi_hotkey_task;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002015
2016/* Acquired while the poller kthread is running, use to sync start/stop */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002017static struct mutex hotkey_thread_mutex;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002018
2019/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002020 * Acquire mutex to write poller control variables as an
2021 * atomic block.
2022 *
2023 * Increment hotkey_config_change when changing them if you
2024 * want the kthread to forget old state.
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002025 *
2026 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
2027 */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002028static struct mutex hotkey_thread_data_mutex;
2029static unsigned int hotkey_config_change;
2030
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002031/*
2032 * hotkey poller control variables
2033 *
2034 * Must be atomic or readers will also need to acquire mutex
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002035 *
2036 * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
2037 * should be used only when the changes need to be taken as
2038 * a block, OR when one needs to force the kthread to forget
2039 * old state.
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002040 */
2041static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
2042static unsigned int hotkey_poll_freq = 10; /* Hz */
2043
2044#define HOTKEY_CONFIG_CRITICAL_START \
2045 do { \
2046 mutex_lock(&hotkey_thread_data_mutex); \
2047 hotkey_config_change++; \
2048 } while (0);
2049#define HOTKEY_CONFIG_CRITICAL_END \
2050 mutex_unlock(&hotkey_thread_data_mutex);
2051
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002052#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2053
2054#define hotkey_source_mask 0U
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002055#define HOTKEY_CONFIG_CRITICAL_START
2056#define HOTKEY_CONFIG_CRITICAL_END
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002057
2058#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2059
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002060static struct mutex hotkey_mutex;
2061
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002062static enum { /* Reasons for waking up */
2063 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
2064 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
2065 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
2066} hotkey_wakeup_reason;
2067
2068static int hotkey_autosleep_ack;
2069
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002070static u32 hotkey_orig_mask; /* events the BIOS had enabled */
2071static u32 hotkey_all_mask; /* all events supported in fw */
2072static u32 hotkey_reserved_mask; /* events better left disabled */
2073static u32 hotkey_driver_mask; /* events needed by the driver */
2074static u32 hotkey_user_mask; /* events visible to userspace */
2075static u32 hotkey_acpi_mask; /* events enabled in firmware */
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002076
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02002077static unsigned int hotkey_report_mode;
2078
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002079static u16 *hotkey_keycode_map;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002080
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -03002081static struct attribute_set *hotkey_dev_attributes;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002082
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002083static void tpacpi_driver_event(const unsigned int hkey_event);
2084static void hotkey_driver_event(const unsigned int scancode);
2085
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002086/* HKEY.MHKG() return bits */
2087#define TP_HOTKEY_TABLET_MASK (1 << 3)
2088
Johannes Berg19d337d2009-06-02 13:01:37 +02002089static int hotkey_get_wlsw(void)
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002090{
Johannes Berg19d337d2009-06-02 13:01:37 +02002091 int status;
2092
2093 if (!tp_features.hotkey_wlsw)
2094 return -ENODEV;
2095
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02002096#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
Johannes Berg19d337d2009-06-02 13:01:37 +02002097 if (dbg_wlswemul)
2098 return (tpacpi_wlsw_emulstate) ?
2099 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02002100#endif
Johannes Berg19d337d2009-06-02 13:01:37 +02002101
2102 if (!acpi_evalf(hkey_handle, &status, "WLSW", "d"))
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002103 return -EIO;
Johannes Berg19d337d2009-06-02 13:01:37 +02002104
2105 return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002106}
2107
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002108static int hotkey_get_tablet_mode(int *status)
2109{
2110 int s;
2111
2112 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
2113 return -EIO;
2114
Henrique de Moraes Holschuhcee47f52008-03-04 14:29:21 -08002115 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
2116 return 0;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002117}
2118
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002119/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002120 * Reads current event mask from firmware, and updates
2121 * hotkey_acpi_mask accordingly. Also resets any bits
2122 * from hotkey_user_mask that are unavailable to be
2123 * delivered (shadow requirement of the userspace ABI).
2124 *
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002125 * Call with hotkey_mutex held
2126 */
2127static int hotkey_mask_get(void)
2128{
2129 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002130 u32 m = 0;
2131
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002132 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002133 return -EIO;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002134
2135 hotkey_acpi_mask = m;
2136 } else {
2137 /* no mask support doesn't mean no event support... */
2138 hotkey_acpi_mask = hotkey_all_mask;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002139 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002140
2141 /* sync userspace-visible mask */
2142 hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002143
2144 return 0;
2145}
2146
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002147void static hotkey_mask_warn_incomplete_mask(void)
2148{
2149 /* log only what the user can fix... */
2150 const u32 wantedmask = hotkey_driver_mask &
2151 ~(hotkey_acpi_mask | hotkey_source_mask) &
2152 (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
2153
2154 if (wantedmask)
2155 printk(TPACPI_NOTICE
2156 "required events 0x%08x not enabled!\n",
2157 wantedmask);
2158}
2159
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002160/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002161 * Set the firmware mask when supported
2162 *
2163 * Also calls hotkey_mask_get to update hotkey_acpi_mask.
2164 *
2165 * NOTE: does not set bits in hotkey_user_mask, but may reset them.
2166 *
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002167 * Call with hotkey_mutex held
2168 */
2169static int hotkey_mask_set(u32 mask)
2170{
2171 int i;
2172 int rc = 0;
2173
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002174 const u32 fwmask = mask & ~hotkey_source_mask;
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -03002175
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002176 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002177 for (i = 0; i < 32; i++) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002178 if (!acpi_evalf(hkey_handle,
2179 NULL, "MHKM", "vdd", i + 1,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002180 !!(mask & (1 << i)))) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002181 rc = -EIO;
2182 break;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002183 }
2184 }
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002185 }
2186
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002187 /*
2188 * We *must* make an inconditional call to hotkey_mask_get to
2189 * refresh hotkey_acpi_mask and update hotkey_user_mask
2190 *
2191 * Take the opportunity to also log when we cannot _enable_
2192 * a given event.
2193 */
2194 if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
2195 printk(TPACPI_NOTICE
2196 "asked for hotkey mask 0x%08x, but "
2197 "firmware forced it to 0x%08x\n",
2198 fwmask, hotkey_acpi_mask);
2199 }
2200
Henrique de Moraes Holschuh6b30eb72009-12-09 01:36:25 +00002201 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING)
2202 hotkey_mask_warn_incomplete_mask();
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002203
2204 return rc;
2205}
2206
2207/*
2208 * Sets hotkey_user_mask and tries to set the firmware mask
2209 *
2210 * Call with hotkey_mutex held
2211 */
2212static int hotkey_user_mask_set(const u32 mask)
2213{
2214 int rc;
2215
2216 /* Give people a chance to notice they are doing something that
2217 * is bound to go boom on their users sooner or later */
2218 if (!tp_warned.hotkey_mask_ff &&
2219 (mask == 0xffff || mask == 0xffffff ||
2220 mask == 0xffffffff)) {
2221 tp_warned.hotkey_mask_ff = 1;
2222 printk(TPACPI_NOTICE
2223 "setting the hotkey mask to 0x%08x is likely "
2224 "not the best way to go about it\n", mask);
2225 printk(TPACPI_NOTICE
2226 "please consider using the driver defaults, "
2227 "and refer to up-to-date thinkpad-acpi "
2228 "documentation\n");
2229 }
2230
2231 /* Try to enable what the user asked for, plus whatever we need.
2232 * this syncs everything but won't enable bits in hotkey_user_mask */
2233 rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
2234
2235 /* Enable the available bits in hotkey_user_mask */
2236 hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
2237
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002238 return rc;
2239}
2240
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002241/*
2242 * Sets the driver hotkey mask.
2243 *
2244 * Can be called even if the hotkey subdriver is inactive
2245 */
2246static int tpacpi_hotkey_driver_mask_set(const u32 mask)
2247{
2248 int rc;
2249
2250 /* Do the right thing if hotkey_init has not been called yet */
2251 if (!tp_features.hotkey) {
2252 hotkey_driver_mask = mask;
2253 return 0;
2254 }
2255
2256 mutex_lock(&hotkey_mutex);
2257
2258 HOTKEY_CONFIG_CRITICAL_START
2259 hotkey_driver_mask = mask;
Henrique de Moraes Holschuhb684a362009-09-26 21:42:49 -03002260#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002261 hotkey_source_mask |= (mask & ~hotkey_all_mask);
Henrique de Moraes Holschuhb684a362009-09-26 21:42:49 -03002262#endif
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002263 HOTKEY_CONFIG_CRITICAL_END
2264
2265 rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) &
2266 ~hotkey_source_mask);
2267 mutex_unlock(&hotkey_mutex);
2268
2269 return rc;
2270}
2271
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002272static int hotkey_status_get(int *status)
2273{
2274 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
2275 return -EIO;
2276
2277 return 0;
2278}
2279
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002280static int hotkey_status_set(bool enable)
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002281{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002282 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002283 return -EIO;
2284
2285 return 0;
2286}
2287
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002288static void tpacpi_input_send_tabletsw(void)
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002289{
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002290 int state;
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002291
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002292 if (tp_features.hotkey_tablet &&
2293 !hotkey_get_tablet_mode(&state)) {
2294 mutex_lock(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002295
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002296 input_report_switch(tpacpi_inputdev,
2297 SW_TABLET_MODE, !!state);
2298 input_sync(tpacpi_inputdev);
2299
2300 mutex_unlock(&tpacpi_inputdev_send_mutex);
2301 }
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002302}
2303
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002304/* Do NOT call without validating scancode first */
2305static void tpacpi_input_send_key(const unsigned int scancode)
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002306{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002307 const unsigned int keycode = hotkey_keycode_map[scancode];
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002308
2309 if (keycode != KEY_RESERVED) {
2310 mutex_lock(&tpacpi_inputdev_send_mutex);
2311
2312 input_report_key(tpacpi_inputdev, keycode, 1);
2313 if (keycode == KEY_UNKNOWN)
2314 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
2315 scancode);
2316 input_sync(tpacpi_inputdev);
2317
2318 input_report_key(tpacpi_inputdev, keycode, 0);
2319 if (keycode == KEY_UNKNOWN)
2320 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
2321 scancode);
2322 input_sync(tpacpi_inputdev);
2323
2324 mutex_unlock(&tpacpi_inputdev_send_mutex);
2325 }
2326}
2327
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002328/* Do NOT call without validating scancode first */
2329static void tpacpi_input_send_key_masked(const unsigned int scancode)
2330{
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002331 hotkey_driver_event(scancode);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002332 if (hotkey_user_mask & (1 << scancode))
2333 tpacpi_input_send_key(scancode);
2334}
2335
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002336#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2337static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
2338
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002339/* Do NOT call without validating scancode first */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002340static void tpacpi_hotkey_send_key(unsigned int scancode)
2341{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002342 tpacpi_input_send_key_masked(scancode);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002343 if (hotkey_report_mode < 2) {
2344 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03002345 0x80, TP_HKEY_EV_HOTKEY_BASE + scancode);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002346 }
2347}
2348
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002349static void hotkey_read_nvram(struct tp_nvram_state *n, const u32 m)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002350{
2351 u8 d;
2352
2353 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
2354 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
2355 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
2356 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
2357 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
2358 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
2359 }
2360 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
2361 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
2362 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
2363 }
2364 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
2365 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
2366 n->displayexp_toggle =
2367 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
2368 }
2369 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
2370 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
2371 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
2372 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
2373 n->brightness_toggle =
2374 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
2375 }
2376 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
2377 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
2378 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
2379 >> TP_NVRAM_POS_LEVEL_VOLUME;
2380 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2381 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2382 }
2383}
2384
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002385static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
2386 struct tp_nvram_state *newn,
2387 const u32 event_mask)
2388{
2389
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002390#define TPACPI_COMPARE_KEY(__scancode, __member) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002391 do { \
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002392 if ((event_mask & (1 << __scancode)) && \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002393 oldn->__member != newn->__member) \
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002394 tpacpi_hotkey_send_key(__scancode); \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002395 } while (0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002396
2397#define TPACPI_MAY_SEND_KEY(__scancode) \
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002398 do { \
2399 if (event_mask & (1 << __scancode)) \
2400 tpacpi_hotkey_send_key(__scancode); \
2401 } while (0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002402
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002403 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2404 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2405 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2406 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2407
2408 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2409
2410 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2411
2412 /* handle volume */
2413 if (oldn->volume_toggle != newn->volume_toggle) {
2414 if (oldn->mute != newn->mute) {
2415 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2416 }
2417 if (oldn->volume_level > newn->volume_level) {
2418 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2419 } else if (oldn->volume_level < newn->volume_level) {
2420 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2421 } else if (oldn->mute == newn->mute) {
2422 /* repeated key presses that didn't change state */
2423 if (newn->mute) {
2424 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2425 } else if (newn->volume_level != 0) {
2426 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2427 } else {
2428 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2429 }
2430 }
2431 }
2432
2433 /* handle brightness */
2434 if (oldn->brightness_toggle != newn->brightness_toggle) {
2435 if (oldn->brightness_level < newn->brightness_level) {
2436 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2437 } else if (oldn->brightness_level > newn->brightness_level) {
2438 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2439 } else {
2440 /* repeated key presses that didn't change state */
2441 if (newn->brightness_level != 0) {
2442 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2443 } else {
2444 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2445 }
2446 }
2447 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002448
2449#undef TPACPI_COMPARE_KEY
2450#undef TPACPI_MAY_SEND_KEY
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002451}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002452
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002453/*
2454 * Polling driver
2455 *
2456 * We track all events in hotkey_source_mask all the time, since
2457 * most of them are edge-based. We only issue those requested by
2458 * hotkey_user_mask or hotkey_driver_mask, though.
2459 */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002460static int hotkey_kthread(void *data)
2461{
2462 struct tp_nvram_state s[2];
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002463 u32 poll_mask, event_mask;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002464 unsigned int si, so;
2465 unsigned long t;
2466 unsigned int change_detector, must_reset;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002467 unsigned int poll_freq;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002468
2469 mutex_lock(&hotkey_thread_mutex);
2470
2471 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2472 goto exit;
2473
2474 set_freezable();
2475
2476 so = 0;
2477 si = 1;
2478 t = 0;
2479
2480 /* Initial state for compares */
2481 mutex_lock(&hotkey_thread_data_mutex);
2482 change_detector = hotkey_config_change;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002483 poll_mask = hotkey_source_mask;
2484 event_mask = hotkey_source_mask &
2485 (hotkey_driver_mask | hotkey_user_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002486 poll_freq = hotkey_poll_freq;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002487 mutex_unlock(&hotkey_thread_data_mutex);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002488 hotkey_read_nvram(&s[so], poll_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002489
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002490 while (!kthread_should_stop()) {
2491 if (t == 0) {
2492 if (likely(poll_freq))
2493 t = 1000/poll_freq;
2494 else
2495 t = 100; /* should never happen... */
2496 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002497 t = msleep_interruptible(t);
2498 if (unlikely(kthread_should_stop()))
2499 break;
2500 must_reset = try_to_freeze();
2501 if (t > 0 && !must_reset)
2502 continue;
2503
2504 mutex_lock(&hotkey_thread_data_mutex);
2505 if (must_reset || hotkey_config_change != change_detector) {
2506 /* forget old state on thaw or config change */
2507 si = so;
2508 t = 0;
2509 change_detector = hotkey_config_change;
2510 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002511 poll_mask = hotkey_source_mask;
2512 event_mask = hotkey_source_mask &
2513 (hotkey_driver_mask | hotkey_user_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002514 poll_freq = hotkey_poll_freq;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002515 mutex_unlock(&hotkey_thread_data_mutex);
2516
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002517 if (likely(poll_mask)) {
2518 hotkey_read_nvram(&s[si], poll_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002519 if (likely(si != so)) {
2520 hotkey_compare_and_issue_event(&s[so], &s[si],
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002521 event_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002522 }
2523 }
2524
2525 so = si;
2526 si ^= 1;
2527 }
2528
2529exit:
2530 mutex_unlock(&hotkey_thread_mutex);
2531 return 0;
2532}
2533
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002534/* call with hotkey_mutex held */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002535static void hotkey_poll_stop_sync(void)
2536{
2537 if (tpacpi_hotkey_task) {
2538 if (frozen(tpacpi_hotkey_task) ||
2539 freezing(tpacpi_hotkey_task))
2540 thaw_process(tpacpi_hotkey_task);
2541
2542 kthread_stop(tpacpi_hotkey_task);
2543 tpacpi_hotkey_task = NULL;
2544 mutex_lock(&hotkey_thread_mutex);
2545 /* at this point, the thread did exit */
2546 mutex_unlock(&hotkey_thread_mutex);
2547 }
2548}
2549
2550/* call with hotkey_mutex held */
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002551static void hotkey_poll_setup(bool may_warn)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002552{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002553 const u32 poll_driver_mask = hotkey_driver_mask & hotkey_source_mask;
2554 const u32 poll_user_mask = hotkey_user_mask & hotkey_source_mask;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002555
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002556 if (hotkey_poll_freq > 0 &&
2557 (poll_driver_mask ||
2558 (poll_user_mask && tpacpi_inputdev->users > 0))) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002559 if (!tpacpi_hotkey_task) {
2560 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03002561 NULL, TPACPI_NVRAM_KTHREAD_NAME);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002562 if (IS_ERR(tpacpi_hotkey_task)) {
2563 tpacpi_hotkey_task = NULL;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002564 printk(TPACPI_ERR
2565 "could not create kernel thread "
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002566 "for hotkey polling\n");
2567 }
2568 }
2569 } else {
2570 hotkey_poll_stop_sync();
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002571 if (may_warn && (poll_driver_mask || poll_user_mask) &&
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002572 hotkey_poll_freq == 0) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002573 printk(TPACPI_NOTICE
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002574 "hot keys 0x%08x and/or events 0x%08x "
2575 "require polling, which is currently "
2576 "disabled\n",
2577 poll_user_mask, poll_driver_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002578 }
2579 }
2580}
2581
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002582static void hotkey_poll_setup_safe(bool may_warn)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002583{
2584 mutex_lock(&hotkey_mutex);
2585 hotkey_poll_setup(may_warn);
2586 mutex_unlock(&hotkey_mutex);
2587}
2588
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002589/* call with hotkey_mutex held */
2590static void hotkey_poll_set_freq(unsigned int freq)
2591{
2592 if (!freq)
2593 hotkey_poll_stop_sync();
2594
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002595 hotkey_poll_freq = freq;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002596}
2597
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02002598#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2599
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002600static void hotkey_poll_setup_safe(bool __unused)
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02002601{
2602}
2603
2604#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2605
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002606static int hotkey_inputdev_open(struct input_dev *dev)
2607{
2608 switch (tpacpi_lifecycle) {
2609 case TPACPI_LIFE_INIT:
2610 /*
2611 * hotkey_init will call hotkey_poll_setup_safe
2612 * at the appropriate moment
2613 */
2614 return 0;
2615 case TPACPI_LIFE_EXITING:
2616 return -EBUSY;
2617 case TPACPI_LIFE_RUNNING:
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002618 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002619 return 0;
2620 }
2621
2622 /* Should only happen if tpacpi_lifecycle is corrupt */
2623 BUG();
2624 return -EBUSY;
2625}
2626
2627static void hotkey_inputdev_close(struct input_dev *dev)
2628{
2629 /* disable hotkey polling when possible */
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002630 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING &&
2631 !(hotkey_source_mask & hotkey_driver_mask))
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002632 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002633}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002634
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002635/* sysfs hotkey enable ------------------------------------------------- */
2636static ssize_t hotkey_enable_show(struct device *dev,
2637 struct device_attribute *attr,
2638 char *buf)
2639{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002640 int res, status;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002641
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002642 printk_deprecated_attribute("hotkey_enable",
2643 "Hotkey reporting is always enabled");
2644
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002645 res = hotkey_status_get(&status);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002646 if (res)
2647 return res;
2648
2649 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2650}
2651
2652static ssize_t hotkey_enable_store(struct device *dev,
2653 struct device_attribute *attr,
2654 const char *buf, size_t count)
2655{
2656 unsigned long t;
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002657
2658 printk_deprecated_attribute("hotkey_enable",
2659 "Hotkeys can be disabled through hotkey_mask");
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002660
2661 if (parse_strtoul(buf, 1, &t))
2662 return -EINVAL;
2663
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002664 if (t == 0)
2665 return -EPERM;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002666
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002667 return count;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002668}
2669
2670static struct device_attribute dev_attr_hotkey_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002671 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002672 hotkey_enable_show, hotkey_enable_store);
2673
2674/* sysfs hotkey mask --------------------------------------------------- */
2675static ssize_t hotkey_mask_show(struct device *dev,
2676 struct device_attribute *attr,
2677 char *buf)
2678{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002679 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_user_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002680}
2681
2682static ssize_t hotkey_mask_store(struct device *dev,
2683 struct device_attribute *attr,
2684 const char *buf, size_t count)
2685{
2686 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002687 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002688
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002689 if (parse_strtoul(buf, 0xffffffffUL, &t))
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002690 return -EINVAL;
2691
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002692 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002693 return -ERESTARTSYS;
2694
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002695 res = hotkey_user_mask_set(t);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002696
2697#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002698 hotkey_poll_setup(true);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002699#endif
2700
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002701 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002702
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002703 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2704
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002705 return (res) ? res : count;
2706}
2707
2708static struct device_attribute dev_attr_hotkey_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002709 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002710 hotkey_mask_show, hotkey_mask_store);
2711
2712/* sysfs hotkey bios_enabled ------------------------------------------- */
2713static ssize_t hotkey_bios_enabled_show(struct device *dev,
2714 struct device_attribute *attr,
2715 char *buf)
2716{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002717 return sprintf(buf, "0\n");
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002718}
2719
2720static struct device_attribute dev_attr_hotkey_bios_enabled =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002721 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002722
2723/* sysfs hotkey bios_mask ---------------------------------------------- */
2724static ssize_t hotkey_bios_mask_show(struct device *dev,
2725 struct device_attribute *attr,
2726 char *buf)
2727{
Henrique de Moraes Holschuh06777be2009-09-12 15:22:15 -03002728 printk_deprecated_attribute("hotkey_bios_mask",
2729 "This attribute is useless.");
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002730 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002731}
2732
2733static struct device_attribute dev_attr_hotkey_bios_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002734 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002735
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002736/* sysfs hotkey all_mask ----------------------------------------------- */
2737static ssize_t hotkey_all_mask_show(struct device *dev,
2738 struct device_attribute *attr,
2739 char *buf)
2740{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002741 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2742 hotkey_all_mask | hotkey_source_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002743}
2744
2745static struct device_attribute dev_attr_hotkey_all_mask =
2746 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
2747
2748/* sysfs hotkey recommended_mask --------------------------------------- */
2749static ssize_t hotkey_recommended_mask_show(struct device *dev,
2750 struct device_attribute *attr,
2751 char *buf)
2752{
2753 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002754 (hotkey_all_mask | hotkey_source_mask)
2755 & ~hotkey_reserved_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002756}
2757
2758static struct device_attribute dev_attr_hotkey_recommended_mask =
2759 __ATTR(hotkey_recommended_mask, S_IRUGO,
2760 hotkey_recommended_mask_show, NULL);
2761
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002762#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2763
2764/* sysfs hotkey hotkey_source_mask ------------------------------------- */
2765static ssize_t hotkey_source_mask_show(struct device *dev,
2766 struct device_attribute *attr,
2767 char *buf)
2768{
2769 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2770}
2771
2772static ssize_t hotkey_source_mask_store(struct device *dev,
2773 struct device_attribute *attr,
2774 const char *buf, size_t count)
2775{
2776 unsigned long t;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002777 u32 r_ev;
2778 int rc;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002779
2780 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2781 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2782 return -EINVAL;
2783
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002784 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002785 return -ERESTARTSYS;
2786
2787 HOTKEY_CONFIG_CRITICAL_START
2788 hotkey_source_mask = t;
2789 HOTKEY_CONFIG_CRITICAL_END
2790
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002791 rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
2792 ~hotkey_source_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002793 hotkey_poll_setup(true);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002794
2795 /* check if events needed by the driver got disabled */
2796 r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
2797 & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002798
2799 mutex_unlock(&hotkey_mutex);
2800
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002801 if (rc < 0)
2802 printk(TPACPI_ERR "hotkey_source_mask: failed to update the"
2803 "firmware event mask!\n");
2804
2805 if (r_ev)
2806 printk(TPACPI_NOTICE "hotkey_source_mask: "
2807 "some important events were disabled: "
2808 "0x%04x\n", r_ev);
2809
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002810 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2811
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002812 return (rc < 0) ? rc : count;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002813}
2814
2815static struct device_attribute dev_attr_hotkey_source_mask =
2816 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2817 hotkey_source_mask_show, hotkey_source_mask_store);
2818
2819/* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2820static ssize_t hotkey_poll_freq_show(struct device *dev,
2821 struct device_attribute *attr,
2822 char *buf)
2823{
2824 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2825}
2826
2827static ssize_t hotkey_poll_freq_store(struct device *dev,
2828 struct device_attribute *attr,
2829 const char *buf, size_t count)
2830{
2831 unsigned long t;
2832
2833 if (parse_strtoul(buf, 25, &t))
2834 return -EINVAL;
2835
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002836 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002837 return -ERESTARTSYS;
2838
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002839 hotkey_poll_set_freq(t);
2840 hotkey_poll_setup(true);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002841
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002842 mutex_unlock(&hotkey_mutex);
2843
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002844 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2845
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002846 return count;
2847}
2848
2849static struct device_attribute dev_attr_hotkey_poll_freq =
2850 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2851 hotkey_poll_freq_show, hotkey_poll_freq_store);
2852
2853#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2854
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002855/* sysfs hotkey radio_sw (pollable) ------------------------------------ */
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002856static ssize_t hotkey_radio_sw_show(struct device *dev,
2857 struct device_attribute *attr,
2858 char *buf)
2859{
Johannes Berg19d337d2009-06-02 13:01:37 +02002860 int res;
2861 res = hotkey_get_wlsw();
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002862 if (res < 0)
2863 return res;
2864
Johannes Berg19d337d2009-06-02 13:01:37 +02002865 /* Opportunistic update */
2866 tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
2867
2868 return snprintf(buf, PAGE_SIZE, "%d\n",
2869 (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002870}
2871
2872static struct device_attribute dev_attr_hotkey_radio_sw =
2873 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2874
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002875static void hotkey_radio_sw_notify_change(void)
2876{
2877 if (tp_features.hotkey_wlsw)
2878 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2879 "hotkey_radio_sw");
2880}
2881
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002882/* sysfs hotkey tablet mode (pollable) --------------------------------- */
2883static ssize_t hotkey_tablet_mode_show(struct device *dev,
2884 struct device_attribute *attr,
2885 char *buf)
2886{
2887 int res, s;
2888 res = hotkey_get_tablet_mode(&s);
2889 if (res < 0)
2890 return res;
2891
2892 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2893}
2894
2895static struct device_attribute dev_attr_hotkey_tablet_mode =
2896 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2897
2898static void hotkey_tablet_mode_notify_change(void)
2899{
2900 if (tp_features.hotkey_tablet)
2901 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2902 "hotkey_tablet_mode");
2903}
2904
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002905/* sysfs hotkey report_mode -------------------------------------------- */
2906static ssize_t hotkey_report_mode_show(struct device *dev,
2907 struct device_attribute *attr,
2908 char *buf)
2909{
2910 return snprintf(buf, PAGE_SIZE, "%d\n",
2911 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2912}
2913
2914static struct device_attribute dev_attr_hotkey_report_mode =
2915 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2916
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002917/* sysfs wakeup reason (pollable) -------------------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002918static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2919 struct device_attribute *attr,
2920 char *buf)
2921{
2922 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2923}
2924
2925static struct device_attribute dev_attr_hotkey_wakeup_reason =
2926 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2927
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02002928static void hotkey_wakeup_reason_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002929{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002930 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2931 "wakeup_reason");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002932}
2933
2934/* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002935static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2936 struct device_attribute *attr,
2937 char *buf)
2938{
2939 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2940}
2941
2942static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2943 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2944 hotkey_wakeup_hotunplug_complete_show, NULL);
2945
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02002946static void hotkey_wakeup_hotunplug_complete_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002947{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002948 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2949 "wakeup_hotunplug_complete");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002950}
2951
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002952/* --------------------------------------------------------------------- */
2953
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002954static struct attribute *hotkey_attributes[] __initdata = {
2955 &dev_attr_hotkey_enable.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002956 &dev_attr_hotkey_bios_enabled.attr,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002957 &dev_attr_hotkey_bios_mask.attr,
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002958 &dev_attr_hotkey_report_mode.attr,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002959 &dev_attr_hotkey_wakeup_reason.attr,
2960 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002961 &dev_attr_hotkey_mask.attr,
2962 &dev_attr_hotkey_all_mask.attr,
2963 &dev_attr_hotkey_recommended_mask.attr,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002964#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002965 &dev_attr_hotkey_source_mask.attr,
2966 &dev_attr_hotkey_poll_freq.attr,
2967#endif
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002968};
2969
Johannes Berg19d337d2009-06-02 13:01:37 +02002970/*
2971 * Sync both the hw and sw blocking state of all switches
2972 */
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002973static void tpacpi_send_radiosw_update(void)
2974{
2975 int wlsw;
2976
Johannes Berg19d337d2009-06-02 13:01:37 +02002977 /*
2978 * We must sync all rfkill controllers *before* issuing any
2979 * rfkill input events, or we will race the rfkill core input
2980 * handler.
2981 *
2982 * tpacpi_inputdev_send_mutex works as a syncronization point
2983 * for the above.
2984 *
2985 * We optimize to avoid numerous calls to hotkey_get_wlsw.
2986 */
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002987
Johannes Berg19d337d2009-06-02 13:01:37 +02002988 wlsw = hotkey_get_wlsw();
2989
2990 /* Sync hw blocking state first if it is hw-blocked */
2991 if (wlsw == TPACPI_RFK_RADIO_OFF)
2992 tpacpi_rfk_update_hwblock_state(true);
2993
2994 /* Sync sw blocking state */
2995 tpacpi_rfk_update_swstate_all();
2996
2997 /* Sync hw blocking state last if it is hw-unblocked */
2998 if (wlsw == TPACPI_RFK_RADIO_ON)
2999 tpacpi_rfk_update_hwblock_state(false);
3000
3001 /* Issue rfkill input event for WLSW switch */
3002 if (!(wlsw < 0)) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003003 mutex_lock(&tpacpi_inputdev_send_mutex);
3004
3005 input_report_switch(tpacpi_inputdev,
Johannes Berg19d337d2009-06-02 13:01:37 +02003006 SW_RFKILL_ALL, (wlsw > 0));
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003007 input_sync(tpacpi_inputdev);
3008
3009 mutex_unlock(&tpacpi_inputdev_send_mutex);
3010 }
Johannes Berg19d337d2009-06-02 13:01:37 +02003011
3012 /*
3013 * this can be unconditional, as we will poll state again
3014 * if userspace uses the notify to read data
3015 */
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003016 hotkey_radio_sw_notify_change();
3017}
3018
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003019static void hotkey_exit(void)
3020{
3021#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003022 mutex_lock(&hotkey_mutex);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003023 hotkey_poll_stop_sync();
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003024 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003025#endif
3026
3027 if (hotkey_dev_attributes)
3028 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3029
3030 kfree(hotkey_keycode_map);
3031
Henrique de Moraes Holschuh4be73002009-09-20 14:09:23 -03003032 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003033 "restoring original HKEY status and mask\n");
3034 /* yes, there is a bitwise or below, we want the
3035 * functions to be called even if one of them fail */
3036 if (((tp_features.hotkey_mask &&
3037 hotkey_mask_set(hotkey_orig_mask)) |
3038 hotkey_status_set(false)) != 0)
Henrique de Moraes Holschuh4be73002009-09-20 14:09:23 -03003039 printk(TPACPI_ERR
3040 "failed to restore hot key mask "
3041 "to BIOS defaults\n");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003042}
3043
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003044static void __init hotkey_unmap(const unsigned int scancode)
3045{
3046 if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
3047 clear_bit(hotkey_keycode_map[scancode],
3048 tpacpi_inputdev->keybit);
3049 hotkey_keycode_map[scancode] = KEY_RESERVED;
3050 }
3051}
3052
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003053/*
3054 * HKEY quirks:
3055 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
3056 */
3057
3058#define TPACPI_HK_Q_INIMASK 0x0001
3059
3060static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
3061 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
3062 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
3063 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
3064 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
3065 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
3066 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
3067 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
3068 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
3069 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
3070 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
3071 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
3072 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
3073 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
3074 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
3075 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
3076 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
3077 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
3078 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
3079 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
3080};
3081
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003082static int __init hotkey_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003083{
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003084 /* Requirements for changing the default keymaps:
3085 *
3086 * 1. Many of the keys are mapped to KEY_RESERVED for very
3087 * good reasons. Do not change them unless you have deep
3088 * knowledge on the IBM and Lenovo ThinkPad firmware for
3089 * the various ThinkPad models. The driver behaves
3090 * differently for KEY_RESERVED: such keys have their
3091 * hot key mask *unset* in mask_recommended, and also
3092 * in the initial hot key mask programmed into the
3093 * firmware at driver load time, which means the firm-
3094 * ware may react very differently if you change them to
3095 * something else;
3096 *
3097 * 2. You must be subscribed to the linux-thinkpad and
3098 * ibm-acpi-devel mailing lists, and you should read the
3099 * list archives since 2007 if you want to change the
3100 * keymaps. This requirement exists so that you will
3101 * know the past history of problems with the thinkpad-
3102 * acpi driver keymaps, and also that you will be
3103 * listening to any bug reports;
3104 *
3105 * 3. Do not send thinkpad-acpi specific patches directly to
3106 * for merging, *ever*. Send them to the linux-acpi
3107 * mailinglist for comments. Merging is to be done only
3108 * through acpi-test and the ACPI maintainer.
3109 *
3110 * If the above is too much to ask, don't change the keymap.
3111 * Ask the thinkpad-acpi maintainer to do it, instead.
3112 */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003113 static u16 ibm_keycode_map[] __initdata = {
3114 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3115 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
3116 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3117 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003118
3119 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003120 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3121 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3122 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003123
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003124 /* brightness: firmware always reacts to them */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003125 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003126 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003127
3128 /* Thinklight: firmware always react to it */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003129 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003130
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003131 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3132 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003133
3134 /* Volume: firmware always react to it and reprograms
3135 * the built-in *extra* mixer. Never map it to control
3136 * another mixer by default. */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003137 KEY_RESERVED, /* 0x14: VOLUME UP */
3138 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3139 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003140
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003141 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003142
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003143 /* (assignments unknown, please report if found) */
3144 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3145 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3146 };
3147 static u16 lenovo_keycode_map[] __initdata = {
3148 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3149 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
3150 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3151 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003152
3153 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003154 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3155 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3156 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003157
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003158 /* These should be enabled --only-- when ACPI video
3159 * is disabled (i.e. in "vendor" mode), and are handled
3160 * in a special way by the init code */
3161 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
3162 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003163
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003164 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003165
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003166 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3167 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003168
3169 /* Volume: z60/z61, T60 (BIOS version?): firmware always
3170 * react to it and reprograms the built-in *extra* mixer.
3171 * Never map it to control another mixer by default.
3172 *
3173 * T60?, T61, R60?, R61: firmware and EC tries to send
3174 * these over the regular keyboard, so these are no-ops,
3175 * but there are still weird bugs re. MUTE, so do not
3176 * change unless you get test reports from all Lenovo
3177 * models. May cause the BIOS to interfere with the
3178 * HDA mixer.
3179 */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003180 KEY_RESERVED, /* 0x14: VOLUME UP */
3181 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3182 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003183
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003184 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003185
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003186 /* (assignments unknown, please report if found) */
3187 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3188 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3189 };
3190
3191#define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
3192#define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
3193#define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
3194
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003195 int res, i;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003196 int status;
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03003197 int hkeyv;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003198 bool radiosw_state = false;
3199 bool tabletsw_state = false;
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03003200
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003201 unsigned long quirks;
3202
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003203 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3204 "initializing hotkey subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003205
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003206 BUG_ON(!tpacpi_inputdev);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003207 BUG_ON(tpacpi_inputdev->open != NULL ||
3208 tpacpi_inputdev->close != NULL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003209
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003210 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003211 mutex_init(&hotkey_mutex);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003212
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003213#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3214 mutex_init(&hotkey_thread_mutex);
3215 mutex_init(&hotkey_thread_data_mutex);
3216#endif
3217
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003218 /* hotkey not supported on 570 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003219 tp_features.hotkey = hkey_handle != NULL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003220
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003221 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3222 "hotkeys are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003223 str_supported(tp_features.hotkey));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003224
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003225 if (!tp_features.hotkey)
3226 return 1;
3227
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003228 quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
3229 ARRAY_SIZE(tpacpi_hotkey_qtable));
3230
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03003231 tpacpi_disable_brightness_delay();
3232
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003233 /* MUST have enough space for all attributes to be added to
3234 * hotkey_dev_attributes */
3235 hotkey_dev_attributes = create_attr_set(
3236 ARRAY_SIZE(hotkey_attributes) + 2,
3237 NULL);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003238 if (!hotkey_dev_attributes)
3239 return -ENOMEM;
3240 res = add_many_to_attr_set(hotkey_dev_attributes,
3241 hotkey_attributes,
3242 ARRAY_SIZE(hotkey_attributes));
3243 if (res)
3244 goto err_exit;
3245
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003246 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003247 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
3248 for HKEY interface version 0x100 */
3249 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
3250 if ((hkeyv >> 8) != 1) {
3251 printk(TPACPI_ERR "unknown version of the "
3252 "HKEY interface: 0x%x\n", hkeyv);
3253 printk(TPACPI_ERR "please report this to %s\n",
3254 TPACPI_MAIL);
3255 } else {
3256 /*
3257 * MHKV 0x100 in A31, R40, R40e,
3258 * T4x, X31, and later
3259 */
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003260 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3261 "firmware HKEY interface version: 0x%x\n",
3262 hkeyv);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003263
3264 /* Paranoia check AND init hotkey_all_mask */
3265 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3266 "MHKA", "qd")) {
3267 printk(TPACPI_ERR
3268 "missing MHKA handler, "
3269 "please report this to %s\n",
3270 TPACPI_MAIL);
3271 /* Fallback: pre-init for FN+F3,F4,F12 */
3272 hotkey_all_mask = 0x080cU;
3273 } else {
3274 tp_features.hotkey_mask = 1;
3275 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003276 }
3277 }
3278
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003279 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3280 "hotkey masks are %s\n",
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003281 str_supported(tp_features.hotkey_mask));
3282
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003283 /* Init hotkey_all_mask if not initialized yet */
3284 if (!tp_features.hotkey_mask && !hotkey_all_mask &&
3285 (quirks & TPACPI_HK_Q_INIMASK))
3286 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003287
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003288 /* Init hotkey_acpi_mask and hotkey_orig_mask */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003289 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003290 /* hotkey_source_mask *must* be zero for
3291 * the first hotkey_mask_get to return hotkey_orig_mask */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003292 res = hotkey_mask_get();
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003293 if (res)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003294 goto err_exit;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003295
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003296 hotkey_orig_mask = hotkey_acpi_mask;
3297 } else {
3298 hotkey_orig_mask = hotkey_all_mask;
3299 hotkey_acpi_mask = hotkey_all_mask;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003300 }
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003301
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003302#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3303 if (dbg_wlswemul) {
3304 tp_features.hotkey_wlsw = 1;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003305 radiosw_state = !!tpacpi_wlsw_emulstate;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003306 printk(TPACPI_INFO
3307 "radio switch emulation enabled\n");
3308 } else
3309#endif
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003310 /* Not all thinkpads have a hardware radio switch */
3311 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
3312 tp_features.hotkey_wlsw = 1;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003313 radiosw_state = !!status;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003314 printk(TPACPI_INFO
3315 "radio switch found; radios are %s\n",
3316 enabled(status, 0));
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003317 }
3318 if (tp_features.hotkey_wlsw)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003319 res = add_to_attr_set(hotkey_dev_attributes,
3320 &dev_attr_hotkey_radio_sw.attr);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003321
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003322 /* For X41t, X60t, X61t Tablets... */
3323 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
3324 tp_features.hotkey_tablet = 1;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003325 tabletsw_state = !!(status & TP_HOTKEY_TABLET_MASK);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003326 printk(TPACPI_INFO
3327 "possible tablet mode switch found; "
3328 "ThinkPad in %s mode\n",
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003329 (tabletsw_state) ? "tablet" : "laptop");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003330 res = add_to_attr_set(hotkey_dev_attributes,
3331 &dev_attr_hotkey_tablet_mode.attr);
3332 }
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02003333
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003334 if (!res)
3335 res = register_attr_set_with_sysfs(
3336 hotkey_dev_attributes,
3337 &tpacpi_pdev->dev.kobj);
3338 if (res)
3339 goto err_exit;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003340
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003341 /* Set up key map */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003342
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003343 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
3344 GFP_KERNEL);
3345 if (!hotkey_keycode_map) {
3346 printk(TPACPI_ERR
3347 "failed to allocate memory for key map\n");
3348 res = -ENOMEM;
3349 goto err_exit;
3350 }
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003351
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003352 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003353 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003354 "using Lenovo default hot key map\n");
3355 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
3356 TPACPI_HOTKEY_MAP_SIZE);
3357 } else {
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003358 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003359 "using IBM default hot key map\n");
3360 memcpy(hotkey_keycode_map, &ibm_keycode_map,
3361 TPACPI_HOTKEY_MAP_SIZE);
3362 }
3363
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003364 input_set_capability(tpacpi_inputdev, EV_MSC, MSC_SCAN);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003365 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
3366 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3367 tpacpi_inputdev->keycode = hotkey_keycode_map;
3368 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3369 if (hotkey_keycode_map[i] != KEY_RESERVED) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003370 input_set_capability(tpacpi_inputdev, EV_KEY,
3371 hotkey_keycode_map[i]);
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003372 } else {
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003373 if (i < sizeof(hotkey_reserved_mask)*8)
3374 hotkey_reserved_mask |= 1 << i;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003375 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003376 }
3377
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003378 if (tp_features.hotkey_wlsw) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003379 input_set_capability(tpacpi_inputdev, EV_SW, SW_RFKILL_ALL);
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003380 input_report_switch(tpacpi_inputdev,
3381 SW_RFKILL_ALL, radiosw_state);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003382 }
3383 if (tp_features.hotkey_tablet) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003384 input_set_capability(tpacpi_inputdev, EV_SW, SW_TABLET_MODE);
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003385 input_report_switch(tpacpi_inputdev,
3386 SW_TABLET_MODE, tabletsw_state);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003387 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003388
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003389 /* Do not issue duplicate brightness change events to
3390 * userspace */
3391 if (!tp_features.bright_acpimode)
3392 /* update bright_acpimode... */
3393 tpacpi_check_std_acpi_brightness_support();
3394
Henrique de Moraes Holschuh8bf3d4c2009-05-30 13:25:09 -03003395 if (tp_features.bright_acpimode && acpi_video_backlight_support()) {
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003396 printk(TPACPI_INFO
3397 "This ThinkPad has standard ACPI backlight "
3398 "brightness control, supported by the ACPI "
3399 "video driver\n");
3400 printk(TPACPI_NOTICE
3401 "Disabling thinkpad-acpi brightness events "
3402 "by default...\n");
3403
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003404 /* Disable brightness up/down on Lenovo thinkpads when
3405 * ACPI is handling them, otherwise it is plain impossible
3406 * for userspace to do something even remotely sane */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003407 hotkey_reserved_mask |=
3408 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3409 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003410 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
3411 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003412 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003413
Henrique de Moraes Holschuh230d8cf252009-09-12 15:22:17 -03003414#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003415 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3416 & ~hotkey_all_mask
3417 & ~hotkey_reserved_mask;
Henrique de Moraes Holschuh230d8cf252009-09-12 15:22:17 -03003418
3419 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3420 "hotkey source mask 0x%08x, polling freq %u\n",
3421 hotkey_source_mask, hotkey_poll_freq);
3422#endif
3423
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003424 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3425 "enabling firmware HKEY event interface...\n");
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003426 res = hotkey_status_set(true);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003427 if (res) {
3428 hotkey_exit();
3429 return res;
3430 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003431 res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
3432 | hotkey_driver_mask)
3433 & ~hotkey_source_mask);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003434 if (res < 0 && res != -ENXIO) {
3435 hotkey_exit();
3436 return res;
3437 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003438 hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
3439 & ~hotkey_reserved_mask;
3440 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3441 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3442 hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003443
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003444 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3445 "legacy ibm/hotkey event reporting over procfs %s\n",
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003446 (hotkey_report_mode < 2) ?
3447 "enabled" : "disabled");
3448
3449 tpacpi_inputdev->open = &hotkey_inputdev_open;
3450 tpacpi_inputdev->close = &hotkey_inputdev_close;
3451
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003452 hotkey_poll_setup_safe(true);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003453
3454 return 0;
3455
3456err_exit:
3457 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3458 hotkey_dev_attributes = NULL;
3459
3460 return (res < 0)? res : 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003461}
3462
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003463static bool hotkey_notify_hotkey(const u32 hkey,
3464 bool *send_acpi_ev,
3465 bool *ignore_acpi_ev)
3466{
3467 /* 0x1000-0x1FFF: key presses */
3468 unsigned int scancode = hkey & 0xfff;
3469 *send_acpi_ev = true;
3470 *ignore_acpi_ev = false;
3471
3472 if (scancode > 0 && scancode < 0x21) {
3473 scancode--;
3474 if (!(hotkey_source_mask & (1 << scancode))) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003475 tpacpi_input_send_key_masked(scancode);
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003476 *send_acpi_ev = false;
3477 } else {
3478 *ignore_acpi_ev = true;
3479 }
3480 return true;
3481 }
3482 return false;
3483}
3484
3485static bool hotkey_notify_wakeup(const u32 hkey,
3486 bool *send_acpi_ev,
3487 bool *ignore_acpi_ev)
3488{
3489 /* 0x2000-0x2FFF: Wakeup reason */
3490 *send_acpi_ev = true;
3491 *ignore_acpi_ev = false;
3492
3493 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003494 case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */
3495 case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003496 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3497 *ignore_acpi_ev = true;
3498 break;
3499
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003500 case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */
3501 case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003502 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3503 *ignore_acpi_ev = true;
3504 break;
3505
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003506 case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */
3507 case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003508 printk(TPACPI_ALERT
3509 "EMERGENCY WAKEUP: battery almost empty\n");
3510 /* how to auto-heal: */
3511 /* 2313: woke up from S3, go to S4/S5 */
3512 /* 2413: woke up from S4, go to S5 */
3513 break;
3514
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003515 default:
3516 return false;
3517 }
3518
3519 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
3520 printk(TPACPI_INFO
3521 "woke up due to a hot-unplug "
3522 "request...\n");
3523 hotkey_wakeup_reason_notify_change();
3524 }
3525 return true;
3526}
3527
3528static bool hotkey_notify_usrevent(const u32 hkey,
3529 bool *send_acpi_ev,
3530 bool *ignore_acpi_ev)
3531{
3532 /* 0x5000-0x5FFF: human interface helpers */
3533 *send_acpi_ev = true;
3534 *ignore_acpi_ev = false;
3535
3536 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003537 case TP_HKEY_EV_PEN_INSERTED: /* X61t: tablet pen inserted into bay */
3538 case TP_HKEY_EV_PEN_REMOVED: /* X61t: tablet pen removed from bay */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003539 return true;
3540
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003541 case TP_HKEY_EV_TABLET_TABLET: /* X41t-X61t: tablet mode */
3542 case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003543 tpacpi_input_send_tabletsw();
3544 hotkey_tablet_mode_notify_change();
3545 *send_acpi_ev = false;
3546 return true;
3547
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003548 case TP_HKEY_EV_LID_CLOSE: /* Lid closed */
3549 case TP_HKEY_EV_LID_OPEN: /* Lid opened */
3550 case TP_HKEY_EV_BRGHT_CHANGED: /* brightness changed */
Henrique de Moraes Holschuh176dd982009-09-20 14:09:24 -03003551 /* do not propagate these events */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003552 *ignore_acpi_ev = true;
3553 return true;
3554
3555 default:
3556 return false;
3557 }
3558}
3559
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003560static void thermal_dump_all_sensors(void);
3561
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003562static bool hotkey_notify_thermal(const u32 hkey,
3563 bool *send_acpi_ev,
3564 bool *ignore_acpi_ev)
3565{
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003566 bool known = true;
3567
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003568 /* 0x6000-0x6FFF: thermal alarms */
3569 *send_acpi_ev = true;
3570 *ignore_acpi_ev = false;
3571
3572 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003573 case TP_HKEY_EV_THM_TABLE_CHANGED:
Henrique de Moraes Holschuh54926ce2009-01-11 03:01:09 -02003574 printk(TPACPI_INFO
3575 "EC reports that Thermal Table has changed\n");
3576 /* recommended action: do nothing, we don't have
3577 * Lenovo ATM information */
3578 return true;
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003579 case TP_HKEY_EV_ALARM_BAT_HOT:
3580 printk(TPACPI_CRIT
3581 "THERMAL ALARM: battery is too hot!\n");
3582 /* recommended action: warn user through gui */
3583 break;
3584 case TP_HKEY_EV_ALARM_BAT_XHOT:
3585 printk(TPACPI_ALERT
3586 "THERMAL EMERGENCY: battery is extremely hot!\n");
3587 /* recommended action: immediate sleep/hibernate */
3588 break;
3589 case TP_HKEY_EV_ALARM_SENSOR_HOT:
3590 printk(TPACPI_CRIT
3591 "THERMAL ALARM: "
3592 "a sensor reports something is too hot!\n");
3593 /* recommended action: warn user through gui, that */
3594 /* some internal component is too hot */
3595 break;
3596 case TP_HKEY_EV_ALARM_SENSOR_XHOT:
3597 printk(TPACPI_ALERT
3598 "THERMAL EMERGENCY: "
3599 "a sensor reports something is extremely hot!\n");
3600 /* recommended action: immediate sleep/hibernate */
3601 break;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003602 default:
3603 printk(TPACPI_ALERT
3604 "THERMAL ALERT: unknown thermal alarm received\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003605 known = false;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003606 }
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003607
3608 thermal_dump_all_sensors();
3609
3610 return known;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003611}
3612
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003613static void hotkey_notify(struct ibm_struct *ibm, u32 event)
3614{
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003615 u32 hkey;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003616 bool send_acpi_ev;
3617 bool ignore_acpi_ev;
3618 bool known_ev;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003619
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003620 if (event != 0x80) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003621 printk(TPACPI_ERR
3622 "unknown HKEY notification event %d\n", event);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003623 /* forward it to userspace, maybe it knows how to handle it */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003624 acpi_bus_generate_netlink_event(
3625 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02003626 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003627 event, 0);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003628 return;
3629 }
3630
3631 while (1) {
3632 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003633 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003634 return;
3635 }
3636
3637 if (hkey == 0) {
3638 /* queue empty */
3639 return;
3640 }
3641
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003642 send_acpi_ev = true;
3643 ignore_acpi_ev = false;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003644
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003645 switch (hkey >> 12) {
3646 case 1:
3647 /* 0x1000-0x1FFF: key presses */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003648 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
3649 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003650 break;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003651 case 2:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003652 /* 0x2000-0x2FFF: Wakeup reason */
3653 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
3654 &ignore_acpi_ev);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003655 break;
3656 case 3:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003657 /* 0x3000-0x3FFF: bay-related wakeups */
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003658 if (hkey == TP_HKEY_EV_BAYEJ_ACK) {
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003659 hotkey_autosleep_ack = 1;
3660 printk(TPACPI_INFO
3661 "bay ejected\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003662 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003663 known_ev = true;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003664 } else {
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003665 known_ev = false;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003666 }
3667 break;
3668 case 4:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003669 /* 0x4000-0x4FFF: dock-related wakeups */
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003670 if (hkey == TP_HKEY_EV_UNDOCK_ACK) {
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003671 hotkey_autosleep_ack = 1;
3672 printk(TPACPI_INFO
3673 "undocked\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003674 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003675 known_ev = true;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003676 } else {
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003677 known_ev = false;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003678 }
3679 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003680 case 5:
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02003681 /* 0x5000-0x5FFF: human interface helpers */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003682 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
3683 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003684 break;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003685 case 6:
3686 /* 0x6000-0x6FFF: thermal alarms */
3687 known_ev = hotkey_notify_thermal(hkey, &send_acpi_ev,
3688 &ignore_acpi_ev);
3689 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003690 case 7:
3691 /* 0x7000-0x7FFF: misc */
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003692 if (tp_features.hotkey_wlsw &&
3693 hkey == TP_HKEY_EV_RFKILL_CHANGED) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003694 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02003695 send_acpi_ev = 0;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003696 known_ev = true;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003697 break;
3698 }
3699 /* fallthrough to default */
3700 default:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003701 known_ev = false;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02003702 }
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003703 if (!known_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003704 printk(TPACPI_NOTICE
3705 "unhandled HKEY event 0x%04x\n", hkey);
Henrique de Moraes Holschuhcb429352009-01-11 03:01:07 -02003706 printk(TPACPI_NOTICE
3707 "please report the conditions when this "
3708 "event happened to %s\n", TPACPI_MAIL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003709 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003710
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003711 /* Legacy events */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003712 if (!ignore_acpi_ev &&
3713 (send_acpi_ev || hotkey_report_mode < 2)) {
3714 acpi_bus_generate_proc_event(ibm->acpi->device,
3715 event, hkey);
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03003716 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003717
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003718 /* netlink events */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03003719 if (!ignore_acpi_ev && send_acpi_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003720 acpi_bus_generate_netlink_event(
3721 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02003722 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003723 event, hkey);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003724 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003725 }
3726}
3727
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003728static void hotkey_suspend(pm_message_t state)
3729{
3730 /* Do these on suspend, we get the events on early resume! */
3731 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
3732 hotkey_autosleep_ack = 0;
3733}
3734
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03003735static void hotkey_resume(void)
3736{
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03003737 tpacpi_disable_brightness_delay();
3738
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003739 if (hotkey_status_set(true) < 0 ||
3740 hotkey_mask_set(hotkey_acpi_mask) < 0)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003741 printk(TPACPI_ERR
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003742 "error while attempting to reset the event "
3743 "firmware interface\n");
3744
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003745 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02003746 hotkey_tablet_mode_notify_change();
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003747 hotkey_wakeup_reason_notify_change();
3748 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003749 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03003750}
3751
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003752/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003753static int hotkey_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03003755 int res, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003757 if (!tp_features.hotkey) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003758 seq_printf(m, "status:\t\tnot supported\n");
3759 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003760 }
3761
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02003762 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02003763 return -ERESTARTSYS;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02003764 res = hotkey_status_get(&status);
3765 if (!res)
3766 res = hotkey_mask_get();
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003767 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03003768 if (res)
3769 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003771 seq_printf(m, "status:\t\t%s\n", enabled(status, 0));
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003772 if (hotkey_all_mask) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003773 seq_printf(m, "mask:\t\t0x%08x\n", hotkey_user_mask);
3774 seq_printf(m, "commands:\tenable, disable, reset, <mask>\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775 } else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003776 seq_printf(m, "mask:\t\tnot supported\n");
3777 seq_printf(m, "commands:\tenable, disable, reset\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778 }
3779
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003780 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781}
3782
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003783static void hotkey_enabledisable_warn(bool enable)
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003784{
3785 tpacpi_log_usertask("procfs hotkey enable/disable");
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003786 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
3787 TPACPI_WARN
3788 "hotkey enable/disable functionality has been "
3789 "removed from the driver. Hotkeys are always "
3790 "enabled\n"))
3791 printk(TPACPI_ERR
3792 "Please remove the hotkey=enable module "
3793 "parameter, it is deprecated. Hotkeys are always "
3794 "enabled\n");
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003795}
3796
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003797static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003799 int res;
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03003800 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003803 if (!tp_features.hotkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804 return -ENODEV;
3805
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02003806 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02003807 return -ERESTARTSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003808
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003809 mask = hotkey_user_mask;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003810
3811 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812 while ((cmd = next_cmd(&buf))) {
3813 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003814 hotkey_enabledisable_warn(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003816 hotkey_enabledisable_warn(0);
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003817 res = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818 } else if (strlencmp(cmd, "reset") == 0) {
Henrique de Moraes Holschuh20c9aa42009-09-12 15:22:16 -03003819 mask = (hotkey_all_mask | hotkey_source_mask)
3820 & ~hotkey_reserved_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
3822 /* mask set */
3823 } else if (sscanf(cmd, "%x", &mask) == 1) {
3824 /* mask set */
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003825 } else {
3826 res = -EINVAL;
3827 goto errexit;
3828 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829 }
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003830
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003831 if (!res) {
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003832 tpacpi_disclose_usertask("procfs hotkey",
3833 "set mask to 0x%08x\n", mask);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003834 res = hotkey_user_mask_set(mask);
3835 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003837errexit:
3838 mutex_unlock(&hotkey_mutex);
3839 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003840}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003841
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003842static const struct acpi_device_id ibm_htk_device_ids[] = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003843 {TPACPI_ACPI_HKEY_HID, 0},
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003844 {"", 0},
3845};
3846
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003847static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003848 .hid = ibm_htk_device_ids,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003849 .notify = hotkey_notify,
3850 .handle = &hkey_handle,
3851 .type = ACPI_DEVICE_NOTIFY,
3852};
3853
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003854static struct ibm_struct hotkey_driver_data = {
3855 .name = "hotkey",
3856 .read = hotkey_read,
3857 .write = hotkey_write,
3858 .exit = hotkey_exit,
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03003859 .resume = hotkey_resume,
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003860 .suspend = hotkey_suspend,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003861 .acpi = &ibm_hotkey_acpidriver,
3862};
3863
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003864/*************************************************************************
3865 * Bluetooth subdriver
3866 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003868enum {
3869 /* ACPI GBDC/SBDC bits */
3870 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
3871 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003872 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
3873 off / last state */
3874};
3875
3876enum {
3877 /* ACPI \BLTH commands */
3878 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
3879 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
3880 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
3881 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
3882 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003883};
3884
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003885#define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
3886
Johannes Berg19d337d2009-06-02 13:01:37 +02003887static int bluetooth_get_status(void)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003888{
3889 int status;
3890
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003891#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3892 if (dbg_bluetoothemul)
3893 return (tpacpi_bluetooth_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02003894 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003895#endif
3896
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003897 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
3898 return -EIO;
3899
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003900 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02003901 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003902}
3903
Johannes Berg19d337d2009-06-02 13:01:37 +02003904static int bluetooth_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003905{
3906 int status;
3907
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003908 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02003909 "will attempt to %s bluetooth\n",
3910 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003911
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003912#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3913 if (dbg_bluetoothemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02003914 tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003915 return 0;
3916 }
3917#endif
3918
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003919 /* We make sure to keep TP_ACPI_BLUETOOTH_RESUMECTRL off */
Henrique de Moraes Holschuh208b9962009-12-09 01:36:22 +00003920 status = TP_ACPI_BLUETOOTH_RESUMECTRL;
Johannes Berg19d337d2009-06-02 13:01:37 +02003921 if (state == TPACPI_RFK_RADIO_ON)
Henrique de Moraes Holschuh208b9962009-12-09 01:36:22 +00003922 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
Johannes Berg19d337d2009-06-02 13:01:37 +02003923
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003924 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
3925 return -EIO;
3926
3927 return 0;
3928}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003929
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003930/* sysfs bluetooth enable ---------------------------------------------- */
3931static ssize_t bluetooth_enable_show(struct device *dev,
3932 struct device_attribute *attr,
3933 char *buf)
3934{
Johannes Berg19d337d2009-06-02 13:01:37 +02003935 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID,
3936 attr, buf);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003937}
3938
3939static ssize_t bluetooth_enable_store(struct device *dev,
3940 struct device_attribute *attr,
3941 const char *buf, size_t count)
3942{
Johannes Berg19d337d2009-06-02 13:01:37 +02003943 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID,
3944 attr, buf, count);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003945}
3946
3947static struct device_attribute dev_attr_bluetooth_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03003948 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003949 bluetooth_enable_show, bluetooth_enable_store);
3950
3951/* --------------------------------------------------------------------- */
3952
3953static struct attribute *bluetooth_attributes[] = {
3954 &dev_attr_bluetooth_enable.attr,
3955 NULL
3956};
3957
3958static const struct attribute_group bluetooth_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003959 .attrs = bluetooth_attributes,
3960};
3961
Johannes Berg19d337d2009-06-02 13:01:37 +02003962static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = {
3963 .get_status = bluetooth_get_status,
3964 .set_status = bluetooth_set_status,
3965};
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003966
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003967static void bluetooth_shutdown(void)
3968{
3969 /* Order firmware to save current state to NVRAM */
3970 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3971 TP_ACPI_BLTH_SAVE_STATE))
3972 printk(TPACPI_NOTICE
3973 "failed to save bluetooth state to NVRAM\n");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003974 else
3975 vdbg_printk(TPACPI_DBG_RFKILL,
3976 "bluestooth state saved to NVRAM\n");
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003977}
3978
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003979static void bluetooth_exit(void)
3980{
3981 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3982 &bluetooth_attr_group);
Johannes Berg19d337d2009-06-02 13:01:37 +02003983
3984 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
3985
3986 bluetooth_shutdown();
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003987}
3988
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003989static int __init bluetooth_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003991 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003992 int status = 0;
3993
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003994 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3995 "initializing bluetooth subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003996
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003997 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003998
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003999 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4000 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004001 tp_features.bluetooth = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004002 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004004 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4005 "bluetooth is %s, status 0x%02x\n",
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004006 str_supported(tp_features.bluetooth),
4007 status);
4008
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004009#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4010 if (dbg_bluetoothemul) {
4011 tp_features.bluetooth = 1;
4012 printk(TPACPI_INFO
4013 "bluetooth switch emulation enabled\n");
4014 } else
4015#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004016 if (tp_features.bluetooth &&
4017 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
4018 /* no bluetooth hardware present in system */
4019 tp_features.bluetooth = 0;
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004020 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004021 "bluetooth hardware not installed\n");
4022 }
4023
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004024 if (!tp_features.bluetooth)
4025 return 1;
4026
Johannes Berg19d337d2009-06-02 13:01:37 +02004027 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
4028 &bluetooth_tprfk_ops,
4029 RFKILL_TYPE_BLUETOOTH,
4030 TPACPI_RFK_BLUETOOTH_SW_NAME,
4031 true);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004032 if (res)
4033 return res;
4034
Johannes Berg19d337d2009-06-02 13:01:37 +02004035 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4036 &bluetooth_attr_group);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004037 if (res) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004038 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004039 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004040 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004041
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004042 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043}
4044
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004045/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004046static int bluetooth_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047{
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004048 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049}
4050
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004051static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052{
Johannes Berg19d337d2009-06-02 13:01:37 +02004053 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054}
4055
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004056static struct ibm_struct bluetooth_driver_data = {
4057 .name = "bluetooth",
4058 .read = bluetooth_read,
4059 .write = bluetooth_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004060 .exit = bluetooth_exit,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004061 .shutdown = bluetooth_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004062};
4063
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004064/*************************************************************************
4065 * Wan subdriver
4066 */
4067
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004068enum {
4069 /* ACPI GWAN/SWAN bits */
4070 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
4071 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02004072 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
4073 off / last state */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004074};
4075
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004076#define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
4077
Johannes Berg19d337d2009-06-02 13:01:37 +02004078static int wan_get_status(void)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004079{
4080 int status;
4081
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004082#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4083 if (dbg_wwanemul)
4084 return (tpacpi_wwan_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004085 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004086#endif
4087
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004088 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
4089 return -EIO;
4090
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004091 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004092 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004093}
4094
Johannes Berg19d337d2009-06-02 13:01:37 +02004095static int wan_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004096{
4097 int status;
4098
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004099 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02004100 "will attempt to %s wwan\n",
4101 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004102
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004103#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4104 if (dbg_wwanemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004105 tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004106 return 0;
4107 }
4108#endif
4109
Henrique de Moraes Holschuh208b9962009-12-09 01:36:22 +00004110 /* We make sure to set TP_ACPI_WANCARD_RESUMECTRL */
4111 status = TP_ACPI_WANCARD_RESUMECTRL;
Johannes Berg19d337d2009-06-02 13:01:37 +02004112 if (state == TPACPI_RFK_RADIO_ON)
Henrique de Moraes Holschuh208b9962009-12-09 01:36:22 +00004113 status |= TP_ACPI_WANCARD_RADIOSSW;
Johannes Berg19d337d2009-06-02 13:01:37 +02004114
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004115 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
4116 return -EIO;
4117
4118 return 0;
4119}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004120
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004121/* sysfs wan enable ---------------------------------------------------- */
4122static ssize_t wan_enable_show(struct device *dev,
4123 struct device_attribute *attr,
4124 char *buf)
4125{
Johannes Berg19d337d2009-06-02 13:01:37 +02004126 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
4127 attr, buf);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004128}
4129
4130static ssize_t wan_enable_store(struct device *dev,
4131 struct device_attribute *attr,
4132 const char *buf, size_t count)
4133{
Johannes Berg19d337d2009-06-02 13:01:37 +02004134 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
4135 attr, buf, count);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004136}
4137
4138static struct device_attribute dev_attr_wan_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03004139 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004140 wan_enable_show, wan_enable_store);
4141
4142/* --------------------------------------------------------------------- */
4143
4144static struct attribute *wan_attributes[] = {
4145 &dev_attr_wan_enable.attr,
4146 NULL
4147};
4148
4149static const struct attribute_group wan_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004150 .attrs = wan_attributes,
4151};
4152
Johannes Berg19d337d2009-06-02 13:01:37 +02004153static const struct tpacpi_rfk_ops wan_tprfk_ops = {
4154 .get_status = wan_get_status,
4155 .set_status = wan_set_status,
4156};
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004157
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004158static void wan_shutdown(void)
4159{
4160 /* Order firmware to save current state to NVRAM */
4161 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
4162 TP_ACPI_WGSV_SAVE_STATE))
4163 printk(TPACPI_NOTICE
4164 "failed to save WWAN state to NVRAM\n");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004165 else
4166 vdbg_printk(TPACPI_DBG_RFKILL,
4167 "WWAN state saved to NVRAM\n");
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004168}
4169
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004170static void wan_exit(void)
4171{
4172 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4173 &wan_attr_group);
Johannes Berg19d337d2009-06-02 13:01:37 +02004174
4175 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4176
4177 wan_shutdown();
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004178}
4179
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004180static int __init wan_init(struct ibm_init_struct *iibm)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004181{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004182 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004183 int status = 0;
4184
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004185 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4186 "initializing wan subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004187
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004188 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004189
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004190 tp_features.wan = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004191 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004192
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004193 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4194 "wan is %s, status 0x%02x\n",
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004195 str_supported(tp_features.wan),
4196 status);
4197
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004198#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4199 if (dbg_wwanemul) {
4200 tp_features.wan = 1;
4201 printk(TPACPI_INFO
4202 "wwan switch emulation enabled\n");
4203 } else
4204#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004205 if (tp_features.wan &&
4206 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
4207 /* no wan hardware present in system */
4208 tp_features.wan = 0;
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004209 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004210 "wan hardware not installed\n");
4211 }
4212
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004213 if (!tp_features.wan)
4214 return 1;
4215
Johannes Berg19d337d2009-06-02 13:01:37 +02004216 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
4217 &wan_tprfk_ops,
4218 RFKILL_TYPE_WWAN,
4219 TPACPI_RFK_WWAN_SW_NAME,
4220 true);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004221 if (res)
4222 return res;
4223
Johannes Berg19d337d2009-06-02 13:01:37 +02004224 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4225 &wan_attr_group);
4226
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004227 if (res) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004228 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004229 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004230 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004231
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004232 return 0;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004233}
4234
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004235/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004236static int wan_read(struct seq_file *m)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004237{
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004238 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, m);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004239}
4240
4241static int wan_write(char *buf)
4242{
Johannes Berg19d337d2009-06-02 13:01:37 +02004243 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004244}
4245
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004246static struct ibm_struct wan_driver_data = {
4247 .name = "wan",
4248 .read = wan_read,
4249 .write = wan_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004250 .exit = wan_exit,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004251 .shutdown = wan_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004252};
4253
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004254/*************************************************************************
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004255 * UWB subdriver
4256 */
4257
4258enum {
4259 /* ACPI GUWB/SUWB bits */
4260 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4261 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4262};
4263
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004264#define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4265
Johannes Berg19d337d2009-06-02 13:01:37 +02004266static int uwb_get_status(void)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004267{
4268 int status;
4269
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004270#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4271 if (dbg_uwbemul)
4272 return (tpacpi_uwb_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004273 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004274#endif
4275
4276 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4277 return -EIO;
4278
4279 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004280 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004281}
4282
Johannes Berg19d337d2009-06-02 13:01:37 +02004283static int uwb_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004284{
4285 int status;
4286
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004287 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02004288 "will attempt to %s UWB\n",
4289 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004290
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004291#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4292 if (dbg_uwbemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004293 tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004294 return 0;
4295 }
4296#endif
4297
Johannes Berg19d337d2009-06-02 13:01:37 +02004298 if (state == TPACPI_RFK_RADIO_ON)
4299 status = TP_ACPI_UWB_RADIOSSW;
4300 else
4301 status = 0;
4302
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004303 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4304 return -EIO;
4305
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004306 return 0;
4307}
4308
4309/* --------------------------------------------------------------------- */
4310
Johannes Berg19d337d2009-06-02 13:01:37 +02004311static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
4312 .get_status = uwb_get_status,
4313 .set_status = uwb_set_status,
4314};
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004315
4316static void uwb_exit(void)
4317{
Johannes Berg19d337d2009-06-02 13:01:37 +02004318 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004319}
4320
4321static int __init uwb_init(struct ibm_init_struct *iibm)
4322{
4323 int res;
4324 int status = 0;
4325
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004326 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4327 "initializing uwb subdriver\n");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004328
4329 TPACPI_ACPIHANDLE_INIT(hkey);
4330
4331 tp_features.uwb = hkey_handle &&
4332 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4333
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004334 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4335 "uwb is %s, status 0x%02x\n",
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004336 str_supported(tp_features.uwb),
4337 status);
4338
4339#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4340 if (dbg_uwbemul) {
4341 tp_features.uwb = 1;
4342 printk(TPACPI_INFO
4343 "uwb switch emulation enabled\n");
4344 } else
4345#endif
4346 if (tp_features.uwb &&
4347 !(status & TP_ACPI_UWB_HWPRESENT)) {
4348 /* no uwb hardware present in system */
4349 tp_features.uwb = 0;
4350 dbg_printk(TPACPI_DBG_INIT,
4351 "uwb hardware not installed\n");
4352 }
4353
4354 if (!tp_features.uwb)
4355 return 1;
4356
4357 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
Johannes Berg19d337d2009-06-02 13:01:37 +02004358 &uwb_tprfk_ops,
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004359 RFKILL_TYPE_UWB,
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004360 TPACPI_RFK_UWB_SW_NAME,
Johannes Berg19d337d2009-06-02 13:01:37 +02004361 false);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004362 return res;
4363}
4364
4365static struct ibm_struct uwb_driver_data = {
4366 .name = "uwb",
4367 .exit = uwb_exit,
4368 .flags.experimental = 1,
4369};
4370
4371/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004372 * Video subdriver
4373 */
4374
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02004375#ifdef CONFIG_THINKPAD_ACPI_VIDEO
4376
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004377enum video_access_mode {
4378 TPACPI_VIDEO_NONE = 0,
4379 TPACPI_VIDEO_570, /* 570 */
4380 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4381 TPACPI_VIDEO_NEW, /* all others */
4382};
4383
4384enum { /* video status flags, based on VIDEO_570 */
4385 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4386 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4387 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4388};
4389
4390enum { /* TPACPI_VIDEO_570 constants */
4391 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4392 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4393 * video_status_flags */
4394 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4395 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4396};
4397
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02004398static enum video_access_mode video_supported;
4399static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004400
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004401static int video_autosw_get(void);
4402static int video_autosw_set(int enable);
4403
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004404TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004405
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004406static int __init video_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004407{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004408 int ivga;
4409
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004410 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4411
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004412 TPACPI_ACPIHANDLE_INIT(vid);
4413 TPACPI_ACPIHANDLE_INIT(vid2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004414
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004415 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4416 /* G41, assume IVGA doesn't change */
4417 vid_handle = vid2_handle;
4418
4419 if (!vid_handle)
4420 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004421 video_supported = TPACPI_VIDEO_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004422 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
4423 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004424 video_supported = TPACPI_VIDEO_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004425 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
4426 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004427 video_supported = TPACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004428 else
4429 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004430 video_supported = TPACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004432 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
4433 str_supported(video_supported != TPACPI_VIDEO_NONE),
4434 video_supported);
4435
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004436 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437}
4438
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004439static void video_exit(void)
4440{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004441 dbg_printk(TPACPI_DBG_EXIT,
4442 "restoring original video autoswitch mode\n");
4443 if (video_autosw_set(video_orig_autosw))
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004444 printk(TPACPI_ERR "error while trying to restore original "
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004445 "video autoswitch mode\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004446}
4447
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004448static int video_outputsw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449{
4450 int status = 0;
4451 int i;
4452
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004453 switch (video_supported) {
4454 case TPACPI_VIDEO_570:
4455 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
4456 TP_ACPI_VIDEO_570_PHSCMD))
4457 return -EIO;
4458 status = i & TP_ACPI_VIDEO_570_PHSMASK;
4459 break;
4460 case TPACPI_VIDEO_770:
4461 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4462 return -EIO;
4463 if (i)
4464 status |= TP_ACPI_VIDEO_S_LCD;
4465 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4466 return -EIO;
4467 if (i)
4468 status |= TP_ACPI_VIDEO_S_CRT;
4469 break;
4470 case TPACPI_VIDEO_NEW:
4471 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4472 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4473 return -EIO;
4474 if (i)
4475 status |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004477 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4478 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4479 return -EIO;
4480 if (i)
4481 status |= TP_ACPI_VIDEO_S_LCD;
4482 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4483 return -EIO;
4484 if (i)
4485 status |= TP_ACPI_VIDEO_S_DVI;
4486 break;
4487 default:
4488 return -ENOSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004489 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490
4491 return status;
4492}
4493
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004494static int video_outputsw_set(int status)
4495{
4496 int autosw;
4497 int res = 0;
4498
4499 switch (video_supported) {
4500 case TPACPI_VIDEO_570:
4501 res = acpi_evalf(NULL, NULL,
4502 "\\_SB.PHS2", "vdd",
4503 TP_ACPI_VIDEO_570_PHS2CMD,
4504 status | TP_ACPI_VIDEO_570_PHS2SET);
4505 break;
4506 case TPACPI_VIDEO_770:
4507 autosw = video_autosw_get();
4508 if (autosw < 0)
4509 return autosw;
4510
4511 res = video_autosw_set(1);
4512 if (res)
4513 return res;
4514 res = acpi_evalf(vid_handle, NULL,
4515 "ASWT", "vdd", status * 0x100, 0);
4516 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004517 printk(TPACPI_ERR
4518 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004519 return -EIO;
4520 }
4521 break;
4522 case TPACPI_VIDEO_NEW:
4523 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004524 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004525 break;
4526 default:
4527 return -ENOSYS;
4528 }
4529
4530 return (res)? 0 : -EIO;
4531}
4532
4533static int video_autosw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004534{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004535 int autosw = 0;
4536
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004537 switch (video_supported) {
4538 case TPACPI_VIDEO_570:
4539 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
4540 return -EIO;
4541 break;
4542 case TPACPI_VIDEO_770:
4543 case TPACPI_VIDEO_NEW:
4544 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4545 return -EIO;
4546 break;
4547 default:
4548 return -ENOSYS;
4549 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004550
4551 return autosw & 1;
4552}
4553
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004554static int video_autosw_set(int enable)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004555{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004556 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004557 return -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004558 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004559}
4560
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004561static int video_outputsw_cycle(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004562{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004563 int autosw = video_autosw_get();
4564 int res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004565
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004566 if (autosw < 0)
4567 return autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004568
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004569 switch (video_supported) {
4570 case TPACPI_VIDEO_570:
4571 res = video_autosw_set(1);
4572 if (res)
4573 return res;
4574 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4575 break;
4576 case TPACPI_VIDEO_770:
4577 case TPACPI_VIDEO_NEW:
4578 res = video_autosw_set(1);
4579 if (res)
4580 return res;
4581 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4582 break;
4583 default:
4584 return -ENOSYS;
4585 }
4586 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004587 printk(TPACPI_ERR
4588 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004589 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004590 }
4591
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004592 return (res)? 0 : -EIO;
4593}
4594
4595static int video_expand_toggle(void)
4596{
4597 switch (video_supported) {
4598 case TPACPI_VIDEO_570:
4599 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
4600 0 : -EIO;
4601 case TPACPI_VIDEO_770:
4602 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
4603 0 : -EIO;
4604 case TPACPI_VIDEO_NEW:
4605 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
4606 0 : -EIO;
4607 default:
4608 return -ENOSYS;
4609 }
4610 /* not reached */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004611}
4612
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004613static int video_read(struct seq_file *m)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004614{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004615 int status, autosw;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004616
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004617 if (video_supported == TPACPI_VIDEO_NONE) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004618 seq_printf(m, "status:\t\tnot supported\n");
4619 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004620 }
4621
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004622 status = video_outputsw_get();
4623 if (status < 0)
4624 return status;
4625
4626 autosw = video_autosw_get();
4627 if (autosw < 0)
4628 return autosw;
4629
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004630 seq_printf(m, "status:\t\tsupported\n");
4631 seq_printf(m, "lcd:\t\t%s\n", enabled(status, 0));
4632 seq_printf(m, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004633 if (video_supported == TPACPI_VIDEO_NEW)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004634 seq_printf(m, "dvi:\t\t%s\n", enabled(status, 3));
4635 seq_printf(m, "auto:\t\t%s\n", enabled(autosw, 0));
4636 seq_printf(m, "commands:\tlcd_enable, lcd_disable\n");
4637 seq_printf(m, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004638 if (video_supported == TPACPI_VIDEO_NEW)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004639 seq_printf(m, "commands:\tdvi_enable, dvi_disable\n");
4640 seq_printf(m, "commands:\tauto_enable, auto_disable\n");
4641 seq_printf(m, "commands:\tvideo_switch, expand_toggle\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004642
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004643 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004644}
4645
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004646static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647{
4648 char *cmd;
4649 int enable, disable, status;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004650 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004652 if (video_supported == TPACPI_VIDEO_NONE)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004653 return -ENODEV;
4654
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004655 enable = 0;
4656 disable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657
4658 while ((cmd = next_cmd(&buf))) {
4659 if (strlencmp(cmd, "lcd_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004660 enable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661 } else if (strlencmp(cmd, "lcd_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004662 disable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663 } else if (strlencmp(cmd, "crt_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004664 enable |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665 } else if (strlencmp(cmd, "crt_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004666 disable |= TP_ACPI_VIDEO_S_CRT;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004667 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004668 strlencmp(cmd, "dvi_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004669 enable |= TP_ACPI_VIDEO_S_DVI;
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_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004672 disable |= TP_ACPI_VIDEO_S_DVI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673 } else if (strlencmp(cmd, "auto_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004674 res = video_autosw_set(1);
4675 if (res)
4676 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677 } else if (strlencmp(cmd, "auto_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004678 res = video_autosw_set(0);
4679 if (res)
4680 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681 } else if (strlencmp(cmd, "video_switch") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004682 res = video_outputsw_cycle();
4683 if (res)
4684 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004686 res = video_expand_toggle();
4687 if (res)
4688 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689 } else
4690 return -EINVAL;
4691 }
4692
4693 if (enable || disable) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004694 status = video_outputsw_get();
4695 if (status < 0)
4696 return status;
4697 res = video_outputsw_set((status & ~disable) | enable);
4698 if (res)
4699 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700 }
4701
4702 return 0;
4703}
4704
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004705static struct ibm_struct video_driver_data = {
4706 .name = "video",
4707 .read = video_read,
4708 .write = video_write,
4709 .exit = video_exit,
4710};
4711
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02004712#endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4713
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004714/*************************************************************************
4715 * Light (thinklight) subdriver
4716 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004717
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004718TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4719TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004720
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004721static int light_get_status(void)
4722{
4723 int status = 0;
4724
4725 if (tp_features.light_status) {
4726 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4727 return -EIO;
4728 return (!!status);
4729 }
4730
4731 return -ENXIO;
4732}
4733
4734static int light_set_status(int status)
4735{
4736 int rc;
4737
4738 if (tp_features.light) {
4739 if (cmos_handle) {
4740 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4741 (status)?
4742 TP_CMOS_THINKLIGHT_ON :
4743 TP_CMOS_THINKLIGHT_OFF);
4744 } else {
4745 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4746 (status)? 1 : 0);
4747 }
4748 return (rc)? 0 : -EIO;
4749 }
4750
4751 return -ENXIO;
4752}
4753
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004754static void light_set_status_worker(struct work_struct *work)
4755{
4756 struct tpacpi_led_classdev *data =
4757 container_of(work, struct tpacpi_led_classdev, work);
4758
4759 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00004760 light_set_status((data->new_state != TPACPI_LED_OFF));
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004761}
4762
4763static void light_sysfs_set(struct led_classdev *led_cdev,
4764 enum led_brightness brightness)
4765{
4766 struct tpacpi_led_classdev *data =
4767 container_of(led_cdev,
4768 struct tpacpi_led_classdev,
4769 led_classdev);
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00004770 data->new_state = (brightness != LED_OFF) ?
4771 TPACPI_LED_ON : TPACPI_LED_OFF;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004772 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004773}
4774
4775static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4776{
4777 return (light_get_status() == 1)? LED_FULL : LED_OFF;
4778}
4779
4780static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4781 .led_classdev = {
4782 .name = "tpacpi::thinklight",
4783 .brightness_set = &light_sysfs_set,
4784 .brightness_get = &light_sysfs_get,
4785 }
4786};
4787
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004788static int __init light_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004789{
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004790 int rc;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004791
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004792 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4793
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004794 TPACPI_ACPIHANDLE_INIT(ledb);
4795 TPACPI_ACPIHANDLE_INIT(lght);
4796 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004797 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004798
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004799 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004800 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004801
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004802 if (tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004803 /* light status not supported on
4804 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004805 tp_features.light_status =
4806 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004808 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4809 str_supported(tp_features.light),
4810 str_supported(tp_features.light_status));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004811
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004812 if (!tp_features.light)
4813 return 1;
4814
4815 rc = led_classdev_register(&tpacpi_pdev->dev,
4816 &tpacpi_led_thinklight.led_classdev);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004817
4818 if (rc < 0) {
4819 tp_features.light = 0;
4820 tp_features.light_status = 0;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004821 } else {
4822 rc = 0;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004823 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004824
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004825 return rc;
4826}
4827
4828static void light_exit(void)
4829{
4830 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
4831 if (work_pending(&tpacpi_led_thinklight.work))
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004832 flush_workqueue(tpacpi_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004833}
4834
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004835static int light_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836{
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004837 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004838
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004839 if (!tp_features.light) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004840 seq_printf(m, "status:\t\tnot supported\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004841 } else if (!tp_features.light_status) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004842 seq_printf(m, "status:\t\tunknown\n");
4843 seq_printf(m, "commands:\ton, off\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004844 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004845 status = light_get_status();
4846 if (status < 0)
4847 return status;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004848 seq_printf(m, "status:\t\t%s\n", onoff(status, 0));
4849 seq_printf(m, "commands:\ton, off\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004850 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004851
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004852 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004853}
4854
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004855static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004857 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004858 int newstatus = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004859
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004860 if (!tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004861 return -ENODEV;
4862
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863 while ((cmd = next_cmd(&buf))) {
4864 if (strlencmp(cmd, "on") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004865 newstatus = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004866 } else if (strlencmp(cmd, "off") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004867 newstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004868 } else
4869 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004870 }
4871
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004872 return light_set_status(newstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873}
4874
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004875static struct ibm_struct light_driver_data = {
4876 .name = "light",
4877 .read = light_read,
4878 .write = light_write,
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004879 .exit = light_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004880};
4881
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004882/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004883 * CMOS subdriver
4884 */
4885
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004886/* sysfs cmos_command -------------------------------------------------- */
4887static ssize_t cmos_command_store(struct device *dev,
4888 struct device_attribute *attr,
4889 const char *buf, size_t count)
4890{
4891 unsigned long cmos_cmd;
4892 int res;
4893
4894 if (parse_strtoul(buf, 21, &cmos_cmd))
4895 return -EINVAL;
4896
4897 res = issue_thinkpad_cmos_command(cmos_cmd);
4898 return (res)? res : count;
4899}
4900
4901static struct device_attribute dev_attr_cmos_command =
4902 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4903
4904/* --------------------------------------------------------------------- */
4905
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004906static int __init cmos_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004907{
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004908 int res;
4909
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004910 vdbg_printk(TPACPI_DBG_INIT,
4911 "initializing cmos commands subdriver\n");
4912
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004913 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004914
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004915 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4916 str_supported(cmos_handle != NULL));
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004917
4918 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4919 if (res)
4920 return res;
4921
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004922 return (cmos_handle)? 0 : 1;
4923}
4924
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004925static void cmos_exit(void)
4926{
4927 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4928}
4929
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004930static int cmos_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004931{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004932 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4933 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004934 if (!cmos_handle)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004935 seq_printf(m, "status:\t\tnot supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004936 else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004937 seq_printf(m, "status:\t\tsupported\n");
4938 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004939 }
4940
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004941 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942}
4943
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004944static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945{
4946 char *cmd;
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004947 int cmos_cmd, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004948
4949 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004950 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
4951 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004952 /* cmos_cmd set */
4953 } else
4954 return -EINVAL;
4955
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004956 res = issue_thinkpad_cmos_command(cmos_cmd);
4957 if (res)
4958 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959 }
4960
4961 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004962}
4963
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004964static struct ibm_struct cmos_driver_data = {
4965 .name = "cmos",
4966 .read = cmos_read,
4967 .write = cmos_write,
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004968 .exit = cmos_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004969};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004970
4971/*************************************************************************
4972 * LED subdriver
4973 */
4974
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004975enum led_access_mode {
4976 TPACPI_LED_NONE = 0,
4977 TPACPI_LED_570, /* 570 */
4978 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4979 TPACPI_LED_NEW, /* all others */
4980};
4981
4982enum { /* For TPACPI_LED_OLD */
4983 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
4984 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
4985 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
4986};
4987
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02004988static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004989
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004990TPACPI_HANDLE(led, ec, "SLED", /* 570 */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004991 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
4992 /* T20-22, X20-21 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004993 "LED", /* all others */
4994 ); /* R30, R31 */
4995
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03004996#define TPACPI_LED_NUMLEDS 16
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004997static struct tpacpi_led_classdev *tpacpi_leds;
4998static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
Harvey Harrisone3aa51f2008-05-29 17:51:57 -07004999static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005000 /* there's a limit of 19 chars + NULL before 2.6.26 */
5001 "tpacpi::power",
5002 "tpacpi:orange:batt",
5003 "tpacpi:green:batt",
5004 "tpacpi::dock_active",
5005 "tpacpi::bay_active",
5006 "tpacpi::dock_batt",
5007 "tpacpi::unknown_led",
5008 "tpacpi::standby",
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005009 "tpacpi::dock_status1",
5010 "tpacpi::dock_status2",
5011 "tpacpi::unknown_led2",
5012 "tpacpi::unknown_led3",
5013 "tpacpi::thinkvantage",
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005014};
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005015#define TPACPI_SAFE_LEDS 0x1081U
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005016
5017static inline bool tpacpi_is_led_restricted(const unsigned int led)
5018{
5019#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5020 return false;
5021#else
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005022 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005023#endif
5024}
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005025
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005026static int led_get_status(const unsigned int led)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005027{
5028 int status;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005029 enum led_status_t led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005030
5031 switch (led_supported) {
5032 case TPACPI_LED_570:
5033 if (!acpi_evalf(ec_handle,
5034 &status, "GLED", "dd", 1 << led))
5035 return -EIO;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005036 led_s = (status == 0)?
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005037 TPACPI_LED_OFF :
5038 ((status == 1)?
5039 TPACPI_LED_ON :
5040 TPACPI_LED_BLINK);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005041 tpacpi_led_state_cache[led] = led_s;
5042 return led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005043 default:
5044 return -ENXIO;
5045 }
5046
5047 /* not reached */
5048}
5049
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005050static int led_set_status(const unsigned int led,
5051 const enum led_status_t ledstatus)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005052{
5053 /* off, on, blink. Index is led_status_t */
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005054 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
5055 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005056
5057 int rc = 0;
5058
5059 switch (led_supported) {
5060 case TPACPI_LED_570:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005061 /* 570 */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005062 if (unlikely(led > 7))
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005063 return -EINVAL;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005064 if (unlikely(tpacpi_is_led_restricted(led)))
5065 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005066 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5067 (1 << led), led_sled_arg1[ledstatus]))
5068 rc = -EIO;
5069 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005070 case TPACPI_LED_OLD:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005071 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005072 if (unlikely(led > 7))
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005073 return -EINVAL;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005074 if (unlikely(tpacpi_is_led_restricted(led)))
5075 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005076 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
5077 if (rc >= 0)
5078 rc = ec_write(TPACPI_LED_EC_HLBL,
5079 (ledstatus == TPACPI_LED_BLINK) << led);
5080 if (rc >= 0)
5081 rc = ec_write(TPACPI_LED_EC_HLCL,
5082 (ledstatus != TPACPI_LED_OFF) << led);
5083 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005084 case TPACPI_LED_NEW:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005085 /* all others */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005086 if (unlikely(led >= TPACPI_LED_NUMLEDS))
5087 return -EINVAL;
5088 if (unlikely(tpacpi_is_led_restricted(led)))
5089 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005090 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5091 led, led_led_arg1[ledstatus]))
5092 rc = -EIO;
5093 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005094 default:
5095 rc = -ENXIO;
5096 }
5097
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005098 if (!rc)
5099 tpacpi_led_state_cache[led] = ledstatus;
5100
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005101 return rc;
5102}
5103
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005104static void led_set_status_worker(struct work_struct *work)
5105{
5106 struct tpacpi_led_classdev *data =
5107 container_of(work, struct tpacpi_led_classdev, work);
5108
5109 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005110 led_set_status(data->led, data->new_state);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005111}
5112
5113static void led_sysfs_set(struct led_classdev *led_cdev,
5114 enum led_brightness brightness)
5115{
5116 struct tpacpi_led_classdev *data = container_of(led_cdev,
5117 struct tpacpi_led_classdev, led_classdev);
5118
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005119 if (brightness == LED_OFF)
5120 data->new_state = TPACPI_LED_OFF;
5121 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
5122 data->new_state = TPACPI_LED_ON;
5123 else
5124 data->new_state = TPACPI_LED_BLINK;
5125
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03005126 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005127}
5128
5129static int led_sysfs_blink_set(struct led_classdev *led_cdev,
5130 unsigned long *delay_on, unsigned long *delay_off)
5131{
5132 struct tpacpi_led_classdev *data = container_of(led_cdev,
5133 struct tpacpi_led_classdev, led_classdev);
5134
5135 /* Can we choose the flash rate? */
5136 if (*delay_on == 0 && *delay_off == 0) {
5137 /* yes. set them to the hardware blink rate (1 Hz) */
5138 *delay_on = 500; /* ms */
5139 *delay_off = 500; /* ms */
5140 } else if ((*delay_on != 500) || (*delay_off != 500))
5141 return -EINVAL;
5142
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005143 data->new_state = TPACPI_LED_BLINK;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03005144 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005145
5146 return 0;
5147}
5148
5149static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
5150{
5151 int rc;
5152
5153 struct tpacpi_led_classdev *data = container_of(led_cdev,
5154 struct tpacpi_led_classdev, led_classdev);
5155
5156 rc = led_get_status(data->led);
5157
5158 if (rc == TPACPI_LED_OFF || rc < 0)
5159 rc = LED_OFF; /* no error handling in led class :( */
5160 else
5161 rc = LED_FULL;
5162
5163 return rc;
5164}
5165
5166static void led_exit(void)
5167{
5168 unsigned int i;
5169
5170 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5171 if (tpacpi_leds[i].led_classdev.name)
5172 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
5173 }
5174
5175 kfree(tpacpi_leds);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005176}
5177
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005178static int __init tpacpi_init_led(unsigned int led)
5179{
5180 int rc;
5181
5182 tpacpi_leds[led].led = led;
5183
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005184 /* LEDs with no name don't get registered */
5185 if (!tpacpi_led_names[led])
5186 return 0;
5187
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005188 tpacpi_leds[led].led_classdev.brightness_set = &led_sysfs_set;
5189 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5190 if (led_supported == TPACPI_LED_570)
5191 tpacpi_leds[led].led_classdev.brightness_get =
5192 &led_sysfs_get;
5193
5194 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5195
5196 INIT_WORK(&tpacpi_leds[led].work, led_set_status_worker);
5197
5198 rc = led_classdev_register(&tpacpi_pdev->dev,
5199 &tpacpi_leds[led].led_classdev);
5200 if (rc < 0)
5201 tpacpi_leds[led].led_classdev.name = NULL;
5202
5203 return rc;
5204}
5205
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005206static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
5207 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5208 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5209 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5210
5211 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5212 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5213 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5214 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5215 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5216 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5217 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5218 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5219
5220 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5221 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5222 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5223 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5224 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5225
5226 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5227 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5228 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5229 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5230
5231 /* (1) - may have excess leds enabled on MSB */
5232
5233 /* Defaults (order matters, keep last, don't reorder!) */
5234 { /* Lenovo */
5235 .vendor = PCI_VENDOR_ID_LENOVO,
5236 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5237 .quirks = 0x1fffU,
5238 },
5239 { /* IBM ThinkPads with no EC version string */
5240 .vendor = PCI_VENDOR_ID_IBM,
5241 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
5242 .quirks = 0x00ffU,
5243 },
5244 { /* IBM ThinkPads with EC version string */
5245 .vendor = PCI_VENDOR_ID_IBM,
5246 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5247 .quirks = 0x00bfU,
5248 },
5249};
5250
5251#undef TPACPI_LEDQ_IBM
5252#undef TPACPI_LEDQ_LNV
5253
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005254static int __init led_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005255{
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005256 unsigned int i;
5257 int rc;
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005258 unsigned long useful_leds;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005259
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005260 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
5261
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005262 TPACPI_ACPIHANDLE_INIT(led);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005263
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005264 if (!led_handle)
5265 /* led not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005266 led_supported = TPACPI_LED_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005267 else if (strlencmp(led_path, "SLED") == 0)
5268 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005269 led_supported = TPACPI_LED_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005270 else if (strlencmp(led_path, "SYSL") == 0)
5271 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005272 led_supported = TPACPI_LED_OLD;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005273 else
5274 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005275 led_supported = TPACPI_LED_NEW;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005276
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005277 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
5278 str_supported(led_supported), led_supported);
5279
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005280 if (led_supported == TPACPI_LED_NONE)
5281 return 1;
5282
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005283 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
5284 GFP_KERNEL);
5285 if (!tpacpi_leds) {
5286 printk(TPACPI_ERR "Out of memory for LED data\n");
5287 return -ENOMEM;
5288 }
5289
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005290 useful_leds = tpacpi_check_quirks(led_useful_qtable,
5291 ARRAY_SIZE(led_useful_qtable));
5292
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005293 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005294 if (!tpacpi_is_led_restricted(i) &&
5295 test_bit(i, &useful_leds)) {
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005296 rc = tpacpi_init_led(i);
5297 if (rc < 0) {
5298 led_exit();
5299 return rc;
5300 }
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005301 }
5302 }
5303
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005304#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005305 printk(TPACPI_NOTICE
5306 "warning: userspace override of important "
5307 "firmware LEDs is enabled\n");
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005308#endif
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005309 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005310}
5311
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005312#define str_led_status(s) \
5313 ((s) == TPACPI_LED_OFF ? "off" : \
5314 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005315
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005316static int led_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005317{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005318 if (!led_supported) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005319 seq_printf(m, "status:\t\tnot supported\n");
5320 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005321 }
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005322 seq_printf(m, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005323
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005324 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005325 /* 570 */
5326 int i, status;
5327 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005328 status = led_get_status(i);
5329 if (status < 0)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005330 return -EIO;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005331 seq_printf(m, "%d:\t\t%s\n",
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005332 i, str_led_status(status));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005333 }
5334 }
5335
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005336 seq_printf(m, "commands:\t"
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005337 "<led> on, <led> off, <led> blink (<led> is 0-15)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005338
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005339 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005340}
5341
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005342static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005343{
5344 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005345 int led, rc;
5346 enum led_status_t s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005347
5348 if (!led_supported)
5349 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005350
5351 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005352 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 15)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005353 return -EINVAL;
5354
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005355 if (strstr(cmd, "off")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005356 s = TPACPI_LED_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005357 } else if (strstr(cmd, "on")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005358 s = TPACPI_LED_ON;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005359 } else if (strstr(cmd, "blink")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005360 s = TPACPI_LED_BLINK;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005361 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005362 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005363 }
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005364
5365 rc = led_set_status(led, s);
5366 if (rc < 0)
5367 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005368 }
5369
5370 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005371}
5372
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005373static struct ibm_struct led_driver_data = {
5374 .name = "led",
5375 .read = led_read,
5376 .write = led_write,
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005377 .exit = led_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005378};
5379
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005380/*************************************************************************
5381 * Beep subdriver
5382 */
5383
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005384TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005385
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005386#define TPACPI_BEEP_Q1 0x0001
5387
5388static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
5389 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
5390 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
5391};
5392
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005393static int __init beep_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005394{
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005395 unsigned long quirks;
5396
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005397 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
5398
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005399 TPACPI_ACPIHANDLE_INIT(beep);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005400
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005401 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
5402 str_supported(beep_handle != NULL));
5403
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005404 quirks = tpacpi_check_quirks(beep_quirk_table,
5405 ARRAY_SIZE(beep_quirk_table));
5406
5407 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
5408
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005409 return (beep_handle)? 0 : 1;
5410}
5411
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005412static int beep_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005413{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005414 if (!beep_handle)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005415 seq_printf(m, "status:\t\tnot supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005416 else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005417 seq_printf(m, "status:\t\tsupported\n");
5418 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-17)\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005419 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005420
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005421 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005422}
5423
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005424static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005425{
5426 char *cmd;
5427 int beep_cmd;
5428
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005429 if (!beep_handle)
5430 return -ENODEV;
5431
Linus Torvalds1da177e2005-04-16 15:20:36 -07005432 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005433 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
5434 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005435 /* beep_cmd set */
5436 } else
5437 return -EINVAL;
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005438 if (tp_features.beep_needs_two_args) {
5439 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
5440 beep_cmd, 0))
5441 return -EIO;
5442 } else {
5443 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
5444 beep_cmd))
5445 return -EIO;
5446 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005447 }
5448
5449 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005450}
5451
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005452static struct ibm_struct beep_driver_data = {
5453 .name = "beep",
5454 .read = beep_read,
5455 .write = beep_write,
5456};
5457
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005458/*************************************************************************
5459 * Thermal subdriver
5460 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005461
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005462enum thermal_access_mode {
5463 TPACPI_THERMAL_NONE = 0, /* No thermal support */
5464 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
5465 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
5466 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
5467 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
5468};
5469
5470enum { /* TPACPI_THERMAL_TPEC_* */
5471 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
5472 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
5473 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005474
5475 TPACPI_THERMAL_SENSOR_NA = -128000, /* Sensor not available */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005476};
5477
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005478
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005479#define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5480struct ibm_thermal_sensors_struct {
5481 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
5482};
5483
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005484static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005485
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005486/* idx is zero-based */
5487static int thermal_get_sensor(int idx, s32 *value)
5488{
5489 int t;
5490 s8 tmp;
5491 char tmpi[5];
5492
5493 t = TP_EC_THERMAL_TMP0;
5494
5495 switch (thermal_read_mode) {
5496#if TPACPI_MAX_THERMAL_SENSORS >= 16
5497 case TPACPI_THERMAL_TPEC_16:
5498 if (idx >= 8 && idx <= 15) {
5499 t = TP_EC_THERMAL_TMP8;
5500 idx -= 8;
5501 }
5502 /* fallthrough */
5503#endif
5504 case TPACPI_THERMAL_TPEC_8:
5505 if (idx <= 7) {
5506 if (!acpi_ec_read(t + idx, &tmp))
5507 return -EIO;
5508 *value = tmp * 1000;
5509 return 0;
5510 }
5511 break;
5512
5513 case TPACPI_THERMAL_ACPI_UPDT:
5514 if (idx <= 7) {
5515 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5516 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
5517 return -EIO;
5518 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5519 return -EIO;
5520 *value = (t - 2732) * 100;
5521 return 0;
5522 }
5523 break;
5524
5525 case TPACPI_THERMAL_ACPI_TMP07:
5526 if (idx <= 7) {
5527 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5528 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5529 return -EIO;
5530 if (t > 127 || t < -127)
5531 t = TP_EC_THERMAL_TMP_NA;
5532 *value = t * 1000;
5533 return 0;
5534 }
5535 break;
5536
5537 case TPACPI_THERMAL_NONE:
5538 default:
5539 return -ENOSYS;
5540 }
5541
5542 return -EINVAL;
5543}
5544
5545static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5546{
5547 int res, i;
5548 int n;
5549
5550 n = 8;
5551 i = 0;
5552
5553 if (!s)
5554 return -EINVAL;
5555
5556 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5557 n = 16;
5558
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005559 for (i = 0 ; i < n; i++) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005560 res = thermal_get_sensor(i, &s->temp[i]);
5561 if (res)
5562 return res;
5563 }
5564
5565 return n;
5566}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005567
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005568static void thermal_dump_all_sensors(void)
5569{
5570 int n, i;
5571 struct ibm_thermal_sensors_struct t;
5572
5573 n = thermal_get_sensors(&t);
5574 if (n <= 0)
5575 return;
5576
5577 printk(TPACPI_NOTICE
5578 "temperatures (Celsius):");
5579
5580 for (i = 0; i < n; i++) {
5581 if (t.temp[i] != TPACPI_THERMAL_SENSOR_NA)
5582 printk(KERN_CONT " %d", (int)(t.temp[i] / 1000));
5583 else
5584 printk(KERN_CONT " N/A");
5585 }
5586
5587 printk(KERN_CONT "\n");
5588}
5589
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005590/* sysfs temp##_input -------------------------------------------------- */
5591
5592static ssize_t thermal_temp_input_show(struct device *dev,
5593 struct device_attribute *attr,
5594 char *buf)
5595{
5596 struct sensor_device_attribute *sensor_attr =
5597 to_sensor_dev_attr(attr);
5598 int idx = sensor_attr->index;
5599 s32 value;
5600 int res;
5601
5602 res = thermal_get_sensor(idx, &value);
5603 if (res)
5604 return res;
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005605 if (value == TPACPI_THERMAL_SENSOR_NA)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005606 return -ENXIO;
5607
5608 return snprintf(buf, PAGE_SIZE, "%d\n", value);
5609}
5610
5611#define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005612 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5613 thermal_temp_input_show, NULL, _idxB)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005614
5615static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5616 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5617 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5618 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5619 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5620 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5621 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5622 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5623 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5624 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5625 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5626 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5627 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5628 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5629 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5630 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5631 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5632};
5633
5634#define THERMAL_ATTRS(X) \
5635 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5636
5637static struct attribute *thermal_temp_input_attr[] = {
5638 THERMAL_ATTRS(8),
5639 THERMAL_ATTRS(9),
5640 THERMAL_ATTRS(10),
5641 THERMAL_ATTRS(11),
5642 THERMAL_ATTRS(12),
5643 THERMAL_ATTRS(13),
5644 THERMAL_ATTRS(14),
5645 THERMAL_ATTRS(15),
5646 THERMAL_ATTRS(0),
5647 THERMAL_ATTRS(1),
5648 THERMAL_ATTRS(2),
5649 THERMAL_ATTRS(3),
5650 THERMAL_ATTRS(4),
5651 THERMAL_ATTRS(5),
5652 THERMAL_ATTRS(6),
5653 THERMAL_ATTRS(7),
5654 NULL
5655};
5656
5657static const struct attribute_group thermal_temp_input16_group = {
5658 .attrs = thermal_temp_input_attr
5659};
5660
5661static const struct attribute_group thermal_temp_input8_group = {
5662 .attrs = &thermal_temp_input_attr[8]
5663};
5664
5665#undef THERMAL_SENSOR_ATTR_TEMP
5666#undef THERMAL_ATTRS
5667
5668/* --------------------------------------------------------------------- */
5669
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005670static int __init thermal_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005671{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005672 u8 t, ta1, ta2;
5673 int i;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005674 int acpi_tmp7;
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005675 int res;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005676
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005677 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5678
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005679 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005680
Henrique de Moraes Holschuh3d6f99c2007-07-18 23:45:46 -03005681 if (thinkpad_id.ec_model) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005682 /*
5683 * Direct EC access mode: sensors at registers
5684 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5685 * non-implemented, thermal sensors return 0x80 when
5686 * not available
5687 */
5688
5689 ta1 = ta2 = 0;
5690 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03005691 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005692 ta1 |= t;
5693 } else {
5694 ta1 = 0;
5695 break;
5696 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03005697 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005698 ta2 |= t;
5699 } else {
5700 ta1 = 0;
5701 break;
5702 }
5703 }
5704 if (ta1 == 0) {
5705 /* This is sheer paranoia, but we handle it anyway */
5706 if (acpi_tmp7) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005707 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005708 "ThinkPad ACPI EC access misbehaving, "
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005709 "falling back to ACPI TMPx access "
5710 "mode\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005711 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005712 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005713 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005714 "ThinkPad ACPI EC access misbehaving, "
5715 "disabling thermal sensors access\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005716 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005717 }
5718 } else {
5719 thermal_read_mode =
5720 (ta2 != 0) ?
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005721 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005722 }
5723 } else if (acpi_tmp7) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005724 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
5725 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005726 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005727 } else {
5728 /* Standard ACPI TMPx access, max 8 sensors */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005729 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005730 }
5731 } else {
5732 /* temperatures not supported on 570, G4x, R30, R31, R32 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005733 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005734 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005735
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005736 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5737 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5738 thermal_read_mode);
5739
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005740 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005741 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005742 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005743 &thermal_temp_input16_group);
5744 if (res)
5745 return res;
5746 break;
5747 case TPACPI_THERMAL_TPEC_8:
5748 case TPACPI_THERMAL_ACPI_TMP07:
5749 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005750 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005751 &thermal_temp_input8_group);
5752 if (res)
5753 return res;
5754 break;
5755 case TPACPI_THERMAL_NONE:
5756 default:
5757 return 1;
5758 }
5759
5760 return 0;
5761}
5762
5763static void thermal_exit(void)
5764{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005765 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005766 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005767 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005768 &thermal_temp_input16_group);
5769 break;
5770 case TPACPI_THERMAL_TPEC_8:
5771 case TPACPI_THERMAL_ACPI_TMP07:
5772 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005773 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005774 &thermal_temp_input16_group);
5775 break;
5776 case TPACPI_THERMAL_NONE:
5777 default:
5778 break;
5779 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005780}
5781
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005782static int thermal_read(struct seq_file *m)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005783{
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005784 int n, i;
5785 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005786
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005787 n = thermal_get_sensors(&t);
5788 if (unlikely(n < 0))
5789 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005790
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005791 seq_printf(m, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005792
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005793 if (n > 0) {
5794 for (i = 0; i < (n - 1); i++)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005795 seq_printf(m, "%d ", t.temp[i] / 1000);
5796 seq_printf(m, "%d\n", t.temp[i] / 1000);
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005797 } else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005798 seq_printf(m, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005799
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005800 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005801}
5802
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005803static struct ibm_struct thermal_driver_data = {
5804 .name = "thermal",
5805 .read = thermal_read,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005806 .exit = thermal_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005807};
5808
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005809/*************************************************************************
5810 * EC Dump subdriver
5811 */
5812
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005813static u8 ecdump_regs[256];
5814
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005815static int ecdump_read(struct seq_file *m)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005816{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005817 int i, j;
5818 u8 v;
5819
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005820 seq_printf(m, "EC "
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005821 " +00 +01 +02 +03 +04 +05 +06 +07"
5822 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
5823 for (i = 0; i < 256; i += 16) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005824 seq_printf(m, "EC 0x%02x:", i);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005825 for (j = 0; j < 16; j++) {
5826 if (!acpi_ec_read(i + j, &v))
5827 break;
5828 if (v != ecdump_regs[i + j])
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005829 seq_printf(m, " *%02x", v);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005830 else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005831 seq_printf(m, " %02x", v);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005832 ecdump_regs[i + j] = v;
5833 }
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005834 seq_putc(m, '\n');
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005835 if (j != 16)
5836 break;
5837 }
5838
5839 /* These are way too dangerous to advertise openly... */
5840#if 0
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005841 seq_printf(m, "commands:\t0x<offset> 0x<value>"
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005842 " (<offset> is 00-ff, <value> is 00-ff)\n");
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005843 seq_printf(m, "commands:\t0x<offset> <value> "
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005844 " (<offset> is 00-ff, <value> is 0-255)\n");
5845#endif
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005846 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005847}
5848
5849static int ecdump_write(char *buf)
5850{
5851 char *cmd;
5852 int i, v;
5853
5854 while ((cmd = next_cmd(&buf))) {
5855 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
5856 /* i and v set */
5857 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
5858 /* i and v set */
5859 } else
5860 return -EINVAL;
5861 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
5862 if (!acpi_ec_write(i, v))
5863 return -EIO;
5864 } else
5865 return -EINVAL;
5866 }
5867
5868 return 0;
5869}
5870
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005871static struct ibm_struct ecdump_driver_data = {
5872 .name = "ecdump",
5873 .read = ecdump_read,
5874 .write = ecdump_write,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03005875 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005876};
5877
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005878/*************************************************************************
5879 * Backlight/brightness subdriver
5880 */
Holger Macht8acb0252006-10-20 14:30:28 -07005881
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005882#define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5883
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005884/*
5885 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
5886 * CMOS NVRAM byte 0x5E, bits 0-3.
5887 *
5888 * EC HBRV (0x31) has the following layout
5889 * Bit 7: unknown function
5890 * Bit 6: unknown function
5891 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5892 * Bit 4: must be set to zero to avoid problems
5893 * Bit 3-0: backlight brightness level
5894 *
5895 * brightness_get_raw returns status data in the HBRV layout
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03005896 *
5897 * WARNING: The X61 has been verified to use HBRV for something else, so
5898 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
5899 * testing on the very early *60 Lenovo models...
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005900 */
5901
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005902enum {
5903 TP_EC_BACKLIGHT = 0x31,
5904
5905 /* TP_EC_BACKLIGHT bitmasks */
5906 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5907 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5908 TP_EC_BACKLIGHT_MAPSW = 0x20,
5909};
5910
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005911enum tpacpi_brightness_access_mode {
5912 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
5913 TPACPI_BRGHT_MODE_EC, /* EC control */
5914 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
5915 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
5916 TPACPI_BRGHT_MODE_MAX
5917};
5918
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -03005919static struct backlight_device *ibm_backlight_device;
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005920
5921static enum tpacpi_brightness_access_mode brightness_mode =
5922 TPACPI_BRGHT_MODE_MAX;
5923
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005924static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5925
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005926static struct mutex brightness_mutex;
5927
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005928/* NVRAM brightness access,
5929 * call with brightness_mutex held! */
5930static unsigned int tpacpi_brightness_nvram_get(void)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005931{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005932 u8 lnvram;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005933
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005934 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5935 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5936 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
5937 lnvram &= (tp_features.bright_16levels) ? 0x0f : 0x07;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005938
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005939 return lnvram;
5940}
5941
5942static void tpacpi_brightness_checkpoint_nvram(void)
5943{
5944 u8 lec = 0;
5945 u8 b_nvram;
5946
5947 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
5948 return;
5949
5950 vdbg_printk(TPACPI_DBG_BRGHT,
5951 "trying to checkpoint backlight level to NVRAM...\n");
5952
5953 if (mutex_lock_killable(&brightness_mutex) < 0)
5954 return;
5955
5956 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5957 goto unlock;
5958 lec &= TP_EC_BACKLIGHT_LVLMSK;
5959 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
5960
5961 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5962 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
5963 /* NVRAM needs update */
5964 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
5965 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
5966 b_nvram |= lec;
5967 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
5968 dbg_printk(TPACPI_DBG_BRGHT,
5969 "updated NVRAM backlight level to %u (0x%02x)\n",
5970 (unsigned int) lec, (unsigned int) b_nvram);
5971 } else
5972 vdbg_printk(TPACPI_DBG_BRGHT,
5973 "NVRAM backlight level already is %u (0x%02x)\n",
5974 (unsigned int) lec, (unsigned int) b_nvram);
5975
5976unlock:
5977 mutex_unlock(&brightness_mutex);
5978}
5979
5980
5981/* call with brightness_mutex held! */
5982static int tpacpi_brightness_get_raw(int *status)
5983{
5984 u8 lec = 0;
5985
5986 switch (brightness_mode) {
5987 case TPACPI_BRGHT_MODE_UCMS_STEP:
5988 *status = tpacpi_brightness_nvram_get();
5989 return 0;
5990 case TPACPI_BRGHT_MODE_EC:
5991 case TPACPI_BRGHT_MODE_ECNVRAM:
5992 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
Henrique de Moraes Holschuh2d5e94d2008-04-26 01:02:20 -03005993 return -EIO;
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005994 *status = lec;
5995 return 0;
5996 default:
5997 return -ENXIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005998 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005999}
6000
6001/* call with brightness_mutex held! */
6002/* do NOT call with illegal backlight level value */
6003static int tpacpi_brightness_set_ec(unsigned int value)
6004{
6005 u8 lec = 0;
6006
6007 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6008 return -EIO;
6009
6010 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
6011 (lec & TP_EC_BACKLIGHT_CMDMSK) |
6012 (value & TP_EC_BACKLIGHT_LVLMSK))))
6013 return -EIO;
6014
6015 return 0;
6016}
6017
6018/* call with brightness_mutex held! */
6019static int tpacpi_brightness_set_ucmsstep(unsigned int value)
6020{
6021 int cmos_cmd, inc;
6022 unsigned int current_value, i;
6023
6024 current_value = tpacpi_brightness_nvram_get();
6025
6026 if (value == current_value)
6027 return 0;
6028
6029 cmos_cmd = (value > current_value) ?
6030 TP_CMOS_BRIGHTNESS_UP :
6031 TP_CMOS_BRIGHTNESS_DOWN;
6032 inc = (value > current_value) ? 1 : -1;
6033
6034 for (i = current_value; i != value; i += inc)
6035 if (issue_thinkpad_cmos_command(cmos_cmd))
6036 return -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006037
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006038 return 0;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006039}
6040
6041/* May return EINTR which can always be mapped to ERESTARTSYS */
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006042static int brightness_set(unsigned int value)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006043{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006044 int res;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006045
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006046 if (value > ((tp_features.bright_16levels)? 15 : 7) ||
6047 value < 0)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006048 return -EINVAL;
6049
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006050 vdbg_printk(TPACPI_DBG_BRGHT,
6051 "set backlight level to %d\n", value);
6052
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006053 res = mutex_lock_killable(&brightness_mutex);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006054 if (res < 0)
6055 return res;
6056
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006057 switch (brightness_mode) {
6058 case TPACPI_BRGHT_MODE_EC:
6059 case TPACPI_BRGHT_MODE_ECNVRAM:
6060 res = tpacpi_brightness_set_ec(value);
6061 break;
6062 case TPACPI_BRGHT_MODE_UCMS_STEP:
6063 res = tpacpi_brightness_set_ucmsstep(value);
6064 break;
6065 default:
6066 res = -ENXIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006067 }
6068
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006069 mutex_unlock(&brightness_mutex);
6070 return res;
6071}
6072
6073/* sysfs backlight class ----------------------------------------------- */
6074
6075static int brightness_update_status(struct backlight_device *bd)
6076{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006077 unsigned int level =
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006078 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
6079 bd->props.power == FB_BLANK_UNBLANK) ?
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006080 bd->props.brightness : 0;
6081
6082 dbg_printk(TPACPI_DBG_BRGHT,
6083 "backlight: attempt to set level to %d\n",
6084 level);
6085
6086 /* it is the backlight class's job (caller) to handle
6087 * EINTR and other errors properly */
6088 return brightness_set(level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006089}
Holger Macht8acb0252006-10-20 14:30:28 -07006090
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006091static int brightness_get(struct backlight_device *bd)
6092{
6093 int status, res;
6094
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006095 res = mutex_lock_killable(&brightness_mutex);
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006096 if (res < 0)
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006097 return 0;
6098
6099 res = tpacpi_brightness_get_raw(&status);
6100
6101 mutex_unlock(&brightness_mutex);
6102
6103 if (res < 0)
6104 return 0;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006105
6106 return status & TP_EC_BACKLIGHT_LVLMSK;
6107}
6108
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00006109static void tpacpi_brightness_notify_change(void)
6110{
6111 backlight_force_update(ibm_backlight_device,
6112 BACKLIGHT_UPDATE_HOTKEY);
6113}
6114
Richard Purdie599a52d2007-02-10 23:07:48 +00006115static struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006116 .get_brightness = brightness_get,
6117 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006118};
6119
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006120/* --------------------------------------------------------------------- */
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03006121
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006122/*
6123 * These are only useful for models that have only one possibility
6124 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6125 * these quirks.
6126 */
6127#define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6128#define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6129#define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6130
6131static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
6132 /* Models with ATI GPUs known to require ECNVRAM mode */
6133 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
6134
Henrique de Moraes Holschuh6da25bf2009-09-12 15:22:11 -03006135 /* Models with ATI GPUs that can use ECNVRAM */
6136 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC),
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006137 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6138 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6139 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6140
Henrique de Moraes Holschuh6da25bf2009-09-12 15:22:11 -03006141 /* Models with Intel Extreme Graphics 2 */
6142 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC),
Henrique de Moraes Holschuha9f8eac2009-12-09 01:36:21 +00006143 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6144 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006145
6146 /* Models with Intel GMA900 */
6147 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
6148 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
6149 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
6150};
6151
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006152static int __init brightness_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006153{
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006154 int b;
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006155 unsigned long quirks;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006156
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006157 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
6158
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03006159 mutex_init(&brightness_mutex);
6160
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006161 quirks = tpacpi_check_quirks(brightness_quirk_table,
6162 ARRAY_SIZE(brightness_quirk_table));
6163
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03006164 /*
6165 * We always attempt to detect acpi support, so as to switch
6166 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6167 * going to publish a backlight interface
6168 */
6169 b = tpacpi_check_std_acpi_brightness_support();
6170 if (b > 0) {
Thomas Renninger2dba1b52008-08-01 17:38:03 +02006171
6172 if (acpi_video_backlight_support()) {
6173 if (brightness_enable > 1) {
6174 printk(TPACPI_NOTICE
6175 "Standard ACPI backlight interface "
6176 "available, not loading native one.\n");
6177 return 1;
6178 } else if (brightness_enable == 1) {
6179 printk(TPACPI_NOTICE
6180 "Backlight control force enabled, even if standard "
6181 "ACPI backlight interface is available\n");
6182 }
6183 } else {
6184 if (brightness_enable > 1) {
6185 printk(TPACPI_NOTICE
6186 "Standard ACPI backlight interface not "
6187 "available, thinkpad_acpi native "
6188 "brightness control enabled\n");
6189 }
Henrique de Moraes Holschuhe11e2112007-10-30 17:46:22 -02006190 }
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02006191 }
6192
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03006193 if (!brightness_enable) {
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006194 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03006195 "brightness support disabled by "
6196 "module parameter\n");
6197 return 1;
6198 }
6199
6200 if (b > 16) {
6201 printk(TPACPI_ERR
6202 "Unsupported brightness interface, "
6203 "please contact %s\n", TPACPI_MAIL);
6204 return 1;
6205 }
6206 if (b == 16)
6207 tp_features.bright_16levels = 1;
6208
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006209 /*
6210 * Check for module parameter bogosity, note that we
6211 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
6212 * able to detect "unspecified"
6213 */
6214 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03006215 return -EINVAL;
6216
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006217 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
6218 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
6219 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006220 if (quirks & TPACPI_BRGHT_Q_EC)
6221 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
6222 else
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006223 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
6224
6225 dbg_printk(TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006226 "driver auto-selected brightness_mode=%d\n",
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006227 brightness_mode);
6228 }
6229
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03006230 /* Safety */
6231 if (thinkpad_id.vendor != PCI_VENDOR_ID_IBM &&
6232 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
6233 brightness_mode == TPACPI_BRGHT_MODE_EC))
6234 return -EINVAL;
6235
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006236 if (tpacpi_brightness_get_raw(&b) < 0)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03006237 return 1;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006238
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02006239 if (tp_features.bright_16levels)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006240 printk(TPACPI_INFO
6241 "detected a 16-level brightness capable ThinkPad\n");
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02006242
Henrique de Moraes Holschuh7d5a0152007-04-24 11:48:20 -03006243 ibm_backlight_device = backlight_device_register(
6244 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
6245 &ibm_backlight_data);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006246 if (IS_ERR(ibm_backlight_device)) {
Henrique de Moraes Holschuh435c47e2009-09-20 14:09:22 -03006247 int rc = PTR_ERR(ibm_backlight_device);
6248 ibm_backlight_device = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006249 printk(TPACPI_ERR "Could not register backlight device\n");
Henrique de Moraes Holschuh435c47e2009-09-20 14:09:22 -03006250 return rc;
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006251 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006252 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6253 "brightness is supported\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006254
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006255 if (quirks & TPACPI_BRGHT_Q_ASK) {
6256 printk(TPACPI_NOTICE
6257 "brightness: will use unverified default: "
6258 "brightness_mode=%d\n", brightness_mode);
6259 printk(TPACPI_NOTICE
6260 "brightness: please report to %s whether it works well "
6261 "or not on your ThinkPad\n", TPACPI_MAIL);
6262 }
6263
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02006264 ibm_backlight_device->props.max_brightness =
6265 (tp_features.bright_16levels)? 15 : 7;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006266 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006267 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00006268
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00006269 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6270 "brightness: registering brightness hotkeys "
6271 "as change notification\n");
6272 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6273 | TP_ACPI_HKEY_BRGHTUP_MASK
6274 | TP_ACPI_HKEY_BRGHTDWN_MASK);;
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006275 return 0;
6276}
6277
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006278static void brightness_suspend(pm_message_t state)
6279{
6280 tpacpi_brightness_checkpoint_nvram();
6281}
6282
6283static void brightness_shutdown(void)
6284{
6285 tpacpi_brightness_checkpoint_nvram();
6286}
6287
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006288static void brightness_exit(void)
6289{
6290 if (ibm_backlight_device) {
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006291 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006292 "calling backlight_device_unregister()\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006293 backlight_device_unregister(ibm_backlight_device);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006294 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006295
6296 tpacpi_brightness_checkpoint_nvram();
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006297}
6298
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006299static int brightness_read(struct seq_file *m)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006300{
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006301 int level;
6302
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006303 level = brightness_get(NULL);
6304 if (level < 0) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006305 seq_printf(m, "level:\t\tunreadable\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006306 } else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006307 seq_printf(m, "level:\t\t%d\n", level);
6308 seq_printf(m, "commands:\tup, down\n");
6309 seq_printf(m, "commands:\tlevel <level>"
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02006310 " (<level> is 0-%d)\n",
6311 (tp_features.bright_16levels) ? 15 : 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006312 }
6313
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006314 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006315}
6316
6317static int brightness_write(char *buf)
6318{
6319 int level;
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006320 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006321 char *cmd;
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02006322 int max_level = (tp_features.bright_16levels) ? 15 : 7;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006323
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006324 level = brightness_get(NULL);
6325 if (level < 0)
6326 return level;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006327
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006328 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006329 if (strlencmp(cmd, "up") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006330 if (level < max_level)
6331 level++;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006332 } else if (strlencmp(cmd, "down") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006333 if (level > 0)
6334 level--;
6335 } else if (sscanf(cmd, "level %d", &level) == 1 &&
6336 level >= 0 && level <= max_level) {
6337 /* new level set */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006338 } else
6339 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006340 }
6341
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006342 tpacpi_disclose_usertask("procfs brightness",
6343 "set level to %d\n", level);
6344
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006345 /*
6346 * Now we know what the final level should be, so we try to set it.
6347 * Doing it this way makes the syscall restartable in case of EINTR
6348 */
6349 rc = brightness_set(level);
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00006350 if (!rc && ibm_backlight_device)
6351 backlight_force_update(ibm_backlight_device,
6352 BACKLIGHT_UPDATE_SYSFS);
Roel Kluin80a8d122009-11-20 19:48:23 +01006353 return (rc == -EINTR)? -ERESTARTSYS : rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006354}
6355
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006356static struct ibm_struct brightness_driver_data = {
6357 .name = "brightness",
6358 .read = brightness_read,
6359 .write = brightness_write,
6360 .exit = brightness_exit,
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006361 .suspend = brightness_suspend,
6362 .shutdown = brightness_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006363};
6364
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006365/*************************************************************************
6366 * Volume subdriver
6367 */
6368
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006369/*
6370 * IBM ThinkPads have a simple volume controller with MUTE gating.
6371 * Very early Lenovo ThinkPads follow the IBM ThinkPad spec.
6372 *
6373 * Since the *61 series (and probably also the later *60 series), Lenovo
6374 * ThinkPads only implement the MUTE gate.
6375 *
6376 * EC register 0x30
6377 * Bit 6: MUTE (1 mutes sound)
6378 * Bit 3-0: Volume
6379 * Other bits should be zero as far as we know.
6380 *
6381 * This is also stored in CMOS NVRAM, byte 0x60, bit 6 (MUTE), and
6382 * bits 3-0 (volume). Other bits in NVRAM may have other functions,
6383 * such as bit 7 which is used to detect repeated presses of MUTE,
6384 * and we leave them unchanged.
6385 */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006386
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02006387#ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
6388
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006389#define TPACPI_ALSA_DRVNAME "ThinkPad EC"
6390#define TPACPI_ALSA_SHRTNAME "ThinkPad Console Audio Control"
6391#define TPACPI_ALSA_MIXERNAME TPACPI_ALSA_SHRTNAME
6392
Henrique de Moraes Holschuhead510c2009-12-26 22:52:13 -02006393static int alsa_index = ~((1 << (SNDRV_CARDS - 3)) - 1); /* last three slots */
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006394static char *alsa_id = "ThinkPadEC";
6395static int alsa_enable = SNDRV_DEFAULT_ENABLE1;
6396
6397struct tpacpi_alsa_data {
6398 struct snd_card *card;
6399 struct snd_ctl_elem_id *ctl_mute_id;
6400 struct snd_ctl_elem_id *ctl_vol_id;
6401};
6402
6403static struct snd_card *alsa_card;
6404
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006405enum {
6406 TP_EC_AUDIO = 0x30,
6407
6408 /* TP_EC_AUDIO bits */
6409 TP_EC_AUDIO_MUTESW = 6,
6410
6411 /* TP_EC_AUDIO bitmasks */
6412 TP_EC_AUDIO_LVL_MSK = 0x0F,
6413 TP_EC_AUDIO_MUTESW_MSK = (1 << TP_EC_AUDIO_MUTESW),
6414
6415 /* Maximum volume */
6416 TP_EC_VOLUME_MAX = 14,
6417};
6418
6419enum tpacpi_volume_access_mode {
6420 TPACPI_VOL_MODE_AUTO = 0, /* Not implemented yet */
6421 TPACPI_VOL_MODE_EC, /* Pure EC control */
6422 TPACPI_VOL_MODE_UCMS_STEP, /* UCMS step-based control: N/A */
6423 TPACPI_VOL_MODE_ECNVRAM, /* EC control w/ NVRAM store */
6424 TPACPI_VOL_MODE_MAX
6425};
6426
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006427enum tpacpi_volume_capabilities {
6428 TPACPI_VOL_CAP_AUTO = 0, /* Use white/blacklist */
6429 TPACPI_VOL_CAP_VOLMUTE, /* Output vol and mute */
6430 TPACPI_VOL_CAP_MUTEONLY, /* Output mute only */
6431 TPACPI_VOL_CAP_MAX
6432};
6433
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006434static enum tpacpi_volume_access_mode volume_mode =
6435 TPACPI_VOL_MODE_MAX;
6436
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006437static enum tpacpi_volume_capabilities volume_capabilities;
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006438static int volume_control_allowed;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006439
6440/*
6441 * Used to syncronize writers to TP_EC_AUDIO and
6442 * TP_NVRAM_ADDR_MIXER, as we need to do read-modify-write
6443 */
6444static struct mutex volume_mutex;
6445
6446static void tpacpi_volume_checkpoint_nvram(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006447{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006448 u8 lec = 0;
6449 u8 b_nvram;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006450 u8 ec_mask;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006451
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006452 if (volume_mode != TPACPI_VOL_MODE_ECNVRAM)
6453 return;
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006454 if (!volume_control_allowed)
6455 return;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006456
6457 vdbg_printk(TPACPI_DBG_MIXER,
6458 "trying to checkpoint mixer state to NVRAM...\n");
6459
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006460 if (tp_features.mixer_no_level_control)
6461 ec_mask = TP_EC_AUDIO_MUTESW_MSK;
6462 else
6463 ec_mask = TP_EC_AUDIO_MUTESW_MSK | TP_EC_AUDIO_LVL_MSK;
6464
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006465 if (mutex_lock_killable(&volume_mutex) < 0)
6466 return;
6467
6468 if (unlikely(!acpi_ec_read(TP_EC_AUDIO, &lec)))
6469 goto unlock;
6470 lec &= ec_mask;
6471 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
6472
6473 if (lec != (b_nvram & ec_mask)) {
6474 /* NVRAM needs update */
6475 b_nvram &= ~ec_mask;
6476 b_nvram |= lec;
6477 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_MIXER);
6478 dbg_printk(TPACPI_DBG_MIXER,
6479 "updated NVRAM mixer status to 0x%02x (0x%02x)\n",
6480 (unsigned int) lec, (unsigned int) b_nvram);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006481 } else {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006482 vdbg_printk(TPACPI_DBG_MIXER,
6483 "NVRAM mixer status already is 0x%02x (0x%02x)\n",
6484 (unsigned int) lec, (unsigned int) b_nvram);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006485 }
6486
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006487unlock:
6488 mutex_unlock(&volume_mutex);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006489}
6490
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006491static int volume_get_status_ec(u8 *status)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006492{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006493 u8 s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006494
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006495 if (!acpi_ec_read(TP_EC_AUDIO, &s))
6496 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006497
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006498 *status = s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006499
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006500 dbg_printk(TPACPI_DBG_MIXER, "status 0x%02x\n", s);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006501
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006502 return 0;
6503}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006504
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006505static int volume_get_status(u8 *status)
6506{
6507 return volume_get_status_ec(status);
6508}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006509
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006510static int volume_set_status_ec(const u8 status)
6511{
6512 if (!acpi_ec_write(TP_EC_AUDIO, status))
6513 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006514
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006515 dbg_printk(TPACPI_DBG_MIXER, "set EC mixer to 0x%02x\n", status);
6516
6517 return 0;
6518}
6519
6520static int volume_set_status(const u8 status)
6521{
6522 return volume_set_status_ec(status);
6523}
6524
6525static int volume_set_mute_ec(const bool mute)
6526{
6527 int rc;
6528 u8 s, n;
6529
6530 if (mutex_lock_killable(&volume_mutex) < 0)
6531 return -EINTR;
6532
6533 rc = volume_get_status_ec(&s);
6534 if (rc)
6535 goto unlock;
6536
6537 n = (mute) ? s | TP_EC_AUDIO_MUTESW_MSK :
6538 s & ~TP_EC_AUDIO_MUTESW_MSK;
6539
6540 if (n != s)
6541 rc = volume_set_status_ec(n);
6542
6543unlock:
6544 mutex_unlock(&volume_mutex);
6545 return rc;
6546}
6547
6548static int volume_set_mute(const bool mute)
6549{
6550 dbg_printk(TPACPI_DBG_MIXER, "trying to %smute\n",
6551 (mute) ? "" : "un");
6552 return volume_set_mute_ec(mute);
6553}
6554
6555static int volume_set_volume_ec(const u8 vol)
6556{
6557 int rc;
6558 u8 s, n;
6559
6560 if (vol > TP_EC_VOLUME_MAX)
6561 return -EINVAL;
6562
6563 if (mutex_lock_killable(&volume_mutex) < 0)
6564 return -EINTR;
6565
6566 rc = volume_get_status_ec(&s);
6567 if (rc)
6568 goto unlock;
6569
6570 n = (s & ~TP_EC_AUDIO_LVL_MSK) | vol;
6571
6572 if (n != s)
6573 rc = volume_set_status_ec(n);
6574
6575unlock:
6576 mutex_unlock(&volume_mutex);
6577 return rc;
6578}
6579
6580static int volume_set_volume(const u8 vol)
6581{
6582 dbg_printk(TPACPI_DBG_MIXER,
6583 "trying to set volume level to %hu\n", vol);
6584 return volume_set_volume_ec(vol);
6585}
6586
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006587static void volume_alsa_notify_change(void)
6588{
6589 struct tpacpi_alsa_data *d;
6590
6591 if (alsa_card && alsa_card->private_data) {
6592 d = alsa_card->private_data;
6593 if (d->ctl_mute_id)
6594 snd_ctl_notify(alsa_card,
6595 SNDRV_CTL_EVENT_MASK_VALUE,
6596 d->ctl_mute_id);
6597 if (d->ctl_vol_id)
6598 snd_ctl_notify(alsa_card,
6599 SNDRV_CTL_EVENT_MASK_VALUE,
6600 d->ctl_vol_id);
6601 }
6602}
6603
6604static int volume_alsa_vol_info(struct snd_kcontrol *kcontrol,
6605 struct snd_ctl_elem_info *uinfo)
6606{
6607 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
6608 uinfo->count = 1;
6609 uinfo->value.integer.min = 0;
6610 uinfo->value.integer.max = TP_EC_VOLUME_MAX;
6611 return 0;
6612}
6613
6614static int volume_alsa_vol_get(struct snd_kcontrol *kcontrol,
6615 struct snd_ctl_elem_value *ucontrol)
6616{
6617 u8 s;
6618 int rc;
6619
6620 rc = volume_get_status(&s);
6621 if (rc < 0)
6622 return rc;
6623
6624 ucontrol->value.integer.value[0] = s & TP_EC_AUDIO_LVL_MSK;
6625 return 0;
6626}
6627
6628static int volume_alsa_vol_put(struct snd_kcontrol *kcontrol,
6629 struct snd_ctl_elem_value *ucontrol)
6630{
6631 return volume_set_volume(ucontrol->value.integer.value[0]);
6632}
6633
6634#define volume_alsa_mute_info snd_ctl_boolean_mono_info
6635
6636static int volume_alsa_mute_get(struct snd_kcontrol *kcontrol,
6637 struct snd_ctl_elem_value *ucontrol)
6638{
6639 u8 s;
6640 int rc;
6641
6642 rc = volume_get_status(&s);
6643 if (rc < 0)
6644 return rc;
6645
6646 ucontrol->value.integer.value[0] =
6647 (s & TP_EC_AUDIO_MUTESW_MSK) ? 0 : 1;
6648 return 0;
6649}
6650
6651static int volume_alsa_mute_put(struct snd_kcontrol *kcontrol,
6652 struct snd_ctl_elem_value *ucontrol)
6653{
6654 return volume_set_mute(!ucontrol->value.integer.value[0]);
6655}
6656
6657static struct snd_kcontrol_new volume_alsa_control_vol __devinitdata = {
6658 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6659 .name = "Console Playback Volume",
6660 .index = 0,
6661 .access = SNDRV_CTL_ELEM_ACCESS_READ,
6662 .info = volume_alsa_vol_info,
6663 .get = volume_alsa_vol_get,
6664};
6665
6666static struct snd_kcontrol_new volume_alsa_control_mute __devinitdata = {
6667 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6668 .name = "Console Playback Switch",
6669 .index = 0,
6670 .access = SNDRV_CTL_ELEM_ACCESS_READ,
6671 .info = volume_alsa_mute_info,
6672 .get = volume_alsa_mute_get,
6673};
6674
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006675static void volume_suspend(pm_message_t state)
6676{
6677 tpacpi_volume_checkpoint_nvram();
6678}
6679
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006680static void volume_resume(void)
6681{
6682 volume_alsa_notify_change();
6683}
6684
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006685static void volume_shutdown(void)
6686{
6687 tpacpi_volume_checkpoint_nvram();
6688}
6689
6690static void volume_exit(void)
6691{
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006692 if (alsa_card) {
6693 snd_card_free(alsa_card);
6694 alsa_card = NULL;
6695 }
6696
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006697 tpacpi_volume_checkpoint_nvram();
6698}
6699
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006700static int __init volume_create_alsa_mixer(void)
6701{
6702 struct snd_card *card;
6703 struct tpacpi_alsa_data *data;
6704 struct snd_kcontrol *ctl_vol;
6705 struct snd_kcontrol *ctl_mute;
6706 int rc;
6707
6708 rc = snd_card_create(alsa_index, alsa_id, THIS_MODULE,
6709 sizeof(struct tpacpi_alsa_data), &card);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02006710 if (rc < 0 || !card) {
6711 printk(TPACPI_ERR
6712 "Failed to create ALSA card structures: %d\n", rc);
6713 return 1;
6714 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006715
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
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02006747 "Failed to create ALSA volume control: %d\n",
6748 rc);
6749 goto err_exit;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006750 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006751 data->ctl_vol_id = &ctl_vol->id;
6752 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006753
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006754 ctl_mute = snd_ctl_new1(&volume_alsa_control_mute, NULL);
6755 rc = snd_ctl_add(card, ctl_mute);
6756 if (rc < 0) {
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02006757 printk(TPACPI_ERR "Failed to create ALSA mute control: %d\n",
6758 rc);
6759 goto err_exit;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006760 }
6761 data->ctl_mute_id = &ctl_mute->id;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006762
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006763 snd_card_set_dev(card, &tpacpi_pdev->dev);
6764 rc = snd_card_register(card);
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006765 if (rc < 0) {
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02006766 printk(TPACPI_ERR "Failed to register ALSA card: %d\n", rc);
6767 goto err_exit;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006768 }
6769
6770 alsa_card = card;
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02006771 return 0;
6772
6773err_exit:
6774 snd_card_free(card);
6775 return 1;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006776}
6777
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006778#define TPACPI_VOL_Q_MUTEONLY 0x0001 /* Mute-only control available */
6779#define TPACPI_VOL_Q_LEVEL 0x0002 /* Volume control available */
6780
6781static const struct tpacpi_quirk volume_quirk_table[] __initconst = {
6782 /* Whitelist volume level on all IBM by default */
6783 { .vendor = PCI_VENDOR_ID_IBM,
6784 .bios = TPACPI_MATCH_ANY,
6785 .ec = TPACPI_MATCH_ANY,
6786 .quirks = TPACPI_VOL_Q_LEVEL },
6787
6788 /* Lenovo models with volume control (needs confirmation) */
6789 TPACPI_QEC_LNV('7', 'C', TPACPI_VOL_Q_LEVEL), /* R60/i */
6790 TPACPI_QEC_LNV('7', 'E', TPACPI_VOL_Q_LEVEL), /* R60e/i */
6791 TPACPI_QEC_LNV('7', '9', TPACPI_VOL_Q_LEVEL), /* T60/p */
6792 TPACPI_QEC_LNV('7', 'B', TPACPI_VOL_Q_LEVEL), /* X60/s */
6793 TPACPI_QEC_LNV('7', 'J', TPACPI_VOL_Q_LEVEL), /* X60t */
6794 TPACPI_QEC_LNV('7', '7', TPACPI_VOL_Q_LEVEL), /* Z60 */
6795 TPACPI_QEC_LNV('7', 'F', TPACPI_VOL_Q_LEVEL), /* Z61 */
6796
6797 /* Whitelist mute-only on all Lenovo by default */
6798 { .vendor = PCI_VENDOR_ID_LENOVO,
6799 .bios = TPACPI_MATCH_ANY,
6800 .ec = TPACPI_MATCH_ANY,
6801 .quirks = TPACPI_VOL_Q_MUTEONLY }
6802};
6803
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006804static int __init volume_init(struct ibm_init_struct *iibm)
6805{
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006806 unsigned long quirks;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006807 int rc;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006808
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006809 vdbg_printk(TPACPI_DBG_INIT, "initializing volume subdriver\n");
6810
6811 mutex_init(&volume_mutex);
6812
6813 /*
6814 * Check for module parameter bogosity, note that we
6815 * init volume_mode to TPACPI_VOL_MODE_MAX in order to be
6816 * able to detect "unspecified"
6817 */
6818 if (volume_mode > TPACPI_VOL_MODE_MAX)
6819 return -EINVAL;
6820
6821 if (volume_mode == TPACPI_VOL_MODE_UCMS_STEP) {
6822 printk(TPACPI_ERR
6823 "UCMS step volume mode not implemented, "
6824 "please contact %s\n", TPACPI_MAIL);
6825 return 1;
6826 }
6827
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006828 if (volume_capabilities >= TPACPI_VOL_CAP_MAX)
6829 return -EINVAL;
6830
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006831 /*
6832 * The ALSA mixer is our primary interface.
6833 * When disabled, don't install the subdriver at all
6834 */
6835 if (!alsa_enable) {
6836 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6837 "ALSA mixer disabled by parameter, "
6838 "not loading volume subdriver...\n");
6839 return 1;
6840 }
6841
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006842 quirks = tpacpi_check_quirks(volume_quirk_table,
6843 ARRAY_SIZE(volume_quirk_table));
6844
6845 switch (volume_capabilities) {
6846 case TPACPI_VOL_CAP_AUTO:
6847 if (quirks & TPACPI_VOL_Q_MUTEONLY)
6848 tp_features.mixer_no_level_control = 1;
6849 else if (quirks & TPACPI_VOL_Q_LEVEL)
6850 tp_features.mixer_no_level_control = 0;
6851 else
6852 return 1; /* no mixer */
6853 break;
6854 case TPACPI_VOL_CAP_VOLMUTE:
6855 tp_features.mixer_no_level_control = 0;
6856 break;
6857 case TPACPI_VOL_CAP_MUTEONLY:
6858 tp_features.mixer_no_level_control = 1;
6859 break;
6860 default:
6861 return 1;
6862 }
6863
6864 if (volume_capabilities != TPACPI_VOL_CAP_AUTO)
6865 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6866 "using user-supplied volume_capabilities=%d\n",
6867 volume_capabilities);
6868
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006869 if (volume_mode == TPACPI_VOL_MODE_AUTO ||
6870 volume_mode == TPACPI_VOL_MODE_MAX) {
6871 volume_mode = TPACPI_VOL_MODE_ECNVRAM;
6872
6873 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6874 "driver auto-selected volume_mode=%d\n",
6875 volume_mode);
6876 } else {
6877 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6878 "using user-supplied volume_mode=%d\n",
6879 volume_mode);
6880 }
6881
6882 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006883 "mute is supported, volume control is %s\n",
6884 str_supported(!tp_features.mixer_no_level_control));
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006885
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006886 rc = volume_create_alsa_mixer();
6887 if (rc) {
6888 printk(TPACPI_ERR
6889 "Could not create the ALSA mixer interface\n");
6890 return rc;
6891 }
6892
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006893 printk(TPACPI_INFO
6894 "Console audio control enabled, mode: %s\n",
6895 (volume_control_allowed) ?
6896 "override (read/write)" :
6897 "monitor (read only)");
6898
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006899 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6900 "registering volume hotkeys as change notification\n");
6901 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6902 | TP_ACPI_HKEY_VOLUP_MASK
6903 | TP_ACPI_HKEY_VOLDWN_MASK
6904 | TP_ACPI_HKEY_MUTE_MASK);
6905
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006906 return 0;
6907}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006908
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006909static int volume_read(struct seq_file *m)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006910{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006911 u8 status;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006912
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006913 if (volume_get_status(&status) < 0) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006914 seq_printf(m, "level:\t\tunreadable\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006915 } else {
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006916 if (tp_features.mixer_no_level_control)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006917 seq_printf(m, "level:\t\tunsupported\n");
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006918 else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006919 seq_printf(m, "level:\t\t%d\n",
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006920 status & TP_EC_AUDIO_LVL_MSK);
6921
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006922 seq_printf(m, "mute:\t\t%s\n",
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006923 onoff(status, TP_EC_AUDIO_MUTESW));
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006924
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006925 if (volume_control_allowed) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006926 seq_printf(m, "commands:\tunmute, mute\n");
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006927 if (!tp_features.mixer_no_level_control) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006928 seq_printf(m,
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006929 "commands:\tup, down\n");
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006930 seq_printf(m,
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006931 "commands:\tlevel <level>"
6932 " (<level> is 0-%d)\n",
6933 TP_EC_VOLUME_MAX);
6934 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006935 }
6936 }
6937
6938 return 0;
6939}
6940
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006941static int volume_write(char *buf)
6942{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006943 u8 s;
6944 u8 new_level, new_mute;
6945 int l;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006946 char *cmd;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006947 int rc;
6948
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006949 /*
6950 * We do allow volume control at driver startup, so that the
6951 * user can set initial state through the volume=... parameter hack.
6952 */
6953 if (!volume_control_allowed && tpacpi_lifecycle != TPACPI_LIFE_INIT) {
6954 if (unlikely(!tp_warned.volume_ctrl_forbidden)) {
6955 tp_warned.volume_ctrl_forbidden = 1;
6956 printk(TPACPI_NOTICE
6957 "Console audio control in monitor mode, "
6958 "changes are not allowed.\n");
6959 printk(TPACPI_NOTICE
6960 "Use the volume_control=1 module parameter "
6961 "to enable volume control\n");
6962 }
6963 return -EPERM;
6964 }
6965
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006966 rc = volume_get_status(&s);
6967 if (rc < 0)
6968 return rc;
6969
6970 new_level = s & TP_EC_AUDIO_LVL_MSK;
6971 new_mute = s & TP_EC_AUDIO_MUTESW_MSK;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006972
6973 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006974 if (!tp_features.mixer_no_level_control) {
6975 if (strlencmp(cmd, "up") == 0) {
6976 if (new_mute)
6977 new_mute = 0;
6978 else if (new_level < TP_EC_VOLUME_MAX)
6979 new_level++;
6980 continue;
6981 } else if (strlencmp(cmd, "down") == 0) {
6982 if (new_mute)
6983 new_mute = 0;
6984 else if (new_level > 0)
6985 new_level--;
6986 continue;
6987 } else if (sscanf(cmd, "level %u", &l) == 1 &&
6988 l >= 0 && l <= TP_EC_VOLUME_MAX) {
6989 new_level = l;
6990 continue;
6991 }
6992 }
6993 if (strlencmp(cmd, "mute") == 0)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006994 new_mute = TP_EC_AUDIO_MUTESW_MSK;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006995 else if (strlencmp(cmd, "unmute") == 0)
6996 new_mute = 0;
6997 else
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006998 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006999 }
7000
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007001 if (tp_features.mixer_no_level_control) {
7002 tpacpi_disclose_usertask("procfs volume", "%smute\n",
7003 new_mute ? "" : "un");
7004 rc = volume_set_mute(!!new_mute);
7005 } else {
7006 tpacpi_disclose_usertask("procfs volume",
7007 "%smute and set level to %d\n",
7008 new_mute ? "" : "un", new_level);
7009 rc = volume_set_status(new_mute | new_level);
7010 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007011 volume_alsa_notify_change();
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007012
7013 return (rc == -EINTR) ? -ERESTARTSYS : rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007014}
7015
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007016static struct ibm_struct volume_driver_data = {
7017 .name = "volume",
7018 .read = volume_read,
7019 .write = volume_write,
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007020 .exit = volume_exit,
7021 .suspend = volume_suspend,
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007022 .resume = volume_resume,
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007023 .shutdown = volume_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007024};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007025
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02007026#else /* !CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7027
7028#define alsa_card NULL
7029
7030static void inline volume_alsa_notify_change(void)
7031{
7032}
7033
7034static int __init volume_init(struct ibm_init_struct *iibm)
7035{
7036 printk(TPACPI_INFO
7037 "volume: disabled as there is no ALSA support in this kernel\n");
7038
7039 return 1;
7040}
7041
7042static struct ibm_struct volume_driver_data = {
7043 .name = "volume",
7044};
7045
7046#endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7047
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007048/*************************************************************************
7049 * Fan subdriver
7050 */
7051
7052/*
7053 * FAN ACCESS MODES
7054 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007055 * TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007056 * ACPI GFAN method: returns fan level
7057 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007058 * see TPACPI_FAN_WR_ACPI_SFAN
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007059 * EC 0x2f (HFSP) not available if GFAN exists
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007060 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007061 * TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007062 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
7063 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007064 * EC 0x2f (HFSP) might be available *for reading*, but do not use
7065 * it for writing.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007066 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007067 * TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007068 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
7069 * Supported on almost all ThinkPads
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007070 *
7071 * Fan speed changes of any sort (including those caused by the
7072 * disengaged mode) are usually done slowly by the firmware as the
7073 * maximum ammount of fan duty cycle change per second seems to be
7074 * limited.
7075 *
7076 * Reading is not available if GFAN exists.
7077 * Writing is not available if SFAN exists.
7078 *
7079 * Bits
7080 * 7 automatic mode engaged;
7081 * (default operation mode of the ThinkPad)
7082 * fan level is ignored in this mode.
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007083 * 6 full speed mode (takes precedence over bit 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007084 * not available on all thinkpads. May disable
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007085 * the tachometer while the fan controller ramps up
7086 * the speed (which can take up to a few *minutes*).
7087 * Speeds up fan to 100% duty-cycle, which is far above
7088 * the standard RPM levels. It is not impossible that
7089 * it could cause hardware damage.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007090 * 5-3 unused in some models. Extra bits for fan level
7091 * in others, but still useless as all values above
7092 * 7 map to the same speed as level 7 in these models.
7093 * 2-0 fan level (0..7 usually)
7094 * 0x00 = stop
7095 * 0x07 = max (set when temperatures critical)
7096 * Some ThinkPads may have other levels, see
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007097 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007098 *
7099 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
7100 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
7101 * does so, its initial value is meaningless (0x07).
7102 *
7103 * For firmware bugs, refer to:
7104 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7105 *
7106 * ----
7107 *
7108 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
7109 * Main fan tachometer reading (in RPM)
7110 *
7111 * This register is present on all ThinkPads with a new-style EC, and
7112 * it is known not to be present on the A21m/e, and T22, as there is
7113 * something else in offset 0x84 according to the ACPI DSDT. Other
7114 * ThinkPads from this same time period (and earlier) probably lack the
7115 * tachometer as well.
7116 *
Nick Andrew877d0312009-01-26 11:06:57 +01007117 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007118 * was never fixed by IBM to report the EC firmware version string
7119 * probably support the tachometer (like the early X models), so
7120 * detecting it is quite hard. We need more data to know for sure.
7121 *
7122 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
7123 * might result.
7124 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007125 * FIRMWARE BUG: may go stale while the EC is switching to full speed
7126 * mode.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007127 *
7128 * For firmware bugs, refer to:
7129 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7130 *
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007131 * ----
7132 *
7133 * ThinkPad EC register 0x31 bit 0 (only on select models)
7134 *
7135 * When bit 0 of EC register 0x31 is zero, the tachometer registers
7136 * show the speed of the main fan. When bit 0 of EC register 0x31
7137 * is one, the tachometer registers show the speed of the auxiliary
7138 * fan.
7139 *
7140 * Fan control seems to affect both fans, regardless of the state
7141 * of this bit.
7142 *
7143 * So far, only the firmware for the X60/X61 non-tablet versions
7144 * seem to support this (firmware TP-7M).
7145 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007146 * TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007147 * ThinkPad X31, X40, X41. Not available in the X60.
7148 *
7149 * FANS ACPI handle: takes three arguments: low speed, medium speed,
7150 * high speed. ACPI DSDT seems to map these three speeds to levels
7151 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
7152 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
7153 *
7154 * The speeds are stored on handles
7155 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
7156 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007157 * There are three default speed sets, accessible as handles:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007158 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
7159 *
7160 * ACPI DSDT switches which set is in use depending on various
7161 * factors.
7162 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007163 * TPACPI_FAN_WR_TPEC is also available and should be used to
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007164 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
7165 * but the ACPI tables just mention level 7.
7166 */
7167
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007168enum { /* Fan control constants */
7169 fan_status_offset = 0x2f, /* EC register 0x2f */
7170 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
7171 * 0x84 must be read before 0x85 */
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007172 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
7173 bit 0 selects which fan is active */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007174
7175 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
7176 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
7177
7178 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
7179};
7180
7181enum fan_status_access_mode {
7182 TPACPI_FAN_NONE = 0, /* No fan status or control */
7183 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
7184 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
7185};
7186
7187enum fan_control_access_mode {
7188 TPACPI_FAN_WR_NONE = 0, /* No fan control */
7189 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
7190 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
7191 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
7192};
7193
7194enum fan_control_commands {
7195 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
7196 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
7197 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
7198 * and also watchdog cmd */
7199};
7200
7201static int fan_control_allowed;
7202
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007203static enum fan_status_access_mode fan_status_access_mode;
7204static enum fan_control_access_mode fan_control_access_mode;
7205static enum fan_control_commands fan_control_commands;
7206
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02007207static u8 fan_control_initial_status;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007208static u8 fan_control_desired_level;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007209static u8 fan_control_resume_level;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007210static int fan_watchdog_maxinterval;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007211
7212static struct mutex fan_mutex;
7213
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007214static void fan_watchdog_fire(struct work_struct *ignored);
Len Brown25c68a32006-12-08 04:43:41 -05007215static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007216
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007217TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
7218TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007219 "\\FSPD", /* 600e/x, 770e, 770x */
7220 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007221TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007222 "JFNS", /* 770x-JL */
7223 ); /* all others */
7224
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007225/*
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007226 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
7227 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
7228 * be in auto mode (0x80).
7229 *
7230 * This is corrected by any write to HFSP either by the driver, or
7231 * by the firmware.
7232 *
7233 * We assume 0x07 really means auto mode while this quirk is active,
7234 * as this is far more likely than the ThinkPad being in level 7,
7235 * which is only used by the firmware during thermal emergencies.
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007236 *
7237 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
7238 * TP-70 (T43, R52), which are known to be buggy.
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007239 */
7240
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007241static void fan_quirk1_setup(void)
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007242{
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007243 if (fan_control_initial_status == 0x07) {
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007244 printk(TPACPI_NOTICE
7245 "fan_init: initial fan status is unknown, "
7246 "assuming it is in auto mode\n");
7247 tp_features.fan_ctrl_status_undef = 1;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007248 }
7249}
7250
7251static void fan_quirk1_handle(u8 *fan_status)
7252{
7253 if (unlikely(tp_features.fan_ctrl_status_undef)) {
7254 if (*fan_status != fan_control_initial_status) {
7255 /* something changed the HFSP regisnter since
7256 * driver init time, so it is not undefined
7257 * anymore */
7258 tp_features.fan_ctrl_status_undef = 0;
7259 } else {
7260 /* Return most likely status. In fact, it
7261 * might be the only possible status */
7262 *fan_status = TP_EC_FAN_AUTO;
7263 }
7264 }
7265}
7266
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007267/* Select main fan on X60/X61, NOOP on others */
7268static bool fan_select_fan1(void)
7269{
7270 if (tp_features.second_fan) {
7271 u8 val;
7272
7273 if (ec_read(fan_select_offset, &val) < 0)
7274 return false;
7275 val &= 0xFEU;
7276 if (ec_write(fan_select_offset, val) < 0)
7277 return false;
7278 }
7279 return true;
7280}
7281
7282/* Select secondary fan on X60/X61 */
7283static bool fan_select_fan2(void)
7284{
7285 u8 val;
7286
7287 if (!tp_features.second_fan)
7288 return false;
7289
7290 if (ec_read(fan_select_offset, &val) < 0)
7291 return false;
7292 val |= 0x01U;
7293 if (ec_write(fan_select_offset, val) < 0)
7294 return false;
7295
7296 return true;
7297}
7298
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007299/*
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007300 * Call with fan_mutex held
7301 */
7302static void fan_update_desired_level(u8 status)
7303{
7304 if ((status &
7305 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7306 if (status > 7)
7307 fan_control_desired_level = 7;
7308 else
7309 fan_control_desired_level = status;
7310 }
7311}
7312
7313static int fan_get_status(u8 *status)
7314{
7315 u8 s;
7316
7317 /* TODO:
7318 * Add TPACPI_FAN_RD_ACPI_FANS ? */
7319
7320 switch (fan_status_access_mode) {
7321 case TPACPI_FAN_RD_ACPI_GFAN:
7322 /* 570, 600e/x, 770e, 770x */
7323
7324 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
7325 return -EIO;
7326
7327 if (likely(status))
7328 *status = s & 0x07;
7329
7330 break;
7331
7332 case TPACPI_FAN_RD_TPEC:
7333 /* all except 570, 600e/x, 770e, 770x */
7334 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
7335 return -EIO;
7336
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007337 if (likely(status)) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007338 *status = s;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007339 fan_quirk1_handle(status);
7340 }
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007341
7342 break;
7343
7344 default:
7345 return -ENXIO;
7346 }
7347
7348 return 0;
7349}
7350
7351static int fan_get_status_safe(u8 *status)
7352{
7353 int rc;
7354 u8 s;
7355
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007356 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007357 return -ERESTARTSYS;
7358 rc = fan_get_status(&s);
7359 if (!rc)
7360 fan_update_desired_level(s);
7361 mutex_unlock(&fan_mutex);
7362
7363 if (status)
7364 *status = s;
7365
7366 return rc;
7367}
7368
7369static int fan_get_speed(unsigned int *speed)
7370{
7371 u8 hi, lo;
7372
7373 switch (fan_status_access_mode) {
7374 case TPACPI_FAN_RD_TPEC:
7375 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007376 if (unlikely(!fan_select_fan1()))
7377 return -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007378 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
7379 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
7380 return -EIO;
7381
7382 if (likely(speed))
7383 *speed = (hi << 8) | lo;
7384
7385 break;
7386
7387 default:
7388 return -ENXIO;
7389 }
7390
7391 return 0;
7392}
7393
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007394static int fan2_get_speed(unsigned int *speed)
7395{
7396 u8 hi, lo;
7397 bool rc;
7398
7399 switch (fan_status_access_mode) {
7400 case TPACPI_FAN_RD_TPEC:
7401 /* all except 570, 600e/x, 770e, 770x */
7402 if (unlikely(!fan_select_fan2()))
7403 return -EIO;
7404 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
7405 !acpi_ec_read(fan_rpm_offset + 1, &hi);
7406 fan_select_fan1(); /* play it safe */
7407 if (rc)
7408 return -EIO;
7409
7410 if (likely(speed))
7411 *speed = (hi << 8) | lo;
7412
7413 break;
7414
7415 default:
7416 return -ENXIO;
7417 }
7418
7419 return 0;
7420}
7421
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007422static int fan_set_level(int level)
7423{
7424 if (!fan_control_allowed)
7425 return -EPERM;
7426
7427 switch (fan_control_access_mode) {
7428 case TPACPI_FAN_WR_ACPI_SFAN:
7429 if (level >= 0 && level <= 7) {
7430 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
7431 return -EIO;
7432 } else
7433 return -EINVAL;
7434 break;
7435
7436 case TPACPI_FAN_WR_ACPI_FANS:
7437 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007438 if (!(level & TP_EC_FAN_AUTO) &&
7439 !(level & TP_EC_FAN_FULLSPEED) &&
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007440 ((level < 0) || (level > 7)))
7441 return -EINVAL;
7442
7443 /* safety net should the EC not support AUTO
7444 * or FULLSPEED mode bits and just ignore them */
7445 if (level & TP_EC_FAN_FULLSPEED)
7446 level |= 7; /* safety min speed 7 */
Roel Kluin547266e2008-02-05 00:24:56 +01007447 else if (level & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007448 level |= 4; /* safety min speed 4 */
7449
7450 if (!acpi_ec_write(fan_status_offset, level))
7451 return -EIO;
7452 else
7453 tp_features.fan_ctrl_status_undef = 0;
7454 break;
7455
7456 default:
7457 return -ENXIO;
7458 }
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007459
7460 vdbg_printk(TPACPI_DBG_FAN,
7461 "fan control: set fan control register to 0x%02x\n", level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007462 return 0;
7463}
7464
7465static int fan_set_level_safe(int level)
7466{
7467 int rc;
7468
7469 if (!fan_control_allowed)
7470 return -EPERM;
7471
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007472 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007473 return -ERESTARTSYS;
7474
7475 if (level == TPACPI_FAN_LAST_LEVEL)
7476 level = fan_control_desired_level;
7477
7478 rc = fan_set_level(level);
7479 if (!rc)
7480 fan_update_desired_level(level);
7481
7482 mutex_unlock(&fan_mutex);
7483 return rc;
7484}
7485
7486static int fan_set_enable(void)
7487{
7488 u8 s;
7489 int rc;
7490
7491 if (!fan_control_allowed)
7492 return -EPERM;
7493
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007494 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007495 return -ERESTARTSYS;
7496
7497 switch (fan_control_access_mode) {
7498 case TPACPI_FAN_WR_ACPI_FANS:
7499 case TPACPI_FAN_WR_TPEC:
7500 rc = fan_get_status(&s);
7501 if (rc < 0)
7502 break;
7503
7504 /* Don't go out of emergency fan mode */
7505 if (s != 7) {
7506 s &= 0x07;
7507 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
7508 }
7509
7510 if (!acpi_ec_write(fan_status_offset, s))
7511 rc = -EIO;
7512 else {
7513 tp_features.fan_ctrl_status_undef = 0;
7514 rc = 0;
7515 }
7516 break;
7517
7518 case TPACPI_FAN_WR_ACPI_SFAN:
7519 rc = fan_get_status(&s);
7520 if (rc < 0)
7521 break;
7522
7523 s &= 0x07;
7524
7525 /* Set fan to at least level 4 */
7526 s |= 4;
7527
7528 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007529 rc = -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007530 else
7531 rc = 0;
7532 break;
7533
7534 default:
7535 rc = -ENXIO;
7536 }
7537
7538 mutex_unlock(&fan_mutex);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007539
7540 if (!rc)
7541 vdbg_printk(TPACPI_DBG_FAN,
7542 "fan control: set fan control register to 0x%02x\n",
7543 s);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007544 return rc;
7545}
7546
7547static int fan_set_disable(void)
7548{
7549 int rc;
7550
7551 if (!fan_control_allowed)
7552 return -EPERM;
7553
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007554 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007555 return -ERESTARTSYS;
7556
7557 rc = 0;
7558 switch (fan_control_access_mode) {
7559 case TPACPI_FAN_WR_ACPI_FANS:
7560 case TPACPI_FAN_WR_TPEC:
7561 if (!acpi_ec_write(fan_status_offset, 0x00))
7562 rc = -EIO;
7563 else {
7564 fan_control_desired_level = 0;
7565 tp_features.fan_ctrl_status_undef = 0;
7566 }
7567 break;
7568
7569 case TPACPI_FAN_WR_ACPI_SFAN:
7570 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
7571 rc = -EIO;
7572 else
7573 fan_control_desired_level = 0;
7574 break;
7575
7576 default:
7577 rc = -ENXIO;
7578 }
7579
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007580 if (!rc)
7581 vdbg_printk(TPACPI_DBG_FAN,
7582 "fan control: set fan control register to 0\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007583
7584 mutex_unlock(&fan_mutex);
7585 return rc;
7586}
7587
7588static int fan_set_speed(int speed)
7589{
7590 int rc;
7591
7592 if (!fan_control_allowed)
7593 return -EPERM;
7594
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007595 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007596 return -ERESTARTSYS;
7597
7598 rc = 0;
7599 switch (fan_control_access_mode) {
7600 case TPACPI_FAN_WR_ACPI_FANS:
7601 if (speed >= 0 && speed <= 65535) {
7602 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
7603 speed, speed, speed))
7604 rc = -EIO;
7605 } else
7606 rc = -EINVAL;
7607 break;
7608
7609 default:
7610 rc = -ENXIO;
7611 }
7612
7613 mutex_unlock(&fan_mutex);
7614 return rc;
7615}
7616
7617static void fan_watchdog_reset(void)
7618{
7619 static int fan_watchdog_active;
7620
7621 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
7622 return;
7623
7624 if (fan_watchdog_active)
7625 cancel_delayed_work(&fan_watchdog_task);
7626
7627 if (fan_watchdog_maxinterval > 0 &&
7628 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
7629 fan_watchdog_active = 1;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03007630 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007631 msecs_to_jiffies(fan_watchdog_maxinterval
7632 * 1000))) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007633 printk(TPACPI_ERR
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03007634 "failed to queue the fan watchdog, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007635 "watchdog will not trigger\n");
7636 }
7637 } else
7638 fan_watchdog_active = 0;
7639}
7640
7641static void fan_watchdog_fire(struct work_struct *ignored)
7642{
7643 int rc;
7644
7645 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
7646 return;
7647
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007648 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007649 rc = fan_set_enable();
7650 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007651 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007652 "will try again later...\n", -rc);
7653 /* reschedule for later */
7654 fan_watchdog_reset();
7655 }
7656}
7657
7658/*
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007659 * SYSFS fan layout: hwmon compatible (device)
7660 *
7661 * pwm*_enable:
7662 * 0: "disengaged" mode
7663 * 1: manual mode
7664 * 2: native EC "auto" mode (recommended, hardware default)
7665 *
7666 * pwm*: set speed in manual mode, ignored otherwise.
7667 * 0 is level 0; 255 is level 7. Intermediate points done with linear
7668 * interpolation.
7669 *
7670 * fan*_input: tachometer reading, RPM
7671 *
7672 *
7673 * SYSFS fan layout: extensions
7674 *
7675 * fan_watchdog (driver):
7676 * fan watchdog interval in seconds, 0 disables (default), max 120
7677 */
7678
7679/* sysfs fan pwm1_enable ----------------------------------------------- */
7680static ssize_t fan_pwm1_enable_show(struct device *dev,
7681 struct device_attribute *attr,
7682 char *buf)
7683{
7684 int res, mode;
7685 u8 status;
7686
7687 res = fan_get_status_safe(&status);
7688 if (res)
7689 return res;
7690
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007691 if (status & TP_EC_FAN_FULLSPEED) {
7692 mode = 0;
7693 } else if (status & TP_EC_FAN_AUTO) {
7694 mode = 2;
7695 } else
7696 mode = 1;
7697
7698 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
7699}
7700
7701static ssize_t fan_pwm1_enable_store(struct device *dev,
7702 struct device_attribute *attr,
7703 const char *buf, size_t count)
7704{
7705 unsigned long t;
7706 int res, level;
7707
7708 if (parse_strtoul(buf, 2, &t))
7709 return -EINVAL;
7710
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007711 tpacpi_disclose_usertask("hwmon pwm1_enable",
7712 "set fan mode to %lu\n", t);
7713
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007714 switch (t) {
7715 case 0:
7716 level = TP_EC_FAN_FULLSPEED;
7717 break;
7718 case 1:
7719 level = TPACPI_FAN_LAST_LEVEL;
7720 break;
7721 case 2:
7722 level = TP_EC_FAN_AUTO;
7723 break;
7724 case 3:
7725 /* reserved for software-controlled auto mode */
7726 return -ENOSYS;
7727 default:
7728 return -EINVAL;
7729 }
7730
7731 res = fan_set_level_safe(level);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03007732 if (res == -ENXIO)
7733 return -EINVAL;
7734 else if (res < 0)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007735 return res;
7736
7737 fan_watchdog_reset();
7738
7739 return count;
7740}
7741
7742static struct device_attribute dev_attr_fan_pwm1_enable =
7743 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
7744 fan_pwm1_enable_show, fan_pwm1_enable_store);
7745
7746/* sysfs fan pwm1 ------------------------------------------------------ */
7747static ssize_t fan_pwm1_show(struct device *dev,
7748 struct device_attribute *attr,
7749 char *buf)
7750{
7751 int res;
7752 u8 status;
7753
7754 res = fan_get_status_safe(&status);
7755 if (res)
7756 return res;
7757
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007758 if ((status &
7759 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
7760 status = fan_control_desired_level;
7761
7762 if (status > 7)
7763 status = 7;
7764
7765 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
7766}
7767
7768static ssize_t fan_pwm1_store(struct device *dev,
7769 struct device_attribute *attr,
7770 const char *buf, size_t count)
7771{
7772 unsigned long s;
7773 int rc;
7774 u8 status, newlevel;
7775
7776 if (parse_strtoul(buf, 255, &s))
7777 return -EINVAL;
7778
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007779 tpacpi_disclose_usertask("hwmon pwm1",
7780 "set fan speed to %lu\n", s);
7781
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007782 /* scale down from 0-255 to 0-7 */
7783 newlevel = (s >> 5) & 0x07;
7784
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007785 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02007786 return -ERESTARTSYS;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007787
7788 rc = fan_get_status(&status);
7789 if (!rc && (status &
7790 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7791 rc = fan_set_level(newlevel);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03007792 if (rc == -ENXIO)
7793 rc = -EINVAL;
7794 else if (!rc) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007795 fan_update_desired_level(newlevel);
Henrique de Moraes Holschuhca4ac2f2007-04-27 22:00:11 -03007796 fan_watchdog_reset();
7797 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007798 }
7799
7800 mutex_unlock(&fan_mutex);
7801 return (rc)? rc : count;
7802}
7803
7804static struct device_attribute dev_attr_fan_pwm1 =
7805 __ATTR(pwm1, S_IWUSR | S_IRUGO,
7806 fan_pwm1_show, fan_pwm1_store);
7807
7808/* sysfs fan fan1_input ------------------------------------------------ */
7809static ssize_t fan_fan1_input_show(struct device *dev,
7810 struct device_attribute *attr,
7811 char *buf)
7812{
7813 int res;
7814 unsigned int speed;
7815
7816 res = fan_get_speed(&speed);
7817 if (res < 0)
7818 return res;
7819
7820 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7821}
7822
7823static struct device_attribute dev_attr_fan_fan1_input =
7824 __ATTR(fan1_input, S_IRUGO,
7825 fan_fan1_input_show, NULL);
7826
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007827/* sysfs fan fan2_input ------------------------------------------------ */
7828static ssize_t fan_fan2_input_show(struct device *dev,
7829 struct device_attribute *attr,
7830 char *buf)
7831{
7832 int res;
7833 unsigned int speed;
7834
7835 res = fan2_get_speed(&speed);
7836 if (res < 0)
7837 return res;
7838
7839 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7840}
7841
7842static struct device_attribute dev_attr_fan_fan2_input =
7843 __ATTR(fan2_input, S_IRUGO,
7844 fan_fan2_input_show, NULL);
7845
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007846/* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007847static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
7848 char *buf)
7849{
7850 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
7851}
7852
7853static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
7854 const char *buf, size_t count)
7855{
7856 unsigned long t;
7857
7858 if (parse_strtoul(buf, 120, &t))
7859 return -EINVAL;
7860
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03007861 if (!fan_control_allowed)
7862 return -EPERM;
7863
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007864 fan_watchdog_maxinterval = t;
7865 fan_watchdog_reset();
7866
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007867 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
7868
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007869 return count;
7870}
7871
7872static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
7873 fan_fan_watchdog_show, fan_fan_watchdog_store);
7874
7875/* --------------------------------------------------------------------- */
7876static struct attribute *fan_attributes[] = {
7877 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
7878 &dev_attr_fan_fan1_input.attr,
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007879 NULL, /* for fan2_input */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007880 NULL
7881};
7882
7883static const struct attribute_group fan_attr_group = {
7884 .attrs = fan_attributes,
7885};
7886
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007887#define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
7888#define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007889
7890#define TPACPI_FAN_QI(__id1, __id2, __quirks) \
7891 { .vendor = PCI_VENDOR_ID_IBM, \
7892 .bios = TPACPI_MATCH_ANY, \
7893 .ec = TPID(__id1, __id2), \
7894 .quirks = __quirks }
7895
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007896#define TPACPI_FAN_QL(__id1, __id2, __quirks) \
7897 { .vendor = PCI_VENDOR_ID_LENOVO, \
7898 .bios = TPACPI_MATCH_ANY, \
7899 .ec = TPID(__id1, __id2), \
7900 .quirks = __quirks }
7901
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007902static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
7903 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
7904 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
7905 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
7906 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007907 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007908};
7909
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007910#undef TPACPI_FAN_QL
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007911#undef TPACPI_FAN_QI
7912
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007913static int __init fan_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007914{
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007915 int rc;
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007916 unsigned long quirks;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007917
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007918 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7919 "initializing fan subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007920
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03007921 mutex_init(&fan_mutex);
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007922 fan_status_access_mode = TPACPI_FAN_NONE;
7923 fan_control_access_mode = TPACPI_FAN_WR_NONE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007924 fan_control_commands = 0;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007925 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03007926 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007927 tp_features.second_fan = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007928 fan_control_desired_level = 7;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007929
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007930 TPACPI_ACPIHANDLE_INIT(fans);
7931 TPACPI_ACPIHANDLE_INIT(gfan);
7932 TPACPI_ACPIHANDLE_INIT(sfan);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007933
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007934 quirks = tpacpi_check_quirks(fan_quirk_table,
7935 ARRAY_SIZE(fan_quirk_table));
7936
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007937 if (gfan_handle) {
7938 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007939 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007940 } else {
7941 /* all other ThinkPads: note that even old-style
7942 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02007943 if (likely(acpi_ec_read(fan_status_offset,
7944 &fan_control_initial_status))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007945 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007946 if (quirks & TPACPI_FAN_Q1)
7947 fan_quirk1_setup();
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007948 if (quirks & TPACPI_FAN_2FAN) {
7949 tp_features.second_fan = 1;
7950 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7951 "secondary fan support enabled\n");
7952 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007953 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007954 printk(TPACPI_ERR
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007955 "ThinkPad ACPI EC access misbehaving, "
7956 "fan status and control unavailable\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007957 return 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007958 }
7959 }
7960
7961 if (sfan_handle) {
7962 /* 570, 770x-JL */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007963 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02007964 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007965 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007966 } else {
7967 if (!gfan_handle) {
7968 /* gfan without sfan means no fan control */
7969 /* all other models implement TP EC 0x2f control */
7970
7971 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02007972 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007973 fan_control_access_mode =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007974 TPACPI_FAN_WR_ACPI_FANS;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007975 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007976 TPACPI_FAN_CMD_SPEED |
7977 TPACPI_FAN_CMD_LEVEL |
7978 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007979 } else {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007980 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02007981 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007982 TPACPI_FAN_CMD_LEVEL |
7983 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007984 }
7985 }
7986 }
7987
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007988 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7989 "fan is %s, modes %d, %d\n",
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007990 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
7991 fan_control_access_mode != TPACPI_FAN_WR_NONE),
7992 fan_status_access_mode, fan_control_access_mode);
7993
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03007994 /* fan control master switch */
7995 if (!fan_control_allowed) {
7996 fan_control_access_mode = TPACPI_FAN_WR_NONE;
7997 fan_control_commands = 0;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007998 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03007999 "fan control features disabled by parameter\n");
8000 }
8001
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008002 /* update fan_control_desired_level */
8003 if (fan_status_access_mode != TPACPI_FAN_NONE)
8004 fan_get_status_safe(NULL);
8005
8006 if (fan_status_access_mode != TPACPI_FAN_NONE ||
8007 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008008 if (tp_features.second_fan) {
8009 /* attach second fan tachometer */
8010 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
8011 &dev_attr_fan_fan2_input.attr;
8012 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008013 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008014 &fan_attr_group);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008015 if (rc < 0)
8016 return rc;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03008017
8018 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
8019 &driver_attr_fan_watchdog);
8020 if (rc < 0) {
8021 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
8022 &fan_attr_group);
8023 return rc;
8024 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008025 return 0;
8026 } else
8027 return 1;
8028}
8029
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008030static void fan_exit(void)
8031{
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008032 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008033 "cancelling any pending fan watchdog tasks\n");
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008034
8035 /* FIXME: can we really do this unconditionally? */
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008036 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008037 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
8038 &driver_attr_fan_watchdog);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008039
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008040 cancel_delayed_work(&fan_watchdog_task);
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03008041 flush_workqueue(tpacpi_wq);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008042}
8043
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008044static void fan_suspend(pm_message_t state)
8045{
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008046 int rc;
8047
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008048 if (!fan_control_allowed)
8049 return;
8050
8051 /* Store fan status in cache */
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008052 fan_control_resume_level = 0;
8053 rc = fan_get_status_safe(&fan_control_resume_level);
8054 if (rc < 0)
8055 printk(TPACPI_NOTICE
8056 "failed to read fan level for later "
8057 "restore during resume: %d\n", rc);
8058
8059 /* if it is undefined, don't attempt to restore it.
8060 * KEEP THIS LAST */
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008061 if (tp_features.fan_ctrl_status_undef)
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008062 fan_control_resume_level = 0;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008063}
8064
8065static void fan_resume(void)
8066{
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008067 u8 current_level = 7;
8068 bool do_set = false;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008069 int rc;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008070
8071 /* DSDT *always* updates status on resume */
8072 tp_features.fan_ctrl_status_undef = 0;
8073
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008074 if (!fan_control_allowed ||
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008075 !fan_control_resume_level ||
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008076 (fan_get_status_safe(&current_level) < 0))
8077 return;
8078
8079 switch (fan_control_access_mode) {
8080 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008081 /* never decrease fan level */
8082 do_set = (fan_control_resume_level > current_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008083 break;
8084 case TPACPI_FAN_WR_ACPI_FANS:
8085 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008086 /* never decrease fan level, scale is:
8087 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
8088 *
8089 * We expect the firmware to set either 7 or AUTO, but we
8090 * handle FULLSPEED out of paranoia.
8091 *
8092 * So, we can safely only restore FULLSPEED or 7, anything
8093 * else could slow the fan. Restoring AUTO is useless, at
8094 * best that's exactly what the DSDT already set (it is the
8095 * slower it uses).
8096 *
8097 * Always keep in mind that the DSDT *will* have set the
8098 * fans to what the vendor supposes is the best level. We
8099 * muck with it only to speed the fan up.
8100 */
8101 if (fan_control_resume_level != 7 &&
8102 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
8103 return;
8104 else
8105 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
8106 (current_level != fan_control_resume_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008107 break;
8108 default:
8109 return;
8110 }
8111 if (do_set) {
8112 printk(TPACPI_NOTICE
8113 "restoring fan level to 0x%02x\n",
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008114 fan_control_resume_level);
8115 rc = fan_set_level_safe(fan_control_resume_level);
8116 if (rc < 0)
8117 printk(TPACPI_NOTICE
8118 "failed to restore fan level: %d\n", rc);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008119 }
8120}
8121
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008122static int fan_read(struct seq_file *m)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008123{
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008124 int rc;
8125 u8 status;
8126 unsigned int speed = 0;
8127
8128 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008129 case TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008130 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008131 rc = fan_get_status_safe(&status);
8132 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008133 return rc;
8134
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008135 seq_printf(m, "status:\t\t%s\n"
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008136 "level:\t\t%d\n",
8137 (status != 0) ? "enabled" : "disabled", status);
8138 break;
8139
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008140 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008141 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008142 rc = fan_get_status_safe(&status);
8143 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008144 return rc;
8145
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008146 seq_printf(m, "status:\t\t%s\n",
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008147 (status != 0) ? "enabled" : "disabled");
8148
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008149 rc = fan_get_speed(&speed);
8150 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008151 return rc;
8152
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008153 seq_printf(m, "speed:\t\t%d\n", speed);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008154
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008155 if (status & TP_EC_FAN_FULLSPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008156 /* Disengaged mode takes precedence */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008157 seq_printf(m, "level:\t\tdisengaged\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008158 else if (status & TP_EC_FAN_AUTO)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008159 seq_printf(m, "level:\t\tauto\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008160 else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008161 seq_printf(m, "level:\t\t%d\n", status);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008162 break;
8163
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008164 case TPACPI_FAN_NONE:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008165 default:
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008166 seq_printf(m, "status:\t\tnot supported\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008167 }
8168
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008169 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008170 seq_printf(m, "commands:\tlevel <level>");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008171
8172 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008173 case TPACPI_FAN_WR_ACPI_SFAN:
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008174 seq_printf(m, " (<level> is 0-7)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008175 break;
8176
8177 default:
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008178 seq_printf(m, " (<level> is 0-7, "
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008179 "auto, disengaged, full-speed)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008180 break;
8181 }
8182 }
8183
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008184 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008185 seq_printf(m, "commands:\tenable, disable\n"
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008186 "commands:\twatchdog <timeout> (<timeout> "
8187 "is 0 (off), 1-120 (seconds))\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008188
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008189 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008190 seq_printf(m, "commands:\tspeed <speed>"
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008191 " (<speed> is 0-65535)\n");
8192
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008193 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008194}
8195
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008196static int fan_write_cmd_level(const char *cmd, int *rc)
8197{
8198 int level;
8199
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02008200 if (strlencmp(cmd, "level auto") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008201 level = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008202 else if ((strlencmp(cmd, "level disengaged") == 0) |
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008203 (strlencmp(cmd, "level full-speed") == 0))
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008204 level = TP_EC_FAN_FULLSPEED;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02008205 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008206 return 0;
8207
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008208 *rc = fan_set_level_safe(level);
8209 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008210 printk(TPACPI_ERR "level command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008211 "access mode %d", fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008212 else if (!*rc)
8213 tpacpi_disclose_usertask("procfs fan",
8214 "set level to %d\n", level);
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008215
8216 return 1;
8217}
8218
8219static int fan_write_cmd_enable(const char *cmd, int *rc)
8220{
8221 if (strlencmp(cmd, "enable") != 0)
8222 return 0;
8223
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008224 *rc = fan_set_enable();
8225 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008226 printk(TPACPI_ERR "enable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008227 "access mode %d", fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008228 else if (!*rc)
8229 tpacpi_disclose_usertask("procfs fan", "enable\n");
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008230
8231 return 1;
8232}
8233
8234static int fan_write_cmd_disable(const char *cmd, int *rc)
8235{
8236 if (strlencmp(cmd, "disable") != 0)
8237 return 0;
8238
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008239 *rc = fan_set_disable();
8240 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008241 printk(TPACPI_ERR "disable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008242 "access mode %d", fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008243 else if (!*rc)
8244 tpacpi_disclose_usertask("procfs fan", "disable\n");
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008245
8246 return 1;
8247}
8248
8249static int fan_write_cmd_speed(const char *cmd, int *rc)
8250{
8251 int speed;
8252
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02008253 /* TODO:
8254 * Support speed <low> <medium> <high> ? */
8255
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008256 if (sscanf(cmd, "speed %d", &speed) != 1)
8257 return 0;
8258
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008259 *rc = fan_set_speed(speed);
8260 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008261 printk(TPACPI_ERR "speed command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008262 "access mode %d", fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008263 else if (!*rc)
8264 tpacpi_disclose_usertask("procfs fan",
8265 "set speed to %d\n", speed);
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008266
8267 return 1;
8268}
8269
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008270static int fan_write_cmd_watchdog(const char *cmd, int *rc)
8271{
8272 int interval;
8273
8274 if (sscanf(cmd, "watchdog %d", &interval) != 1)
8275 return 0;
8276
8277 if (interval < 0 || interval > 120)
8278 *rc = -EINVAL;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008279 else {
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008280 fan_watchdog_maxinterval = interval;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008281 tpacpi_disclose_usertask("procfs fan",
8282 "set watchdog timer to %d\n",
8283 interval);
8284 }
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008285
8286 return 1;
8287}
8288
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008289static int fan_write(char *buf)
8290{
8291 char *cmd;
8292 int rc = 0;
8293
8294 while (!rc && (cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008295 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008296 fan_write_cmd_level(cmd, &rc)) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008297 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008298 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008299 fan_write_cmd_disable(cmd, &rc) ||
8300 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008301 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008302 fan_write_cmd_speed(cmd, &rc))
8303 )
8304 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008305 else if (!rc)
8306 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008307 }
8308
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008309 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008310}
8311
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008312static struct ibm_struct fan_driver_data = {
8313 .name = "fan",
8314 .read = fan_read,
8315 .write = fan_write,
8316 .exit = fan_exit,
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008317 .suspend = fan_suspend,
8318 .resume = fan_resume,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008319};
8320
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008321/****************************************************************************
8322 ****************************************************************************
8323 *
8324 * Infrastructure
8325 *
8326 ****************************************************************************
8327 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07008328
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03008329/*
8330 * HKEY event callout for other subdrivers go here
8331 * (yes, it is ugly, but it is quick, safe, and gets the job done
8332 */
8333static void tpacpi_driver_event(const unsigned int hkey_event)
8334{
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00008335 if (ibm_backlight_device) {
8336 switch (hkey_event) {
8337 case TP_HKEY_EV_BRGHT_UP:
8338 case TP_HKEY_EV_BRGHT_DOWN:
8339 tpacpi_brightness_notify_change();
8340 }
8341 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02008342 if (alsa_card) {
8343 switch (hkey_event) {
8344 case TP_HKEY_EV_VOL_UP:
8345 case TP_HKEY_EV_VOL_DOWN:
8346 case TP_HKEY_EV_VOL_MUTE:
8347 volume_alsa_notify_change();
8348 }
8349 }
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03008350}
8351
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03008352static void hotkey_driver_event(const unsigned int scancode)
8353{
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03008354 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode);
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03008355}
8356
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008357/* sysfs name ---------------------------------------------------------- */
8358static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
8359 struct device_attribute *attr,
8360 char *buf)
8361{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008362 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008363}
8364
8365static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
8366 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
8367
8368/* --------------------------------------------------------------------- */
8369
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008370/* /proc support */
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -03008371static struct proc_dir_entry *proc_dir;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008372
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008373/*
8374 * Module and infrastructure proble, init and exit handling
8375 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008376
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008377static int force_load;
8378
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008379#ifdef CONFIG_THINKPAD_ACPI_DEBUG
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008380static const char * __init str_supported(int is_supported)
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008381{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008382 static char text_unsupported[] __initdata = "not supported";
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008383
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008384 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008385}
8386#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
8387
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008388static void ibm_exit(struct ibm_struct *ibm)
8389{
8390 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
8391
8392 list_del_init(&ibm->all_drivers);
8393
8394 if (ibm->flags.acpi_notify_installed) {
8395 dbg_printk(TPACPI_DBG_EXIT,
8396 "%s: acpi_remove_notify_handler\n", ibm->name);
8397 BUG_ON(!ibm->acpi);
8398 acpi_remove_notify_handler(*ibm->acpi->handle,
8399 ibm->acpi->type,
8400 dispatch_acpi_notify);
8401 ibm->flags.acpi_notify_installed = 0;
8402 ibm->flags.acpi_notify_installed = 0;
8403 }
8404
8405 if (ibm->flags.proc_created) {
8406 dbg_printk(TPACPI_DBG_EXIT,
8407 "%s: remove_proc_entry\n", ibm->name);
8408 remove_proc_entry(ibm->name, proc_dir);
8409 ibm->flags.proc_created = 0;
8410 }
8411
8412 if (ibm->flags.acpi_driver_registered) {
8413 dbg_printk(TPACPI_DBG_EXIT,
8414 "%s: acpi_bus_unregister_driver\n", ibm->name);
8415 BUG_ON(!ibm->acpi);
8416 acpi_bus_unregister_driver(ibm->acpi->driver);
8417 kfree(ibm->acpi->driver);
8418 ibm->acpi->driver = NULL;
8419 ibm->flags.acpi_driver_registered = 0;
8420 }
8421
8422 if (ibm->flags.init_called && ibm->exit) {
8423 ibm->exit();
8424 ibm->flags.init_called = 0;
8425 }
8426
8427 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
8428}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02008429
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008430static int __init ibm_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008431{
8432 int ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008433 struct ibm_struct *ibm = iibm->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008434 struct proc_dir_entry *entry;
8435
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008436 BUG_ON(ibm == NULL);
8437
8438 INIT_LIST_HEAD(&ibm->all_drivers);
8439
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03008440 if (ibm->flags.experimental && !experimental)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008441 return 0;
8442
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008443 dbg_printk(TPACPI_DBG_INIT,
8444 "probing for %s\n", ibm->name);
8445
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008446 if (iibm->init) {
8447 ret = iibm->init(iibm);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008448 if (ret > 0)
8449 return 0; /* probe failed */
8450 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008451 return ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008452
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03008453 ibm->flags.init_called = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008454 }
8455
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03008456 if (ibm->acpi) {
8457 if (ibm->acpi->hid) {
8458 ret = register_tpacpi_subdriver(ibm);
8459 if (ret)
8460 goto err_out;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008461 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03008462
8463 if (ibm->acpi->notify) {
8464 ret = setup_acpi_notify(ibm);
8465 if (ret == -ENODEV) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008466 printk(TPACPI_NOTICE "disabling subdriver %s\n",
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03008467 ibm->name);
8468 ret = 0;
8469 goto err_out;
8470 }
8471 if (ret < 0)
8472 goto err_out;
8473 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008474 }
8475
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008476 dbg_printk(TPACPI_DBG_INIT,
8477 "%s installed\n", ibm->name);
8478
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008479 if (ibm->read) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008480 mode_t mode;
8481
8482 mode = S_IRUGO;
8483 if (ibm->write)
8484 mode |= S_IWUSR;
8485 entry = proc_create_data(ibm->name, mode, proc_dir,
8486 &dispatch_proc_fops, ibm);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008487 if (!entry) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008488 printk(TPACPI_ERR "unable to create proc entry %s\n",
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008489 ibm->name);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008490 ret = -ENODEV;
8491 goto err_out;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008492 }
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03008493 ibm->flags.proc_created = 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008494 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008495
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008496 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
8497
Linus Torvalds1da177e2005-04-16 15:20:36 -07008498 return 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008499
8500err_out:
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008501 dbg_printk(TPACPI_DBG_INIT,
8502 "%s: at error exit path with result %d\n",
8503 ibm->name, ret);
8504
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008505 ibm_exit(ibm);
8506 return (ret < 0)? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008507}
8508
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008509/* Probing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008510
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008511static bool __pure __init tpacpi_is_fw_digit(const char c)
8512{
8513 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
8514}
8515
8516/* Most models: xxyTkkWW (#.##c); Ancient 570/600 and -SL lacks (#.##c) */
8517static bool __pure __init tpacpi_is_valid_fw_id(const char* const s,
8518 const char t)
8519{
8520 return s && strlen(s) >= 8 &&
8521 tpacpi_is_fw_digit(s[0]) &&
8522 tpacpi_is_fw_digit(s[1]) &&
8523 s[2] == t && s[3] == 'T' &&
8524 tpacpi_is_fw_digit(s[4]) &&
8525 tpacpi_is_fw_digit(s[5]) &&
8526 s[6] == 'W' && s[7] == 'W';
8527}
8528
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008529/* returns 0 - probe ok, or < 0 - probe error.
8530 * Probe ok doesn't mean thinkpad found.
8531 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
8532static int __must_check __init get_thinkpad_model_data(
8533 struct thinkpad_id_data *tp)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02008534{
Jeff Garzik18552562007-10-03 15:15:40 -04008535 const struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02008536 char ec_fw_string[18];
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008537 char const *s;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02008538
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008539 if (!tp)
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008540 return -EINVAL;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008541
8542 memset(tp, 0, sizeof(*tp));
8543
8544 if (dmi_name_in_vendors("IBM"))
8545 tp->vendor = PCI_VENDOR_ID_IBM;
8546 else if (dmi_name_in_vendors("LENOVO"))
8547 tp->vendor = PCI_VENDOR_ID_LENOVO;
8548 else
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008549 return 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008550
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008551 s = dmi_get_system_info(DMI_BIOS_VERSION);
8552 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
8553 if (s && !tp->bios_version_str)
8554 return -ENOMEM;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008555
8556 /* Really ancient ThinkPad 240X will fail this, which is fine */
8557 if (!tpacpi_is_valid_fw_id(tp->bios_version_str, 'E'))
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008558 return 0;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008559
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008560 tp->bios_model = tp->bios_version_str[0]
8561 | (tp->bios_version_str[1] << 8);
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008562 tp->bios_release = (tp->bios_version_str[4] << 8)
8563 | tp->bios_version_str[5];
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008564
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02008565 /*
8566 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
8567 * X32 or newer, all Z series; Some models must have an
8568 * up-to-date BIOS or they will not be detected.
8569 *
8570 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
8571 */
8572 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02008573 if (sscanf(dev->name,
8574 "IBM ThinkPad Embedded Controller -[%17c",
8575 ec_fw_string) == 1) {
8576 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
8577 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008578
8579 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008580 if (!tp->ec_version_str)
8581 return -ENOMEM;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008582
8583 if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) {
8584 tp->ec_model = ec_fw_string[0]
8585 | (ec_fw_string[1] << 8);
8586 tp->ec_release = (ec_fw_string[4] << 8)
8587 | ec_fw_string[5];
8588 } else {
8589 printk(TPACPI_NOTICE
8590 "ThinkPad firmware release %s "
8591 "doesn't match the known patterns\n",
8592 ec_fw_string);
8593 printk(TPACPI_NOTICE
8594 "please report this to %s\n",
8595 TPACPI_MAIL);
8596 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008597 break;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02008598 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02008599 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008600
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008601 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
8602 if (s && !strnicmp(s, "ThinkPad", 8)) {
8603 tp->model_str = kstrdup(s, GFP_KERNEL);
8604 if (!tp->model_str)
8605 return -ENOMEM;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008606 }
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03008607
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008608 s = dmi_get_system_info(DMI_PRODUCT_NAME);
8609 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
8610 if (s && !tp->nummodel_str)
8611 return -ENOMEM;
8612
8613 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008614}
8615
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008616static int __init probe_for_thinkpad(void)
8617{
8618 int is_thinkpad;
8619
8620 if (acpi_disabled)
8621 return -ENODEV;
8622
8623 /*
8624 * Non-ancient models have better DMI tagging, but very old models
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03008625 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008626 */
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03008627 is_thinkpad = (thinkpad_id.model_str != NULL) ||
8628 (thinkpad_id.ec_model != 0) ||
8629 tpacpi_is_fw_known();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008630
8631 /* ec is required because many other handles are relative to it */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008632 TPACPI_ACPIHANDLE_INIT(ec);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008633 if (!ec_handle) {
8634 if (is_thinkpad)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008635 printk(TPACPI_ERR
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008636 "Not yet supported ThinkPad detected!\n");
8637 return -ENODEV;
8638 }
8639
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03008640 if (!is_thinkpad && !force_load)
8641 return -ENODEV;
8642
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008643 return 0;
8644}
8645
8646
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008647/* Module init, exit, parameters */
8648
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008649static struct ibm_init_struct ibms_init[] __initdata = {
8650 {
8651 .init = thinkpad_acpi_driver_init,
8652 .data = &thinkpad_acpi_driver_data,
8653 },
8654 {
8655 .init = hotkey_init,
8656 .data = &hotkey_driver_data,
8657 },
8658 {
8659 .init = bluetooth_init,
8660 .data = &bluetooth_driver_data,
8661 },
8662 {
8663 .init = wan_init,
8664 .data = &wan_driver_data,
8665 },
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02008666 {
8667 .init = uwb_init,
8668 .data = &uwb_driver_data,
8669 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02008670#ifdef CONFIG_THINKPAD_ACPI_VIDEO
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008671 {
8672 .init = video_init,
8673 .data = &video_driver_data,
8674 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02008675#endif
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008676 {
8677 .init = light_init,
8678 .data = &light_driver_data,
8679 },
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008680 {
8681 .init = cmos_init,
8682 .data = &cmos_driver_data,
8683 },
8684 {
8685 .init = led_init,
8686 .data = &led_driver_data,
8687 },
8688 {
8689 .init = beep_init,
8690 .data = &beep_driver_data,
8691 },
8692 {
8693 .init = thermal_init,
8694 .data = &thermal_driver_data,
8695 },
8696 {
8697 .data = &ecdump_driver_data,
8698 },
8699 {
8700 .init = brightness_init,
8701 .data = &brightness_driver_data,
8702 },
8703 {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02008704 .init = volume_init,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008705 .data = &volume_driver_data,
8706 },
8707 {
8708 .init = fan_init,
8709 .data = &fan_driver_data,
8710 },
8711};
8712
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008713static int __init set_ibm_param(const char *val, struct kernel_param *kp)
8714{
8715 unsigned int i;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008716 struct ibm_struct *ibm;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008717
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02008718 if (!kp || !kp->name || !val)
8719 return -EINVAL;
8720
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008721 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
8722 ibm = ibms_init[i].data;
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02008723 WARN_ON(ibm == NULL);
8724
8725 if (!ibm || !ibm->name)
8726 continue;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008727
8728 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
8729 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008730 return -ENOSPC;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008731 strcpy(ibms_init[i].param, val);
8732 strcat(ibms_init[i].param, ",");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008733 return 0;
8734 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008735 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008736
8737 return -EINVAL;
8738}
8739
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008740module_param(experimental, int, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008741MODULE_PARM_DESC(experimental,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008742 "Enables experimental features when non-zero");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008743
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03008744module_param_named(debug, dbg_level, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008745MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03008746
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008747module_param(force_load, bool, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008748MODULE_PARM_DESC(force_load,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008749 "Attempts to load the driver even on a "
8750 "mis-identified ThinkPad when true");
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03008751
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008752module_param_named(fan_control, fan_control_allowed, bool, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008753MODULE_PARM_DESC(fan_control,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008754 "Enables setting fan parameters features when true");
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008755
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008756module_param_named(brightness_mode, brightness_mode, uint, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008757MODULE_PARM_DESC(brightness_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008758 "Selects brightness control strategy: "
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00008759 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03008760
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008761module_param(brightness_enable, uint, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008762MODULE_PARM_DESC(brightness_enable,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008763 "Enables backlight control when 1, disables when 0");
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02008764
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008765module_param(hotkey_report_mode, uint, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008766MODULE_PARM_DESC(hotkey_report_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008767 "used for backwards compatibility with userspace, "
8768 "see documentation");
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03008769
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02008770#ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02008771module_param_named(volume_mode, volume_mode, uint, 0444);
8772MODULE_PARM_DESC(volume_mode,
8773 "Selects volume control strategy: "
8774 "0=auto, 1=EC, 2=N/A, 3=EC+NVRAM");
8775
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02008776module_param_named(volume_capabilities, volume_capabilities, uint, 0444);
8777MODULE_PARM_DESC(volume_capabilities,
8778 "Selects the mixer capabilites: "
8779 "0=auto, 1=volume and mute, 2=mute only");
8780
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02008781module_param_named(volume_control, volume_control_allowed, bool, 0444);
8782MODULE_PARM_DESC(volume_control,
8783 "Enables software override for the console audio "
8784 "control when true");
8785
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02008786/* ALSA module API parameters */
8787module_param_named(index, alsa_index, int, 0444);
8788MODULE_PARM_DESC(index, "ALSA index for the ACPI EC Mixer");
8789module_param_named(id, alsa_id, charp, 0444);
8790MODULE_PARM_DESC(id, "ALSA id for the ACPI EC Mixer");
8791module_param_named(enable, alsa_enable, bool, 0444);
8792MODULE_PARM_DESC(enable, "Enable the ALSA interface for the ACPI EC Mixer");
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02008793#endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02008794
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008795#define TPACPI_PARAM(feature) \
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008796 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
Henrique de Moraes Holschuhcbb14842008-02-16 02:17:50 -02008797 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008798 "at module load, see documentation")
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008799
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008800TPACPI_PARAM(hotkey);
8801TPACPI_PARAM(bluetooth);
8802TPACPI_PARAM(video);
8803TPACPI_PARAM(light);
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008804TPACPI_PARAM(cmos);
8805TPACPI_PARAM(led);
8806TPACPI_PARAM(beep);
8807TPACPI_PARAM(ecdump);
8808TPACPI_PARAM(brightness);
8809TPACPI_PARAM(volume);
8810TPACPI_PARAM(fan);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008811
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02008812#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008813module_param(dbg_wlswemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02008814MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
8815module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
8816MODULE_PARM_DESC(wlsw_state,
8817 "Initial state of the emulated WLSW switch");
8818
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008819module_param(dbg_bluetoothemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02008820MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
8821module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
8822MODULE_PARM_DESC(bluetooth_state,
8823 "Initial state of the emulated bluetooth switch");
8824
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008825module_param(dbg_wwanemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02008826MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
8827module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
8828MODULE_PARM_DESC(wwan_state,
8829 "Initial state of the emulated WWAN switch");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02008830
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008831module_param(dbg_uwbemul, uint, 0444);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02008832MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
8833module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
8834MODULE_PARM_DESC(uwb_state,
8835 "Initial state of the emulated UWB switch");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02008836#endif
8837
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008838static void thinkpad_acpi_module_exit(void)
8839{
8840 struct ibm_struct *ibm, *itmp;
8841
8842 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
8843
8844 list_for_each_entry_safe_reverse(ibm, itmp,
8845 &tpacpi_all_drivers,
8846 all_drivers) {
8847 ibm_exit(ibm);
8848 }
8849
8850 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
8851
8852 if (tpacpi_inputdev) {
8853 if (tp_features.input_device_registered)
8854 input_unregister_device(tpacpi_inputdev);
8855 else
8856 input_free_device(tpacpi_inputdev);
8857 }
8858
8859 if (tpacpi_hwmon)
8860 hwmon_device_unregister(tpacpi_hwmon);
8861
8862 if (tp_features.sensors_pdev_attrs_registered)
8863 device_remove_file(&tpacpi_sensors_pdev->dev,
8864 &dev_attr_thinkpad_acpi_pdev_name);
8865 if (tpacpi_sensors_pdev)
8866 platform_device_unregister(tpacpi_sensors_pdev);
8867 if (tpacpi_pdev)
8868 platform_device_unregister(tpacpi_pdev);
8869
8870 if (tp_features.sensors_pdrv_attrs_registered)
8871 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
8872 if (tp_features.platform_drv_attrs_registered)
8873 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
8874
8875 if (tp_features.sensors_pdrv_registered)
8876 platform_driver_unregister(&tpacpi_hwmon_pdriver);
8877
8878 if (tp_features.platform_drv_registered)
8879 platform_driver_unregister(&tpacpi_pdriver);
8880
8881 if (proc_dir)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008882 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008883
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03008884 if (tpacpi_wq)
8885 destroy_workqueue(tpacpi_wq);
8886
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008887 kfree(thinkpad_id.bios_version_str);
8888 kfree(thinkpad_id.ec_version_str);
8889 kfree(thinkpad_id.model_str);
8890}
8891
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02008892
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03008893static int __init thinkpad_acpi_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008894{
8895 int ret, i;
8896
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03008897 tpacpi_lifecycle = TPACPI_LIFE_INIT;
8898
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03008899 /* Parameter checking */
8900 if (hotkey_report_mode > 2)
8901 return -EINVAL;
8902
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008903 /* Driver-level probe */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008904
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008905 ret = get_thinkpad_model_data(&thinkpad_id);
8906 if (ret) {
8907 printk(TPACPI_ERR
8908 "unable to get DMI data: %d\n", ret);
8909 thinkpad_acpi_module_exit();
8910 return ret;
8911 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008912 ret = probe_for_thinkpad();
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008913 if (ret) {
8914 thinkpad_acpi_module_exit();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008915 return ret;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008916 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008917
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008918 /* Driver initialization */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008919
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008920 TPACPI_ACPIHANDLE_INIT(ecrd);
8921 TPACPI_ACPIHANDLE_INIT(ecwr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008922
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03008923 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
8924 if (!tpacpi_wq) {
8925 thinkpad_acpi_module_exit();
8926 return -ENOMEM;
8927 }
8928
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008929 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008930 if (!proc_dir) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008931 printk(TPACPI_ERR
8932 "unable to create proc dir " TPACPI_PROC_DIR);
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03008933 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008934 return -ENODEV;
8935 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008936
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008937 ret = platform_driver_register(&tpacpi_pdriver);
8938 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008939 printk(TPACPI_ERR
8940 "unable to register main platform driver\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008941 thinkpad_acpi_module_exit();
8942 return ret;
8943 }
Henrique de Moraes Holschuhac363932007-07-27 17:04:40 -03008944 tp_features.platform_drv_registered = 1;
8945
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008946 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
8947 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008948 printk(TPACPI_ERR
8949 "unable to register hwmon platform driver\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008950 thinkpad_acpi_module_exit();
8951 return ret;
8952 }
8953 tp_features.sensors_pdrv_registered = 1;
8954
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03008955 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03008956 if (!ret) {
8957 tp_features.platform_drv_attrs_registered = 1;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008958 ret = tpacpi_create_driver_attributes(
8959 &tpacpi_hwmon_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03008960 }
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03008961 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008962 printk(TPACPI_ERR
8963 "unable to create sysfs driver attributes\n");
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03008964 thinkpad_acpi_module_exit();
8965 return ret;
8966 }
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03008967 tp_features.sensors_pdrv_attrs_registered = 1;
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03008968
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008969
8970 /* Device initialization */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008971 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008972 NULL, 0);
8973 if (IS_ERR(tpacpi_pdev)) {
8974 ret = PTR_ERR(tpacpi_pdev);
8975 tpacpi_pdev = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008976 printk(TPACPI_ERR "unable to register platform device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008977 thinkpad_acpi_module_exit();
8978 return ret;
8979 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008980 tpacpi_sensors_pdev = platform_device_register_simple(
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008981 TPACPI_HWMON_DRVR_NAME,
8982 -1, NULL, 0);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008983 if (IS_ERR(tpacpi_sensors_pdev)) {
8984 ret = PTR_ERR(tpacpi_sensors_pdev);
8985 tpacpi_sensors_pdev = NULL;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008986 printk(TPACPI_ERR
8987 "unable to register hwmon platform device\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008988 thinkpad_acpi_module_exit();
8989 return ret;
8990 }
8991 ret = device_create_file(&tpacpi_sensors_pdev->dev,
8992 &dev_attr_thinkpad_acpi_pdev_name);
8993 if (ret) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008994 printk(TPACPI_ERR
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008995 "unable to create sysfs hwmon device attributes\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008996 thinkpad_acpi_module_exit();
8997 return ret;
8998 }
8999 tp_features.sensors_pdev_attrs_registered = 1;
9000 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009001 if (IS_ERR(tpacpi_hwmon)) {
9002 ret = PTR_ERR(tpacpi_hwmon);
9003 tpacpi_hwmon = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009004 printk(TPACPI_ERR "unable to register hwmon device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009005 thinkpad_acpi_module_exit();
9006 return ret;
9007 }
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -03009008 mutex_init(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009009 tpacpi_inputdev = input_allocate_device();
9010 if (!tpacpi_inputdev) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009011 printk(TPACPI_ERR "unable to allocate input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009012 thinkpad_acpi_module_exit();
9013 return -ENOMEM;
9014 } else {
9015 /* Prepare input device, but don't register */
9016 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009017 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009018 tpacpi_inputdev->id.bustype = BUS_HOST;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03009019 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
9020 thinkpad_id.vendor :
9021 PCI_VENDOR_ID_IBM;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009022 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
9023 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
Henrique de Moraes Holschuhd112ef92009-12-09 01:36:26 +00009024 tpacpi_inputdev->dev.parent = &tpacpi_pdev->dev;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009025 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009026 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
9027 ret = ibm_init(&ibms_init[i]);
9028 if (ret >= 0 && *ibms_init[i].param)
9029 ret = ibms_init[i].data->write(ibms_init[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009030 if (ret < 0) {
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03009031 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07009032 return ret;
9033 }
9034 }
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009035 ret = input_register_device(tpacpi_inputdev);
9036 if (ret < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009037 printk(TPACPI_ERR "unable to register input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009038 thinkpad_acpi_module_exit();
9039 return ret;
9040 } else {
9041 tp_features.input_device_registered = 1;
9042 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009043
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03009044 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009045 return 0;
9046}
9047
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03009048MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
9049
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009050/*
Henrique de Moraes Holschuh922fe092009-04-14 02:44:13 +00009051 * This will autoload the driver in almost every ThinkPad
9052 * in widespread use.
9053 *
9054 * Only _VERY_ old models, like the 240, 240x and 570 lack
9055 * the HKEY event interface.
9056 */
9057MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
9058
9059/*
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009060 * DMI matching for module autoloading
9061 *
9062 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
9063 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
9064 *
9065 * Only models listed in thinkwiki will be supported, so add yours
9066 * if it is not there yet.
9067 */
9068#define IBM_BIOS_MODULE_ALIAS(__type) \
Mathieu Chouquet-Stringerb36a50f2009-03-14 16:35:26 +01009069 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009070
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009071/* Ancient thinkpad BIOSes have to be identified by
9072 * BIOS type or model number, and there are far less
9073 * BIOS types than model numbers... */
Henrique de Moraes Holschuh922fe092009-04-14 02:44:13 +00009074IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009075
Henrique de Moraes Holschuhf68f53a2009-04-14 02:44:12 +00009076MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
9077MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009078MODULE_DESCRIPTION(TPACPI_DESC);
9079MODULE_VERSION(TPACPI_VERSION);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009080MODULE_LICENSE("GPL");
9081
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03009082module_init(thinkpad_acpi_module_init);
9083module_exit(thinkpad_acpi_module_exit);