blob: 933bcc477f4a1d89bc7882fc6ecffc0dc27c052b [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:
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002610 case TPACPI_LIFE_RUNNING:
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002611 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002612 return 0;
Henrique de Moraes Holschuhb589ea42010-02-25 21:28:58 -03002613 case TPACPI_LIFE_EXITING:
2614 return -EBUSY;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002615 }
2616
2617 /* Should only happen if tpacpi_lifecycle is corrupt */
2618 BUG();
2619 return -EBUSY;
2620}
2621
2622static void hotkey_inputdev_close(struct input_dev *dev)
2623{
2624 /* disable hotkey polling when possible */
Henrique de Moraes Holschuhb589ea42010-02-25 21:28:58 -03002625 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING &&
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002626 !(hotkey_source_mask & hotkey_driver_mask))
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002627 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002628}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002629
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002630/* sysfs hotkey enable ------------------------------------------------- */
2631static ssize_t hotkey_enable_show(struct device *dev,
2632 struct device_attribute *attr,
2633 char *buf)
2634{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002635 int res, status;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002636
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002637 printk_deprecated_attribute("hotkey_enable",
2638 "Hotkey reporting is always enabled");
2639
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002640 res = hotkey_status_get(&status);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002641 if (res)
2642 return res;
2643
2644 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2645}
2646
2647static ssize_t hotkey_enable_store(struct device *dev,
2648 struct device_attribute *attr,
2649 const char *buf, size_t count)
2650{
2651 unsigned long t;
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002652
2653 printk_deprecated_attribute("hotkey_enable",
2654 "Hotkeys can be disabled through hotkey_mask");
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002655
2656 if (parse_strtoul(buf, 1, &t))
2657 return -EINVAL;
2658
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002659 if (t == 0)
2660 return -EPERM;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002661
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002662 return count;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002663}
2664
2665static struct device_attribute dev_attr_hotkey_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002666 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002667 hotkey_enable_show, hotkey_enable_store);
2668
2669/* sysfs hotkey mask --------------------------------------------------- */
2670static ssize_t hotkey_mask_show(struct device *dev,
2671 struct device_attribute *attr,
2672 char *buf)
2673{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002674 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_user_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002675}
2676
2677static ssize_t hotkey_mask_store(struct device *dev,
2678 struct device_attribute *attr,
2679 const char *buf, size_t count)
2680{
2681 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002682 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002683
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002684 if (parse_strtoul(buf, 0xffffffffUL, &t))
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002685 return -EINVAL;
2686
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002687 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002688 return -ERESTARTSYS;
2689
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002690 res = hotkey_user_mask_set(t);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002691
2692#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002693 hotkey_poll_setup(true);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002694#endif
2695
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002696 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002697
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002698 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2699
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002700 return (res) ? res : count;
2701}
2702
2703static struct device_attribute dev_attr_hotkey_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002704 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002705 hotkey_mask_show, hotkey_mask_store);
2706
2707/* sysfs hotkey bios_enabled ------------------------------------------- */
2708static ssize_t hotkey_bios_enabled_show(struct device *dev,
2709 struct device_attribute *attr,
2710 char *buf)
2711{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002712 return sprintf(buf, "0\n");
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002713}
2714
2715static struct device_attribute dev_attr_hotkey_bios_enabled =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002716 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002717
2718/* sysfs hotkey bios_mask ---------------------------------------------- */
2719static ssize_t hotkey_bios_mask_show(struct device *dev,
2720 struct device_attribute *attr,
2721 char *buf)
2722{
Henrique de Moraes Holschuh06777be2009-09-12 15:22:15 -03002723 printk_deprecated_attribute("hotkey_bios_mask",
2724 "This attribute is useless.");
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002725 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002726}
2727
2728static struct device_attribute dev_attr_hotkey_bios_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002729 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002730
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002731/* sysfs hotkey all_mask ----------------------------------------------- */
2732static ssize_t hotkey_all_mask_show(struct device *dev,
2733 struct device_attribute *attr,
2734 char *buf)
2735{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002736 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2737 hotkey_all_mask | hotkey_source_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002738}
2739
2740static struct device_attribute dev_attr_hotkey_all_mask =
2741 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
2742
2743/* sysfs hotkey recommended_mask --------------------------------------- */
2744static ssize_t hotkey_recommended_mask_show(struct device *dev,
2745 struct device_attribute *attr,
2746 char *buf)
2747{
2748 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002749 (hotkey_all_mask | hotkey_source_mask)
2750 & ~hotkey_reserved_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002751}
2752
2753static struct device_attribute dev_attr_hotkey_recommended_mask =
2754 __ATTR(hotkey_recommended_mask, S_IRUGO,
2755 hotkey_recommended_mask_show, NULL);
2756
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002757#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2758
2759/* sysfs hotkey hotkey_source_mask ------------------------------------- */
2760static ssize_t hotkey_source_mask_show(struct device *dev,
2761 struct device_attribute *attr,
2762 char *buf)
2763{
2764 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2765}
2766
2767static ssize_t hotkey_source_mask_store(struct device *dev,
2768 struct device_attribute *attr,
2769 const char *buf, size_t count)
2770{
2771 unsigned long t;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002772 u32 r_ev;
2773 int rc;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002774
2775 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2776 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2777 return -EINVAL;
2778
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002779 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002780 return -ERESTARTSYS;
2781
2782 HOTKEY_CONFIG_CRITICAL_START
2783 hotkey_source_mask = t;
2784 HOTKEY_CONFIG_CRITICAL_END
2785
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002786 rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
2787 ~hotkey_source_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002788 hotkey_poll_setup(true);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002789
2790 /* check if events needed by the driver got disabled */
2791 r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
2792 & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002793
2794 mutex_unlock(&hotkey_mutex);
2795
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002796 if (rc < 0)
2797 printk(TPACPI_ERR "hotkey_source_mask: failed to update the"
2798 "firmware event mask!\n");
2799
2800 if (r_ev)
2801 printk(TPACPI_NOTICE "hotkey_source_mask: "
2802 "some important events were disabled: "
2803 "0x%04x\n", r_ev);
2804
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002805 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2806
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002807 return (rc < 0) ? rc : count;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002808}
2809
2810static struct device_attribute dev_attr_hotkey_source_mask =
2811 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2812 hotkey_source_mask_show, hotkey_source_mask_store);
2813
2814/* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2815static ssize_t hotkey_poll_freq_show(struct device *dev,
2816 struct device_attribute *attr,
2817 char *buf)
2818{
2819 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2820}
2821
2822static ssize_t hotkey_poll_freq_store(struct device *dev,
2823 struct device_attribute *attr,
2824 const char *buf, size_t count)
2825{
2826 unsigned long t;
2827
2828 if (parse_strtoul(buf, 25, &t))
2829 return -EINVAL;
2830
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002831 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002832 return -ERESTARTSYS;
2833
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002834 hotkey_poll_set_freq(t);
2835 hotkey_poll_setup(true);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002836
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002837 mutex_unlock(&hotkey_mutex);
2838
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002839 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2840
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002841 return count;
2842}
2843
2844static struct device_attribute dev_attr_hotkey_poll_freq =
2845 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2846 hotkey_poll_freq_show, hotkey_poll_freq_store);
2847
2848#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2849
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002850/* sysfs hotkey radio_sw (pollable) ------------------------------------ */
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002851static ssize_t hotkey_radio_sw_show(struct device *dev,
2852 struct device_attribute *attr,
2853 char *buf)
2854{
Johannes Berg19d337d2009-06-02 13:01:37 +02002855 int res;
2856 res = hotkey_get_wlsw();
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002857 if (res < 0)
2858 return res;
2859
Johannes Berg19d337d2009-06-02 13:01:37 +02002860 /* Opportunistic update */
2861 tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
2862
2863 return snprintf(buf, PAGE_SIZE, "%d\n",
2864 (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002865}
2866
2867static struct device_attribute dev_attr_hotkey_radio_sw =
2868 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2869
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002870static void hotkey_radio_sw_notify_change(void)
2871{
2872 if (tp_features.hotkey_wlsw)
2873 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2874 "hotkey_radio_sw");
2875}
2876
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002877/* sysfs hotkey tablet mode (pollable) --------------------------------- */
2878static ssize_t hotkey_tablet_mode_show(struct device *dev,
2879 struct device_attribute *attr,
2880 char *buf)
2881{
2882 int res, s;
2883 res = hotkey_get_tablet_mode(&s);
2884 if (res < 0)
2885 return res;
2886
2887 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2888}
2889
2890static struct device_attribute dev_attr_hotkey_tablet_mode =
2891 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2892
2893static void hotkey_tablet_mode_notify_change(void)
2894{
2895 if (tp_features.hotkey_tablet)
2896 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2897 "hotkey_tablet_mode");
2898}
2899
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002900/* sysfs hotkey report_mode -------------------------------------------- */
2901static ssize_t hotkey_report_mode_show(struct device *dev,
2902 struct device_attribute *attr,
2903 char *buf)
2904{
2905 return snprintf(buf, PAGE_SIZE, "%d\n",
2906 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2907}
2908
2909static struct device_attribute dev_attr_hotkey_report_mode =
2910 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2911
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002912/* sysfs wakeup reason (pollable) -------------------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002913static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2914 struct device_attribute *attr,
2915 char *buf)
2916{
2917 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2918}
2919
2920static struct device_attribute dev_attr_hotkey_wakeup_reason =
2921 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2922
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02002923static void hotkey_wakeup_reason_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002924{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002925 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2926 "wakeup_reason");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002927}
2928
2929/* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002930static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2931 struct device_attribute *attr,
2932 char *buf)
2933{
2934 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2935}
2936
2937static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2938 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2939 hotkey_wakeup_hotunplug_complete_show, NULL);
2940
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02002941static void hotkey_wakeup_hotunplug_complete_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002942{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002943 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2944 "wakeup_hotunplug_complete");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002945}
2946
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002947/* --------------------------------------------------------------------- */
2948
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002949static struct attribute *hotkey_attributes[] __initdata = {
2950 &dev_attr_hotkey_enable.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002951 &dev_attr_hotkey_bios_enabled.attr,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002952 &dev_attr_hotkey_bios_mask.attr,
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002953 &dev_attr_hotkey_report_mode.attr,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002954 &dev_attr_hotkey_wakeup_reason.attr,
2955 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002956 &dev_attr_hotkey_mask.attr,
2957 &dev_attr_hotkey_all_mask.attr,
2958 &dev_attr_hotkey_recommended_mask.attr,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002959#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002960 &dev_attr_hotkey_source_mask.attr,
2961 &dev_attr_hotkey_poll_freq.attr,
2962#endif
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002963};
2964
Johannes Berg19d337d2009-06-02 13:01:37 +02002965/*
2966 * Sync both the hw and sw blocking state of all switches
2967 */
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002968static void tpacpi_send_radiosw_update(void)
2969{
2970 int wlsw;
2971
Johannes Berg19d337d2009-06-02 13:01:37 +02002972 /*
2973 * We must sync all rfkill controllers *before* issuing any
2974 * rfkill input events, or we will race the rfkill core input
2975 * handler.
2976 *
2977 * tpacpi_inputdev_send_mutex works as a syncronization point
2978 * for the above.
2979 *
2980 * We optimize to avoid numerous calls to hotkey_get_wlsw.
2981 */
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002982
Johannes Berg19d337d2009-06-02 13:01:37 +02002983 wlsw = hotkey_get_wlsw();
2984
2985 /* Sync hw blocking state first if it is hw-blocked */
2986 if (wlsw == TPACPI_RFK_RADIO_OFF)
2987 tpacpi_rfk_update_hwblock_state(true);
2988
2989 /* Sync sw blocking state */
2990 tpacpi_rfk_update_swstate_all();
2991
2992 /* Sync hw blocking state last if it is hw-unblocked */
2993 if (wlsw == TPACPI_RFK_RADIO_ON)
2994 tpacpi_rfk_update_hwblock_state(false);
2995
2996 /* Issue rfkill input event for WLSW switch */
2997 if (!(wlsw < 0)) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002998 mutex_lock(&tpacpi_inputdev_send_mutex);
2999
3000 input_report_switch(tpacpi_inputdev,
Johannes Berg19d337d2009-06-02 13:01:37 +02003001 SW_RFKILL_ALL, (wlsw > 0));
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003002 input_sync(tpacpi_inputdev);
3003
3004 mutex_unlock(&tpacpi_inputdev_send_mutex);
3005 }
Johannes Berg19d337d2009-06-02 13:01:37 +02003006
3007 /*
3008 * this can be unconditional, as we will poll state again
3009 * if userspace uses the notify to read data
3010 */
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003011 hotkey_radio_sw_notify_change();
3012}
3013
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003014static void hotkey_exit(void)
3015{
3016#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003017 mutex_lock(&hotkey_mutex);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003018 hotkey_poll_stop_sync();
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003019 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003020#endif
3021
3022 if (hotkey_dev_attributes)
3023 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3024
3025 kfree(hotkey_keycode_map);
3026
Henrique de Moraes Holschuh4be73002009-09-20 14:09:23 -03003027 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003028 "restoring original HKEY status and mask\n");
3029 /* yes, there is a bitwise or below, we want the
3030 * functions to be called even if one of them fail */
3031 if (((tp_features.hotkey_mask &&
3032 hotkey_mask_set(hotkey_orig_mask)) |
3033 hotkey_status_set(false)) != 0)
Henrique de Moraes Holschuh4be73002009-09-20 14:09:23 -03003034 printk(TPACPI_ERR
3035 "failed to restore hot key mask "
3036 "to BIOS defaults\n");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003037}
3038
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003039static void __init hotkey_unmap(const unsigned int scancode)
3040{
3041 if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
3042 clear_bit(hotkey_keycode_map[scancode],
3043 tpacpi_inputdev->keybit);
3044 hotkey_keycode_map[scancode] = KEY_RESERVED;
3045 }
3046}
3047
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003048/*
3049 * HKEY quirks:
3050 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
3051 */
3052
3053#define TPACPI_HK_Q_INIMASK 0x0001
3054
3055static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
3056 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
3057 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
3058 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
3059 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
3060 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
3061 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
3062 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
3063 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
3064 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
3065 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
3066 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
3067 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
3068 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
3069 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
3070 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
3071 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
3072 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
3073 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
3074 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
3075};
3076
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003077static int __init hotkey_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003078{
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003079 /* Requirements for changing the default keymaps:
3080 *
3081 * 1. Many of the keys are mapped to KEY_RESERVED for very
3082 * good reasons. Do not change them unless you have deep
3083 * knowledge on the IBM and Lenovo ThinkPad firmware for
3084 * the various ThinkPad models. The driver behaves
3085 * differently for KEY_RESERVED: such keys have their
3086 * hot key mask *unset* in mask_recommended, and also
3087 * in the initial hot key mask programmed into the
3088 * firmware at driver load time, which means the firm-
3089 * ware may react very differently if you change them to
3090 * something else;
3091 *
3092 * 2. You must be subscribed to the linux-thinkpad and
3093 * ibm-acpi-devel mailing lists, and you should read the
3094 * list archives since 2007 if you want to change the
3095 * keymaps. This requirement exists so that you will
3096 * know the past history of problems with the thinkpad-
3097 * acpi driver keymaps, and also that you will be
3098 * listening to any bug reports;
3099 *
3100 * 3. Do not send thinkpad-acpi specific patches directly to
3101 * for merging, *ever*. Send them to the linux-acpi
3102 * mailinglist for comments. Merging is to be done only
3103 * through acpi-test and the ACPI maintainer.
3104 *
3105 * If the above is too much to ask, don't change the keymap.
3106 * Ask the thinkpad-acpi maintainer to do it, instead.
3107 */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003108 static u16 ibm_keycode_map[] __initdata = {
3109 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3110 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
3111 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3112 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003113
3114 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003115 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3116 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3117 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003118
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003119 /* brightness: firmware always reacts to them */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003120 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003121 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003122
3123 /* Thinklight: firmware always react to it */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003124 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003125
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003126 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3127 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003128
3129 /* Volume: firmware always react to it and reprograms
3130 * the built-in *extra* mixer. Never map it to control
3131 * another mixer by default. */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003132 KEY_RESERVED, /* 0x14: VOLUME UP */
3133 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3134 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003135
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003136 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003137
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003138 /* (assignments unknown, please report if found) */
3139 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3140 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3141 };
3142 static u16 lenovo_keycode_map[] __initdata = {
3143 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3144 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
3145 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3146 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003147
3148 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003149 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3150 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3151 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003152
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003153 /* These should be enabled --only-- when ACPI video
3154 * is disabled (i.e. in "vendor" mode), and are handled
3155 * in a special way by the init code */
3156 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
3157 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003158
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003159 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003160
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003161 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3162 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003163
3164 /* Volume: z60/z61, T60 (BIOS version?): firmware always
3165 * react to it and reprograms the built-in *extra* mixer.
3166 * Never map it to control another mixer by default.
3167 *
3168 * T60?, T61, R60?, R61: firmware and EC tries to send
3169 * these over the regular keyboard, so these are no-ops,
3170 * but there are still weird bugs re. MUTE, so do not
3171 * change unless you get test reports from all Lenovo
3172 * models. May cause the BIOS to interfere with the
3173 * HDA mixer.
3174 */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003175 KEY_RESERVED, /* 0x14: VOLUME UP */
3176 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3177 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003178
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003179 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003180
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003181 /* (assignments unknown, please report if found) */
3182 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3183 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3184 };
3185
3186#define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
3187#define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
3188#define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
3189
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003190 int res, i;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003191 int status;
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03003192 int hkeyv;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003193 bool radiosw_state = false;
3194 bool tabletsw_state = false;
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03003195
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003196 unsigned long quirks;
3197
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003198 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3199 "initializing hotkey subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003200
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003201 BUG_ON(!tpacpi_inputdev);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003202 BUG_ON(tpacpi_inputdev->open != NULL ||
3203 tpacpi_inputdev->close != NULL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003204
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003205 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003206 mutex_init(&hotkey_mutex);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003207
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003208#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3209 mutex_init(&hotkey_thread_mutex);
3210 mutex_init(&hotkey_thread_data_mutex);
3211#endif
3212
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003213 /* hotkey not supported on 570 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003214 tp_features.hotkey = hkey_handle != NULL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003215
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003216 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3217 "hotkeys are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003218 str_supported(tp_features.hotkey));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003219
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003220 if (!tp_features.hotkey)
3221 return 1;
3222
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003223 quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
3224 ARRAY_SIZE(tpacpi_hotkey_qtable));
3225
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03003226 tpacpi_disable_brightness_delay();
3227
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003228 /* MUST have enough space for all attributes to be added to
3229 * hotkey_dev_attributes */
3230 hotkey_dev_attributes = create_attr_set(
3231 ARRAY_SIZE(hotkey_attributes) + 2,
3232 NULL);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003233 if (!hotkey_dev_attributes)
3234 return -ENOMEM;
3235 res = add_many_to_attr_set(hotkey_dev_attributes,
3236 hotkey_attributes,
3237 ARRAY_SIZE(hotkey_attributes));
3238 if (res)
3239 goto err_exit;
3240
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003241 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003242 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
3243 for HKEY interface version 0x100 */
3244 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
3245 if ((hkeyv >> 8) != 1) {
3246 printk(TPACPI_ERR "unknown version of the "
3247 "HKEY interface: 0x%x\n", hkeyv);
3248 printk(TPACPI_ERR "please report this to %s\n",
3249 TPACPI_MAIL);
3250 } else {
3251 /*
3252 * MHKV 0x100 in A31, R40, R40e,
3253 * T4x, X31, and later
3254 */
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003255 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3256 "firmware HKEY interface version: 0x%x\n",
3257 hkeyv);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003258
3259 /* Paranoia check AND init hotkey_all_mask */
3260 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3261 "MHKA", "qd")) {
3262 printk(TPACPI_ERR
3263 "missing MHKA handler, "
3264 "please report this to %s\n",
3265 TPACPI_MAIL);
3266 /* Fallback: pre-init for FN+F3,F4,F12 */
3267 hotkey_all_mask = 0x080cU;
3268 } else {
3269 tp_features.hotkey_mask = 1;
3270 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003271 }
3272 }
3273
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003274 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3275 "hotkey masks are %s\n",
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003276 str_supported(tp_features.hotkey_mask));
3277
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003278 /* Init hotkey_all_mask if not initialized yet */
3279 if (!tp_features.hotkey_mask && !hotkey_all_mask &&
3280 (quirks & TPACPI_HK_Q_INIMASK))
3281 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003282
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003283 /* Init hotkey_acpi_mask and hotkey_orig_mask */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003284 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003285 /* hotkey_source_mask *must* be zero for
3286 * the first hotkey_mask_get to return hotkey_orig_mask */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003287 res = hotkey_mask_get();
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003288 if (res)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003289 goto err_exit;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003290
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003291 hotkey_orig_mask = hotkey_acpi_mask;
3292 } else {
3293 hotkey_orig_mask = hotkey_all_mask;
3294 hotkey_acpi_mask = hotkey_all_mask;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003295 }
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003296
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003297#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3298 if (dbg_wlswemul) {
3299 tp_features.hotkey_wlsw = 1;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003300 radiosw_state = !!tpacpi_wlsw_emulstate;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003301 printk(TPACPI_INFO
3302 "radio switch emulation enabled\n");
3303 } else
3304#endif
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003305 /* Not all thinkpads have a hardware radio switch */
3306 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
3307 tp_features.hotkey_wlsw = 1;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003308 radiosw_state = !!status;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003309 printk(TPACPI_INFO
3310 "radio switch found; radios are %s\n",
3311 enabled(status, 0));
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003312 }
3313 if (tp_features.hotkey_wlsw)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003314 res = add_to_attr_set(hotkey_dev_attributes,
3315 &dev_attr_hotkey_radio_sw.attr);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003316
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003317 /* For X41t, X60t, X61t Tablets... */
3318 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
3319 tp_features.hotkey_tablet = 1;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003320 tabletsw_state = !!(status & TP_HOTKEY_TABLET_MASK);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003321 printk(TPACPI_INFO
3322 "possible tablet mode switch found; "
3323 "ThinkPad in %s mode\n",
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003324 (tabletsw_state) ? "tablet" : "laptop");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003325 res = add_to_attr_set(hotkey_dev_attributes,
3326 &dev_attr_hotkey_tablet_mode.attr);
3327 }
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02003328
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003329 if (!res)
3330 res = register_attr_set_with_sysfs(
3331 hotkey_dev_attributes,
3332 &tpacpi_pdev->dev.kobj);
3333 if (res)
3334 goto err_exit;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003335
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003336 /* Set up key map */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003337
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003338 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
3339 GFP_KERNEL);
3340 if (!hotkey_keycode_map) {
3341 printk(TPACPI_ERR
3342 "failed to allocate memory for key map\n");
3343 res = -ENOMEM;
3344 goto err_exit;
3345 }
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003346
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003347 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003348 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003349 "using Lenovo default hot key map\n");
3350 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
3351 TPACPI_HOTKEY_MAP_SIZE);
3352 } else {
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 IBM default hot key map\n");
3355 memcpy(hotkey_keycode_map, &ibm_keycode_map,
3356 TPACPI_HOTKEY_MAP_SIZE);
3357 }
3358
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003359 input_set_capability(tpacpi_inputdev, EV_MSC, MSC_SCAN);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003360 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
3361 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3362 tpacpi_inputdev->keycode = hotkey_keycode_map;
3363 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3364 if (hotkey_keycode_map[i] != KEY_RESERVED) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003365 input_set_capability(tpacpi_inputdev, EV_KEY,
3366 hotkey_keycode_map[i]);
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003367 } else {
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003368 if (i < sizeof(hotkey_reserved_mask)*8)
3369 hotkey_reserved_mask |= 1 << i;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003370 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003371 }
3372
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003373 if (tp_features.hotkey_wlsw) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003374 input_set_capability(tpacpi_inputdev, EV_SW, SW_RFKILL_ALL);
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003375 input_report_switch(tpacpi_inputdev,
3376 SW_RFKILL_ALL, radiosw_state);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003377 }
3378 if (tp_features.hotkey_tablet) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003379 input_set_capability(tpacpi_inputdev, EV_SW, SW_TABLET_MODE);
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003380 input_report_switch(tpacpi_inputdev,
3381 SW_TABLET_MODE, tabletsw_state);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003382 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003383
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003384 /* Do not issue duplicate brightness change events to
3385 * userspace */
3386 if (!tp_features.bright_acpimode)
3387 /* update bright_acpimode... */
3388 tpacpi_check_std_acpi_brightness_support();
3389
Henrique de Moraes Holschuh8bf3d4c2009-05-30 13:25:09 -03003390 if (tp_features.bright_acpimode && acpi_video_backlight_support()) {
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003391 printk(TPACPI_INFO
3392 "This ThinkPad has standard ACPI backlight "
3393 "brightness control, supported by the ACPI "
3394 "video driver\n");
3395 printk(TPACPI_NOTICE
3396 "Disabling thinkpad-acpi brightness events "
3397 "by default...\n");
3398
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003399 /* Disable brightness up/down on Lenovo thinkpads when
3400 * ACPI is handling them, otherwise it is plain impossible
3401 * for userspace to do something even remotely sane */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003402 hotkey_reserved_mask |=
3403 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3404 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003405 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
3406 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003407 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003408
Henrique de Moraes Holschuh230d8cf2009-09-12 15:22:17 -03003409#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003410 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3411 & ~hotkey_all_mask
3412 & ~hotkey_reserved_mask;
Henrique de Moraes Holschuh230d8cf2009-09-12 15:22:17 -03003413
3414 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3415 "hotkey source mask 0x%08x, polling freq %u\n",
3416 hotkey_source_mask, hotkey_poll_freq);
3417#endif
3418
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003419 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3420 "enabling firmware HKEY event interface...\n");
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003421 res = hotkey_status_set(true);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003422 if (res) {
3423 hotkey_exit();
3424 return res;
3425 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003426 res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
3427 | hotkey_driver_mask)
3428 & ~hotkey_source_mask);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003429 if (res < 0 && res != -ENXIO) {
3430 hotkey_exit();
3431 return res;
3432 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003433 hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
3434 & ~hotkey_reserved_mask;
3435 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3436 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3437 hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003438
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003439 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3440 "legacy ibm/hotkey event reporting over procfs %s\n",
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003441 (hotkey_report_mode < 2) ?
3442 "enabled" : "disabled");
3443
3444 tpacpi_inputdev->open = &hotkey_inputdev_open;
3445 tpacpi_inputdev->close = &hotkey_inputdev_close;
3446
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003447 hotkey_poll_setup_safe(true);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003448
3449 return 0;
3450
3451err_exit:
3452 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3453 hotkey_dev_attributes = NULL;
3454
3455 return (res < 0)? res : 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003456}
3457
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003458static bool hotkey_notify_hotkey(const u32 hkey,
3459 bool *send_acpi_ev,
3460 bool *ignore_acpi_ev)
3461{
3462 /* 0x1000-0x1FFF: key presses */
3463 unsigned int scancode = hkey & 0xfff;
3464 *send_acpi_ev = true;
3465 *ignore_acpi_ev = false;
3466
3467 if (scancode > 0 && scancode < 0x21) {
3468 scancode--;
3469 if (!(hotkey_source_mask & (1 << scancode))) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003470 tpacpi_input_send_key_masked(scancode);
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003471 *send_acpi_ev = false;
3472 } else {
3473 *ignore_acpi_ev = true;
3474 }
3475 return true;
3476 }
3477 return false;
3478}
3479
3480static bool hotkey_notify_wakeup(const u32 hkey,
3481 bool *send_acpi_ev,
3482 bool *ignore_acpi_ev)
3483{
3484 /* 0x2000-0x2FFF: Wakeup reason */
3485 *send_acpi_ev = true;
3486 *ignore_acpi_ev = false;
3487
3488 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003489 case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */
3490 case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003491 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3492 *ignore_acpi_ev = true;
3493 break;
3494
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003495 case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */
3496 case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003497 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3498 *ignore_acpi_ev = true;
3499 break;
3500
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003501 case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */
3502 case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003503 printk(TPACPI_ALERT
3504 "EMERGENCY WAKEUP: battery almost empty\n");
3505 /* how to auto-heal: */
3506 /* 2313: woke up from S3, go to S4/S5 */
3507 /* 2413: woke up from S4, go to S5 */
3508 break;
3509
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003510 default:
3511 return false;
3512 }
3513
3514 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
3515 printk(TPACPI_INFO
3516 "woke up due to a hot-unplug "
3517 "request...\n");
3518 hotkey_wakeup_reason_notify_change();
3519 }
3520 return true;
3521}
3522
3523static bool hotkey_notify_usrevent(const u32 hkey,
3524 bool *send_acpi_ev,
3525 bool *ignore_acpi_ev)
3526{
3527 /* 0x5000-0x5FFF: human interface helpers */
3528 *send_acpi_ev = true;
3529 *ignore_acpi_ev = false;
3530
3531 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003532 case TP_HKEY_EV_PEN_INSERTED: /* X61t: tablet pen inserted into bay */
3533 case TP_HKEY_EV_PEN_REMOVED: /* X61t: tablet pen removed from bay */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003534 return true;
3535
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003536 case TP_HKEY_EV_TABLET_TABLET: /* X41t-X61t: tablet mode */
3537 case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003538 tpacpi_input_send_tabletsw();
3539 hotkey_tablet_mode_notify_change();
3540 *send_acpi_ev = false;
3541 return true;
3542
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003543 case TP_HKEY_EV_LID_CLOSE: /* Lid closed */
3544 case TP_HKEY_EV_LID_OPEN: /* Lid opened */
3545 case TP_HKEY_EV_BRGHT_CHANGED: /* brightness changed */
Henrique de Moraes Holschuh176dd982009-09-20 14:09:24 -03003546 /* do not propagate these events */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003547 *ignore_acpi_ev = true;
3548 return true;
3549
3550 default:
3551 return false;
3552 }
3553}
3554
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003555static void thermal_dump_all_sensors(void);
3556
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003557static bool hotkey_notify_thermal(const u32 hkey,
3558 bool *send_acpi_ev,
3559 bool *ignore_acpi_ev)
3560{
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003561 bool known = true;
3562
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003563 /* 0x6000-0x6FFF: thermal alarms */
3564 *send_acpi_ev = true;
3565 *ignore_acpi_ev = false;
3566
3567 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003568 case TP_HKEY_EV_THM_TABLE_CHANGED:
Henrique de Moraes Holschuh54926ce2009-01-11 03:01:09 -02003569 printk(TPACPI_INFO
3570 "EC reports that Thermal Table has changed\n");
3571 /* recommended action: do nothing, we don't have
3572 * Lenovo ATM information */
3573 return true;
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003574 case TP_HKEY_EV_ALARM_BAT_HOT:
3575 printk(TPACPI_CRIT
3576 "THERMAL ALARM: battery is too hot!\n");
3577 /* recommended action: warn user through gui */
3578 break;
3579 case TP_HKEY_EV_ALARM_BAT_XHOT:
3580 printk(TPACPI_ALERT
3581 "THERMAL EMERGENCY: battery is extremely hot!\n");
3582 /* recommended action: immediate sleep/hibernate */
3583 break;
3584 case TP_HKEY_EV_ALARM_SENSOR_HOT:
3585 printk(TPACPI_CRIT
3586 "THERMAL ALARM: "
3587 "a sensor reports something is too hot!\n");
3588 /* recommended action: warn user through gui, that */
3589 /* some internal component is too hot */
3590 break;
3591 case TP_HKEY_EV_ALARM_SENSOR_XHOT:
3592 printk(TPACPI_ALERT
3593 "THERMAL EMERGENCY: "
3594 "a sensor reports something is extremely hot!\n");
3595 /* recommended action: immediate sleep/hibernate */
3596 break;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003597 default:
3598 printk(TPACPI_ALERT
3599 "THERMAL ALERT: unknown thermal alarm received\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003600 known = false;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003601 }
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003602
3603 thermal_dump_all_sensors();
3604
3605 return known;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003606}
3607
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003608static void hotkey_notify(struct ibm_struct *ibm, u32 event)
3609{
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003610 u32 hkey;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003611 bool send_acpi_ev;
3612 bool ignore_acpi_ev;
3613 bool known_ev;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003614
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003615 if (event != 0x80) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003616 printk(TPACPI_ERR
3617 "unknown HKEY notification event %d\n", event);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003618 /* forward it to userspace, maybe it knows how to handle it */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003619 acpi_bus_generate_netlink_event(
3620 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02003621 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003622 event, 0);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003623 return;
3624 }
3625
3626 while (1) {
3627 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003628 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003629 return;
3630 }
3631
3632 if (hkey == 0) {
3633 /* queue empty */
3634 return;
3635 }
3636
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003637 send_acpi_ev = true;
3638 ignore_acpi_ev = false;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003639
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003640 switch (hkey >> 12) {
3641 case 1:
3642 /* 0x1000-0x1FFF: key presses */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003643 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
3644 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003645 break;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003646 case 2:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003647 /* 0x2000-0x2FFF: Wakeup reason */
3648 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
3649 &ignore_acpi_ev);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003650 break;
3651 case 3:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003652 /* 0x3000-0x3FFF: bay-related wakeups */
Henrique de Moraes Holschuhbf8b29c2010-02-25 21:28:56 -03003653 switch (hkey) {
3654 case TP_HKEY_EV_BAYEJ_ACK:
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003655 hotkey_autosleep_ack = 1;
3656 printk(TPACPI_INFO
3657 "bay ejected\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003658 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003659 known_ev = true;
Henrique de Moraes Holschuhbf8b29c2010-02-25 21:28:56 -03003660 break;
3661 case TP_HKEY_EV_OPTDRV_EJ:
3662 /* FIXME: kick libata if SATA link offline */
3663 known_ev = true;
3664 break;
3665 default:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003666 known_ev = false;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003667 }
3668 break;
3669 case 4:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003670 /* 0x4000-0x4FFF: dock-related wakeups */
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003671 if (hkey == TP_HKEY_EV_UNDOCK_ACK) {
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003672 hotkey_autosleep_ack = 1;
3673 printk(TPACPI_INFO
3674 "undocked\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003675 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003676 known_ev = true;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003677 } else {
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003678 known_ev = false;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003679 }
3680 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003681 case 5:
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02003682 /* 0x5000-0x5FFF: human interface helpers */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003683 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
3684 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003685 break;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003686 case 6:
3687 /* 0x6000-0x6FFF: thermal alarms */
3688 known_ev = hotkey_notify_thermal(hkey, &send_acpi_ev,
3689 &ignore_acpi_ev);
3690 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003691 case 7:
3692 /* 0x7000-0x7FFF: misc */
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003693 if (tp_features.hotkey_wlsw &&
3694 hkey == TP_HKEY_EV_RFKILL_CHANGED) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003695 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02003696 send_acpi_ev = 0;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003697 known_ev = true;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003698 break;
3699 }
3700 /* fallthrough to default */
3701 default:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003702 known_ev = false;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02003703 }
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003704 if (!known_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003705 printk(TPACPI_NOTICE
3706 "unhandled HKEY event 0x%04x\n", hkey);
Henrique de Moraes Holschuhcb429352009-01-11 03:01:07 -02003707 printk(TPACPI_NOTICE
3708 "please report the conditions when this "
3709 "event happened to %s\n", TPACPI_MAIL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003710 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003711
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003712 /* Legacy events */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003713 if (!ignore_acpi_ev &&
3714 (send_acpi_ev || hotkey_report_mode < 2)) {
3715 acpi_bus_generate_proc_event(ibm->acpi->device,
3716 event, hkey);
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03003717 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003718
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003719 /* netlink events */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03003720 if (!ignore_acpi_ev && send_acpi_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003721 acpi_bus_generate_netlink_event(
3722 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02003723 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003724 event, hkey);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003725 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003726 }
3727}
3728
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003729static void hotkey_suspend(pm_message_t state)
3730{
3731 /* Do these on suspend, we get the events on early resume! */
3732 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
3733 hotkey_autosleep_ack = 0;
3734}
3735
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03003736static void hotkey_resume(void)
3737{
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03003738 tpacpi_disable_brightness_delay();
3739
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003740 if (hotkey_status_set(true) < 0 ||
3741 hotkey_mask_set(hotkey_acpi_mask) < 0)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003742 printk(TPACPI_ERR
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003743 "error while attempting to reset the event "
3744 "firmware interface\n");
3745
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003746 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02003747 hotkey_tablet_mode_notify_change();
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003748 hotkey_wakeup_reason_notify_change();
3749 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003750 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03003751}
3752
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003753/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003754static int hotkey_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03003756 int res, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003758 if (!tp_features.hotkey) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003759 seq_printf(m, "status:\t\tnot supported\n");
3760 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003761 }
3762
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02003763 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02003764 return -ERESTARTSYS;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02003765 res = hotkey_status_get(&status);
3766 if (!res)
3767 res = hotkey_mask_get();
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003768 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03003769 if (res)
3770 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003772 seq_printf(m, "status:\t\t%s\n", enabled(status, 0));
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003773 if (hotkey_all_mask) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003774 seq_printf(m, "mask:\t\t0x%08x\n", hotkey_user_mask);
3775 seq_printf(m, "commands:\tenable, disable, reset, <mask>\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776 } else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003777 seq_printf(m, "mask:\t\tnot supported\n");
3778 seq_printf(m, "commands:\tenable, disable, reset\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779 }
3780
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003781 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782}
3783
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003784static void hotkey_enabledisable_warn(bool enable)
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003785{
3786 tpacpi_log_usertask("procfs hotkey enable/disable");
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003787 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
3788 TPACPI_WARN
3789 "hotkey enable/disable functionality has been "
3790 "removed from the driver. Hotkeys are always "
3791 "enabled\n"))
3792 printk(TPACPI_ERR
3793 "Please remove the hotkey=enable module "
3794 "parameter, it is deprecated. Hotkeys are always "
3795 "enabled\n");
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003796}
3797
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003798static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003800 int res;
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03003801 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003804 if (!tp_features.hotkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805 return -ENODEV;
3806
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02003807 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02003808 return -ERESTARTSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003809
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003810 mask = hotkey_user_mask;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003811
3812 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813 while ((cmd = next_cmd(&buf))) {
3814 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003815 hotkey_enabledisable_warn(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003817 hotkey_enabledisable_warn(0);
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003818 res = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819 } else if (strlencmp(cmd, "reset") == 0) {
Henrique de Moraes Holschuh20c9aa42009-09-12 15:22:16 -03003820 mask = (hotkey_all_mask | hotkey_source_mask)
3821 & ~hotkey_reserved_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
3823 /* mask set */
3824 } else if (sscanf(cmd, "%x", &mask) == 1) {
3825 /* mask set */
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003826 } else {
3827 res = -EINVAL;
3828 goto errexit;
3829 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830 }
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003831
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003832 if (!res) {
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003833 tpacpi_disclose_usertask("procfs hotkey",
3834 "set mask to 0x%08x\n", mask);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003835 res = hotkey_user_mask_set(mask);
3836 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003838errexit:
3839 mutex_unlock(&hotkey_mutex);
3840 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003841}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003842
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003843static const struct acpi_device_id ibm_htk_device_ids[] = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003844 {TPACPI_ACPI_HKEY_HID, 0},
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003845 {"", 0},
3846};
3847
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003848static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003849 .hid = ibm_htk_device_ids,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003850 .notify = hotkey_notify,
3851 .handle = &hkey_handle,
3852 .type = ACPI_DEVICE_NOTIFY,
3853};
3854
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003855static struct ibm_struct hotkey_driver_data = {
3856 .name = "hotkey",
3857 .read = hotkey_read,
3858 .write = hotkey_write,
3859 .exit = hotkey_exit,
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03003860 .resume = hotkey_resume,
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003861 .suspend = hotkey_suspend,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003862 .acpi = &ibm_hotkey_acpidriver,
3863};
3864
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003865/*************************************************************************
3866 * Bluetooth subdriver
3867 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003868
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003869enum {
3870 /* ACPI GBDC/SBDC bits */
3871 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
3872 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003873 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
3874 off / last state */
3875};
3876
3877enum {
3878 /* ACPI \BLTH commands */
3879 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
3880 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
3881 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
3882 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
3883 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003884};
3885
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003886#define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
3887
Johannes Berg19d337d2009-06-02 13:01:37 +02003888static int bluetooth_get_status(void)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003889{
3890 int status;
3891
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003892#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3893 if (dbg_bluetoothemul)
3894 return (tpacpi_bluetooth_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02003895 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003896#endif
3897
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003898 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
3899 return -EIO;
3900
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003901 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02003902 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003903}
3904
Johannes Berg19d337d2009-06-02 13:01:37 +02003905static int bluetooth_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003906{
3907 int status;
3908
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003909 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02003910 "will attempt to %s bluetooth\n",
3911 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003912
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003913#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3914 if (dbg_bluetoothemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02003915 tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003916 return 0;
3917 }
3918#endif
3919
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003920 /* We make sure to keep TP_ACPI_BLUETOOTH_RESUMECTRL off */
Henrique de Moraes Holschuh208b9962009-12-09 01:36:22 +00003921 status = TP_ACPI_BLUETOOTH_RESUMECTRL;
Johannes Berg19d337d2009-06-02 13:01:37 +02003922 if (state == TPACPI_RFK_RADIO_ON)
Henrique de Moraes Holschuh208b9962009-12-09 01:36:22 +00003923 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
Johannes Berg19d337d2009-06-02 13:01:37 +02003924
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003925 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
3926 return -EIO;
3927
3928 return 0;
3929}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003930
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003931/* sysfs bluetooth enable ---------------------------------------------- */
3932static ssize_t bluetooth_enable_show(struct device *dev,
3933 struct device_attribute *attr,
3934 char *buf)
3935{
Johannes Berg19d337d2009-06-02 13:01:37 +02003936 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID,
3937 attr, buf);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003938}
3939
3940static ssize_t bluetooth_enable_store(struct device *dev,
3941 struct device_attribute *attr,
3942 const char *buf, size_t count)
3943{
Johannes Berg19d337d2009-06-02 13:01:37 +02003944 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID,
3945 attr, buf, count);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003946}
3947
3948static struct device_attribute dev_attr_bluetooth_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03003949 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003950 bluetooth_enable_show, bluetooth_enable_store);
3951
3952/* --------------------------------------------------------------------- */
3953
3954static struct attribute *bluetooth_attributes[] = {
3955 &dev_attr_bluetooth_enable.attr,
3956 NULL
3957};
3958
3959static const struct attribute_group bluetooth_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003960 .attrs = bluetooth_attributes,
3961};
3962
Johannes Berg19d337d2009-06-02 13:01:37 +02003963static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = {
3964 .get_status = bluetooth_get_status,
3965 .set_status = bluetooth_set_status,
3966};
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003967
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003968static void bluetooth_shutdown(void)
3969{
3970 /* Order firmware to save current state to NVRAM */
3971 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3972 TP_ACPI_BLTH_SAVE_STATE))
3973 printk(TPACPI_NOTICE
3974 "failed to save bluetooth state to NVRAM\n");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003975 else
3976 vdbg_printk(TPACPI_DBG_RFKILL,
3977 "bluestooth state saved to NVRAM\n");
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003978}
3979
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003980static void bluetooth_exit(void)
3981{
3982 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3983 &bluetooth_attr_group);
Johannes Berg19d337d2009-06-02 13:01:37 +02003984
3985 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
3986
3987 bluetooth_shutdown();
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003988}
3989
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003990static int __init bluetooth_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003992 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003993 int status = 0;
3994
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003995 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3996 "initializing bluetooth subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003997
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003998 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003999
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004000 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4001 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004002 tp_features.bluetooth = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004003 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004005 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4006 "bluetooth is %s, status 0x%02x\n",
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004007 str_supported(tp_features.bluetooth),
4008 status);
4009
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004010#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4011 if (dbg_bluetoothemul) {
4012 tp_features.bluetooth = 1;
4013 printk(TPACPI_INFO
4014 "bluetooth switch emulation enabled\n");
4015 } else
4016#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004017 if (tp_features.bluetooth &&
4018 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
4019 /* no bluetooth hardware present in system */
4020 tp_features.bluetooth = 0;
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004021 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004022 "bluetooth hardware not installed\n");
4023 }
4024
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004025 if (!tp_features.bluetooth)
4026 return 1;
4027
Johannes Berg19d337d2009-06-02 13:01:37 +02004028 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
4029 &bluetooth_tprfk_ops,
4030 RFKILL_TYPE_BLUETOOTH,
4031 TPACPI_RFK_BLUETOOTH_SW_NAME,
4032 true);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004033 if (res)
4034 return res;
4035
Johannes Berg19d337d2009-06-02 13:01:37 +02004036 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4037 &bluetooth_attr_group);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004038 if (res) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004039 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004040 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004041 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004042
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004043 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044}
4045
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004046/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004047static int bluetooth_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048{
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004049 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050}
4051
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004052static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053{
Johannes Berg19d337d2009-06-02 13:01:37 +02004054 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055}
4056
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004057static struct ibm_struct bluetooth_driver_data = {
4058 .name = "bluetooth",
4059 .read = bluetooth_read,
4060 .write = bluetooth_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004061 .exit = bluetooth_exit,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004062 .shutdown = bluetooth_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004063};
4064
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004065/*************************************************************************
4066 * Wan subdriver
4067 */
4068
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004069enum {
4070 /* ACPI GWAN/SWAN bits */
4071 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
4072 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02004073 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
4074 off / last state */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004075};
4076
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004077#define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
4078
Johannes Berg19d337d2009-06-02 13:01:37 +02004079static int wan_get_status(void)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004080{
4081 int status;
4082
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004083#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4084 if (dbg_wwanemul)
4085 return (tpacpi_wwan_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004086 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004087#endif
4088
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004089 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
4090 return -EIO;
4091
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004092 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004093 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004094}
4095
Johannes Berg19d337d2009-06-02 13:01:37 +02004096static int wan_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004097{
4098 int status;
4099
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004100 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02004101 "will attempt to %s wwan\n",
4102 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004103
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004104#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4105 if (dbg_wwanemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004106 tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004107 return 0;
4108 }
4109#endif
4110
Henrique de Moraes Holschuh208b9962009-12-09 01:36:22 +00004111 /* We make sure to set TP_ACPI_WANCARD_RESUMECTRL */
4112 status = TP_ACPI_WANCARD_RESUMECTRL;
Johannes Berg19d337d2009-06-02 13:01:37 +02004113 if (state == TPACPI_RFK_RADIO_ON)
Henrique de Moraes Holschuh208b9962009-12-09 01:36:22 +00004114 status |= TP_ACPI_WANCARD_RADIOSSW;
Johannes Berg19d337d2009-06-02 13:01:37 +02004115
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004116 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
4117 return -EIO;
4118
4119 return 0;
4120}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004121
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004122/* sysfs wan enable ---------------------------------------------------- */
4123static ssize_t wan_enable_show(struct device *dev,
4124 struct device_attribute *attr,
4125 char *buf)
4126{
Johannes Berg19d337d2009-06-02 13:01:37 +02004127 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
4128 attr, buf);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004129}
4130
4131static ssize_t wan_enable_store(struct device *dev,
4132 struct device_attribute *attr,
4133 const char *buf, size_t count)
4134{
Johannes Berg19d337d2009-06-02 13:01:37 +02004135 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
4136 attr, buf, count);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004137}
4138
4139static struct device_attribute dev_attr_wan_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03004140 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004141 wan_enable_show, wan_enable_store);
4142
4143/* --------------------------------------------------------------------- */
4144
4145static struct attribute *wan_attributes[] = {
4146 &dev_attr_wan_enable.attr,
4147 NULL
4148};
4149
4150static const struct attribute_group wan_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004151 .attrs = wan_attributes,
4152};
4153
Johannes Berg19d337d2009-06-02 13:01:37 +02004154static const struct tpacpi_rfk_ops wan_tprfk_ops = {
4155 .get_status = wan_get_status,
4156 .set_status = wan_set_status,
4157};
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004158
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004159static void wan_shutdown(void)
4160{
4161 /* Order firmware to save current state to NVRAM */
4162 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
4163 TP_ACPI_WGSV_SAVE_STATE))
4164 printk(TPACPI_NOTICE
4165 "failed to save WWAN state to NVRAM\n");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004166 else
4167 vdbg_printk(TPACPI_DBG_RFKILL,
4168 "WWAN state saved to NVRAM\n");
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004169}
4170
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004171static void wan_exit(void)
4172{
4173 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4174 &wan_attr_group);
Johannes Berg19d337d2009-06-02 13:01:37 +02004175
4176 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4177
4178 wan_shutdown();
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004179}
4180
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004181static int __init wan_init(struct ibm_init_struct *iibm)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004182{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004183 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004184 int status = 0;
4185
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004186 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4187 "initializing wan subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004188
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004189 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004190
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004191 tp_features.wan = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004192 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004193
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004194 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4195 "wan is %s, status 0x%02x\n",
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004196 str_supported(tp_features.wan),
4197 status);
4198
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004199#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4200 if (dbg_wwanemul) {
4201 tp_features.wan = 1;
4202 printk(TPACPI_INFO
4203 "wwan switch emulation enabled\n");
4204 } else
4205#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004206 if (tp_features.wan &&
4207 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
4208 /* no wan hardware present in system */
4209 tp_features.wan = 0;
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004210 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004211 "wan hardware not installed\n");
4212 }
4213
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004214 if (!tp_features.wan)
4215 return 1;
4216
Johannes Berg19d337d2009-06-02 13:01:37 +02004217 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
4218 &wan_tprfk_ops,
4219 RFKILL_TYPE_WWAN,
4220 TPACPI_RFK_WWAN_SW_NAME,
4221 true);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004222 if (res)
4223 return res;
4224
Johannes Berg19d337d2009-06-02 13:01:37 +02004225 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4226 &wan_attr_group);
4227
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004228 if (res) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004229 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004230 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004231 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004232
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004233 return 0;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004234}
4235
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004236/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004237static int wan_read(struct seq_file *m)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004238{
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004239 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, m);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004240}
4241
4242static int wan_write(char *buf)
4243{
Johannes Berg19d337d2009-06-02 13:01:37 +02004244 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004245}
4246
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004247static struct ibm_struct wan_driver_data = {
4248 .name = "wan",
4249 .read = wan_read,
4250 .write = wan_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004251 .exit = wan_exit,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004252 .shutdown = wan_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004253};
4254
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004255/*************************************************************************
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004256 * UWB subdriver
4257 */
4258
4259enum {
4260 /* ACPI GUWB/SUWB bits */
4261 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4262 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4263};
4264
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004265#define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4266
Johannes Berg19d337d2009-06-02 13:01:37 +02004267static int uwb_get_status(void)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004268{
4269 int status;
4270
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004271#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4272 if (dbg_uwbemul)
4273 return (tpacpi_uwb_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004274 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004275#endif
4276
4277 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4278 return -EIO;
4279
4280 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004281 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004282}
4283
Johannes Berg19d337d2009-06-02 13:01:37 +02004284static int uwb_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004285{
4286 int status;
4287
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004288 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02004289 "will attempt to %s UWB\n",
4290 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004291
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004292#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4293 if (dbg_uwbemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004294 tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004295 return 0;
4296 }
4297#endif
4298
Johannes Berg19d337d2009-06-02 13:01:37 +02004299 if (state == TPACPI_RFK_RADIO_ON)
4300 status = TP_ACPI_UWB_RADIOSSW;
4301 else
4302 status = 0;
4303
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004304 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4305 return -EIO;
4306
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004307 return 0;
4308}
4309
4310/* --------------------------------------------------------------------- */
4311
Johannes Berg19d337d2009-06-02 13:01:37 +02004312static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
4313 .get_status = uwb_get_status,
4314 .set_status = uwb_set_status,
4315};
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004316
4317static void uwb_exit(void)
4318{
Johannes Berg19d337d2009-06-02 13:01:37 +02004319 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004320}
4321
4322static int __init uwb_init(struct ibm_init_struct *iibm)
4323{
4324 int res;
4325 int status = 0;
4326
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004327 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4328 "initializing uwb subdriver\n");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004329
4330 TPACPI_ACPIHANDLE_INIT(hkey);
4331
4332 tp_features.uwb = hkey_handle &&
4333 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4334
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004335 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4336 "uwb is %s, status 0x%02x\n",
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004337 str_supported(tp_features.uwb),
4338 status);
4339
4340#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4341 if (dbg_uwbemul) {
4342 tp_features.uwb = 1;
4343 printk(TPACPI_INFO
4344 "uwb switch emulation enabled\n");
4345 } else
4346#endif
4347 if (tp_features.uwb &&
4348 !(status & TP_ACPI_UWB_HWPRESENT)) {
4349 /* no uwb hardware present in system */
4350 tp_features.uwb = 0;
4351 dbg_printk(TPACPI_DBG_INIT,
4352 "uwb hardware not installed\n");
4353 }
4354
4355 if (!tp_features.uwb)
4356 return 1;
4357
4358 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
Johannes Berg19d337d2009-06-02 13:01:37 +02004359 &uwb_tprfk_ops,
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004360 RFKILL_TYPE_UWB,
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004361 TPACPI_RFK_UWB_SW_NAME,
Johannes Berg19d337d2009-06-02 13:01:37 +02004362 false);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004363 return res;
4364}
4365
4366static struct ibm_struct uwb_driver_data = {
4367 .name = "uwb",
4368 .exit = uwb_exit,
4369 .flags.experimental = 1,
4370};
4371
4372/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004373 * Video subdriver
4374 */
4375
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02004376#ifdef CONFIG_THINKPAD_ACPI_VIDEO
4377
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004378enum video_access_mode {
4379 TPACPI_VIDEO_NONE = 0,
4380 TPACPI_VIDEO_570, /* 570 */
4381 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4382 TPACPI_VIDEO_NEW, /* all others */
4383};
4384
4385enum { /* video status flags, based on VIDEO_570 */
4386 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4387 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4388 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4389};
4390
4391enum { /* TPACPI_VIDEO_570 constants */
4392 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4393 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4394 * video_status_flags */
4395 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4396 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4397};
4398
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02004399static enum video_access_mode video_supported;
4400static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004401
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004402static int video_autosw_get(void);
4403static int video_autosw_set(int enable);
4404
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004405TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004406
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004407static int __init video_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004409 int ivga;
4410
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004411 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4412
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004413 TPACPI_ACPIHANDLE_INIT(vid);
4414 TPACPI_ACPIHANDLE_INIT(vid2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004415
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004416 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4417 /* G41, assume IVGA doesn't change */
4418 vid_handle = vid2_handle;
4419
4420 if (!vid_handle)
4421 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004422 video_supported = TPACPI_VIDEO_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004423 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
4424 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004425 video_supported = TPACPI_VIDEO_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004426 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
4427 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004428 video_supported = TPACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004429 else
4430 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004431 video_supported = TPACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004433 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
4434 str_supported(video_supported != TPACPI_VIDEO_NONE),
4435 video_supported);
4436
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004437 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438}
4439
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004440static void video_exit(void)
4441{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004442 dbg_printk(TPACPI_DBG_EXIT,
4443 "restoring original video autoswitch mode\n");
4444 if (video_autosw_set(video_orig_autosw))
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004445 printk(TPACPI_ERR "error while trying to restore original "
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004446 "video autoswitch mode\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004447}
4448
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004449static int video_outputsw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450{
4451 int status = 0;
4452 int i;
4453
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004454 switch (video_supported) {
4455 case TPACPI_VIDEO_570:
4456 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
4457 TP_ACPI_VIDEO_570_PHSCMD))
4458 return -EIO;
4459 status = i & TP_ACPI_VIDEO_570_PHSMASK;
4460 break;
4461 case TPACPI_VIDEO_770:
4462 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4463 return -EIO;
4464 if (i)
4465 status |= TP_ACPI_VIDEO_S_LCD;
4466 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4467 return -EIO;
4468 if (i)
4469 status |= TP_ACPI_VIDEO_S_CRT;
4470 break;
4471 case TPACPI_VIDEO_NEW:
4472 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4473 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4474 return -EIO;
4475 if (i)
4476 status |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004478 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4479 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4480 return -EIO;
4481 if (i)
4482 status |= TP_ACPI_VIDEO_S_LCD;
4483 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4484 return -EIO;
4485 if (i)
4486 status |= TP_ACPI_VIDEO_S_DVI;
4487 break;
4488 default:
4489 return -ENOSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004490 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491
4492 return status;
4493}
4494
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004495static int video_outputsw_set(int status)
4496{
4497 int autosw;
4498 int res = 0;
4499
4500 switch (video_supported) {
4501 case TPACPI_VIDEO_570:
4502 res = acpi_evalf(NULL, NULL,
4503 "\\_SB.PHS2", "vdd",
4504 TP_ACPI_VIDEO_570_PHS2CMD,
4505 status | TP_ACPI_VIDEO_570_PHS2SET);
4506 break;
4507 case TPACPI_VIDEO_770:
4508 autosw = video_autosw_get();
4509 if (autosw < 0)
4510 return autosw;
4511
4512 res = video_autosw_set(1);
4513 if (res)
4514 return res;
4515 res = acpi_evalf(vid_handle, NULL,
4516 "ASWT", "vdd", status * 0x100, 0);
4517 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004518 printk(TPACPI_ERR
4519 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004520 return -EIO;
4521 }
4522 break;
4523 case TPACPI_VIDEO_NEW:
4524 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004525 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004526 break;
4527 default:
4528 return -ENOSYS;
4529 }
4530
4531 return (res)? 0 : -EIO;
4532}
4533
4534static int video_autosw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004536 int autosw = 0;
4537
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004538 switch (video_supported) {
4539 case TPACPI_VIDEO_570:
4540 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
4541 return -EIO;
4542 break;
4543 case TPACPI_VIDEO_770:
4544 case TPACPI_VIDEO_NEW:
4545 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4546 return -EIO;
4547 break;
4548 default:
4549 return -ENOSYS;
4550 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004551
4552 return autosw & 1;
4553}
4554
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004555static int video_autosw_set(int enable)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004556{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004557 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004558 return -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004559 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004560}
4561
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004562static int video_outputsw_cycle(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004563{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004564 int autosw = video_autosw_get();
4565 int res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004566
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004567 if (autosw < 0)
4568 return autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004569
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004570 switch (video_supported) {
4571 case TPACPI_VIDEO_570:
4572 res = video_autosw_set(1);
4573 if (res)
4574 return res;
4575 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4576 break;
4577 case TPACPI_VIDEO_770:
4578 case TPACPI_VIDEO_NEW:
4579 res = video_autosw_set(1);
4580 if (res)
4581 return res;
4582 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4583 break;
4584 default:
4585 return -ENOSYS;
4586 }
4587 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004588 printk(TPACPI_ERR
4589 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004590 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004591 }
4592
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004593 return (res)? 0 : -EIO;
4594}
4595
4596static int video_expand_toggle(void)
4597{
4598 switch (video_supported) {
4599 case TPACPI_VIDEO_570:
4600 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
4601 0 : -EIO;
4602 case TPACPI_VIDEO_770:
4603 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
4604 0 : -EIO;
4605 case TPACPI_VIDEO_NEW:
4606 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
4607 0 : -EIO;
4608 default:
4609 return -ENOSYS;
4610 }
4611 /* not reached */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004612}
4613
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004614static int video_read(struct seq_file *m)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004615{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004616 int status, autosw;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004617
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004618 if (video_supported == TPACPI_VIDEO_NONE) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004619 seq_printf(m, "status:\t\tnot supported\n");
4620 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004621 }
4622
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004623 status = video_outputsw_get();
4624 if (status < 0)
4625 return status;
4626
4627 autosw = video_autosw_get();
4628 if (autosw < 0)
4629 return autosw;
4630
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004631 seq_printf(m, "status:\t\tsupported\n");
4632 seq_printf(m, "lcd:\t\t%s\n", enabled(status, 0));
4633 seq_printf(m, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004634 if (video_supported == TPACPI_VIDEO_NEW)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004635 seq_printf(m, "dvi:\t\t%s\n", enabled(status, 3));
4636 seq_printf(m, "auto:\t\t%s\n", enabled(autosw, 0));
4637 seq_printf(m, "commands:\tlcd_enable, lcd_disable\n");
4638 seq_printf(m, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004639 if (video_supported == TPACPI_VIDEO_NEW)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004640 seq_printf(m, "commands:\tdvi_enable, dvi_disable\n");
4641 seq_printf(m, "commands:\tauto_enable, auto_disable\n");
4642 seq_printf(m, "commands:\tvideo_switch, expand_toggle\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004643
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004644 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004645}
4646
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004647static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648{
4649 char *cmd;
4650 int enable, disable, status;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004651 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004652
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004653 if (video_supported == TPACPI_VIDEO_NONE)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004654 return -ENODEV;
4655
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004656 enable = 0;
4657 disable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658
4659 while ((cmd = next_cmd(&buf))) {
4660 if (strlencmp(cmd, "lcd_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004661 enable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662 } else if (strlencmp(cmd, "lcd_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004663 disable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664 } else if (strlencmp(cmd, "crt_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004665 enable |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666 } else if (strlencmp(cmd, "crt_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004667 disable |= TP_ACPI_VIDEO_S_CRT;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004668 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004669 strlencmp(cmd, "dvi_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004670 enable |= TP_ACPI_VIDEO_S_DVI;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004671 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004672 strlencmp(cmd, "dvi_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004673 disable |= TP_ACPI_VIDEO_S_DVI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674 } else if (strlencmp(cmd, "auto_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004675 res = video_autosw_set(1);
4676 if (res)
4677 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678 } else if (strlencmp(cmd, "auto_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004679 res = video_autosw_set(0);
4680 if (res)
4681 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682 } else if (strlencmp(cmd, "video_switch") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004683 res = video_outputsw_cycle();
4684 if (res)
4685 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004686 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004687 res = video_expand_toggle();
4688 if (res)
4689 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690 } else
4691 return -EINVAL;
4692 }
4693
4694 if (enable || disable) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004695 status = video_outputsw_get();
4696 if (status < 0)
4697 return status;
4698 res = video_outputsw_set((status & ~disable) | enable);
4699 if (res)
4700 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004701 }
4702
4703 return 0;
4704}
4705
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004706static struct ibm_struct video_driver_data = {
4707 .name = "video",
4708 .read = video_read,
4709 .write = video_write,
4710 .exit = video_exit,
4711};
4712
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02004713#endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4714
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004715/*************************************************************************
4716 * Light (thinklight) subdriver
4717 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004718
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004719TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4720TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004721
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004722static int light_get_status(void)
4723{
4724 int status = 0;
4725
4726 if (tp_features.light_status) {
4727 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4728 return -EIO;
4729 return (!!status);
4730 }
4731
4732 return -ENXIO;
4733}
4734
4735static int light_set_status(int status)
4736{
4737 int rc;
4738
4739 if (tp_features.light) {
4740 if (cmos_handle) {
4741 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4742 (status)?
4743 TP_CMOS_THINKLIGHT_ON :
4744 TP_CMOS_THINKLIGHT_OFF);
4745 } else {
4746 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4747 (status)? 1 : 0);
4748 }
4749 return (rc)? 0 : -EIO;
4750 }
4751
4752 return -ENXIO;
4753}
4754
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004755static void light_set_status_worker(struct work_struct *work)
4756{
4757 struct tpacpi_led_classdev *data =
4758 container_of(work, struct tpacpi_led_classdev, work);
4759
4760 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00004761 light_set_status((data->new_state != TPACPI_LED_OFF));
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004762}
4763
4764static void light_sysfs_set(struct led_classdev *led_cdev,
4765 enum led_brightness brightness)
4766{
4767 struct tpacpi_led_classdev *data =
4768 container_of(led_cdev,
4769 struct tpacpi_led_classdev,
4770 led_classdev);
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00004771 data->new_state = (brightness != LED_OFF) ?
4772 TPACPI_LED_ON : TPACPI_LED_OFF;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004773 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004774}
4775
4776static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4777{
4778 return (light_get_status() == 1)? LED_FULL : LED_OFF;
4779}
4780
4781static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4782 .led_classdev = {
4783 .name = "tpacpi::thinklight",
4784 .brightness_set = &light_sysfs_set,
4785 .brightness_get = &light_sysfs_get,
4786 }
4787};
4788
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004789static int __init light_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004790{
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004791 int rc;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004792
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004793 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4794
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004795 TPACPI_ACPIHANDLE_INIT(ledb);
4796 TPACPI_ACPIHANDLE_INIT(lght);
4797 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004798 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004799
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004800 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004801 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004802
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004803 if (tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004804 /* light status not supported on
4805 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004806 tp_features.light_status =
4807 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004808
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004809 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4810 str_supported(tp_features.light),
4811 str_supported(tp_features.light_status));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004812
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004813 if (!tp_features.light)
4814 return 1;
4815
4816 rc = led_classdev_register(&tpacpi_pdev->dev,
4817 &tpacpi_led_thinklight.led_classdev);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004818
4819 if (rc < 0) {
4820 tp_features.light = 0;
4821 tp_features.light_status = 0;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004822 } else {
4823 rc = 0;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004824 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004825
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004826 return rc;
4827}
4828
4829static void light_exit(void)
4830{
4831 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
4832 if (work_pending(&tpacpi_led_thinklight.work))
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004833 flush_workqueue(tpacpi_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004834}
4835
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004836static int light_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004837{
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004838 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004839
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004840 if (!tp_features.light) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004841 seq_printf(m, "status:\t\tnot supported\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004842 } else if (!tp_features.light_status) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004843 seq_printf(m, "status:\t\tunknown\n");
4844 seq_printf(m, "commands:\ton, off\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004845 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004846 status = light_get_status();
4847 if (status < 0)
4848 return status;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004849 seq_printf(m, "status:\t\t%s\n", onoff(status, 0));
4850 seq_printf(m, "commands:\ton, off\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004851 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004853 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854}
4855
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004856static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004857{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004858 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004859 int newstatus = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004860
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004861 if (!tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004862 return -ENODEV;
4863
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864 while ((cmd = next_cmd(&buf))) {
4865 if (strlencmp(cmd, "on") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004866 newstatus = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867 } else if (strlencmp(cmd, "off") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004868 newstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004869 } else
4870 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871 }
4872
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004873 return light_set_status(newstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874}
4875
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004876static struct ibm_struct light_driver_data = {
4877 .name = "light",
4878 .read = light_read,
4879 .write = light_write,
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004880 .exit = light_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004881};
4882
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004883/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004884 * CMOS subdriver
4885 */
4886
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004887/* sysfs cmos_command -------------------------------------------------- */
4888static ssize_t cmos_command_store(struct device *dev,
4889 struct device_attribute *attr,
4890 const char *buf, size_t count)
4891{
4892 unsigned long cmos_cmd;
4893 int res;
4894
4895 if (parse_strtoul(buf, 21, &cmos_cmd))
4896 return -EINVAL;
4897
4898 res = issue_thinkpad_cmos_command(cmos_cmd);
4899 return (res)? res : count;
4900}
4901
4902static struct device_attribute dev_attr_cmos_command =
4903 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4904
4905/* --------------------------------------------------------------------- */
4906
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004907static int __init cmos_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004908{
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004909 int res;
4910
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004911 vdbg_printk(TPACPI_DBG_INIT,
4912 "initializing cmos commands subdriver\n");
4913
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004914 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004915
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004916 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4917 str_supported(cmos_handle != NULL));
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004918
4919 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4920 if (res)
4921 return res;
4922
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004923 return (cmos_handle)? 0 : 1;
4924}
4925
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004926static void cmos_exit(void)
4927{
4928 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4929}
4930
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004931static int cmos_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004933 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4934 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004935 if (!cmos_handle)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004936 seq_printf(m, "status:\t\tnot supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937 else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004938 seq_printf(m, "status:\t\tsupported\n");
4939 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004940 }
4941
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004942 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004943}
4944
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004945static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946{
4947 char *cmd;
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004948 int cmos_cmd, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949
4950 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004951 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
4952 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004953 /* cmos_cmd set */
4954 } else
4955 return -EINVAL;
4956
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004957 res = issue_thinkpad_cmos_command(cmos_cmd);
4958 if (res)
4959 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004960 }
4961
4962 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004963}
4964
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004965static struct ibm_struct cmos_driver_data = {
4966 .name = "cmos",
4967 .read = cmos_read,
4968 .write = cmos_write,
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004969 .exit = cmos_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004970};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004971
4972/*************************************************************************
4973 * LED subdriver
4974 */
4975
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004976enum led_access_mode {
4977 TPACPI_LED_NONE = 0,
4978 TPACPI_LED_570, /* 570 */
4979 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4980 TPACPI_LED_NEW, /* all others */
4981};
4982
4983enum { /* For TPACPI_LED_OLD */
4984 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
4985 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
4986 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
4987};
4988
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02004989static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004990
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004991TPACPI_HANDLE(led, ec, "SLED", /* 570 */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004992 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
4993 /* T20-22, X20-21 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004994 "LED", /* all others */
4995 ); /* R30, R31 */
4996
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03004997#define TPACPI_LED_NUMLEDS 16
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004998static struct tpacpi_led_classdev *tpacpi_leds;
4999static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
Harvey Harrisone3aa51f2008-05-29 17:51:57 -07005000static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005001 /* there's a limit of 19 chars + NULL before 2.6.26 */
5002 "tpacpi::power",
5003 "tpacpi:orange:batt",
5004 "tpacpi:green:batt",
5005 "tpacpi::dock_active",
5006 "tpacpi::bay_active",
5007 "tpacpi::dock_batt",
5008 "tpacpi::unknown_led",
5009 "tpacpi::standby",
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005010 "tpacpi::dock_status1",
5011 "tpacpi::dock_status2",
5012 "tpacpi::unknown_led2",
5013 "tpacpi::unknown_led3",
5014 "tpacpi::thinkvantage",
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005015};
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005016#define TPACPI_SAFE_LEDS 0x1081U
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005017
5018static inline bool tpacpi_is_led_restricted(const unsigned int led)
5019{
5020#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5021 return false;
5022#else
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005023 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005024#endif
5025}
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005026
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005027static int led_get_status(const unsigned int led)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005028{
5029 int status;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005030 enum led_status_t led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005031
5032 switch (led_supported) {
5033 case TPACPI_LED_570:
5034 if (!acpi_evalf(ec_handle,
5035 &status, "GLED", "dd", 1 << led))
5036 return -EIO;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005037 led_s = (status == 0)?
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005038 TPACPI_LED_OFF :
5039 ((status == 1)?
5040 TPACPI_LED_ON :
5041 TPACPI_LED_BLINK);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005042 tpacpi_led_state_cache[led] = led_s;
5043 return led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005044 default:
5045 return -ENXIO;
5046 }
5047
5048 /* not reached */
5049}
5050
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005051static int led_set_status(const unsigned int led,
5052 const enum led_status_t ledstatus)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005053{
5054 /* off, on, blink. Index is led_status_t */
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005055 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
5056 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005057
5058 int rc = 0;
5059
5060 switch (led_supported) {
5061 case TPACPI_LED_570:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005062 /* 570 */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005063 if (unlikely(led > 7))
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005064 return -EINVAL;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005065 if (unlikely(tpacpi_is_led_restricted(led)))
5066 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005067 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5068 (1 << led), led_sled_arg1[ledstatus]))
5069 rc = -EIO;
5070 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005071 case TPACPI_LED_OLD:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005072 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005073 if (unlikely(led > 7))
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005074 return -EINVAL;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005075 if (unlikely(tpacpi_is_led_restricted(led)))
5076 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005077 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
5078 if (rc >= 0)
5079 rc = ec_write(TPACPI_LED_EC_HLBL,
5080 (ledstatus == TPACPI_LED_BLINK) << led);
5081 if (rc >= 0)
5082 rc = ec_write(TPACPI_LED_EC_HLCL,
5083 (ledstatus != TPACPI_LED_OFF) << led);
5084 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005085 case TPACPI_LED_NEW:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005086 /* all others */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005087 if (unlikely(led >= TPACPI_LED_NUMLEDS))
5088 return -EINVAL;
5089 if (unlikely(tpacpi_is_led_restricted(led)))
5090 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005091 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5092 led, led_led_arg1[ledstatus]))
5093 rc = -EIO;
5094 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005095 default:
5096 rc = -ENXIO;
5097 }
5098
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005099 if (!rc)
5100 tpacpi_led_state_cache[led] = ledstatus;
5101
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005102 return rc;
5103}
5104
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005105static void led_set_status_worker(struct work_struct *work)
5106{
5107 struct tpacpi_led_classdev *data =
5108 container_of(work, struct tpacpi_led_classdev, work);
5109
5110 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005111 led_set_status(data->led, data->new_state);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005112}
5113
5114static void led_sysfs_set(struct led_classdev *led_cdev,
5115 enum led_brightness brightness)
5116{
5117 struct tpacpi_led_classdev *data = container_of(led_cdev,
5118 struct tpacpi_led_classdev, led_classdev);
5119
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005120 if (brightness == LED_OFF)
5121 data->new_state = TPACPI_LED_OFF;
5122 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
5123 data->new_state = TPACPI_LED_ON;
5124 else
5125 data->new_state = TPACPI_LED_BLINK;
5126
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03005127 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005128}
5129
5130static int led_sysfs_blink_set(struct led_classdev *led_cdev,
5131 unsigned long *delay_on, unsigned long *delay_off)
5132{
5133 struct tpacpi_led_classdev *data = container_of(led_cdev,
5134 struct tpacpi_led_classdev, led_classdev);
5135
5136 /* Can we choose the flash rate? */
5137 if (*delay_on == 0 && *delay_off == 0) {
5138 /* yes. set them to the hardware blink rate (1 Hz) */
5139 *delay_on = 500; /* ms */
5140 *delay_off = 500; /* ms */
5141 } else if ((*delay_on != 500) || (*delay_off != 500))
5142 return -EINVAL;
5143
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005144 data->new_state = TPACPI_LED_BLINK;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03005145 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005146
5147 return 0;
5148}
5149
5150static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
5151{
5152 int rc;
5153
5154 struct tpacpi_led_classdev *data = container_of(led_cdev,
5155 struct tpacpi_led_classdev, led_classdev);
5156
5157 rc = led_get_status(data->led);
5158
5159 if (rc == TPACPI_LED_OFF || rc < 0)
5160 rc = LED_OFF; /* no error handling in led class :( */
5161 else
5162 rc = LED_FULL;
5163
5164 return rc;
5165}
5166
5167static void led_exit(void)
5168{
5169 unsigned int i;
5170
5171 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5172 if (tpacpi_leds[i].led_classdev.name)
5173 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
5174 }
5175
5176 kfree(tpacpi_leds);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005177}
5178
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005179static int __init tpacpi_init_led(unsigned int led)
5180{
5181 int rc;
5182
5183 tpacpi_leds[led].led = led;
5184
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005185 /* LEDs with no name don't get registered */
5186 if (!tpacpi_led_names[led])
5187 return 0;
5188
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005189 tpacpi_leds[led].led_classdev.brightness_set = &led_sysfs_set;
5190 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5191 if (led_supported == TPACPI_LED_570)
5192 tpacpi_leds[led].led_classdev.brightness_get =
5193 &led_sysfs_get;
5194
5195 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5196
5197 INIT_WORK(&tpacpi_leds[led].work, led_set_status_worker);
5198
5199 rc = led_classdev_register(&tpacpi_pdev->dev,
5200 &tpacpi_leds[led].led_classdev);
5201 if (rc < 0)
5202 tpacpi_leds[led].led_classdev.name = NULL;
5203
5204 return rc;
5205}
5206
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005207static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
5208 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5209 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5210 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5211
5212 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5213 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5214 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5215 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5216 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5217 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5218 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5219 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5220
5221 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5222 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5223 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5224 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5225 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5226
5227 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5228 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5229 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5230 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5231
5232 /* (1) - may have excess leds enabled on MSB */
5233
5234 /* Defaults (order matters, keep last, don't reorder!) */
5235 { /* Lenovo */
5236 .vendor = PCI_VENDOR_ID_LENOVO,
5237 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5238 .quirks = 0x1fffU,
5239 },
5240 { /* IBM ThinkPads with no EC version string */
5241 .vendor = PCI_VENDOR_ID_IBM,
5242 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
5243 .quirks = 0x00ffU,
5244 },
5245 { /* IBM ThinkPads with EC version string */
5246 .vendor = PCI_VENDOR_ID_IBM,
5247 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5248 .quirks = 0x00bfU,
5249 },
5250};
5251
5252#undef TPACPI_LEDQ_IBM
5253#undef TPACPI_LEDQ_LNV
5254
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005255static int __init led_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005256{
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005257 unsigned int i;
5258 int rc;
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005259 unsigned long useful_leds;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005260
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005261 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
5262
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005263 TPACPI_ACPIHANDLE_INIT(led);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005264
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005265 if (!led_handle)
5266 /* led not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005267 led_supported = TPACPI_LED_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005268 else if (strlencmp(led_path, "SLED") == 0)
5269 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005270 led_supported = TPACPI_LED_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005271 else if (strlencmp(led_path, "SYSL") == 0)
5272 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005273 led_supported = TPACPI_LED_OLD;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005274 else
5275 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005276 led_supported = TPACPI_LED_NEW;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005277
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005278 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
5279 str_supported(led_supported), led_supported);
5280
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005281 if (led_supported == TPACPI_LED_NONE)
5282 return 1;
5283
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005284 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
5285 GFP_KERNEL);
5286 if (!tpacpi_leds) {
5287 printk(TPACPI_ERR "Out of memory for LED data\n");
5288 return -ENOMEM;
5289 }
5290
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005291 useful_leds = tpacpi_check_quirks(led_useful_qtable,
5292 ARRAY_SIZE(led_useful_qtable));
5293
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005294 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005295 if (!tpacpi_is_led_restricted(i) &&
5296 test_bit(i, &useful_leds)) {
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005297 rc = tpacpi_init_led(i);
5298 if (rc < 0) {
5299 led_exit();
5300 return rc;
5301 }
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005302 }
5303 }
5304
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005305#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005306 printk(TPACPI_NOTICE
5307 "warning: userspace override of important "
5308 "firmware LEDs is enabled\n");
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005309#endif
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005310 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005311}
5312
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005313#define str_led_status(s) \
5314 ((s) == TPACPI_LED_OFF ? "off" : \
5315 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005316
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005317static int led_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005318{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005319 if (!led_supported) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005320 seq_printf(m, "status:\t\tnot supported\n");
5321 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005322 }
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005323 seq_printf(m, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005324
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005325 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005326 /* 570 */
5327 int i, status;
5328 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005329 status = led_get_status(i);
5330 if (status < 0)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005331 return -EIO;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005332 seq_printf(m, "%d:\t\t%s\n",
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005333 i, str_led_status(status));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005334 }
5335 }
5336
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005337 seq_printf(m, "commands:\t"
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005338 "<led> on, <led> off, <led> blink (<led> is 0-15)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005339
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005340 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005341}
5342
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005343static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005344{
5345 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005346 int led, rc;
5347 enum led_status_t s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005348
5349 if (!led_supported)
5350 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005351
5352 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005353 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 15)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005354 return -EINVAL;
5355
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005356 if (strstr(cmd, "off")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005357 s = TPACPI_LED_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005358 } else if (strstr(cmd, "on")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005359 s = TPACPI_LED_ON;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005360 } else if (strstr(cmd, "blink")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005361 s = TPACPI_LED_BLINK;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005362 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005363 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005364 }
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005365
5366 rc = led_set_status(led, s);
5367 if (rc < 0)
5368 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005369 }
5370
5371 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005372}
5373
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005374static struct ibm_struct led_driver_data = {
5375 .name = "led",
5376 .read = led_read,
5377 .write = led_write,
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005378 .exit = led_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005379};
5380
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005381/*************************************************************************
5382 * Beep subdriver
5383 */
5384
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005385TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005386
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005387#define TPACPI_BEEP_Q1 0x0001
5388
5389static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
5390 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
5391 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
5392};
5393
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005394static int __init beep_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005395{
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005396 unsigned long quirks;
5397
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005398 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
5399
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005400 TPACPI_ACPIHANDLE_INIT(beep);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005401
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005402 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
5403 str_supported(beep_handle != NULL));
5404
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005405 quirks = tpacpi_check_quirks(beep_quirk_table,
5406 ARRAY_SIZE(beep_quirk_table));
5407
5408 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
5409
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005410 return (beep_handle)? 0 : 1;
5411}
5412
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005413static int beep_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005414{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005415 if (!beep_handle)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005416 seq_printf(m, "status:\t\tnot supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005417 else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005418 seq_printf(m, "status:\t\tsupported\n");
5419 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-17)\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005420 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005421
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005422 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005423}
5424
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005425static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005426{
5427 char *cmd;
5428 int beep_cmd;
5429
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005430 if (!beep_handle)
5431 return -ENODEV;
5432
Linus Torvalds1da177e2005-04-16 15:20:36 -07005433 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005434 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
5435 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436 /* beep_cmd set */
5437 } else
5438 return -EINVAL;
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005439 if (tp_features.beep_needs_two_args) {
5440 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
5441 beep_cmd, 0))
5442 return -EIO;
5443 } else {
5444 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
5445 beep_cmd))
5446 return -EIO;
5447 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005448 }
5449
5450 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005451}
5452
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005453static struct ibm_struct beep_driver_data = {
5454 .name = "beep",
5455 .read = beep_read,
5456 .write = beep_write,
5457};
5458
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005459/*************************************************************************
5460 * Thermal subdriver
5461 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005462
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005463enum thermal_access_mode {
5464 TPACPI_THERMAL_NONE = 0, /* No thermal support */
5465 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
5466 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
5467 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
5468 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
5469};
5470
5471enum { /* TPACPI_THERMAL_TPEC_* */
5472 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
5473 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
5474 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005475
5476 TPACPI_THERMAL_SENSOR_NA = -128000, /* Sensor not available */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005477};
5478
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005479
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005480#define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5481struct ibm_thermal_sensors_struct {
5482 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
5483};
5484
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005485static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005486
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005487/* idx is zero-based */
5488static int thermal_get_sensor(int idx, s32 *value)
5489{
5490 int t;
5491 s8 tmp;
5492 char tmpi[5];
5493
5494 t = TP_EC_THERMAL_TMP0;
5495
5496 switch (thermal_read_mode) {
5497#if TPACPI_MAX_THERMAL_SENSORS >= 16
5498 case TPACPI_THERMAL_TPEC_16:
5499 if (idx >= 8 && idx <= 15) {
5500 t = TP_EC_THERMAL_TMP8;
5501 idx -= 8;
5502 }
5503 /* fallthrough */
5504#endif
5505 case TPACPI_THERMAL_TPEC_8:
5506 if (idx <= 7) {
5507 if (!acpi_ec_read(t + idx, &tmp))
5508 return -EIO;
5509 *value = tmp * 1000;
5510 return 0;
5511 }
5512 break;
5513
5514 case TPACPI_THERMAL_ACPI_UPDT:
5515 if (idx <= 7) {
5516 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5517 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
5518 return -EIO;
5519 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5520 return -EIO;
5521 *value = (t - 2732) * 100;
5522 return 0;
5523 }
5524 break;
5525
5526 case TPACPI_THERMAL_ACPI_TMP07:
5527 if (idx <= 7) {
5528 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5529 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5530 return -EIO;
5531 if (t > 127 || t < -127)
5532 t = TP_EC_THERMAL_TMP_NA;
5533 *value = t * 1000;
5534 return 0;
5535 }
5536 break;
5537
5538 case TPACPI_THERMAL_NONE:
5539 default:
5540 return -ENOSYS;
5541 }
5542
5543 return -EINVAL;
5544}
5545
5546static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5547{
5548 int res, i;
5549 int n;
5550
5551 n = 8;
5552 i = 0;
5553
5554 if (!s)
5555 return -EINVAL;
5556
5557 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5558 n = 16;
5559
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005560 for (i = 0 ; i < n; i++) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005561 res = thermal_get_sensor(i, &s->temp[i]);
5562 if (res)
5563 return res;
5564 }
5565
5566 return n;
5567}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005568
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005569static void thermal_dump_all_sensors(void)
5570{
5571 int n, i;
5572 struct ibm_thermal_sensors_struct t;
5573
5574 n = thermal_get_sensors(&t);
5575 if (n <= 0)
5576 return;
5577
5578 printk(TPACPI_NOTICE
5579 "temperatures (Celsius):");
5580
5581 for (i = 0; i < n; i++) {
5582 if (t.temp[i] != TPACPI_THERMAL_SENSOR_NA)
5583 printk(KERN_CONT " %d", (int)(t.temp[i] / 1000));
5584 else
5585 printk(KERN_CONT " N/A");
5586 }
5587
5588 printk(KERN_CONT "\n");
5589}
5590
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005591/* sysfs temp##_input -------------------------------------------------- */
5592
5593static ssize_t thermal_temp_input_show(struct device *dev,
5594 struct device_attribute *attr,
5595 char *buf)
5596{
5597 struct sensor_device_attribute *sensor_attr =
5598 to_sensor_dev_attr(attr);
5599 int idx = sensor_attr->index;
5600 s32 value;
5601 int res;
5602
5603 res = thermal_get_sensor(idx, &value);
5604 if (res)
5605 return res;
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005606 if (value == TPACPI_THERMAL_SENSOR_NA)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005607 return -ENXIO;
5608
5609 return snprintf(buf, PAGE_SIZE, "%d\n", value);
5610}
5611
5612#define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005613 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5614 thermal_temp_input_show, NULL, _idxB)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005615
5616static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5617 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5618 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5619 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5620 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5621 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5622 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5623 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5624 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5625 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5626 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5627 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5628 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5629 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5630 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5631 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5632 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5633};
5634
5635#define THERMAL_ATTRS(X) \
5636 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5637
5638static struct attribute *thermal_temp_input_attr[] = {
5639 THERMAL_ATTRS(8),
5640 THERMAL_ATTRS(9),
5641 THERMAL_ATTRS(10),
5642 THERMAL_ATTRS(11),
5643 THERMAL_ATTRS(12),
5644 THERMAL_ATTRS(13),
5645 THERMAL_ATTRS(14),
5646 THERMAL_ATTRS(15),
5647 THERMAL_ATTRS(0),
5648 THERMAL_ATTRS(1),
5649 THERMAL_ATTRS(2),
5650 THERMAL_ATTRS(3),
5651 THERMAL_ATTRS(4),
5652 THERMAL_ATTRS(5),
5653 THERMAL_ATTRS(6),
5654 THERMAL_ATTRS(7),
5655 NULL
5656};
5657
5658static const struct attribute_group thermal_temp_input16_group = {
5659 .attrs = thermal_temp_input_attr
5660};
5661
5662static const struct attribute_group thermal_temp_input8_group = {
5663 .attrs = &thermal_temp_input_attr[8]
5664};
5665
5666#undef THERMAL_SENSOR_ATTR_TEMP
5667#undef THERMAL_ATTRS
5668
5669/* --------------------------------------------------------------------- */
5670
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005671static int __init thermal_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005672{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005673 u8 t, ta1, ta2;
5674 int i;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005675 int acpi_tmp7;
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005676 int res;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005677
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005678 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5679
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005680 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005681
Henrique de Moraes Holschuh3d6f99c2007-07-18 23:45:46 -03005682 if (thinkpad_id.ec_model) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005683 /*
5684 * Direct EC access mode: sensors at registers
5685 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5686 * non-implemented, thermal sensors return 0x80 when
5687 * not available
5688 */
5689
5690 ta1 = ta2 = 0;
5691 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03005692 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005693 ta1 |= t;
5694 } else {
5695 ta1 = 0;
5696 break;
5697 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03005698 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005699 ta2 |= t;
5700 } else {
5701 ta1 = 0;
5702 break;
5703 }
5704 }
5705 if (ta1 == 0) {
5706 /* This is sheer paranoia, but we handle it anyway */
5707 if (acpi_tmp7) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005708 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005709 "ThinkPad ACPI EC access misbehaving, "
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005710 "falling back to ACPI TMPx access "
5711 "mode\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005712 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005713 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005714 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005715 "ThinkPad ACPI EC access misbehaving, "
5716 "disabling thermal sensors access\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005717 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005718 }
5719 } else {
5720 thermal_read_mode =
5721 (ta2 != 0) ?
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005722 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005723 }
5724 } else if (acpi_tmp7) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005725 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
5726 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005727 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005728 } else {
5729 /* Standard ACPI TMPx access, max 8 sensors */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005730 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005731 }
5732 } else {
5733 /* temperatures not supported on 570, G4x, R30, R31, R32 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005734 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005735 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005736
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005737 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5738 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5739 thermal_read_mode);
5740
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005741 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005742 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005743 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005744 &thermal_temp_input16_group);
5745 if (res)
5746 return res;
5747 break;
5748 case TPACPI_THERMAL_TPEC_8:
5749 case TPACPI_THERMAL_ACPI_TMP07:
5750 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005751 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005752 &thermal_temp_input8_group);
5753 if (res)
5754 return res;
5755 break;
5756 case TPACPI_THERMAL_NONE:
5757 default:
5758 return 1;
5759 }
5760
5761 return 0;
5762}
5763
5764static void thermal_exit(void)
5765{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005766 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005767 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005768 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005769 &thermal_temp_input16_group);
5770 break;
5771 case TPACPI_THERMAL_TPEC_8:
5772 case TPACPI_THERMAL_ACPI_TMP07:
5773 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005774 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Roel Kluinf04d5e02010-02-02 14:37:58 -08005775 &thermal_temp_input8_group);
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005776 break;
5777 case TPACPI_THERMAL_NONE:
5778 default:
5779 break;
5780 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005781}
5782
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005783static int thermal_read(struct seq_file *m)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005784{
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005785 int n, i;
5786 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005787
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005788 n = thermal_get_sensors(&t);
5789 if (unlikely(n < 0))
5790 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005791
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005792 seq_printf(m, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005793
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005794 if (n > 0) {
5795 for (i = 0; i < (n - 1); i++)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005796 seq_printf(m, "%d ", t.temp[i] / 1000);
5797 seq_printf(m, "%d\n", t.temp[i] / 1000);
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005798 } else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005799 seq_printf(m, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005800
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005801 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005802}
5803
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005804static struct ibm_struct thermal_driver_data = {
5805 .name = "thermal",
5806 .read = thermal_read,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005807 .exit = thermal_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005808};
5809
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005810/*************************************************************************
5811 * EC Dump subdriver
5812 */
5813
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005814static u8 ecdump_regs[256];
5815
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005816static int ecdump_read(struct seq_file *m)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005817{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005818 int i, j;
5819 u8 v;
5820
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005821 seq_printf(m, "EC "
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005822 " +00 +01 +02 +03 +04 +05 +06 +07"
5823 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
5824 for (i = 0; i < 256; i += 16) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005825 seq_printf(m, "EC 0x%02x:", i);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005826 for (j = 0; j < 16; j++) {
5827 if (!acpi_ec_read(i + j, &v))
5828 break;
5829 if (v != ecdump_regs[i + j])
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005830 seq_printf(m, " *%02x", v);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005831 else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005832 seq_printf(m, " %02x", v);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005833 ecdump_regs[i + j] = v;
5834 }
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005835 seq_putc(m, '\n');
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005836 if (j != 16)
5837 break;
5838 }
5839
5840 /* These are way too dangerous to advertise openly... */
5841#if 0
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005842 seq_printf(m, "commands:\t0x<offset> 0x<value>"
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005843 " (<offset> is 00-ff, <value> is 00-ff)\n");
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005844 seq_printf(m, "commands:\t0x<offset> <value> "
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005845 " (<offset> is 00-ff, <value> is 0-255)\n");
5846#endif
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005847 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005848}
5849
5850static int ecdump_write(char *buf)
5851{
5852 char *cmd;
5853 int i, v;
5854
5855 while ((cmd = next_cmd(&buf))) {
5856 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
5857 /* i and v set */
5858 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
5859 /* i and v set */
5860 } else
5861 return -EINVAL;
5862 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
5863 if (!acpi_ec_write(i, v))
5864 return -EIO;
5865 } else
5866 return -EINVAL;
5867 }
5868
5869 return 0;
5870}
5871
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005872static struct ibm_struct ecdump_driver_data = {
5873 .name = "ecdump",
5874 .read = ecdump_read,
5875 .write = ecdump_write,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03005876 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005877};
5878
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005879/*************************************************************************
5880 * Backlight/brightness subdriver
5881 */
Holger Macht8acb0252006-10-20 14:30:28 -07005882
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005883#define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5884
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005885/*
5886 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
5887 * CMOS NVRAM byte 0x5E, bits 0-3.
5888 *
5889 * EC HBRV (0x31) has the following layout
5890 * Bit 7: unknown function
5891 * Bit 6: unknown function
5892 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5893 * Bit 4: must be set to zero to avoid problems
5894 * Bit 3-0: backlight brightness level
5895 *
5896 * brightness_get_raw returns status data in the HBRV layout
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03005897 *
5898 * WARNING: The X61 has been verified to use HBRV for something else, so
5899 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
5900 * testing on the very early *60 Lenovo models...
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005901 */
5902
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005903enum {
5904 TP_EC_BACKLIGHT = 0x31,
5905
5906 /* TP_EC_BACKLIGHT bitmasks */
5907 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5908 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5909 TP_EC_BACKLIGHT_MAPSW = 0x20,
5910};
5911
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005912enum tpacpi_brightness_access_mode {
5913 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
5914 TPACPI_BRGHT_MODE_EC, /* EC control */
5915 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
5916 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
5917 TPACPI_BRGHT_MODE_MAX
5918};
5919
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -03005920static struct backlight_device *ibm_backlight_device;
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005921
5922static enum tpacpi_brightness_access_mode brightness_mode =
5923 TPACPI_BRGHT_MODE_MAX;
5924
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005925static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5926
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005927static struct mutex brightness_mutex;
5928
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005929/* NVRAM brightness access,
5930 * call with brightness_mutex held! */
5931static unsigned int tpacpi_brightness_nvram_get(void)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005932{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005933 u8 lnvram;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005934
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005935 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5936 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5937 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
5938 lnvram &= (tp_features.bright_16levels) ? 0x0f : 0x07;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005939
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005940 return lnvram;
5941}
5942
5943static void tpacpi_brightness_checkpoint_nvram(void)
5944{
5945 u8 lec = 0;
5946 u8 b_nvram;
5947
5948 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
5949 return;
5950
5951 vdbg_printk(TPACPI_DBG_BRGHT,
5952 "trying to checkpoint backlight level to NVRAM...\n");
5953
5954 if (mutex_lock_killable(&brightness_mutex) < 0)
5955 return;
5956
5957 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5958 goto unlock;
5959 lec &= TP_EC_BACKLIGHT_LVLMSK;
5960 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
5961
5962 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5963 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
5964 /* NVRAM needs update */
5965 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
5966 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
5967 b_nvram |= lec;
5968 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
5969 dbg_printk(TPACPI_DBG_BRGHT,
5970 "updated NVRAM backlight level to %u (0x%02x)\n",
5971 (unsigned int) lec, (unsigned int) b_nvram);
5972 } else
5973 vdbg_printk(TPACPI_DBG_BRGHT,
5974 "NVRAM backlight level already is %u (0x%02x)\n",
5975 (unsigned int) lec, (unsigned int) b_nvram);
5976
5977unlock:
5978 mutex_unlock(&brightness_mutex);
5979}
5980
5981
5982/* call with brightness_mutex held! */
5983static int tpacpi_brightness_get_raw(int *status)
5984{
5985 u8 lec = 0;
5986
5987 switch (brightness_mode) {
5988 case TPACPI_BRGHT_MODE_UCMS_STEP:
5989 *status = tpacpi_brightness_nvram_get();
5990 return 0;
5991 case TPACPI_BRGHT_MODE_EC:
5992 case TPACPI_BRGHT_MODE_ECNVRAM:
5993 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
Henrique de Moraes Holschuh2d5e94d2008-04-26 01:02:20 -03005994 return -EIO;
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005995 *status = lec;
5996 return 0;
5997 default:
5998 return -ENXIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005999 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006000}
6001
6002/* call with brightness_mutex held! */
6003/* do NOT call with illegal backlight level value */
6004static int tpacpi_brightness_set_ec(unsigned int value)
6005{
6006 u8 lec = 0;
6007
6008 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6009 return -EIO;
6010
6011 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
6012 (lec & TP_EC_BACKLIGHT_CMDMSK) |
6013 (value & TP_EC_BACKLIGHT_LVLMSK))))
6014 return -EIO;
6015
6016 return 0;
6017}
6018
6019/* call with brightness_mutex held! */
6020static int tpacpi_brightness_set_ucmsstep(unsigned int value)
6021{
6022 int cmos_cmd, inc;
6023 unsigned int current_value, i;
6024
6025 current_value = tpacpi_brightness_nvram_get();
6026
6027 if (value == current_value)
6028 return 0;
6029
6030 cmos_cmd = (value > current_value) ?
6031 TP_CMOS_BRIGHTNESS_UP :
6032 TP_CMOS_BRIGHTNESS_DOWN;
6033 inc = (value > current_value) ? 1 : -1;
6034
6035 for (i = current_value; i != value; i += inc)
6036 if (issue_thinkpad_cmos_command(cmos_cmd))
6037 return -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006038
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006039 return 0;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006040}
6041
6042/* May return EINTR which can always be mapped to ERESTARTSYS */
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006043static int brightness_set(unsigned int value)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006044{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006045 int res;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006046
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006047 if (value > ((tp_features.bright_16levels)? 15 : 7) ||
6048 value < 0)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006049 return -EINVAL;
6050
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006051 vdbg_printk(TPACPI_DBG_BRGHT,
6052 "set backlight level to %d\n", value);
6053
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006054 res = mutex_lock_killable(&brightness_mutex);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006055 if (res < 0)
6056 return res;
6057
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006058 switch (brightness_mode) {
6059 case TPACPI_BRGHT_MODE_EC:
6060 case TPACPI_BRGHT_MODE_ECNVRAM:
6061 res = tpacpi_brightness_set_ec(value);
6062 break;
6063 case TPACPI_BRGHT_MODE_UCMS_STEP:
6064 res = tpacpi_brightness_set_ucmsstep(value);
6065 break;
6066 default:
6067 res = -ENXIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006068 }
6069
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006070 mutex_unlock(&brightness_mutex);
6071 return res;
6072}
6073
6074/* sysfs backlight class ----------------------------------------------- */
6075
6076static int brightness_update_status(struct backlight_device *bd)
6077{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006078 unsigned int level =
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006079 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
6080 bd->props.power == FB_BLANK_UNBLANK) ?
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006081 bd->props.brightness : 0;
6082
6083 dbg_printk(TPACPI_DBG_BRGHT,
6084 "backlight: attempt to set level to %d\n",
6085 level);
6086
6087 /* it is the backlight class's job (caller) to handle
6088 * EINTR and other errors properly */
6089 return brightness_set(level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006090}
Holger Macht8acb0252006-10-20 14:30:28 -07006091
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006092static int brightness_get(struct backlight_device *bd)
6093{
6094 int status, res;
6095
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006096 res = mutex_lock_killable(&brightness_mutex);
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006097 if (res < 0)
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006098 return 0;
6099
6100 res = tpacpi_brightness_get_raw(&status);
6101
6102 mutex_unlock(&brightness_mutex);
6103
6104 if (res < 0)
6105 return 0;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006106
6107 return status & TP_EC_BACKLIGHT_LVLMSK;
6108}
6109
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00006110static void tpacpi_brightness_notify_change(void)
6111{
6112 backlight_force_update(ibm_backlight_device,
6113 BACKLIGHT_UPDATE_HOTKEY);
6114}
6115
Richard Purdie599a52d2007-02-10 23:07:48 +00006116static struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006117 .get_brightness = brightness_get,
6118 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006119};
6120
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006121/* --------------------------------------------------------------------- */
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03006122
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006123/*
6124 * These are only useful for models that have only one possibility
6125 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6126 * these quirks.
6127 */
6128#define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6129#define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6130#define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6131
6132static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
6133 /* Models with ATI GPUs known to require ECNVRAM mode */
6134 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
6135
Henrique de Moraes Holschuh6da25bf2009-09-12 15:22:11 -03006136 /* Models with ATI GPUs that can use ECNVRAM */
Henrique de Moraes Holschuh7d1894d2010-02-25 21:28:56 -03006137 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC), /* R50,51 T40-42 */
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006138 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
Henrique de Moraes Holschuh7d1894d2010-02-25 21:28:56 -03006139 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_EC), /* R52 */
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006140 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6141
Henrique de Moraes Holschuh6da25bf2009-09-12 15:22:11 -03006142 /* Models with Intel Extreme Graphics 2 */
Henrique de Moraes Holschuh7d1894d2010-02-25 21:28:56 -03006143 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC), /* X40 */
Henrique de Moraes Holschuha9f8eac2009-12-09 01:36:21 +00006144 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6145 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006146
6147 /* Models with Intel GMA900 */
6148 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
6149 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
6150 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
6151};
6152
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006153static int __init brightness_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006154{
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006155 int b;
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006156 unsigned long quirks;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006157
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006158 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
6159
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03006160 mutex_init(&brightness_mutex);
6161
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006162 quirks = tpacpi_check_quirks(brightness_quirk_table,
6163 ARRAY_SIZE(brightness_quirk_table));
6164
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03006165 /*
6166 * We always attempt to detect acpi support, so as to switch
6167 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6168 * going to publish a backlight interface
6169 */
6170 b = tpacpi_check_std_acpi_brightness_support();
6171 if (b > 0) {
Thomas Renninger2dba1b52008-08-01 17:38:03 +02006172
6173 if (acpi_video_backlight_support()) {
6174 if (brightness_enable > 1) {
6175 printk(TPACPI_NOTICE
6176 "Standard ACPI backlight interface "
6177 "available, not loading native one.\n");
6178 return 1;
6179 } else if (brightness_enable == 1) {
6180 printk(TPACPI_NOTICE
6181 "Backlight control force enabled, even if standard "
6182 "ACPI backlight interface is available\n");
6183 }
6184 } else {
6185 if (brightness_enable > 1) {
6186 printk(TPACPI_NOTICE
6187 "Standard ACPI backlight interface not "
6188 "available, thinkpad_acpi native "
6189 "brightness control enabled\n");
6190 }
Henrique de Moraes Holschuhe11e2112007-10-30 17:46:22 -02006191 }
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02006192 }
6193
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03006194 if (!brightness_enable) {
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006195 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03006196 "brightness support disabled by "
6197 "module parameter\n");
6198 return 1;
6199 }
6200
6201 if (b > 16) {
6202 printk(TPACPI_ERR
6203 "Unsupported brightness interface, "
6204 "please contact %s\n", TPACPI_MAIL);
6205 return 1;
6206 }
6207 if (b == 16)
6208 tp_features.bright_16levels = 1;
6209
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006210 /*
6211 * Check for module parameter bogosity, note that we
6212 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
6213 * able to detect "unspecified"
6214 */
6215 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03006216 return -EINVAL;
6217
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006218 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
6219 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
6220 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006221 if (quirks & TPACPI_BRGHT_Q_EC)
6222 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
6223 else
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006224 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
6225
6226 dbg_printk(TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006227 "driver auto-selected brightness_mode=%d\n",
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006228 brightness_mode);
6229 }
6230
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03006231 /* Safety */
6232 if (thinkpad_id.vendor != PCI_VENDOR_ID_IBM &&
6233 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
6234 brightness_mode == TPACPI_BRGHT_MODE_EC))
6235 return -EINVAL;
6236
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006237 if (tpacpi_brightness_get_raw(&b) < 0)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03006238 return 1;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006239
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02006240 if (tp_features.bright_16levels)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006241 printk(TPACPI_INFO
6242 "detected a 16-level brightness capable ThinkPad\n");
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02006243
Henrique de Moraes Holschuh7d5a0152007-04-24 11:48:20 -03006244 ibm_backlight_device = backlight_device_register(
6245 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
6246 &ibm_backlight_data);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006247 if (IS_ERR(ibm_backlight_device)) {
Henrique de Moraes Holschuh435c47e2009-09-20 14:09:22 -03006248 int rc = PTR_ERR(ibm_backlight_device);
6249 ibm_backlight_device = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006250 printk(TPACPI_ERR "Could not register backlight device\n");
Henrique de Moraes Holschuh435c47e2009-09-20 14:09:22 -03006251 return rc;
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006252 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006253 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6254 "brightness is supported\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006255
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006256 if (quirks & TPACPI_BRGHT_Q_ASK) {
6257 printk(TPACPI_NOTICE
6258 "brightness: will use unverified default: "
6259 "brightness_mode=%d\n", brightness_mode);
6260 printk(TPACPI_NOTICE
6261 "brightness: please report to %s whether it works well "
6262 "or not on your ThinkPad\n", TPACPI_MAIL);
6263 }
6264
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02006265 ibm_backlight_device->props.max_brightness =
6266 (tp_features.bright_16levels)? 15 : 7;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006267 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006268 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00006269
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00006270 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6271 "brightness: registering brightness hotkeys "
6272 "as change notification\n");
6273 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6274 | TP_ACPI_HKEY_BRGHTUP_MASK
6275 | TP_ACPI_HKEY_BRGHTDWN_MASK);;
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006276 return 0;
6277}
6278
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006279static void brightness_suspend(pm_message_t state)
6280{
6281 tpacpi_brightness_checkpoint_nvram();
6282}
6283
6284static void brightness_shutdown(void)
6285{
6286 tpacpi_brightness_checkpoint_nvram();
6287}
6288
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006289static void brightness_exit(void)
6290{
6291 if (ibm_backlight_device) {
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006292 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006293 "calling backlight_device_unregister()\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006294 backlight_device_unregister(ibm_backlight_device);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006295 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006296
6297 tpacpi_brightness_checkpoint_nvram();
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006298}
6299
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006300static int brightness_read(struct seq_file *m)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006301{
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006302 int level;
6303
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006304 level = brightness_get(NULL);
6305 if (level < 0) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006306 seq_printf(m, "level:\t\tunreadable\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006307 } else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006308 seq_printf(m, "level:\t\t%d\n", level);
6309 seq_printf(m, "commands:\tup, down\n");
6310 seq_printf(m, "commands:\tlevel <level>"
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02006311 " (<level> is 0-%d)\n",
6312 (tp_features.bright_16levels) ? 15 : 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006313 }
6314
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006315 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006316}
6317
6318static int brightness_write(char *buf)
6319{
6320 int level;
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006321 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006322 char *cmd;
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02006323 int max_level = (tp_features.bright_16levels) ? 15 : 7;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006324
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006325 level = brightness_get(NULL);
6326 if (level < 0)
6327 return level;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006328
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006329 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006330 if (strlencmp(cmd, "up") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006331 if (level < max_level)
6332 level++;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006333 } else if (strlencmp(cmd, "down") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006334 if (level > 0)
6335 level--;
6336 } else if (sscanf(cmd, "level %d", &level) == 1 &&
6337 level >= 0 && level <= max_level) {
6338 /* new level set */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006339 } else
6340 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006341 }
6342
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006343 tpacpi_disclose_usertask("procfs brightness",
6344 "set level to %d\n", level);
6345
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006346 /*
6347 * Now we know what the final level should be, so we try to set it.
6348 * Doing it this way makes the syscall restartable in case of EINTR
6349 */
6350 rc = brightness_set(level);
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00006351 if (!rc && ibm_backlight_device)
6352 backlight_force_update(ibm_backlight_device,
6353 BACKLIGHT_UPDATE_SYSFS);
Roel Kluin80a8d122009-11-20 19:48:23 +01006354 return (rc == -EINTR)? -ERESTARTSYS : rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006355}
6356
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006357static struct ibm_struct brightness_driver_data = {
6358 .name = "brightness",
6359 .read = brightness_read,
6360 .write = brightness_write,
6361 .exit = brightness_exit,
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006362 .suspend = brightness_suspend,
6363 .shutdown = brightness_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006364};
6365
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006366/*************************************************************************
6367 * Volume subdriver
6368 */
6369
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006370/*
6371 * IBM ThinkPads have a simple volume controller with MUTE gating.
6372 * Very early Lenovo ThinkPads follow the IBM ThinkPad spec.
6373 *
6374 * Since the *61 series (and probably also the later *60 series), Lenovo
6375 * ThinkPads only implement the MUTE gate.
6376 *
6377 * EC register 0x30
6378 * Bit 6: MUTE (1 mutes sound)
6379 * Bit 3-0: Volume
6380 * Other bits should be zero as far as we know.
6381 *
6382 * This is also stored in CMOS NVRAM, byte 0x60, bit 6 (MUTE), and
6383 * bits 3-0 (volume). Other bits in NVRAM may have other functions,
6384 * such as bit 7 which is used to detect repeated presses of MUTE,
6385 * and we leave them unchanged.
6386 */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006387
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02006388#ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
6389
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006390#define TPACPI_ALSA_DRVNAME "ThinkPad EC"
6391#define TPACPI_ALSA_SHRTNAME "ThinkPad Console Audio Control"
6392#define TPACPI_ALSA_MIXERNAME TPACPI_ALSA_SHRTNAME
6393
Henrique de Moraes Holschuhead510c2009-12-26 22:52:13 -02006394static int alsa_index = ~((1 << (SNDRV_CARDS - 3)) - 1); /* last three slots */
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006395static char *alsa_id = "ThinkPadEC";
6396static int alsa_enable = SNDRV_DEFAULT_ENABLE1;
6397
6398struct tpacpi_alsa_data {
6399 struct snd_card *card;
6400 struct snd_ctl_elem_id *ctl_mute_id;
6401 struct snd_ctl_elem_id *ctl_vol_id;
6402};
6403
6404static struct snd_card *alsa_card;
6405
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006406enum {
6407 TP_EC_AUDIO = 0x30,
6408
6409 /* TP_EC_AUDIO bits */
6410 TP_EC_AUDIO_MUTESW = 6,
6411
6412 /* TP_EC_AUDIO bitmasks */
6413 TP_EC_AUDIO_LVL_MSK = 0x0F,
6414 TP_EC_AUDIO_MUTESW_MSK = (1 << TP_EC_AUDIO_MUTESW),
6415
6416 /* Maximum volume */
6417 TP_EC_VOLUME_MAX = 14,
6418};
6419
6420enum tpacpi_volume_access_mode {
6421 TPACPI_VOL_MODE_AUTO = 0, /* Not implemented yet */
6422 TPACPI_VOL_MODE_EC, /* Pure EC control */
6423 TPACPI_VOL_MODE_UCMS_STEP, /* UCMS step-based control: N/A */
6424 TPACPI_VOL_MODE_ECNVRAM, /* EC control w/ NVRAM store */
6425 TPACPI_VOL_MODE_MAX
6426};
6427
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006428enum tpacpi_volume_capabilities {
6429 TPACPI_VOL_CAP_AUTO = 0, /* Use white/blacklist */
6430 TPACPI_VOL_CAP_VOLMUTE, /* Output vol and mute */
6431 TPACPI_VOL_CAP_MUTEONLY, /* Output mute only */
6432 TPACPI_VOL_CAP_MAX
6433};
6434
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006435static enum tpacpi_volume_access_mode volume_mode =
6436 TPACPI_VOL_MODE_MAX;
6437
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006438static enum tpacpi_volume_capabilities volume_capabilities;
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006439static int volume_control_allowed;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006440
6441/*
6442 * Used to syncronize writers to TP_EC_AUDIO and
6443 * TP_NVRAM_ADDR_MIXER, as we need to do read-modify-write
6444 */
6445static struct mutex volume_mutex;
6446
6447static void tpacpi_volume_checkpoint_nvram(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006448{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006449 u8 lec = 0;
6450 u8 b_nvram;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006451 u8 ec_mask;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006452
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006453 if (volume_mode != TPACPI_VOL_MODE_ECNVRAM)
6454 return;
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006455 if (!volume_control_allowed)
6456 return;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006457
6458 vdbg_printk(TPACPI_DBG_MIXER,
6459 "trying to checkpoint mixer state to NVRAM...\n");
6460
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006461 if (tp_features.mixer_no_level_control)
6462 ec_mask = TP_EC_AUDIO_MUTESW_MSK;
6463 else
6464 ec_mask = TP_EC_AUDIO_MUTESW_MSK | TP_EC_AUDIO_LVL_MSK;
6465
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006466 if (mutex_lock_killable(&volume_mutex) < 0)
6467 return;
6468
6469 if (unlikely(!acpi_ec_read(TP_EC_AUDIO, &lec)))
6470 goto unlock;
6471 lec &= ec_mask;
6472 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
6473
6474 if (lec != (b_nvram & ec_mask)) {
6475 /* NVRAM needs update */
6476 b_nvram &= ~ec_mask;
6477 b_nvram |= lec;
6478 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_MIXER);
6479 dbg_printk(TPACPI_DBG_MIXER,
6480 "updated NVRAM mixer status to 0x%02x (0x%02x)\n",
6481 (unsigned int) lec, (unsigned int) b_nvram);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006482 } else {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006483 vdbg_printk(TPACPI_DBG_MIXER,
6484 "NVRAM mixer status already is 0x%02x (0x%02x)\n",
6485 (unsigned int) lec, (unsigned int) b_nvram);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006486 }
6487
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006488unlock:
6489 mutex_unlock(&volume_mutex);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006490}
6491
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006492static int volume_get_status_ec(u8 *status)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006493{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006494 u8 s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006495
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006496 if (!acpi_ec_read(TP_EC_AUDIO, &s))
6497 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006498
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006499 *status = s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006500
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006501 dbg_printk(TPACPI_DBG_MIXER, "status 0x%02x\n", s);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006502
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006503 return 0;
6504}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006505
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006506static int volume_get_status(u8 *status)
6507{
6508 return volume_get_status_ec(status);
6509}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006510
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006511static int volume_set_status_ec(const u8 status)
6512{
6513 if (!acpi_ec_write(TP_EC_AUDIO, status))
6514 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006515
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006516 dbg_printk(TPACPI_DBG_MIXER, "set EC mixer to 0x%02x\n", status);
6517
6518 return 0;
6519}
6520
6521static int volume_set_status(const u8 status)
6522{
6523 return volume_set_status_ec(status);
6524}
6525
6526static int volume_set_mute_ec(const bool mute)
6527{
6528 int rc;
6529 u8 s, n;
6530
6531 if (mutex_lock_killable(&volume_mutex) < 0)
6532 return -EINTR;
6533
6534 rc = volume_get_status_ec(&s);
6535 if (rc)
6536 goto unlock;
6537
6538 n = (mute) ? s | TP_EC_AUDIO_MUTESW_MSK :
6539 s & ~TP_EC_AUDIO_MUTESW_MSK;
6540
6541 if (n != s)
6542 rc = volume_set_status_ec(n);
6543
6544unlock:
6545 mutex_unlock(&volume_mutex);
6546 return rc;
6547}
6548
6549static int volume_set_mute(const bool mute)
6550{
6551 dbg_printk(TPACPI_DBG_MIXER, "trying to %smute\n",
6552 (mute) ? "" : "un");
6553 return volume_set_mute_ec(mute);
6554}
6555
6556static int volume_set_volume_ec(const u8 vol)
6557{
6558 int rc;
6559 u8 s, n;
6560
6561 if (vol > TP_EC_VOLUME_MAX)
6562 return -EINVAL;
6563
6564 if (mutex_lock_killable(&volume_mutex) < 0)
6565 return -EINTR;
6566
6567 rc = volume_get_status_ec(&s);
6568 if (rc)
6569 goto unlock;
6570
6571 n = (s & ~TP_EC_AUDIO_LVL_MSK) | vol;
6572
6573 if (n != s)
6574 rc = volume_set_status_ec(n);
6575
6576unlock:
6577 mutex_unlock(&volume_mutex);
6578 return rc;
6579}
6580
6581static int volume_set_volume(const u8 vol)
6582{
6583 dbg_printk(TPACPI_DBG_MIXER,
6584 "trying to set volume level to %hu\n", vol);
6585 return volume_set_volume_ec(vol);
6586}
6587
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006588static void volume_alsa_notify_change(void)
6589{
6590 struct tpacpi_alsa_data *d;
6591
6592 if (alsa_card && alsa_card->private_data) {
6593 d = alsa_card->private_data;
6594 if (d->ctl_mute_id)
6595 snd_ctl_notify(alsa_card,
6596 SNDRV_CTL_EVENT_MASK_VALUE,
6597 d->ctl_mute_id);
6598 if (d->ctl_vol_id)
6599 snd_ctl_notify(alsa_card,
6600 SNDRV_CTL_EVENT_MASK_VALUE,
6601 d->ctl_vol_id);
6602 }
6603}
6604
6605static int volume_alsa_vol_info(struct snd_kcontrol *kcontrol,
6606 struct snd_ctl_elem_info *uinfo)
6607{
6608 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
6609 uinfo->count = 1;
6610 uinfo->value.integer.min = 0;
6611 uinfo->value.integer.max = TP_EC_VOLUME_MAX;
6612 return 0;
6613}
6614
6615static int volume_alsa_vol_get(struct snd_kcontrol *kcontrol,
6616 struct snd_ctl_elem_value *ucontrol)
6617{
6618 u8 s;
6619 int rc;
6620
6621 rc = volume_get_status(&s);
6622 if (rc < 0)
6623 return rc;
6624
6625 ucontrol->value.integer.value[0] = s & TP_EC_AUDIO_LVL_MSK;
6626 return 0;
6627}
6628
6629static int volume_alsa_vol_put(struct snd_kcontrol *kcontrol,
6630 struct snd_ctl_elem_value *ucontrol)
6631{
6632 return volume_set_volume(ucontrol->value.integer.value[0]);
6633}
6634
6635#define volume_alsa_mute_info snd_ctl_boolean_mono_info
6636
6637static int volume_alsa_mute_get(struct snd_kcontrol *kcontrol,
6638 struct snd_ctl_elem_value *ucontrol)
6639{
6640 u8 s;
6641 int rc;
6642
6643 rc = volume_get_status(&s);
6644 if (rc < 0)
6645 return rc;
6646
6647 ucontrol->value.integer.value[0] =
6648 (s & TP_EC_AUDIO_MUTESW_MSK) ? 0 : 1;
6649 return 0;
6650}
6651
6652static int volume_alsa_mute_put(struct snd_kcontrol *kcontrol,
6653 struct snd_ctl_elem_value *ucontrol)
6654{
6655 return volume_set_mute(!ucontrol->value.integer.value[0]);
6656}
6657
6658static struct snd_kcontrol_new volume_alsa_control_vol __devinitdata = {
6659 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6660 .name = "Console Playback Volume",
6661 .index = 0,
6662 .access = SNDRV_CTL_ELEM_ACCESS_READ,
6663 .info = volume_alsa_vol_info,
6664 .get = volume_alsa_vol_get,
6665};
6666
6667static struct snd_kcontrol_new volume_alsa_control_mute __devinitdata = {
6668 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6669 .name = "Console Playback Switch",
6670 .index = 0,
6671 .access = SNDRV_CTL_ELEM_ACCESS_READ,
6672 .info = volume_alsa_mute_info,
6673 .get = volume_alsa_mute_get,
6674};
6675
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006676static void volume_suspend(pm_message_t state)
6677{
6678 tpacpi_volume_checkpoint_nvram();
6679}
6680
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006681static void volume_resume(void)
6682{
6683 volume_alsa_notify_change();
6684}
6685
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006686static void volume_shutdown(void)
6687{
6688 tpacpi_volume_checkpoint_nvram();
6689}
6690
6691static void volume_exit(void)
6692{
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006693 if (alsa_card) {
6694 snd_card_free(alsa_card);
6695 alsa_card = NULL;
6696 }
6697
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006698 tpacpi_volume_checkpoint_nvram();
6699}
6700
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006701static int __init volume_create_alsa_mixer(void)
6702{
6703 struct snd_card *card;
6704 struct tpacpi_alsa_data *data;
6705 struct snd_kcontrol *ctl_vol;
6706 struct snd_kcontrol *ctl_mute;
6707 int rc;
6708
6709 rc = snd_card_create(alsa_index, alsa_id, THIS_MODULE,
6710 sizeof(struct tpacpi_alsa_data), &card);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02006711 if (rc < 0 || !card) {
6712 printk(TPACPI_ERR
6713 "Failed to create ALSA card structures: %d\n", rc);
6714 return 1;
6715 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006716
6717 BUG_ON(!card->private_data);
6718 data = card->private_data;
6719 data->card = card;
6720
6721 strlcpy(card->driver, TPACPI_ALSA_DRVNAME,
6722 sizeof(card->driver));
6723 strlcpy(card->shortname, TPACPI_ALSA_SHRTNAME,
6724 sizeof(card->shortname));
6725 snprintf(card->mixername, sizeof(card->mixername), "ThinkPad EC %s",
6726 (thinkpad_id.ec_version_str) ?
6727 thinkpad_id.ec_version_str : "(unknown)");
6728 snprintf(card->longname, sizeof(card->longname),
6729 "%s at EC reg 0x%02x, fw %s", card->shortname, TP_EC_AUDIO,
6730 (thinkpad_id.ec_version_str) ?
6731 thinkpad_id.ec_version_str : "unknown");
6732
6733 if (volume_control_allowed) {
6734 volume_alsa_control_vol.put = volume_alsa_vol_put;
6735 volume_alsa_control_vol.access =
6736 SNDRV_CTL_ELEM_ACCESS_READWRITE;
6737
6738 volume_alsa_control_mute.put = volume_alsa_mute_put;
6739 volume_alsa_control_mute.access =
6740 SNDRV_CTL_ELEM_ACCESS_READWRITE;
6741 }
6742
6743 if (!tp_features.mixer_no_level_control) {
6744 ctl_vol = snd_ctl_new1(&volume_alsa_control_vol, NULL);
6745 rc = snd_ctl_add(card, ctl_vol);
6746 if (rc < 0) {
6747 printk(TPACPI_ERR
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02006748 "Failed to create ALSA volume control: %d\n",
6749 rc);
6750 goto err_exit;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006751 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006752 data->ctl_vol_id = &ctl_vol->id;
6753 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006754
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006755 ctl_mute = snd_ctl_new1(&volume_alsa_control_mute, NULL);
6756 rc = snd_ctl_add(card, ctl_mute);
6757 if (rc < 0) {
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02006758 printk(TPACPI_ERR "Failed to create ALSA mute control: %d\n",
6759 rc);
6760 goto err_exit;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006761 }
6762 data->ctl_mute_id = &ctl_mute->id;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006763
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006764 snd_card_set_dev(card, &tpacpi_pdev->dev);
6765 rc = snd_card_register(card);
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006766 if (rc < 0) {
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02006767 printk(TPACPI_ERR "Failed to register ALSA card: %d\n", rc);
6768 goto err_exit;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006769 }
6770
6771 alsa_card = card;
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02006772 return 0;
6773
6774err_exit:
6775 snd_card_free(card);
6776 return 1;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006777}
6778
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006779#define TPACPI_VOL_Q_MUTEONLY 0x0001 /* Mute-only control available */
6780#define TPACPI_VOL_Q_LEVEL 0x0002 /* Volume control available */
6781
6782static const struct tpacpi_quirk volume_quirk_table[] __initconst = {
6783 /* Whitelist volume level on all IBM by default */
6784 { .vendor = PCI_VENDOR_ID_IBM,
6785 .bios = TPACPI_MATCH_ANY,
6786 .ec = TPACPI_MATCH_ANY,
6787 .quirks = TPACPI_VOL_Q_LEVEL },
6788
6789 /* Lenovo models with volume control (needs confirmation) */
6790 TPACPI_QEC_LNV('7', 'C', TPACPI_VOL_Q_LEVEL), /* R60/i */
6791 TPACPI_QEC_LNV('7', 'E', TPACPI_VOL_Q_LEVEL), /* R60e/i */
6792 TPACPI_QEC_LNV('7', '9', TPACPI_VOL_Q_LEVEL), /* T60/p */
6793 TPACPI_QEC_LNV('7', 'B', TPACPI_VOL_Q_LEVEL), /* X60/s */
6794 TPACPI_QEC_LNV('7', 'J', TPACPI_VOL_Q_LEVEL), /* X60t */
6795 TPACPI_QEC_LNV('7', '7', TPACPI_VOL_Q_LEVEL), /* Z60 */
6796 TPACPI_QEC_LNV('7', 'F', TPACPI_VOL_Q_LEVEL), /* Z61 */
6797
6798 /* Whitelist mute-only on all Lenovo by default */
6799 { .vendor = PCI_VENDOR_ID_LENOVO,
6800 .bios = TPACPI_MATCH_ANY,
6801 .ec = TPACPI_MATCH_ANY,
6802 .quirks = TPACPI_VOL_Q_MUTEONLY }
6803};
6804
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006805static int __init volume_init(struct ibm_init_struct *iibm)
6806{
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006807 unsigned long quirks;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006808 int rc;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006809
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006810 vdbg_printk(TPACPI_DBG_INIT, "initializing volume subdriver\n");
6811
6812 mutex_init(&volume_mutex);
6813
6814 /*
6815 * Check for module parameter bogosity, note that we
6816 * init volume_mode to TPACPI_VOL_MODE_MAX in order to be
6817 * able to detect "unspecified"
6818 */
6819 if (volume_mode > TPACPI_VOL_MODE_MAX)
6820 return -EINVAL;
6821
6822 if (volume_mode == TPACPI_VOL_MODE_UCMS_STEP) {
6823 printk(TPACPI_ERR
6824 "UCMS step volume mode not implemented, "
6825 "please contact %s\n", TPACPI_MAIL);
6826 return 1;
6827 }
6828
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006829 if (volume_capabilities >= TPACPI_VOL_CAP_MAX)
6830 return -EINVAL;
6831
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006832 /*
6833 * The ALSA mixer is our primary interface.
6834 * When disabled, don't install the subdriver at all
6835 */
6836 if (!alsa_enable) {
6837 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6838 "ALSA mixer disabled by parameter, "
6839 "not loading volume subdriver...\n");
6840 return 1;
6841 }
6842
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006843 quirks = tpacpi_check_quirks(volume_quirk_table,
6844 ARRAY_SIZE(volume_quirk_table));
6845
6846 switch (volume_capabilities) {
6847 case TPACPI_VOL_CAP_AUTO:
6848 if (quirks & TPACPI_VOL_Q_MUTEONLY)
6849 tp_features.mixer_no_level_control = 1;
6850 else if (quirks & TPACPI_VOL_Q_LEVEL)
6851 tp_features.mixer_no_level_control = 0;
6852 else
6853 return 1; /* no mixer */
6854 break;
6855 case TPACPI_VOL_CAP_VOLMUTE:
6856 tp_features.mixer_no_level_control = 0;
6857 break;
6858 case TPACPI_VOL_CAP_MUTEONLY:
6859 tp_features.mixer_no_level_control = 1;
6860 break;
6861 default:
6862 return 1;
6863 }
6864
6865 if (volume_capabilities != TPACPI_VOL_CAP_AUTO)
6866 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6867 "using user-supplied volume_capabilities=%d\n",
6868 volume_capabilities);
6869
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006870 if (volume_mode == TPACPI_VOL_MODE_AUTO ||
6871 volume_mode == TPACPI_VOL_MODE_MAX) {
6872 volume_mode = TPACPI_VOL_MODE_ECNVRAM;
6873
6874 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6875 "driver auto-selected volume_mode=%d\n",
6876 volume_mode);
6877 } else {
6878 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6879 "using user-supplied volume_mode=%d\n",
6880 volume_mode);
6881 }
6882
6883 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006884 "mute is supported, volume control is %s\n",
6885 str_supported(!tp_features.mixer_no_level_control));
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006886
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006887 rc = volume_create_alsa_mixer();
6888 if (rc) {
6889 printk(TPACPI_ERR
6890 "Could not create the ALSA mixer interface\n");
6891 return rc;
6892 }
6893
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006894 printk(TPACPI_INFO
6895 "Console audio control enabled, mode: %s\n",
6896 (volume_control_allowed) ?
6897 "override (read/write)" :
6898 "monitor (read only)");
6899
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006900 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6901 "registering volume hotkeys as change notification\n");
6902 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6903 | TP_ACPI_HKEY_VOLUP_MASK
6904 | TP_ACPI_HKEY_VOLDWN_MASK
6905 | TP_ACPI_HKEY_MUTE_MASK);
6906
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006907 return 0;
6908}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006909
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006910static int volume_read(struct seq_file *m)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006911{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006912 u8 status;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006913
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006914 if (volume_get_status(&status) < 0) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006915 seq_printf(m, "level:\t\tunreadable\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006916 } else {
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006917 if (tp_features.mixer_no_level_control)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006918 seq_printf(m, "level:\t\tunsupported\n");
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006919 else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006920 seq_printf(m, "level:\t\t%d\n",
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006921 status & TP_EC_AUDIO_LVL_MSK);
6922
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006923 seq_printf(m, "mute:\t\t%s\n",
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006924 onoff(status, TP_EC_AUDIO_MUTESW));
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006925
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006926 if (volume_control_allowed) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006927 seq_printf(m, "commands:\tunmute, mute\n");
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006928 if (!tp_features.mixer_no_level_control) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006929 seq_printf(m,
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006930 "commands:\tup, down\n");
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006931 seq_printf(m,
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006932 "commands:\tlevel <level>"
6933 " (<level> is 0-%d)\n",
6934 TP_EC_VOLUME_MAX);
6935 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006936 }
6937 }
6938
6939 return 0;
6940}
6941
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006942static int volume_write(char *buf)
6943{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006944 u8 s;
6945 u8 new_level, new_mute;
6946 int l;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006947 char *cmd;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006948 int rc;
6949
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006950 /*
6951 * We do allow volume control at driver startup, so that the
6952 * user can set initial state through the volume=... parameter hack.
6953 */
6954 if (!volume_control_allowed && tpacpi_lifecycle != TPACPI_LIFE_INIT) {
6955 if (unlikely(!tp_warned.volume_ctrl_forbidden)) {
6956 tp_warned.volume_ctrl_forbidden = 1;
6957 printk(TPACPI_NOTICE
6958 "Console audio control in monitor mode, "
6959 "changes are not allowed.\n");
6960 printk(TPACPI_NOTICE
6961 "Use the volume_control=1 module parameter "
6962 "to enable volume control\n");
6963 }
6964 return -EPERM;
6965 }
6966
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006967 rc = volume_get_status(&s);
6968 if (rc < 0)
6969 return rc;
6970
6971 new_level = s & TP_EC_AUDIO_LVL_MSK;
6972 new_mute = s & TP_EC_AUDIO_MUTESW_MSK;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006973
6974 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006975 if (!tp_features.mixer_no_level_control) {
6976 if (strlencmp(cmd, "up") == 0) {
6977 if (new_mute)
6978 new_mute = 0;
6979 else if (new_level < TP_EC_VOLUME_MAX)
6980 new_level++;
6981 continue;
6982 } else if (strlencmp(cmd, "down") == 0) {
6983 if (new_mute)
6984 new_mute = 0;
6985 else if (new_level > 0)
6986 new_level--;
6987 continue;
6988 } else if (sscanf(cmd, "level %u", &l) == 1 &&
6989 l >= 0 && l <= TP_EC_VOLUME_MAX) {
6990 new_level = l;
6991 continue;
6992 }
6993 }
6994 if (strlencmp(cmd, "mute") == 0)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006995 new_mute = TP_EC_AUDIO_MUTESW_MSK;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006996 else if (strlencmp(cmd, "unmute") == 0)
6997 new_mute = 0;
6998 else
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006999 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007000 }
7001
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007002 if (tp_features.mixer_no_level_control) {
7003 tpacpi_disclose_usertask("procfs volume", "%smute\n",
7004 new_mute ? "" : "un");
7005 rc = volume_set_mute(!!new_mute);
7006 } else {
7007 tpacpi_disclose_usertask("procfs volume",
7008 "%smute and set level to %d\n",
7009 new_mute ? "" : "un", new_level);
7010 rc = volume_set_status(new_mute | new_level);
7011 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007012 volume_alsa_notify_change();
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007013
7014 return (rc == -EINTR) ? -ERESTARTSYS : rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007015}
7016
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007017static struct ibm_struct volume_driver_data = {
7018 .name = "volume",
7019 .read = volume_read,
7020 .write = volume_write,
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007021 .exit = volume_exit,
7022 .suspend = volume_suspend,
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007023 .resume = volume_resume,
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007024 .shutdown = volume_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007025};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007026
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02007027#else /* !CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7028
7029#define alsa_card NULL
7030
7031static void inline volume_alsa_notify_change(void)
7032{
7033}
7034
7035static int __init volume_init(struct ibm_init_struct *iibm)
7036{
7037 printk(TPACPI_INFO
7038 "volume: disabled as there is no ALSA support in this kernel\n");
7039
7040 return 1;
7041}
7042
7043static struct ibm_struct volume_driver_data = {
7044 .name = "volume",
7045};
7046
7047#endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7048
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007049/*************************************************************************
7050 * Fan subdriver
7051 */
7052
7053/*
7054 * FAN ACCESS MODES
7055 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007056 * TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007057 * ACPI GFAN method: returns fan level
7058 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007059 * see TPACPI_FAN_WR_ACPI_SFAN
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007060 * EC 0x2f (HFSP) not available if GFAN exists
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007061 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007062 * TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007063 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
7064 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007065 * EC 0x2f (HFSP) might be available *for reading*, but do not use
7066 * it for writing.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007067 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007068 * TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007069 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
7070 * Supported on almost all ThinkPads
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007071 *
7072 * Fan speed changes of any sort (including those caused by the
7073 * disengaged mode) are usually done slowly by the firmware as the
7074 * maximum ammount of fan duty cycle change per second seems to be
7075 * limited.
7076 *
7077 * Reading is not available if GFAN exists.
7078 * Writing is not available if SFAN exists.
7079 *
7080 * Bits
7081 * 7 automatic mode engaged;
7082 * (default operation mode of the ThinkPad)
7083 * fan level is ignored in this mode.
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007084 * 6 full speed mode (takes precedence over bit 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007085 * not available on all thinkpads. May disable
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007086 * the tachometer while the fan controller ramps up
7087 * the speed (which can take up to a few *minutes*).
7088 * Speeds up fan to 100% duty-cycle, which is far above
7089 * the standard RPM levels. It is not impossible that
7090 * it could cause hardware damage.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007091 * 5-3 unused in some models. Extra bits for fan level
7092 * in others, but still useless as all values above
7093 * 7 map to the same speed as level 7 in these models.
7094 * 2-0 fan level (0..7 usually)
7095 * 0x00 = stop
7096 * 0x07 = max (set when temperatures critical)
7097 * Some ThinkPads may have other levels, see
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007098 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007099 *
7100 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
7101 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
7102 * does so, its initial value is meaningless (0x07).
7103 *
7104 * For firmware bugs, refer to:
7105 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7106 *
7107 * ----
7108 *
7109 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
7110 * Main fan tachometer reading (in RPM)
7111 *
7112 * This register is present on all ThinkPads with a new-style EC, and
7113 * it is known not to be present on the A21m/e, and T22, as there is
7114 * something else in offset 0x84 according to the ACPI DSDT. Other
7115 * ThinkPads from this same time period (and earlier) probably lack the
7116 * tachometer as well.
7117 *
Nick Andrew877d0312009-01-26 11:06:57 +01007118 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007119 * was never fixed by IBM to report the EC firmware version string
7120 * probably support the tachometer (like the early X models), so
7121 * detecting it is quite hard. We need more data to know for sure.
7122 *
7123 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
7124 * might result.
7125 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007126 * FIRMWARE BUG: may go stale while the EC is switching to full speed
7127 * mode.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007128 *
7129 * For firmware bugs, refer to:
7130 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7131 *
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007132 * ----
7133 *
7134 * ThinkPad EC register 0x31 bit 0 (only on select models)
7135 *
7136 * When bit 0 of EC register 0x31 is zero, the tachometer registers
7137 * show the speed of the main fan. When bit 0 of EC register 0x31
7138 * is one, the tachometer registers show the speed of the auxiliary
7139 * fan.
7140 *
7141 * Fan control seems to affect both fans, regardless of the state
7142 * of this bit.
7143 *
7144 * So far, only the firmware for the X60/X61 non-tablet versions
7145 * seem to support this (firmware TP-7M).
7146 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007147 * TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007148 * ThinkPad X31, X40, X41. Not available in the X60.
7149 *
7150 * FANS ACPI handle: takes three arguments: low speed, medium speed,
7151 * high speed. ACPI DSDT seems to map these three speeds to levels
7152 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
7153 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
7154 *
7155 * The speeds are stored on handles
7156 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
7157 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007158 * There are three default speed sets, accessible as handles:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007159 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
7160 *
7161 * ACPI DSDT switches which set is in use depending on various
7162 * factors.
7163 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007164 * TPACPI_FAN_WR_TPEC is also available and should be used to
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007165 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
7166 * but the ACPI tables just mention level 7.
7167 */
7168
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007169enum { /* Fan control constants */
7170 fan_status_offset = 0x2f, /* EC register 0x2f */
7171 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
7172 * 0x84 must be read before 0x85 */
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007173 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
7174 bit 0 selects which fan is active */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007175
7176 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
7177 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
7178
7179 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
7180};
7181
7182enum fan_status_access_mode {
7183 TPACPI_FAN_NONE = 0, /* No fan status or control */
7184 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
7185 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
7186};
7187
7188enum fan_control_access_mode {
7189 TPACPI_FAN_WR_NONE = 0, /* No fan control */
7190 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
7191 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
7192 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
7193};
7194
7195enum fan_control_commands {
7196 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
7197 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
7198 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
7199 * and also watchdog cmd */
7200};
7201
7202static int fan_control_allowed;
7203
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007204static enum fan_status_access_mode fan_status_access_mode;
7205static enum fan_control_access_mode fan_control_access_mode;
7206static enum fan_control_commands fan_control_commands;
7207
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02007208static u8 fan_control_initial_status;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007209static u8 fan_control_desired_level;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007210static u8 fan_control_resume_level;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007211static int fan_watchdog_maxinterval;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007212
7213static struct mutex fan_mutex;
7214
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007215static void fan_watchdog_fire(struct work_struct *ignored);
Len Brown25c68a32006-12-08 04:43:41 -05007216static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007217
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007218TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
7219TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007220 "\\FSPD", /* 600e/x, 770e, 770x */
7221 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007222TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007223 "JFNS", /* 770x-JL */
7224 ); /* all others */
7225
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007226/*
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007227 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
7228 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
7229 * be in auto mode (0x80).
7230 *
7231 * This is corrected by any write to HFSP either by the driver, or
7232 * by the firmware.
7233 *
7234 * We assume 0x07 really means auto mode while this quirk is active,
7235 * as this is far more likely than the ThinkPad being in level 7,
7236 * which is only used by the firmware during thermal emergencies.
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007237 *
7238 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
7239 * TP-70 (T43, R52), which are known to be buggy.
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007240 */
7241
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007242static void fan_quirk1_setup(void)
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007243{
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007244 if (fan_control_initial_status == 0x07) {
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007245 printk(TPACPI_NOTICE
7246 "fan_init: initial fan status is unknown, "
7247 "assuming it is in auto mode\n");
7248 tp_features.fan_ctrl_status_undef = 1;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007249 }
7250}
7251
7252static void fan_quirk1_handle(u8 *fan_status)
7253{
7254 if (unlikely(tp_features.fan_ctrl_status_undef)) {
7255 if (*fan_status != fan_control_initial_status) {
7256 /* something changed the HFSP regisnter since
7257 * driver init time, so it is not undefined
7258 * anymore */
7259 tp_features.fan_ctrl_status_undef = 0;
7260 } else {
7261 /* Return most likely status. In fact, it
7262 * might be the only possible status */
7263 *fan_status = TP_EC_FAN_AUTO;
7264 }
7265 }
7266}
7267
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007268/* Select main fan on X60/X61, NOOP on others */
7269static bool fan_select_fan1(void)
7270{
7271 if (tp_features.second_fan) {
7272 u8 val;
7273
7274 if (ec_read(fan_select_offset, &val) < 0)
7275 return false;
7276 val &= 0xFEU;
7277 if (ec_write(fan_select_offset, val) < 0)
7278 return false;
7279 }
7280 return true;
7281}
7282
7283/* Select secondary fan on X60/X61 */
7284static bool fan_select_fan2(void)
7285{
7286 u8 val;
7287
7288 if (!tp_features.second_fan)
7289 return false;
7290
7291 if (ec_read(fan_select_offset, &val) < 0)
7292 return false;
7293 val |= 0x01U;
7294 if (ec_write(fan_select_offset, val) < 0)
7295 return false;
7296
7297 return true;
7298}
7299
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007300/*
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007301 * Call with fan_mutex held
7302 */
7303static void fan_update_desired_level(u8 status)
7304{
7305 if ((status &
7306 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7307 if (status > 7)
7308 fan_control_desired_level = 7;
7309 else
7310 fan_control_desired_level = status;
7311 }
7312}
7313
7314static int fan_get_status(u8 *status)
7315{
7316 u8 s;
7317
7318 /* TODO:
7319 * Add TPACPI_FAN_RD_ACPI_FANS ? */
7320
7321 switch (fan_status_access_mode) {
7322 case TPACPI_FAN_RD_ACPI_GFAN:
7323 /* 570, 600e/x, 770e, 770x */
7324
7325 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
7326 return -EIO;
7327
7328 if (likely(status))
7329 *status = s & 0x07;
7330
7331 break;
7332
7333 case TPACPI_FAN_RD_TPEC:
7334 /* all except 570, 600e/x, 770e, 770x */
7335 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
7336 return -EIO;
7337
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007338 if (likely(status)) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007339 *status = s;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007340 fan_quirk1_handle(status);
7341 }
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007342
7343 break;
7344
7345 default:
7346 return -ENXIO;
7347 }
7348
7349 return 0;
7350}
7351
7352static int fan_get_status_safe(u8 *status)
7353{
7354 int rc;
7355 u8 s;
7356
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007357 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007358 return -ERESTARTSYS;
7359 rc = fan_get_status(&s);
7360 if (!rc)
7361 fan_update_desired_level(s);
7362 mutex_unlock(&fan_mutex);
7363
7364 if (status)
7365 *status = s;
7366
7367 return rc;
7368}
7369
7370static int fan_get_speed(unsigned int *speed)
7371{
7372 u8 hi, lo;
7373
7374 switch (fan_status_access_mode) {
7375 case TPACPI_FAN_RD_TPEC:
7376 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007377 if (unlikely(!fan_select_fan1()))
7378 return -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007379 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
7380 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
7381 return -EIO;
7382
7383 if (likely(speed))
7384 *speed = (hi << 8) | lo;
7385
7386 break;
7387
7388 default:
7389 return -ENXIO;
7390 }
7391
7392 return 0;
7393}
7394
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007395static int fan2_get_speed(unsigned int *speed)
7396{
7397 u8 hi, lo;
7398 bool rc;
7399
7400 switch (fan_status_access_mode) {
7401 case TPACPI_FAN_RD_TPEC:
7402 /* all except 570, 600e/x, 770e, 770x */
7403 if (unlikely(!fan_select_fan2()))
7404 return -EIO;
7405 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
7406 !acpi_ec_read(fan_rpm_offset + 1, &hi);
7407 fan_select_fan1(); /* play it safe */
7408 if (rc)
7409 return -EIO;
7410
7411 if (likely(speed))
7412 *speed = (hi << 8) | lo;
7413
7414 break;
7415
7416 default:
7417 return -ENXIO;
7418 }
7419
7420 return 0;
7421}
7422
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007423static int fan_set_level(int level)
7424{
7425 if (!fan_control_allowed)
7426 return -EPERM;
7427
7428 switch (fan_control_access_mode) {
7429 case TPACPI_FAN_WR_ACPI_SFAN:
7430 if (level >= 0 && level <= 7) {
7431 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
7432 return -EIO;
7433 } else
7434 return -EINVAL;
7435 break;
7436
7437 case TPACPI_FAN_WR_ACPI_FANS:
7438 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007439 if (!(level & TP_EC_FAN_AUTO) &&
7440 !(level & TP_EC_FAN_FULLSPEED) &&
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007441 ((level < 0) || (level > 7)))
7442 return -EINVAL;
7443
7444 /* safety net should the EC not support AUTO
7445 * or FULLSPEED mode bits and just ignore them */
7446 if (level & TP_EC_FAN_FULLSPEED)
7447 level |= 7; /* safety min speed 7 */
Roel Kluin547266e2008-02-05 00:24:56 +01007448 else if (level & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007449 level |= 4; /* safety min speed 4 */
7450
7451 if (!acpi_ec_write(fan_status_offset, level))
7452 return -EIO;
7453 else
7454 tp_features.fan_ctrl_status_undef = 0;
7455 break;
7456
7457 default:
7458 return -ENXIO;
7459 }
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007460
7461 vdbg_printk(TPACPI_DBG_FAN,
7462 "fan control: set fan control register to 0x%02x\n", level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007463 return 0;
7464}
7465
7466static int fan_set_level_safe(int level)
7467{
7468 int rc;
7469
7470 if (!fan_control_allowed)
7471 return -EPERM;
7472
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007473 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007474 return -ERESTARTSYS;
7475
7476 if (level == TPACPI_FAN_LAST_LEVEL)
7477 level = fan_control_desired_level;
7478
7479 rc = fan_set_level(level);
7480 if (!rc)
7481 fan_update_desired_level(level);
7482
7483 mutex_unlock(&fan_mutex);
7484 return rc;
7485}
7486
7487static int fan_set_enable(void)
7488{
7489 u8 s;
7490 int rc;
7491
7492 if (!fan_control_allowed)
7493 return -EPERM;
7494
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007495 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007496 return -ERESTARTSYS;
7497
7498 switch (fan_control_access_mode) {
7499 case TPACPI_FAN_WR_ACPI_FANS:
7500 case TPACPI_FAN_WR_TPEC:
7501 rc = fan_get_status(&s);
7502 if (rc < 0)
7503 break;
7504
7505 /* Don't go out of emergency fan mode */
7506 if (s != 7) {
7507 s &= 0x07;
7508 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
7509 }
7510
7511 if (!acpi_ec_write(fan_status_offset, s))
7512 rc = -EIO;
7513 else {
7514 tp_features.fan_ctrl_status_undef = 0;
7515 rc = 0;
7516 }
7517 break;
7518
7519 case TPACPI_FAN_WR_ACPI_SFAN:
7520 rc = fan_get_status(&s);
7521 if (rc < 0)
7522 break;
7523
7524 s &= 0x07;
7525
7526 /* Set fan to at least level 4 */
7527 s |= 4;
7528
7529 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007530 rc = -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007531 else
7532 rc = 0;
7533 break;
7534
7535 default:
7536 rc = -ENXIO;
7537 }
7538
7539 mutex_unlock(&fan_mutex);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007540
7541 if (!rc)
7542 vdbg_printk(TPACPI_DBG_FAN,
7543 "fan control: set fan control register to 0x%02x\n",
7544 s);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007545 return rc;
7546}
7547
7548static int fan_set_disable(void)
7549{
7550 int rc;
7551
7552 if (!fan_control_allowed)
7553 return -EPERM;
7554
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007555 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007556 return -ERESTARTSYS;
7557
7558 rc = 0;
7559 switch (fan_control_access_mode) {
7560 case TPACPI_FAN_WR_ACPI_FANS:
7561 case TPACPI_FAN_WR_TPEC:
7562 if (!acpi_ec_write(fan_status_offset, 0x00))
7563 rc = -EIO;
7564 else {
7565 fan_control_desired_level = 0;
7566 tp_features.fan_ctrl_status_undef = 0;
7567 }
7568 break;
7569
7570 case TPACPI_FAN_WR_ACPI_SFAN:
7571 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
7572 rc = -EIO;
7573 else
7574 fan_control_desired_level = 0;
7575 break;
7576
7577 default:
7578 rc = -ENXIO;
7579 }
7580
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007581 if (!rc)
7582 vdbg_printk(TPACPI_DBG_FAN,
7583 "fan control: set fan control register to 0\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007584
7585 mutex_unlock(&fan_mutex);
7586 return rc;
7587}
7588
7589static int fan_set_speed(int speed)
7590{
7591 int rc;
7592
7593 if (!fan_control_allowed)
7594 return -EPERM;
7595
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007596 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007597 return -ERESTARTSYS;
7598
7599 rc = 0;
7600 switch (fan_control_access_mode) {
7601 case TPACPI_FAN_WR_ACPI_FANS:
7602 if (speed >= 0 && speed <= 65535) {
7603 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
7604 speed, speed, speed))
7605 rc = -EIO;
7606 } else
7607 rc = -EINVAL;
7608 break;
7609
7610 default:
7611 rc = -ENXIO;
7612 }
7613
7614 mutex_unlock(&fan_mutex);
7615 return rc;
7616}
7617
7618static void fan_watchdog_reset(void)
7619{
7620 static int fan_watchdog_active;
7621
7622 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
7623 return;
7624
7625 if (fan_watchdog_active)
7626 cancel_delayed_work(&fan_watchdog_task);
7627
7628 if (fan_watchdog_maxinterval > 0 &&
7629 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
7630 fan_watchdog_active = 1;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03007631 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007632 msecs_to_jiffies(fan_watchdog_maxinterval
7633 * 1000))) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007634 printk(TPACPI_ERR
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03007635 "failed to queue the fan watchdog, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007636 "watchdog will not trigger\n");
7637 }
7638 } else
7639 fan_watchdog_active = 0;
7640}
7641
7642static void fan_watchdog_fire(struct work_struct *ignored)
7643{
7644 int rc;
7645
7646 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
7647 return;
7648
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007649 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007650 rc = fan_set_enable();
7651 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007652 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007653 "will try again later...\n", -rc);
7654 /* reschedule for later */
7655 fan_watchdog_reset();
7656 }
7657}
7658
7659/*
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007660 * SYSFS fan layout: hwmon compatible (device)
7661 *
7662 * pwm*_enable:
7663 * 0: "disengaged" mode
7664 * 1: manual mode
7665 * 2: native EC "auto" mode (recommended, hardware default)
7666 *
7667 * pwm*: set speed in manual mode, ignored otherwise.
7668 * 0 is level 0; 255 is level 7. Intermediate points done with linear
7669 * interpolation.
7670 *
7671 * fan*_input: tachometer reading, RPM
7672 *
7673 *
7674 * SYSFS fan layout: extensions
7675 *
7676 * fan_watchdog (driver):
7677 * fan watchdog interval in seconds, 0 disables (default), max 120
7678 */
7679
7680/* sysfs fan pwm1_enable ----------------------------------------------- */
7681static ssize_t fan_pwm1_enable_show(struct device *dev,
7682 struct device_attribute *attr,
7683 char *buf)
7684{
7685 int res, mode;
7686 u8 status;
7687
7688 res = fan_get_status_safe(&status);
7689 if (res)
7690 return res;
7691
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007692 if (status & TP_EC_FAN_FULLSPEED) {
7693 mode = 0;
7694 } else if (status & TP_EC_FAN_AUTO) {
7695 mode = 2;
7696 } else
7697 mode = 1;
7698
7699 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
7700}
7701
7702static ssize_t fan_pwm1_enable_store(struct device *dev,
7703 struct device_attribute *attr,
7704 const char *buf, size_t count)
7705{
7706 unsigned long t;
7707 int res, level;
7708
7709 if (parse_strtoul(buf, 2, &t))
7710 return -EINVAL;
7711
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007712 tpacpi_disclose_usertask("hwmon pwm1_enable",
7713 "set fan mode to %lu\n", t);
7714
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007715 switch (t) {
7716 case 0:
7717 level = TP_EC_FAN_FULLSPEED;
7718 break;
7719 case 1:
7720 level = TPACPI_FAN_LAST_LEVEL;
7721 break;
7722 case 2:
7723 level = TP_EC_FAN_AUTO;
7724 break;
7725 case 3:
7726 /* reserved for software-controlled auto mode */
7727 return -ENOSYS;
7728 default:
7729 return -EINVAL;
7730 }
7731
7732 res = fan_set_level_safe(level);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03007733 if (res == -ENXIO)
7734 return -EINVAL;
7735 else if (res < 0)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007736 return res;
7737
7738 fan_watchdog_reset();
7739
7740 return count;
7741}
7742
7743static struct device_attribute dev_attr_fan_pwm1_enable =
7744 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
7745 fan_pwm1_enable_show, fan_pwm1_enable_store);
7746
7747/* sysfs fan pwm1 ------------------------------------------------------ */
7748static ssize_t fan_pwm1_show(struct device *dev,
7749 struct device_attribute *attr,
7750 char *buf)
7751{
7752 int res;
7753 u8 status;
7754
7755 res = fan_get_status_safe(&status);
7756 if (res)
7757 return res;
7758
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007759 if ((status &
7760 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
7761 status = fan_control_desired_level;
7762
7763 if (status > 7)
7764 status = 7;
7765
7766 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
7767}
7768
7769static ssize_t fan_pwm1_store(struct device *dev,
7770 struct device_attribute *attr,
7771 const char *buf, size_t count)
7772{
7773 unsigned long s;
7774 int rc;
7775 u8 status, newlevel;
7776
7777 if (parse_strtoul(buf, 255, &s))
7778 return -EINVAL;
7779
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007780 tpacpi_disclose_usertask("hwmon pwm1",
7781 "set fan speed to %lu\n", s);
7782
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007783 /* scale down from 0-255 to 0-7 */
7784 newlevel = (s >> 5) & 0x07;
7785
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007786 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02007787 return -ERESTARTSYS;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007788
7789 rc = fan_get_status(&status);
7790 if (!rc && (status &
7791 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7792 rc = fan_set_level(newlevel);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03007793 if (rc == -ENXIO)
7794 rc = -EINVAL;
7795 else if (!rc) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007796 fan_update_desired_level(newlevel);
Henrique de Moraes Holschuhca4ac2f2007-04-27 22:00:11 -03007797 fan_watchdog_reset();
7798 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007799 }
7800
7801 mutex_unlock(&fan_mutex);
7802 return (rc)? rc : count;
7803}
7804
7805static struct device_attribute dev_attr_fan_pwm1 =
7806 __ATTR(pwm1, S_IWUSR | S_IRUGO,
7807 fan_pwm1_show, fan_pwm1_store);
7808
7809/* sysfs fan fan1_input ------------------------------------------------ */
7810static ssize_t fan_fan1_input_show(struct device *dev,
7811 struct device_attribute *attr,
7812 char *buf)
7813{
7814 int res;
7815 unsigned int speed;
7816
7817 res = fan_get_speed(&speed);
7818 if (res < 0)
7819 return res;
7820
7821 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7822}
7823
7824static struct device_attribute dev_attr_fan_fan1_input =
7825 __ATTR(fan1_input, S_IRUGO,
7826 fan_fan1_input_show, NULL);
7827
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007828/* sysfs fan fan2_input ------------------------------------------------ */
7829static ssize_t fan_fan2_input_show(struct device *dev,
7830 struct device_attribute *attr,
7831 char *buf)
7832{
7833 int res;
7834 unsigned int speed;
7835
7836 res = fan2_get_speed(&speed);
7837 if (res < 0)
7838 return res;
7839
7840 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7841}
7842
7843static struct device_attribute dev_attr_fan_fan2_input =
7844 __ATTR(fan2_input, S_IRUGO,
7845 fan_fan2_input_show, NULL);
7846
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007847/* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007848static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
7849 char *buf)
7850{
7851 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
7852}
7853
7854static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
7855 const char *buf, size_t count)
7856{
7857 unsigned long t;
7858
7859 if (parse_strtoul(buf, 120, &t))
7860 return -EINVAL;
7861
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03007862 if (!fan_control_allowed)
7863 return -EPERM;
7864
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007865 fan_watchdog_maxinterval = t;
7866 fan_watchdog_reset();
7867
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007868 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
7869
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007870 return count;
7871}
7872
7873static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
7874 fan_fan_watchdog_show, fan_fan_watchdog_store);
7875
7876/* --------------------------------------------------------------------- */
7877static struct attribute *fan_attributes[] = {
7878 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
7879 &dev_attr_fan_fan1_input.attr,
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007880 NULL, /* for fan2_input */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007881 NULL
7882};
7883
7884static const struct attribute_group fan_attr_group = {
7885 .attrs = fan_attributes,
7886};
7887
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007888#define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
7889#define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007890
7891#define TPACPI_FAN_QI(__id1, __id2, __quirks) \
7892 { .vendor = PCI_VENDOR_ID_IBM, \
7893 .bios = TPACPI_MATCH_ANY, \
7894 .ec = TPID(__id1, __id2), \
7895 .quirks = __quirks }
7896
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007897#define TPACPI_FAN_QL(__id1, __id2, __quirks) \
7898 { .vendor = PCI_VENDOR_ID_LENOVO, \
7899 .bios = TPACPI_MATCH_ANY, \
7900 .ec = TPID(__id1, __id2), \
7901 .quirks = __quirks }
7902
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007903static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
7904 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
7905 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
7906 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
7907 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007908 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007909};
7910
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007911#undef TPACPI_FAN_QL
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007912#undef TPACPI_FAN_QI
7913
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007914static int __init fan_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007915{
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007916 int rc;
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007917 unsigned long quirks;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007918
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007919 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7920 "initializing fan subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007921
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03007922 mutex_init(&fan_mutex);
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007923 fan_status_access_mode = TPACPI_FAN_NONE;
7924 fan_control_access_mode = TPACPI_FAN_WR_NONE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007925 fan_control_commands = 0;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007926 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03007927 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007928 tp_features.second_fan = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007929 fan_control_desired_level = 7;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007930
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007931 TPACPI_ACPIHANDLE_INIT(fans);
7932 TPACPI_ACPIHANDLE_INIT(gfan);
7933 TPACPI_ACPIHANDLE_INIT(sfan);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007934
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007935 quirks = tpacpi_check_quirks(fan_quirk_table,
7936 ARRAY_SIZE(fan_quirk_table));
7937
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007938 if (gfan_handle) {
7939 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007940 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007941 } else {
7942 /* all other ThinkPads: note that even old-style
7943 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02007944 if (likely(acpi_ec_read(fan_status_offset,
7945 &fan_control_initial_status))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007946 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007947 if (quirks & TPACPI_FAN_Q1)
7948 fan_quirk1_setup();
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007949 if (quirks & TPACPI_FAN_2FAN) {
7950 tp_features.second_fan = 1;
7951 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7952 "secondary fan support enabled\n");
7953 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007954 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007955 printk(TPACPI_ERR
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007956 "ThinkPad ACPI EC access misbehaving, "
7957 "fan status and control unavailable\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007958 return 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007959 }
7960 }
7961
7962 if (sfan_handle) {
7963 /* 570, 770x-JL */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007964 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02007965 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007966 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007967 } else {
7968 if (!gfan_handle) {
7969 /* gfan without sfan means no fan control */
7970 /* all other models implement TP EC 0x2f control */
7971
7972 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02007973 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007974 fan_control_access_mode =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007975 TPACPI_FAN_WR_ACPI_FANS;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007976 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007977 TPACPI_FAN_CMD_SPEED |
7978 TPACPI_FAN_CMD_LEVEL |
7979 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007980 } else {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007981 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02007982 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007983 TPACPI_FAN_CMD_LEVEL |
7984 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007985 }
7986 }
7987 }
7988
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007989 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7990 "fan is %s, modes %d, %d\n",
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007991 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
7992 fan_control_access_mode != TPACPI_FAN_WR_NONE),
7993 fan_status_access_mode, fan_control_access_mode);
7994
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03007995 /* fan control master switch */
7996 if (!fan_control_allowed) {
7997 fan_control_access_mode = TPACPI_FAN_WR_NONE;
7998 fan_control_commands = 0;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007999 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008000 "fan control features disabled by parameter\n");
8001 }
8002
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008003 /* update fan_control_desired_level */
8004 if (fan_status_access_mode != TPACPI_FAN_NONE)
8005 fan_get_status_safe(NULL);
8006
8007 if (fan_status_access_mode != TPACPI_FAN_NONE ||
8008 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008009 if (tp_features.second_fan) {
8010 /* attach second fan tachometer */
8011 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
8012 &dev_attr_fan_fan2_input.attr;
8013 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008014 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008015 &fan_attr_group);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008016 if (rc < 0)
8017 return rc;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03008018
8019 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
8020 &driver_attr_fan_watchdog);
8021 if (rc < 0) {
8022 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
8023 &fan_attr_group);
8024 return rc;
8025 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008026 return 0;
8027 } else
8028 return 1;
8029}
8030
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008031static void fan_exit(void)
8032{
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008033 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008034 "cancelling any pending fan watchdog tasks\n");
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008035
8036 /* FIXME: can we really do this unconditionally? */
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008037 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008038 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
8039 &driver_attr_fan_watchdog);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008040
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008041 cancel_delayed_work(&fan_watchdog_task);
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03008042 flush_workqueue(tpacpi_wq);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008043}
8044
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008045static void fan_suspend(pm_message_t state)
8046{
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008047 int rc;
8048
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008049 if (!fan_control_allowed)
8050 return;
8051
8052 /* Store fan status in cache */
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008053 fan_control_resume_level = 0;
8054 rc = fan_get_status_safe(&fan_control_resume_level);
8055 if (rc < 0)
8056 printk(TPACPI_NOTICE
8057 "failed to read fan level for later "
8058 "restore during resume: %d\n", rc);
8059
8060 /* if it is undefined, don't attempt to restore it.
8061 * KEEP THIS LAST */
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008062 if (tp_features.fan_ctrl_status_undef)
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008063 fan_control_resume_level = 0;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008064}
8065
8066static void fan_resume(void)
8067{
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008068 u8 current_level = 7;
8069 bool do_set = false;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008070 int rc;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008071
8072 /* DSDT *always* updates status on resume */
8073 tp_features.fan_ctrl_status_undef = 0;
8074
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008075 if (!fan_control_allowed ||
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008076 !fan_control_resume_level ||
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008077 (fan_get_status_safe(&current_level) < 0))
8078 return;
8079
8080 switch (fan_control_access_mode) {
8081 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008082 /* never decrease fan level */
8083 do_set = (fan_control_resume_level > current_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008084 break;
8085 case TPACPI_FAN_WR_ACPI_FANS:
8086 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008087 /* never decrease fan level, scale is:
8088 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
8089 *
8090 * We expect the firmware to set either 7 or AUTO, but we
8091 * handle FULLSPEED out of paranoia.
8092 *
8093 * So, we can safely only restore FULLSPEED or 7, anything
8094 * else could slow the fan. Restoring AUTO is useless, at
8095 * best that's exactly what the DSDT already set (it is the
8096 * slower it uses).
8097 *
8098 * Always keep in mind that the DSDT *will* have set the
8099 * fans to what the vendor supposes is the best level. We
8100 * muck with it only to speed the fan up.
8101 */
8102 if (fan_control_resume_level != 7 &&
8103 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
8104 return;
8105 else
8106 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
8107 (current_level != fan_control_resume_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008108 break;
8109 default:
8110 return;
8111 }
8112 if (do_set) {
8113 printk(TPACPI_NOTICE
8114 "restoring fan level to 0x%02x\n",
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008115 fan_control_resume_level);
8116 rc = fan_set_level_safe(fan_control_resume_level);
8117 if (rc < 0)
8118 printk(TPACPI_NOTICE
8119 "failed to restore fan level: %d\n", rc);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008120 }
8121}
8122
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008123static int fan_read(struct seq_file *m)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008124{
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008125 int rc;
8126 u8 status;
8127 unsigned int speed = 0;
8128
8129 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008130 case TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008131 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008132 rc = fan_get_status_safe(&status);
8133 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008134 return rc;
8135
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008136 seq_printf(m, "status:\t\t%s\n"
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008137 "level:\t\t%d\n",
8138 (status != 0) ? "enabled" : "disabled", status);
8139 break;
8140
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008141 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008142 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008143 rc = fan_get_status_safe(&status);
8144 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008145 return rc;
8146
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008147 seq_printf(m, "status:\t\t%s\n",
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008148 (status != 0) ? "enabled" : "disabled");
8149
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008150 rc = fan_get_speed(&speed);
8151 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008152 return rc;
8153
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008154 seq_printf(m, "speed:\t\t%d\n", speed);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008155
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008156 if (status & TP_EC_FAN_FULLSPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008157 /* Disengaged mode takes precedence */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008158 seq_printf(m, "level:\t\tdisengaged\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008159 else if (status & TP_EC_FAN_AUTO)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008160 seq_printf(m, "level:\t\tauto\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008161 else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008162 seq_printf(m, "level:\t\t%d\n", status);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008163 break;
8164
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008165 case TPACPI_FAN_NONE:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008166 default:
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008167 seq_printf(m, "status:\t\tnot supported\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008168 }
8169
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008170 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008171 seq_printf(m, "commands:\tlevel <level>");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008172
8173 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008174 case TPACPI_FAN_WR_ACPI_SFAN:
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008175 seq_printf(m, " (<level> is 0-7)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008176 break;
8177
8178 default:
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008179 seq_printf(m, " (<level> is 0-7, "
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008180 "auto, disengaged, full-speed)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008181 break;
8182 }
8183 }
8184
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008185 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008186 seq_printf(m, "commands:\tenable, disable\n"
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008187 "commands:\twatchdog <timeout> (<timeout> "
8188 "is 0 (off), 1-120 (seconds))\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008189
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008190 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008191 seq_printf(m, "commands:\tspeed <speed>"
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008192 " (<speed> is 0-65535)\n");
8193
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008194 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008195}
8196
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008197static int fan_write_cmd_level(const char *cmd, int *rc)
8198{
8199 int level;
8200
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02008201 if (strlencmp(cmd, "level auto") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008202 level = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008203 else if ((strlencmp(cmd, "level disengaged") == 0) |
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008204 (strlencmp(cmd, "level full-speed") == 0))
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008205 level = TP_EC_FAN_FULLSPEED;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02008206 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008207 return 0;
8208
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008209 *rc = fan_set_level_safe(level);
8210 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008211 printk(TPACPI_ERR "level command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008212 "access mode %d", fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008213 else if (!*rc)
8214 tpacpi_disclose_usertask("procfs fan",
8215 "set level to %d\n", level);
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008216
8217 return 1;
8218}
8219
8220static int fan_write_cmd_enable(const char *cmd, int *rc)
8221{
8222 if (strlencmp(cmd, "enable") != 0)
8223 return 0;
8224
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008225 *rc = fan_set_enable();
8226 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008227 printk(TPACPI_ERR "enable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008228 "access mode %d", fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008229 else if (!*rc)
8230 tpacpi_disclose_usertask("procfs fan", "enable\n");
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008231
8232 return 1;
8233}
8234
8235static int fan_write_cmd_disable(const char *cmd, int *rc)
8236{
8237 if (strlencmp(cmd, "disable") != 0)
8238 return 0;
8239
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008240 *rc = fan_set_disable();
8241 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008242 printk(TPACPI_ERR "disable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008243 "access mode %d", fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008244 else if (!*rc)
8245 tpacpi_disclose_usertask("procfs fan", "disable\n");
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008246
8247 return 1;
8248}
8249
8250static int fan_write_cmd_speed(const char *cmd, int *rc)
8251{
8252 int speed;
8253
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02008254 /* TODO:
8255 * Support speed <low> <medium> <high> ? */
8256
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008257 if (sscanf(cmd, "speed %d", &speed) != 1)
8258 return 0;
8259
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008260 *rc = fan_set_speed(speed);
8261 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008262 printk(TPACPI_ERR "speed command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008263 "access mode %d", fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008264 else if (!*rc)
8265 tpacpi_disclose_usertask("procfs fan",
8266 "set speed to %d\n", speed);
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008267
8268 return 1;
8269}
8270
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008271static int fan_write_cmd_watchdog(const char *cmd, int *rc)
8272{
8273 int interval;
8274
8275 if (sscanf(cmd, "watchdog %d", &interval) != 1)
8276 return 0;
8277
8278 if (interval < 0 || interval > 120)
8279 *rc = -EINVAL;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008280 else {
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008281 fan_watchdog_maxinterval = interval;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008282 tpacpi_disclose_usertask("procfs fan",
8283 "set watchdog timer to %d\n",
8284 interval);
8285 }
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008286
8287 return 1;
8288}
8289
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008290static int fan_write(char *buf)
8291{
8292 char *cmd;
8293 int rc = 0;
8294
8295 while (!rc && (cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008296 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008297 fan_write_cmd_level(cmd, &rc)) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008298 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008299 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008300 fan_write_cmd_disable(cmd, &rc) ||
8301 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008302 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008303 fan_write_cmd_speed(cmd, &rc))
8304 )
8305 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008306 else if (!rc)
8307 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008308 }
8309
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008310 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008311}
8312
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008313static struct ibm_struct fan_driver_data = {
8314 .name = "fan",
8315 .read = fan_read,
8316 .write = fan_write,
8317 .exit = fan_exit,
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008318 .suspend = fan_suspend,
8319 .resume = fan_resume,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008320};
8321
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008322/****************************************************************************
8323 ****************************************************************************
8324 *
8325 * Infrastructure
8326 *
8327 ****************************************************************************
8328 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07008329
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03008330/*
8331 * HKEY event callout for other subdrivers go here
8332 * (yes, it is ugly, but it is quick, safe, and gets the job done
8333 */
8334static void tpacpi_driver_event(const unsigned int hkey_event)
8335{
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00008336 if (ibm_backlight_device) {
8337 switch (hkey_event) {
8338 case TP_HKEY_EV_BRGHT_UP:
8339 case TP_HKEY_EV_BRGHT_DOWN:
8340 tpacpi_brightness_notify_change();
8341 }
8342 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02008343 if (alsa_card) {
8344 switch (hkey_event) {
8345 case TP_HKEY_EV_VOL_UP:
8346 case TP_HKEY_EV_VOL_DOWN:
8347 case TP_HKEY_EV_VOL_MUTE:
8348 volume_alsa_notify_change();
8349 }
8350 }
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03008351}
8352
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03008353static void hotkey_driver_event(const unsigned int scancode)
8354{
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03008355 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode);
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03008356}
8357
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008358/* sysfs name ---------------------------------------------------------- */
8359static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
8360 struct device_attribute *attr,
8361 char *buf)
8362{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008363 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008364}
8365
8366static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
8367 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
8368
8369/* --------------------------------------------------------------------- */
8370
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008371/* /proc support */
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -03008372static struct proc_dir_entry *proc_dir;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008373
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008374/*
8375 * Module and infrastructure proble, init and exit handling
8376 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008377
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008378static int force_load;
8379
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008380#ifdef CONFIG_THINKPAD_ACPI_DEBUG
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008381static const char * __init str_supported(int is_supported)
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008382{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008383 static char text_unsupported[] __initdata = "not supported";
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008384
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008385 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008386}
8387#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
8388
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008389static void ibm_exit(struct ibm_struct *ibm)
8390{
8391 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
8392
8393 list_del_init(&ibm->all_drivers);
8394
8395 if (ibm->flags.acpi_notify_installed) {
8396 dbg_printk(TPACPI_DBG_EXIT,
8397 "%s: acpi_remove_notify_handler\n", ibm->name);
8398 BUG_ON(!ibm->acpi);
8399 acpi_remove_notify_handler(*ibm->acpi->handle,
8400 ibm->acpi->type,
8401 dispatch_acpi_notify);
8402 ibm->flags.acpi_notify_installed = 0;
8403 ibm->flags.acpi_notify_installed = 0;
8404 }
8405
8406 if (ibm->flags.proc_created) {
8407 dbg_printk(TPACPI_DBG_EXIT,
8408 "%s: remove_proc_entry\n", ibm->name);
8409 remove_proc_entry(ibm->name, proc_dir);
8410 ibm->flags.proc_created = 0;
8411 }
8412
8413 if (ibm->flags.acpi_driver_registered) {
8414 dbg_printk(TPACPI_DBG_EXIT,
8415 "%s: acpi_bus_unregister_driver\n", ibm->name);
8416 BUG_ON(!ibm->acpi);
8417 acpi_bus_unregister_driver(ibm->acpi->driver);
8418 kfree(ibm->acpi->driver);
8419 ibm->acpi->driver = NULL;
8420 ibm->flags.acpi_driver_registered = 0;
8421 }
8422
8423 if (ibm->flags.init_called && ibm->exit) {
8424 ibm->exit();
8425 ibm->flags.init_called = 0;
8426 }
8427
8428 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
8429}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02008430
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008431static int __init ibm_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008432{
8433 int ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008434 struct ibm_struct *ibm = iibm->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008435 struct proc_dir_entry *entry;
8436
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008437 BUG_ON(ibm == NULL);
8438
8439 INIT_LIST_HEAD(&ibm->all_drivers);
8440
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03008441 if (ibm->flags.experimental && !experimental)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008442 return 0;
8443
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008444 dbg_printk(TPACPI_DBG_INIT,
8445 "probing for %s\n", ibm->name);
8446
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008447 if (iibm->init) {
8448 ret = iibm->init(iibm);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008449 if (ret > 0)
8450 return 0; /* probe failed */
8451 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008452 return ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008453
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03008454 ibm->flags.init_called = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008455 }
8456
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03008457 if (ibm->acpi) {
8458 if (ibm->acpi->hid) {
8459 ret = register_tpacpi_subdriver(ibm);
8460 if (ret)
8461 goto err_out;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008462 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03008463
8464 if (ibm->acpi->notify) {
8465 ret = setup_acpi_notify(ibm);
8466 if (ret == -ENODEV) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008467 printk(TPACPI_NOTICE "disabling subdriver %s\n",
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03008468 ibm->name);
8469 ret = 0;
8470 goto err_out;
8471 }
8472 if (ret < 0)
8473 goto err_out;
8474 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008475 }
8476
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008477 dbg_printk(TPACPI_DBG_INIT,
8478 "%s installed\n", ibm->name);
8479
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008480 if (ibm->read) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008481 mode_t mode;
8482
8483 mode = S_IRUGO;
8484 if (ibm->write)
8485 mode |= S_IWUSR;
8486 entry = proc_create_data(ibm->name, mode, proc_dir,
8487 &dispatch_proc_fops, ibm);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008488 if (!entry) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008489 printk(TPACPI_ERR "unable to create proc entry %s\n",
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008490 ibm->name);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008491 ret = -ENODEV;
8492 goto err_out;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008493 }
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03008494 ibm->flags.proc_created = 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008495 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008496
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008497 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
8498
Linus Torvalds1da177e2005-04-16 15:20:36 -07008499 return 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008500
8501err_out:
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008502 dbg_printk(TPACPI_DBG_INIT,
8503 "%s: at error exit path with result %d\n",
8504 ibm->name, ret);
8505
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008506 ibm_exit(ibm);
8507 return (ret < 0)? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008508}
8509
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008510/* Probing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008511
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008512static bool __pure __init tpacpi_is_fw_digit(const char c)
8513{
8514 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
8515}
8516
8517/* Most models: xxyTkkWW (#.##c); Ancient 570/600 and -SL lacks (#.##c) */
8518static bool __pure __init tpacpi_is_valid_fw_id(const char* const s,
8519 const char t)
8520{
8521 return s && strlen(s) >= 8 &&
8522 tpacpi_is_fw_digit(s[0]) &&
8523 tpacpi_is_fw_digit(s[1]) &&
8524 s[2] == t && s[3] == 'T' &&
8525 tpacpi_is_fw_digit(s[4]) &&
8526 tpacpi_is_fw_digit(s[5]) &&
8527 s[6] == 'W' && s[7] == 'W';
8528}
8529
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008530/* returns 0 - probe ok, or < 0 - probe error.
8531 * Probe ok doesn't mean thinkpad found.
8532 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
8533static int __must_check __init get_thinkpad_model_data(
8534 struct thinkpad_id_data *tp)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02008535{
Jeff Garzik18552562007-10-03 15:15:40 -04008536 const struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02008537 char ec_fw_string[18];
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008538 char const *s;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02008539
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008540 if (!tp)
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008541 return -EINVAL;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008542
8543 memset(tp, 0, sizeof(*tp));
8544
8545 if (dmi_name_in_vendors("IBM"))
8546 tp->vendor = PCI_VENDOR_ID_IBM;
8547 else if (dmi_name_in_vendors("LENOVO"))
8548 tp->vendor = PCI_VENDOR_ID_LENOVO;
8549 else
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008550 return 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008551
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008552 s = dmi_get_system_info(DMI_BIOS_VERSION);
8553 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
8554 if (s && !tp->bios_version_str)
8555 return -ENOMEM;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008556
8557 /* Really ancient ThinkPad 240X will fail this, which is fine */
8558 if (!tpacpi_is_valid_fw_id(tp->bios_version_str, 'E'))
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008559 return 0;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008560
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008561 tp->bios_model = tp->bios_version_str[0]
8562 | (tp->bios_version_str[1] << 8);
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008563 tp->bios_release = (tp->bios_version_str[4] << 8)
8564 | tp->bios_version_str[5];
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008565
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02008566 /*
8567 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
8568 * X32 or newer, all Z series; Some models must have an
8569 * up-to-date BIOS or they will not be detected.
8570 *
8571 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
8572 */
8573 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02008574 if (sscanf(dev->name,
8575 "IBM ThinkPad Embedded Controller -[%17c",
8576 ec_fw_string) == 1) {
8577 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
8578 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008579
8580 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008581 if (!tp->ec_version_str)
8582 return -ENOMEM;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008583
8584 if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) {
8585 tp->ec_model = ec_fw_string[0]
8586 | (ec_fw_string[1] << 8);
8587 tp->ec_release = (ec_fw_string[4] << 8)
8588 | ec_fw_string[5];
8589 } else {
8590 printk(TPACPI_NOTICE
8591 "ThinkPad firmware release %s "
8592 "doesn't match the known patterns\n",
8593 ec_fw_string);
8594 printk(TPACPI_NOTICE
8595 "please report this to %s\n",
8596 TPACPI_MAIL);
8597 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008598 break;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02008599 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02008600 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008601
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008602 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
8603 if (s && !strnicmp(s, "ThinkPad", 8)) {
8604 tp->model_str = kstrdup(s, GFP_KERNEL);
8605 if (!tp->model_str)
8606 return -ENOMEM;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008607 }
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03008608
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008609 s = dmi_get_system_info(DMI_PRODUCT_NAME);
8610 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
8611 if (s && !tp->nummodel_str)
8612 return -ENOMEM;
8613
8614 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008615}
8616
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008617static int __init probe_for_thinkpad(void)
8618{
8619 int is_thinkpad;
8620
8621 if (acpi_disabled)
8622 return -ENODEV;
8623
8624 /*
8625 * Non-ancient models have better DMI tagging, but very old models
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03008626 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008627 */
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03008628 is_thinkpad = (thinkpad_id.model_str != NULL) ||
8629 (thinkpad_id.ec_model != 0) ||
8630 tpacpi_is_fw_known();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008631
8632 /* ec is required because many other handles are relative to it */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008633 TPACPI_ACPIHANDLE_INIT(ec);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008634 if (!ec_handle) {
8635 if (is_thinkpad)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008636 printk(TPACPI_ERR
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008637 "Not yet supported ThinkPad detected!\n");
8638 return -ENODEV;
8639 }
8640
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03008641 if (!is_thinkpad && !force_load)
8642 return -ENODEV;
8643
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008644 return 0;
8645}
8646
8647
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008648/* Module init, exit, parameters */
8649
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008650static struct ibm_init_struct ibms_init[] __initdata = {
8651 {
8652 .init = thinkpad_acpi_driver_init,
8653 .data = &thinkpad_acpi_driver_data,
8654 },
8655 {
8656 .init = hotkey_init,
8657 .data = &hotkey_driver_data,
8658 },
8659 {
8660 .init = bluetooth_init,
8661 .data = &bluetooth_driver_data,
8662 },
8663 {
8664 .init = wan_init,
8665 .data = &wan_driver_data,
8666 },
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02008667 {
8668 .init = uwb_init,
8669 .data = &uwb_driver_data,
8670 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02008671#ifdef CONFIG_THINKPAD_ACPI_VIDEO
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008672 {
8673 .init = video_init,
8674 .data = &video_driver_data,
8675 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02008676#endif
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008677 {
8678 .init = light_init,
8679 .data = &light_driver_data,
8680 },
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008681 {
8682 .init = cmos_init,
8683 .data = &cmos_driver_data,
8684 },
8685 {
8686 .init = led_init,
8687 .data = &led_driver_data,
8688 },
8689 {
8690 .init = beep_init,
8691 .data = &beep_driver_data,
8692 },
8693 {
8694 .init = thermal_init,
8695 .data = &thermal_driver_data,
8696 },
8697 {
8698 .data = &ecdump_driver_data,
8699 },
8700 {
8701 .init = brightness_init,
8702 .data = &brightness_driver_data,
8703 },
8704 {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02008705 .init = volume_init,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008706 .data = &volume_driver_data,
8707 },
8708 {
8709 .init = fan_init,
8710 .data = &fan_driver_data,
8711 },
8712};
8713
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008714static int __init set_ibm_param(const char *val, struct kernel_param *kp)
8715{
8716 unsigned int i;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008717 struct ibm_struct *ibm;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008718
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02008719 if (!kp || !kp->name || !val)
8720 return -EINVAL;
8721
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008722 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
8723 ibm = ibms_init[i].data;
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02008724 WARN_ON(ibm == NULL);
8725
8726 if (!ibm || !ibm->name)
8727 continue;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008728
8729 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
8730 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008731 return -ENOSPC;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008732 strcpy(ibms_init[i].param, val);
8733 strcat(ibms_init[i].param, ",");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008734 return 0;
8735 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008736 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008737
8738 return -EINVAL;
8739}
8740
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008741module_param(experimental, int, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008742MODULE_PARM_DESC(experimental,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008743 "Enables experimental features when non-zero");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008744
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03008745module_param_named(debug, dbg_level, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008746MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03008747
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008748module_param(force_load, bool, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008749MODULE_PARM_DESC(force_load,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008750 "Attempts to load the driver even on a "
8751 "mis-identified ThinkPad when true");
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03008752
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008753module_param_named(fan_control, fan_control_allowed, bool, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008754MODULE_PARM_DESC(fan_control,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008755 "Enables setting fan parameters features when true");
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008756
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008757module_param_named(brightness_mode, brightness_mode, uint, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008758MODULE_PARM_DESC(brightness_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008759 "Selects brightness control strategy: "
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00008760 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03008761
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008762module_param(brightness_enable, uint, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008763MODULE_PARM_DESC(brightness_enable,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008764 "Enables backlight control when 1, disables when 0");
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02008765
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008766module_param(hotkey_report_mode, uint, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008767MODULE_PARM_DESC(hotkey_report_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008768 "used for backwards compatibility with userspace, "
8769 "see documentation");
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03008770
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02008771#ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02008772module_param_named(volume_mode, volume_mode, uint, 0444);
8773MODULE_PARM_DESC(volume_mode,
8774 "Selects volume control strategy: "
8775 "0=auto, 1=EC, 2=N/A, 3=EC+NVRAM");
8776
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02008777module_param_named(volume_capabilities, volume_capabilities, uint, 0444);
8778MODULE_PARM_DESC(volume_capabilities,
8779 "Selects the mixer capabilites: "
8780 "0=auto, 1=volume and mute, 2=mute only");
8781
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02008782module_param_named(volume_control, volume_control_allowed, bool, 0444);
8783MODULE_PARM_DESC(volume_control,
8784 "Enables software override for the console audio "
8785 "control when true");
8786
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02008787/* ALSA module API parameters */
8788module_param_named(index, alsa_index, int, 0444);
8789MODULE_PARM_DESC(index, "ALSA index for the ACPI EC Mixer");
8790module_param_named(id, alsa_id, charp, 0444);
8791MODULE_PARM_DESC(id, "ALSA id for the ACPI EC Mixer");
8792module_param_named(enable, alsa_enable, bool, 0444);
8793MODULE_PARM_DESC(enable, "Enable the ALSA interface for the ACPI EC Mixer");
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02008794#endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02008795
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008796#define TPACPI_PARAM(feature) \
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008797 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
Henrique de Moraes Holschuhcbb14842008-02-16 02:17:50 -02008798 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008799 "at module load, see documentation")
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008800
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008801TPACPI_PARAM(hotkey);
8802TPACPI_PARAM(bluetooth);
8803TPACPI_PARAM(video);
8804TPACPI_PARAM(light);
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008805TPACPI_PARAM(cmos);
8806TPACPI_PARAM(led);
8807TPACPI_PARAM(beep);
8808TPACPI_PARAM(ecdump);
8809TPACPI_PARAM(brightness);
8810TPACPI_PARAM(volume);
8811TPACPI_PARAM(fan);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008812
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02008813#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008814module_param(dbg_wlswemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02008815MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
8816module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
8817MODULE_PARM_DESC(wlsw_state,
8818 "Initial state of the emulated WLSW switch");
8819
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008820module_param(dbg_bluetoothemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02008821MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
8822module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
8823MODULE_PARM_DESC(bluetooth_state,
8824 "Initial state of the emulated bluetooth switch");
8825
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008826module_param(dbg_wwanemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02008827MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
8828module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
8829MODULE_PARM_DESC(wwan_state,
8830 "Initial state of the emulated WWAN switch");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02008831
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008832module_param(dbg_uwbemul, uint, 0444);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02008833MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
8834module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
8835MODULE_PARM_DESC(uwb_state,
8836 "Initial state of the emulated UWB switch");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02008837#endif
8838
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008839static void thinkpad_acpi_module_exit(void)
8840{
8841 struct ibm_struct *ibm, *itmp;
8842
8843 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
8844
8845 list_for_each_entry_safe_reverse(ibm, itmp,
8846 &tpacpi_all_drivers,
8847 all_drivers) {
8848 ibm_exit(ibm);
8849 }
8850
8851 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
8852
8853 if (tpacpi_inputdev) {
8854 if (tp_features.input_device_registered)
8855 input_unregister_device(tpacpi_inputdev);
8856 else
8857 input_free_device(tpacpi_inputdev);
8858 }
8859
8860 if (tpacpi_hwmon)
8861 hwmon_device_unregister(tpacpi_hwmon);
8862
8863 if (tp_features.sensors_pdev_attrs_registered)
8864 device_remove_file(&tpacpi_sensors_pdev->dev,
8865 &dev_attr_thinkpad_acpi_pdev_name);
8866 if (tpacpi_sensors_pdev)
8867 platform_device_unregister(tpacpi_sensors_pdev);
8868 if (tpacpi_pdev)
8869 platform_device_unregister(tpacpi_pdev);
8870
8871 if (tp_features.sensors_pdrv_attrs_registered)
8872 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
8873 if (tp_features.platform_drv_attrs_registered)
8874 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
8875
8876 if (tp_features.sensors_pdrv_registered)
8877 platform_driver_unregister(&tpacpi_hwmon_pdriver);
8878
8879 if (tp_features.platform_drv_registered)
8880 platform_driver_unregister(&tpacpi_pdriver);
8881
8882 if (proc_dir)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008883 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008884
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03008885 if (tpacpi_wq)
8886 destroy_workqueue(tpacpi_wq);
8887
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008888 kfree(thinkpad_id.bios_version_str);
8889 kfree(thinkpad_id.ec_version_str);
8890 kfree(thinkpad_id.model_str);
8891}
8892
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02008893
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03008894static int __init thinkpad_acpi_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008895{
8896 int ret, i;
8897
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03008898 tpacpi_lifecycle = TPACPI_LIFE_INIT;
8899
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03008900 /* Parameter checking */
8901 if (hotkey_report_mode > 2)
8902 return -EINVAL;
8903
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008904 /* Driver-level probe */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008905
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008906 ret = get_thinkpad_model_data(&thinkpad_id);
8907 if (ret) {
8908 printk(TPACPI_ERR
8909 "unable to get DMI data: %d\n", ret);
8910 thinkpad_acpi_module_exit();
8911 return ret;
8912 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008913 ret = probe_for_thinkpad();
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008914 if (ret) {
8915 thinkpad_acpi_module_exit();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008916 return ret;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008917 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008918
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008919 /* Driver initialization */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008920
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008921 TPACPI_ACPIHANDLE_INIT(ecrd);
8922 TPACPI_ACPIHANDLE_INIT(ecwr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008923
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03008924 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
8925 if (!tpacpi_wq) {
8926 thinkpad_acpi_module_exit();
8927 return -ENOMEM;
8928 }
8929
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008930 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008931 if (!proc_dir) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008932 printk(TPACPI_ERR
8933 "unable to create proc dir " TPACPI_PROC_DIR);
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03008934 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008935 return -ENODEV;
8936 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008937
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008938 ret = platform_driver_register(&tpacpi_pdriver);
8939 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008940 printk(TPACPI_ERR
8941 "unable to register main platform driver\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008942 thinkpad_acpi_module_exit();
8943 return ret;
8944 }
Henrique de Moraes Holschuhac363932007-07-27 17:04:40 -03008945 tp_features.platform_drv_registered = 1;
8946
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008947 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
8948 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008949 printk(TPACPI_ERR
8950 "unable to register hwmon platform driver\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008951 thinkpad_acpi_module_exit();
8952 return ret;
8953 }
8954 tp_features.sensors_pdrv_registered = 1;
8955
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03008956 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03008957 if (!ret) {
8958 tp_features.platform_drv_attrs_registered = 1;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008959 ret = tpacpi_create_driver_attributes(
8960 &tpacpi_hwmon_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03008961 }
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03008962 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008963 printk(TPACPI_ERR
8964 "unable to create sysfs driver attributes\n");
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03008965 thinkpad_acpi_module_exit();
8966 return ret;
8967 }
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03008968 tp_features.sensors_pdrv_attrs_registered = 1;
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03008969
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008970
8971 /* Device initialization */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008972 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008973 NULL, 0);
8974 if (IS_ERR(tpacpi_pdev)) {
8975 ret = PTR_ERR(tpacpi_pdev);
8976 tpacpi_pdev = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008977 printk(TPACPI_ERR "unable to register platform device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008978 thinkpad_acpi_module_exit();
8979 return ret;
8980 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008981 tpacpi_sensors_pdev = platform_device_register_simple(
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008982 TPACPI_HWMON_DRVR_NAME,
8983 -1, NULL, 0);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008984 if (IS_ERR(tpacpi_sensors_pdev)) {
8985 ret = PTR_ERR(tpacpi_sensors_pdev);
8986 tpacpi_sensors_pdev = NULL;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008987 printk(TPACPI_ERR
8988 "unable to register hwmon platform device\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008989 thinkpad_acpi_module_exit();
8990 return ret;
8991 }
8992 ret = device_create_file(&tpacpi_sensors_pdev->dev,
8993 &dev_attr_thinkpad_acpi_pdev_name);
8994 if (ret) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008995 printk(TPACPI_ERR
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008996 "unable to create sysfs hwmon device attributes\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008997 thinkpad_acpi_module_exit();
8998 return ret;
8999 }
9000 tp_features.sensors_pdev_attrs_registered = 1;
9001 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009002 if (IS_ERR(tpacpi_hwmon)) {
9003 ret = PTR_ERR(tpacpi_hwmon);
9004 tpacpi_hwmon = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009005 printk(TPACPI_ERR "unable to register hwmon device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009006 thinkpad_acpi_module_exit();
9007 return ret;
9008 }
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -03009009 mutex_init(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009010 tpacpi_inputdev = input_allocate_device();
9011 if (!tpacpi_inputdev) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009012 printk(TPACPI_ERR "unable to allocate input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009013 thinkpad_acpi_module_exit();
9014 return -ENOMEM;
9015 } else {
9016 /* Prepare input device, but don't register */
9017 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009018 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009019 tpacpi_inputdev->id.bustype = BUS_HOST;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03009020 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
9021 thinkpad_id.vendor :
9022 PCI_VENDOR_ID_IBM;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009023 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
9024 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
Henrique de Moraes Holschuhd112ef92009-12-09 01:36:26 +00009025 tpacpi_inputdev->dev.parent = &tpacpi_pdev->dev;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009026 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009027 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
9028 ret = ibm_init(&ibms_init[i]);
9029 if (ret >= 0 && *ibms_init[i].param)
9030 ret = ibms_init[i].data->write(ibms_init[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009031 if (ret < 0) {
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03009032 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07009033 return ret;
9034 }
9035 }
Henrique de Moraes Holschuhb589ea42010-02-25 21:28:58 -03009036
9037 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
9038
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009039 ret = input_register_device(tpacpi_inputdev);
9040 if (ret < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009041 printk(TPACPI_ERR "unable to register input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009042 thinkpad_acpi_module_exit();
9043 return ret;
9044 } else {
9045 tp_features.input_device_registered = 1;
9046 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009047
9048 return 0;
9049}
9050
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03009051MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
9052
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009053/*
Henrique de Moraes Holschuh922fe092009-04-14 02:44:13 +00009054 * This will autoload the driver in almost every ThinkPad
9055 * in widespread use.
9056 *
9057 * Only _VERY_ old models, like the 240, 240x and 570 lack
9058 * the HKEY event interface.
9059 */
9060MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
9061
9062/*
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009063 * DMI matching for module autoloading
9064 *
9065 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
9066 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
9067 *
9068 * Only models listed in thinkwiki will be supported, so add yours
9069 * if it is not there yet.
9070 */
9071#define IBM_BIOS_MODULE_ALIAS(__type) \
Mathieu Chouquet-Stringerb36a50f2009-03-14 16:35:26 +01009072 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009073
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009074/* Ancient thinkpad BIOSes have to be identified by
9075 * BIOS type or model number, and there are far less
9076 * BIOS types than model numbers... */
Henrique de Moraes Holschuh922fe092009-04-14 02:44:13 +00009077IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009078
Henrique de Moraes Holschuhf68f53a2009-04-14 02:44:12 +00009079MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
9080MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009081MODULE_DESCRIPTION(TPACPI_DESC);
9082MODULE_VERSION(TPACPI_VERSION);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009083MODULE_LICENSE("GPL");
9084
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03009085module_init(thinkpad_acpi_module_init);
9086module_exit(thinkpad_acpi_module_exit);