blob: b03bf5153b0f326d3e54a3544a4bc4356ed5747d [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090061#include <linux/slab.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020062
63#include <linux/nvram.h>
64#include <linux/proc_fs.h>
Alexey Dobriyan887965e2009-12-15 21:51:12 -020065#include <linux/seq_file.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020066#include <linux/sysfs.h>
67#include <linux/backlight.h>
68#include <linux/fb.h>
69#include <linux/platform_device.h>
70#include <linux/hwmon.h>
71#include <linux/hwmon-sysfs.h>
72#include <linux/input.h>
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -030073#include <linux/leds.h>
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -030074#include <linux/rfkill.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020075#include <asm/uaccess.h>
76
77#include <linux/dmi.h>
78#include <linux/jiffies.h>
79#include <linux/workqueue.h>
80
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -020081#include <sound/core.h>
82#include <sound/control.h>
83#include <sound/initval.h>
84
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020085#include <acpi/acpi_drivers.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020086
87#include <linux/pci_ids.h>
88
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020089
90/* ThinkPad CMOS commands */
91#define TP_CMOS_VOLUME_DOWN 0
92#define TP_CMOS_VOLUME_UP 1
93#define TP_CMOS_VOLUME_MUTE 2
94#define TP_CMOS_BRIGHTNESS_UP 4
95#define TP_CMOS_BRIGHTNESS_DOWN 5
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -030096#define TP_CMOS_THINKLIGHT_ON 12
97#define TP_CMOS_THINKLIGHT_OFF 13
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020098
99/* NVRAM Addresses */
100enum tp_nvram_addr {
101 TP_NVRAM_ADDR_HK2 = 0x57,
102 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
103 TP_NVRAM_ADDR_VIDEO = 0x59,
104 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
105 TP_NVRAM_ADDR_MIXER = 0x60,
106};
107
108/* NVRAM bit masks */
109enum {
110 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
111 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
112 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
113 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
114 TP_NVRAM_MASK_THINKLIGHT = 0x10,
115 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
116 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
117 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
118 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
119 TP_NVRAM_MASK_MUTE = 0x40,
120 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
121 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
122 TP_NVRAM_POS_LEVEL_VOLUME = 0,
123};
124
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -0300125/* Misc NVRAM-related */
126enum {
127 TP_NVRAM_LEVEL_VOLUME_MAX = 14,
128};
129
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200130/* ACPI HIDs */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200131#define TPACPI_ACPI_HKEY_HID "IBM0068"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200132
133/* Input IDs */
134#define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
135#define TPACPI_HKEY_INPUT_VERSION 0x4101
136
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -0200137/* ACPI \WGSV commands */
138enum {
139 TP_ACPI_WGSV_GET_STATE = 0x01, /* Get state information */
140 TP_ACPI_WGSV_PWR_ON_ON_RESUME = 0x02, /* Resume WWAN powered on */
141 TP_ACPI_WGSV_PWR_OFF_ON_RESUME = 0x03, /* Resume WWAN powered off */
142 TP_ACPI_WGSV_SAVE_STATE = 0x04, /* Save state for S4/S5 */
143};
144
145/* TP_ACPI_WGSV_GET_STATE bits */
146enum {
147 TP_ACPI_WGSV_STATE_WWANEXIST = 0x0001, /* WWAN hw available */
148 TP_ACPI_WGSV_STATE_WWANPWR = 0x0002, /* WWAN radio enabled */
149 TP_ACPI_WGSV_STATE_WWANPWRRES = 0x0004, /* WWAN state at resume */
150 TP_ACPI_WGSV_STATE_WWANBIOSOFF = 0x0008, /* WWAN disabled in BIOS */
151 TP_ACPI_WGSV_STATE_BLTHEXIST = 0x0001, /* BLTH hw available */
152 TP_ACPI_WGSV_STATE_BLTHPWR = 0x0002, /* BLTH radio enabled */
153 TP_ACPI_WGSV_STATE_BLTHPWRRES = 0x0004, /* BLTH state at resume */
154 TP_ACPI_WGSV_STATE_BLTHBIOSOFF = 0x0008, /* BLTH disabled in BIOS */
155 TP_ACPI_WGSV_STATE_UWBEXIST = 0x0010, /* UWB hw available */
156 TP_ACPI_WGSV_STATE_UWBPWR = 0x0020, /* UWB radio enabled */
157};
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200158
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -0300159/* HKEY events */
160enum tpacpi_hkey_event_t {
161 /* Hotkey-related */
162 TP_HKEY_EV_HOTKEY_BASE = 0x1001, /* first hotkey (FN+F1) */
163 TP_HKEY_EV_BRGHT_UP = 0x1010, /* Brightness up */
164 TP_HKEY_EV_BRGHT_DOWN = 0x1011, /* Brightness down */
165 TP_HKEY_EV_VOL_UP = 0x1015, /* Volume up or unmute */
166 TP_HKEY_EV_VOL_DOWN = 0x1016, /* Volume down or unmute */
167 TP_HKEY_EV_VOL_MUTE = 0x1017, /* Mixer output mute */
168
169 /* Reasons for waking up from S3/S4 */
170 TP_HKEY_EV_WKUP_S3_UNDOCK = 0x2304, /* undock requested, S3 */
171 TP_HKEY_EV_WKUP_S4_UNDOCK = 0x2404, /* undock requested, S4 */
172 TP_HKEY_EV_WKUP_S3_BAYEJ = 0x2305, /* bay ejection req, S3 */
173 TP_HKEY_EV_WKUP_S4_BAYEJ = 0x2405, /* bay ejection req, S4 */
174 TP_HKEY_EV_WKUP_S3_BATLOW = 0x2313, /* battery empty, S3 */
175 TP_HKEY_EV_WKUP_S4_BATLOW = 0x2413, /* battery empty, S4 */
176
177 /* Auto-sleep after eject request */
178 TP_HKEY_EV_BAYEJ_ACK = 0x3003, /* bay ejection complete */
179 TP_HKEY_EV_UNDOCK_ACK = 0x4003, /* undock complete */
180
181 /* Misc bay events */
182 TP_HKEY_EV_OPTDRV_EJ = 0x3006, /* opt. drive tray ejected */
183
184 /* User-interface events */
185 TP_HKEY_EV_LID_CLOSE = 0x5001, /* laptop lid closed */
186 TP_HKEY_EV_LID_OPEN = 0x5002, /* laptop lid opened */
187 TP_HKEY_EV_TABLET_TABLET = 0x5009, /* tablet swivel up */
188 TP_HKEY_EV_TABLET_NOTEBOOK = 0x500a, /* tablet swivel down */
189 TP_HKEY_EV_PEN_INSERTED = 0x500b, /* tablet pen inserted */
190 TP_HKEY_EV_PEN_REMOVED = 0x500c, /* tablet pen removed */
191 TP_HKEY_EV_BRGHT_CHANGED = 0x5010, /* backlight control event */
192
193 /* Thermal events */
194 TP_HKEY_EV_ALARM_BAT_HOT = 0x6011, /* battery too hot */
195 TP_HKEY_EV_ALARM_BAT_XHOT = 0x6012, /* battery critically hot */
196 TP_HKEY_EV_ALARM_SENSOR_HOT = 0x6021, /* sensor too hot */
197 TP_HKEY_EV_ALARM_SENSOR_XHOT = 0x6022, /* sensor critically hot */
198 TP_HKEY_EV_THM_TABLE_CHANGED = 0x6030, /* thermal table changed */
199
200 /* Misc */
201 TP_HKEY_EV_RFKILL_CHANGED = 0x7000, /* rfkill switch changed */
202};
203
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200204/****************************************************************************
205 * Main driver
206 */
207
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200208#define TPACPI_NAME "thinkpad"
209#define TPACPI_DESC "ThinkPad ACPI Extras"
210#define TPACPI_FILE TPACPI_NAME "_acpi"
211#define TPACPI_URL "http://ibm-acpi.sf.net/"
212#define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200213
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200214#define TPACPI_PROC_DIR "ibm"
215#define TPACPI_ACPI_EVENT_PREFIX "ibm"
216#define TPACPI_DRVR_NAME TPACPI_FILE
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300217#define TPACPI_DRVR_SHORTNAME "tpacpi"
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200218#define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200219
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300220#define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -0300221#define TPACPI_WORKQUEUE_NAME "ktpacpid"
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300222
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200223#define TPACPI_MAX_ACPI_ARGS 3
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200224
Henrique de Moraes Holschuh7ff8d622009-04-04 04:25:46 +0000225/* printk headers */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200226#define TPACPI_LOG TPACPI_FILE ": "
Henrique de Moraes Holschuh7ff8d622009-04-04 04:25:46 +0000227#define TPACPI_EMERG KERN_EMERG TPACPI_LOG
228#define TPACPI_ALERT KERN_ALERT TPACPI_LOG
229#define TPACPI_CRIT KERN_CRIT TPACPI_LOG
230#define TPACPI_ERR KERN_ERR TPACPI_LOG
231#define TPACPI_WARN KERN_WARNING TPACPI_LOG
232#define TPACPI_NOTICE KERN_NOTICE TPACPI_LOG
233#define TPACPI_INFO KERN_INFO TPACPI_LOG
234#define TPACPI_DEBUG KERN_DEBUG TPACPI_LOG
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200235
Henrique de Moraes Holschuh7ff8d622009-04-04 04:25:46 +0000236/* Debugging printk groups */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200237#define TPACPI_DBG_ALL 0xffff
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +0000238#define TPACPI_DBG_DISCLOSETASK 0x8000
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200239#define TPACPI_DBG_INIT 0x0001
240#define TPACPI_DBG_EXIT 0x0002
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +0000241#define TPACPI_DBG_RFKILL 0x0004
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +0000242#define TPACPI_DBG_HKEY 0x0008
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +0000243#define TPACPI_DBG_FAN 0x0010
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +0000244#define TPACPI_DBG_BRGHT 0x0020
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -0200245#define TPACPI_DBG_MIXER 0x0040
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200246
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200247#define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
248#define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
249#define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200250
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200251
252/****************************************************************************
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200253 * Driver-wide structs and misc. variables
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200254 */
255
256struct ibm_struct;
257
258struct tp_acpi_drv_struct {
259 const struct acpi_device_id *hid;
260 struct acpi_driver *driver;
261
262 void (*notify) (struct ibm_struct *, u32);
263 acpi_handle *handle;
264 u32 type;
265 struct acpi_device *device;
266};
267
268struct ibm_struct {
269 char *name;
270
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200271 int (*read) (struct seq_file *);
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200272 int (*write) (char *);
273 void (*exit) (void);
274 void (*resume) (void);
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200275 void (*suspend) (pm_message_t state);
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200276 void (*shutdown) (void);
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200277
278 struct list_head all_drivers;
279
280 struct tp_acpi_drv_struct *acpi;
281
282 struct {
283 u8 acpi_driver_registered:1;
284 u8 acpi_notify_installed:1;
285 u8 proc_created:1;
286 u8 init_called:1;
287 u8 experimental:1;
288 } flags;
289};
290
291struct ibm_init_struct {
292 char param[32];
293
294 int (*init) (struct ibm_init_struct *);
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -0300295 mode_t base_procfs_mode;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200296 struct ibm_struct *data;
297};
298
299static struct {
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200300 u32 bluetooth:1;
301 u32 hotkey:1;
302 u32 hotkey_mask:1;
303 u32 hotkey_wlsw:1;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -0200304 u32 hotkey_tablet:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200305 u32 light:1;
306 u32 light_status:1;
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -0300307 u32 bright_acpimode:1;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -0300308 u32 bright_unkfw:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200309 u32 wan:1;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -0200310 u32 uwb:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200311 u32 fan_ctrl_status_undef:1;
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -0300312 u32 second_fan:1;
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -0300313 u32 beep_needs_two_args:1;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -0200314 u32 mixer_no_level_control:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200315 u32 input_device_registered:1;
316 u32 platform_drv_registered:1;
317 u32 platform_drv_attrs_registered:1;
318 u32 sensors_pdrv_registered:1;
319 u32 sensors_pdrv_attrs_registered:1;
320 u32 sensors_pdev_attrs_registered:1;
321 u32 hotkey_poll_active:1;
322} tp_features;
323
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -0300324static struct {
325 u16 hotkey_mask_ff:1;
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -0200326 u16 volume_ctrl_forbidden:1;
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -0300327} tp_warned;
328
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200329struct thinkpad_id_data {
330 unsigned int vendor; /* ThinkPad vendor:
331 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
332
333 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
334 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
335
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -0300336 u16 bios_model; /* 1Y = 0x5931, 0 = unknown */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200337 u16 ec_model;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -0300338 u16 bios_release; /* 1ZETK1WW = 0x314b, 0 = unknown */
339 u16 ec_release;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200340
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -0300341 char *model_str; /* ThinkPad T43 */
342 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200343};
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200344static struct thinkpad_id_data thinkpad_id;
345
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300346static enum {
347 TPACPI_LIFE_INIT = 0,
348 TPACPI_LIFE_RUNNING,
349 TPACPI_LIFE_EXITING,
350} tpacpi_lifecycle;
351
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200352static int experimental;
353static u32 dbg_level;
354
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -0300355static struct workqueue_struct *tpacpi_wq;
356
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +0000357enum led_status_t {
358 TPACPI_LED_OFF = 0,
359 TPACPI_LED_ON,
360 TPACPI_LED_BLINK,
361};
362
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -0300363/* Special LED class that can defer work */
364struct tpacpi_led_classdev {
365 struct led_classdev led_classdev;
366 struct work_struct work;
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +0000367 enum led_status_t new_state;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -0300368 unsigned int led;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -0300369};
370
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -0300371/* brightness level capabilities */
372static unsigned int bright_maxlvl; /* 0 = unknown */
373
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200374#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
375static int dbg_wlswemul;
376static int tpacpi_wlsw_emulstate;
377static int dbg_bluetoothemul;
378static int tpacpi_bluetooth_emulstate;
379static int dbg_wwanemul;
380static int tpacpi_wwan_emulstate;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -0200381static int dbg_uwbemul;
382static int tpacpi_uwb_emulstate;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200383#endif
384
385
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000386/*************************************************************************
387 * Debugging helpers
388 */
389
390#define dbg_printk(a_dbg_level, format, arg...) \
391 do { if (dbg_level & (a_dbg_level)) \
392 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
393 } while (0)
394
395#ifdef CONFIG_THINKPAD_ACPI_DEBUG
396#define vdbg_printk dbg_printk
397static const char *str_supported(int is_supported);
398#else
399#define vdbg_printk(a_dbg_level, format, arg...) \
400 do { } while (0)
401#endif
402
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +0000403static void tpacpi_log_usertask(const char * const what)
404{
405 printk(TPACPI_DEBUG "%s: access by process with PID %d\n",
406 what, task_tgid_vnr(current));
407}
408
409#define tpacpi_disclose_usertask(what, format, arg...) \
410 do { \
411 if (unlikely( \
412 (dbg_level & TPACPI_DBG_DISCLOSETASK) && \
413 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
414 printk(TPACPI_DEBUG "%s: PID %d: " format, \
415 what, task_tgid_vnr(current), ## arg); \
416 } \
417 } while (0)
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000418
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -0300419/*
420 * Quirk handling helpers
421 *
422 * ThinkPad IDs and versions seen in the field so far
423 * are two-characters from the set [0-9A-Z], i.e. base 36.
424 *
425 * We use values well outside that range as specials.
426 */
427
428#define TPACPI_MATCH_ANY 0xffffU
429#define TPACPI_MATCH_UNKNOWN 0U
430
431/* TPID('1', 'Y') == 0x5931 */
432#define TPID(__c1, __c2) (((__c2) << 8) | (__c1))
433
434#define TPACPI_Q_IBM(__id1, __id2, __quirk) \
435 { .vendor = PCI_VENDOR_ID_IBM, \
436 .bios = TPID(__id1, __id2), \
437 .ec = TPACPI_MATCH_ANY, \
438 .quirks = (__quirk) }
439
440#define TPACPI_Q_LNV(__id1, __id2, __quirk) \
441 { .vendor = PCI_VENDOR_ID_LENOVO, \
442 .bios = TPID(__id1, __id2), \
443 .ec = TPACPI_MATCH_ANY, \
444 .quirks = (__quirk) }
445
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -0200446#define TPACPI_QEC_LNV(__id1, __id2, __quirk) \
447 { .vendor = PCI_VENDOR_ID_LENOVO, \
448 .bios = TPACPI_MATCH_ANY, \
449 .ec = TPID(__id1, __id2), \
450 .quirks = (__quirk) }
451
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -0300452struct tpacpi_quirk {
453 unsigned int vendor;
454 u16 bios;
455 u16 ec;
456 unsigned long quirks;
457};
458
459/**
460 * tpacpi_check_quirks() - search BIOS/EC version on a list
461 * @qlist: array of &struct tpacpi_quirk
462 * @qlist_size: number of elements in @qlist
463 *
464 * Iterates over a quirks list until one is found that matches the
465 * ThinkPad's vendor, BIOS and EC model.
466 *
467 * Returns 0 if nothing matches, otherwise returns the quirks field of
468 * the matching &struct tpacpi_quirk entry.
469 *
470 * The match criteria is: vendor, ec and bios much match.
471 */
472static unsigned long __init tpacpi_check_quirks(
473 const struct tpacpi_quirk *qlist,
474 unsigned int qlist_size)
475{
476 while (qlist_size) {
477 if ((qlist->vendor == thinkpad_id.vendor ||
478 qlist->vendor == TPACPI_MATCH_ANY) &&
479 (qlist->bios == thinkpad_id.bios_model ||
480 qlist->bios == TPACPI_MATCH_ANY) &&
481 (qlist->ec == thinkpad_id.ec_model ||
482 qlist->ec == TPACPI_MATCH_ANY))
483 return qlist->quirks;
484
485 qlist_size--;
486 qlist++;
487 }
488 return 0;
489}
490
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -0300491static inline bool __pure __init tpacpi_is_lenovo(void)
492{
493 return thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO;
494}
495
496static inline bool __pure __init tpacpi_is_ibm(void)
497{
498 return thinkpad_id.vendor == PCI_VENDOR_ID_IBM;
499}
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -0300500
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300501/****************************************************************************
502 ****************************************************************************
503 *
504 * ACPI Helpers and device model
505 *
506 ****************************************************************************
507 ****************************************************************************/
508
509/*************************************************************************
510 * ACPI basic handles
511 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -0300513static acpi_handle root_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200515#define TPACPI_HANDLE(object, parent, paths...) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 static acpi_handle object##_handle; \
517 static acpi_handle *object##_parent = &parent##_handle; \
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400518 static char *object##_path; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 static char *object##_paths[] = { paths }
520
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200521TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400522 "\\_SB.PCI.ISA.EC", /* 570 */
523 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
524 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
525 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
526 "\\_SB.PCI0.ICH3.EC0", /* R31 */
Henrique de Moraes Holschuha3189302010-05-16 19:45:26 -0300527 "\\_SB.PCI0.LPC0.EC", /* X100e and a few others */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400528 "\\_SB.PCI0.LPC.EC", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300529 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200531TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
532TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200534TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
535 /* T4x, X31, X40 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400536 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
537 "\\CMS", /* R40, R40e */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300538 ); /* all others */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400539
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200540TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400541 "^HKEY", /* R30, R31 */
542 "HKEY", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300543 ); /* 570 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400544
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -0200545TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
546 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
547 "\\_SB.PCI0.VID0", /* 770e */
548 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
Henrique de Moraes Holschuha3189302010-05-16 19:45:26 -0300549 "\\_SB.PCI0.AGP.VGA", /* X100e and a few others */
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -0200550 "\\_SB.PCI0.AGP.VID", /* all others */
551 ); /* R30, R31 */
552
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400553
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300554/*************************************************************************
555 * ACPI helpers
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -0200556 */
557
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558static int acpi_evalf(acpi_handle handle,
559 void *res, char *method, char *fmt, ...)
560{
561 char *fmt0 = fmt;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400562 struct acpi_object_list params;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200563 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400564 struct acpi_buffer result, *resultp;
565 union acpi_object out_obj;
566 acpi_status status;
567 va_list ap;
568 char res_type;
569 int success;
570 int quiet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
572 if (!*fmt) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200573 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 return 0;
575 }
576
577 if (*fmt == 'q') {
578 quiet = 1;
579 fmt++;
580 } else
581 quiet = 0;
582
583 res_type = *(fmt++);
584
585 params.count = 0;
586 params.pointer = &in_objs[0];
587
588 va_start(ap, fmt);
589 while (*fmt) {
590 char c = *(fmt++);
591 switch (c) {
592 case 'd': /* int */
593 in_objs[params.count].integer.value = va_arg(ap, int);
594 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
595 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400596 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 default:
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200598 printk(TPACPI_ERR "acpi_evalf() called "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 "with invalid format character '%c'\n", c);
600 return 0;
601 }
602 }
603 va_end(ap);
604
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400605 if (res_type != 'v') {
606 result.length = sizeof(out_obj);
607 result.pointer = &out_obj;
608 resultp = &result;
609 } else
610 resultp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400612 status = acpi_evaluate_object(handle, method, &params, resultp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
614 switch (res_type) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400615 case 'd': /* int */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 if (res)
617 *(int *)res = out_obj.integer.value;
618 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
619 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400620 case 'v': /* void */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 success = status == AE_OK;
622 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400623 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 default:
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200625 printk(TPACPI_ERR "acpi_evalf() called "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 "with invalid format character '%c'\n", res_type);
627 return 0;
628 }
629
630 if (!success && !quiet)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200631 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 method, fmt0, status);
633
634 return success;
635}
636
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200637static int acpi_ec_read(int i, u8 *p)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300638{
639 int v;
640
641 if (ecrd_handle) {
642 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
643 return 0;
644 *p = v;
645 } else {
646 if (ec_read(i, p) < 0)
647 return 0;
648 }
649
650 return 1;
651}
652
653static int acpi_ec_write(int i, u8 v)
654{
655 if (ecwr_handle) {
656 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
657 return 0;
658 } else {
659 if (ec_write(i, v) < 0)
660 return 0;
661 }
662
663 return 1;
664}
665
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -0300666static int issue_thinkpad_cmos_command(int cmos_cmd)
667{
668 if (!cmos_handle)
669 return -ENXIO;
670
671 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
672 return -EIO;
673
674 return 0;
675}
676
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300677/*************************************************************************
678 * ACPI device model
679 */
680
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200681#define TPACPI_ACPIHANDLE_INIT(object) \
682 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200683 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
684
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300685static void drv_acpi_handle_init(char *name,
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300686 acpi_handle *handle, acpi_handle parent,
687 char **paths, int num_paths, char **path)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300688{
689 int i;
690 acpi_status status;
691
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300692 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
693 name);
694
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300695 for (i = 0; i < num_paths; i++) {
696 status = acpi_get_handle(parent, paths[i], handle);
697 if (ACPI_SUCCESS(status)) {
698 *path = paths[i];
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300699 dbg_printk(TPACPI_DBG_INIT,
700 "Found ACPI handle %s for %s\n",
701 *path, name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300702 return;
703 }
704 }
705
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300706 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
707 name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300708 *handle = NULL;
709}
710
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300711static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300712{
713 struct ibm_struct *ibm = data;
714
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300715 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
716 return;
717
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300718 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300719 return;
720
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300721 ibm->acpi->notify(ibm, event);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300722}
723
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300724static int __init setup_acpi_notify(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300725{
726 acpi_status status;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300727 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300728
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300729 BUG_ON(!ibm->acpi);
730
731 if (!*ibm->acpi->handle)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300732 return 0;
733
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300734 vdbg_printk(TPACPI_DBG_INIT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300735 "setting up ACPI notify for %s\n", ibm->name);
736
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300737 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
738 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200739 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300740 ibm->name, rc);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300741 return -ENODEV;
742 }
743
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700744 ibm->acpi->device->driver_data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300745 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200746 TPACPI_ACPI_EVENT_PREFIX,
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -0300747 ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300748
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300749 status = acpi_install_notify_handler(*ibm->acpi->handle,
750 ibm->acpi->type, dispatch_acpi_notify, ibm);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300751 if (ACPI_FAILURE(status)) {
752 if (status == AE_ALREADY_EXISTS) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200753 printk(TPACPI_NOTICE
754 "another device driver is already "
755 "handling %s events\n", ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300756 } else {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200757 printk(TPACPI_ERR
758 "acpi_install_notify_handler(%s) failed: %d\n",
759 ibm->name, status);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300760 }
761 return -ENODEV;
762 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300763 ibm->flags.acpi_notify_installed = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300764 return 0;
765}
766
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300767static int __init tpacpi_device_add(struct acpi_device *device)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300768{
769 return 0;
770}
771
Henrique de Moraes Holschuh67001212007-04-21 11:08:25 -0300772static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300773{
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300774 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300775
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300776 dbg_printk(TPACPI_DBG_INIT,
777 "registering %s as an ACPI driver\n", ibm->name);
778
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300779 BUG_ON(!ibm->acpi);
780
781 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
782 if (!ibm->acpi->driver) {
Mariusz Kozlowski4f778b92008-10-18 14:23:53 -0300783 printk(TPACPI_ERR
784 "failed to allocate memory for ibm->acpi->driver\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300785 return -ENOMEM;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300786 }
787
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200788 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300789 ibm->acpi->driver->ids = ibm->acpi->hid;
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200790
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300791 ibm->acpi->driver->ops.add = &tpacpi_device_add;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300792
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300793 rc = acpi_bus_register_driver(ibm->acpi->driver);
794 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200795 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200796 ibm->name, rc);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300797 kfree(ibm->acpi->driver);
798 ibm->acpi->driver = NULL;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300799 } else if (!rc)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300800 ibm->flags.acpi_driver_registered = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300801
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300802 return rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300803}
804
805
806/****************************************************************************
807 ****************************************************************************
808 *
809 * Procfs Helpers
810 *
811 ****************************************************************************
812 ****************************************************************************/
813
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200814static int dispatch_proc_show(struct seq_file *m, void *v)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300815{
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200816 struct ibm_struct *ibm = m->private;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300817
818 if (!ibm || !ibm->read)
819 return -EINVAL;
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200820 return ibm->read(m);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300821}
822
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200823static int dispatch_proc_open(struct inode *inode, struct file *file)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300824{
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200825 return single_open(file, dispatch_proc_show, PDE(inode)->data);
826}
827
828static ssize_t dispatch_proc_write(struct file *file,
829 const char __user *userbuf,
830 size_t count, loff_t *pos)
831{
832 struct ibm_struct *ibm = PDE(file->f_path.dentry->d_inode)->data;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300833 char *kernbuf;
834 int ret;
835
836 if (!ibm || !ibm->write)
837 return -EINVAL;
Michael Buesch5b05d462009-08-01 12:04:19 -0300838 if (count > PAGE_SIZE - 2)
839 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300840
841 kernbuf = kmalloc(count + 2, GFP_KERNEL);
842 if (!kernbuf)
843 return -ENOMEM;
844
845 if (copy_from_user(kernbuf, userbuf, count)) {
846 kfree(kernbuf);
847 return -EFAULT;
848 }
849
850 kernbuf[count] = 0;
851 strcat(kernbuf, ",");
852 ret = ibm->write(kernbuf);
853 if (ret == 0)
854 ret = count;
855
856 kfree(kernbuf);
857
858 return ret;
859}
860
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200861static const struct file_operations dispatch_proc_fops = {
862 .owner = THIS_MODULE,
863 .open = dispatch_proc_open,
864 .read = seq_read,
865 .llseek = seq_lseek,
866 .release = single_release,
867 .write = dispatch_proc_write,
868};
869
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870static char *next_cmd(char **cmds)
871{
872 char *start = *cmds;
873 char *end;
874
875 while ((end = strchr(start, ',')) && end == start)
876 start = end + 1;
877
878 if (!end)
879 return NULL;
880
881 *end = 0;
882 *cmds = end + 1;
883 return start;
884}
885
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300886
887/****************************************************************************
888 ****************************************************************************
889 *
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300890 * Device model: input, hwmon and platform
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300891 *
892 ****************************************************************************
893 ****************************************************************************/
894
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -0300895static struct platform_device *tpacpi_pdev;
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300896static struct platform_device *tpacpi_sensors_pdev;
Tony Jones1beeffe2007-08-20 13:46:20 -0700897static struct device *tpacpi_hwmon;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300898static struct input_dev *tpacpi_inputdev;
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -0300899static struct mutex tpacpi_inputdev_send_mutex;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200900static LIST_HEAD(tpacpi_all_drivers);
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300901
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200902static int tpacpi_suspend_handler(struct platform_device *pdev,
903 pm_message_t state)
904{
905 struct ibm_struct *ibm, *itmp;
906
907 list_for_each_entry_safe(ibm, itmp,
908 &tpacpi_all_drivers,
909 all_drivers) {
910 if (ibm->suspend)
911 (ibm->suspend)(state);
912 }
913
914 return 0;
915}
916
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300917static int tpacpi_resume_handler(struct platform_device *pdev)
918{
919 struct ibm_struct *ibm, *itmp;
920
921 list_for_each_entry_safe(ibm, itmp,
922 &tpacpi_all_drivers,
923 all_drivers) {
924 if (ibm->resume)
925 (ibm->resume)();
926 }
927
928 return 0;
929}
930
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200931static void tpacpi_shutdown_handler(struct platform_device *pdev)
932{
933 struct ibm_struct *ibm, *itmp;
934
935 list_for_each_entry_safe(ibm, itmp,
936 &tpacpi_all_drivers,
937 all_drivers) {
938 if (ibm->shutdown)
939 (ibm->shutdown)();
940 }
941}
942
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300943static struct platform_driver tpacpi_pdriver = {
944 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200945 .name = TPACPI_DRVR_NAME,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300946 .owner = THIS_MODULE,
947 },
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200948 .suspend = tpacpi_suspend_handler,
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300949 .resume = tpacpi_resume_handler,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200950 .shutdown = tpacpi_shutdown_handler,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300951};
952
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300953static struct platform_driver tpacpi_hwmon_pdriver = {
954 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200955 .name = TPACPI_HWMON_DRVR_NAME,
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300956 .owner = THIS_MODULE,
957 },
958};
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300959
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -0300960/*************************************************************************
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300961 * sysfs support helpers
962 */
963
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200964struct attribute_set {
965 unsigned int members, max_members;
966 struct attribute_group group;
967};
968
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300969struct attribute_set_obj {
970 struct attribute_set s;
971 struct attribute *a;
972} __attribute__((packed));
973
974static struct attribute_set *create_attr_set(unsigned int max_members,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200975 const char *name)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300976{
977 struct attribute_set_obj *sobj;
978
979 if (max_members == 0)
980 return NULL;
981
982 /* Allocates space for implicit NULL at the end too */
983 sobj = kzalloc(sizeof(struct attribute_set_obj) +
984 max_members * sizeof(struct attribute *),
985 GFP_KERNEL);
986 if (!sobj)
987 return NULL;
988 sobj->s.max_members = max_members;
989 sobj->s.group.attrs = &sobj->a;
990 sobj->s.group.name = name;
991
992 return &sobj->s;
993}
994
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200995#define destroy_attr_set(_set) \
996 kfree(_set);
997
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300998/* not multi-threaded safe, use it in a single thread per set */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200999static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001000{
1001 if (!s || !attr)
1002 return -EINVAL;
1003
1004 if (s->members >= s->max_members)
1005 return -ENOMEM;
1006
1007 s->group.attrs[s->members] = attr;
1008 s->members++;
1009
1010 return 0;
1011}
1012
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001013static int add_many_to_attr_set(struct attribute_set *s,
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001014 struct attribute **attr,
1015 unsigned int count)
1016{
1017 int i, res;
1018
1019 for (i = 0; i < count; i++) {
1020 res = add_to_attr_set(s, attr[i]);
1021 if (res)
1022 return res;
1023 }
1024
1025 return 0;
1026}
1027
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001028static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001029{
1030 sysfs_remove_group(kobj, &s->group);
1031 destroy_attr_set(s);
1032}
1033
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001034#define register_attr_set_with_sysfs(_attr_set, _kobj) \
1035 sysfs_create_group(_kobj, &_attr_set->group)
1036
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001037static int parse_strtoul(const char *buf,
1038 unsigned long max, unsigned long *value)
1039{
1040 char *endp;
1041
André Goddard Rosae7d28602009-12-14 18:01:06 -08001042 *value = simple_strtoul(skip_spaces(buf), &endp, 0);
1043 endp = skip_spaces(endp);
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001044 if (*endp || *value > max)
1045 return -EINVAL;
1046
1047 return 0;
1048}
1049
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03001050static void tpacpi_disable_brightness_delay(void)
1051{
1052 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
1053 printk(TPACPI_NOTICE
1054 "ACPI backlight control delay disabled\n");
1055}
1056
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00001057static void printk_deprecated_attribute(const char * const what,
1058 const char * const details)
1059{
1060 tpacpi_log_usertask("deprecated sysfs attribute");
1061 printk(TPACPI_WARN "WARNING: sysfs attribute %s is deprecated and "
1062 "will be removed. %s\n",
1063 what, details);
1064}
1065
Johannes Berg19d337d2009-06-02 13:01:37 +02001066/*************************************************************************
1067 * rfkill and radio control support helpers
1068 */
1069
1070/*
1071 * ThinkPad-ACPI firmware handling model:
1072 *
1073 * WLSW (master wireless switch) is event-driven, and is common to all
1074 * firmware-controlled radios. It cannot be controlled, just monitored,
1075 * as expected. It overrides all radio state in firmware
1076 *
1077 * The kernel, a masked-off hotkey, and WLSW can change the radio state
1078 * (TODO: verify how WLSW interacts with the returned radio state).
1079 *
1080 * The only time there are shadow radio state changes, is when
1081 * masked-off hotkeys are used.
1082 */
1083
1084/*
1085 * Internal driver API for radio state:
1086 *
1087 * int: < 0 = error, otherwise enum tpacpi_rfkill_state
1088 * bool: true means radio blocked (off)
1089 */
1090enum tpacpi_rfkill_state {
1091 TPACPI_RFK_RADIO_OFF = 0,
1092 TPACPI_RFK_RADIO_ON
1093};
1094
1095/* rfkill switches */
1096enum tpacpi_rfk_id {
1097 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
1098 TPACPI_RFK_WWAN_SW_ID,
1099 TPACPI_RFK_UWB_SW_ID,
1100 TPACPI_RFK_SW_MAX
1101};
1102
1103static const char *tpacpi_rfkill_names[] = {
1104 [TPACPI_RFK_BLUETOOTH_SW_ID] = "bluetooth",
1105 [TPACPI_RFK_WWAN_SW_ID] = "wwan",
1106 [TPACPI_RFK_UWB_SW_ID] = "uwb",
1107 [TPACPI_RFK_SW_MAX] = NULL
1108};
1109
1110/* ThinkPad-ACPI rfkill subdriver */
1111struct tpacpi_rfk {
1112 struct rfkill *rfkill;
1113 enum tpacpi_rfk_id id;
1114 const struct tpacpi_rfk_ops *ops;
1115};
1116
1117struct tpacpi_rfk_ops {
1118 /* firmware interface */
1119 int (*get_status)(void);
1120 int (*set_status)(const enum tpacpi_rfkill_state);
1121};
1122
1123static struct tpacpi_rfk *tpacpi_rfkill_switches[TPACPI_RFK_SW_MAX];
1124
1125/* Query FW and update rfkill sw state for a given rfkill switch */
1126static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk *tp_rfk)
1127{
1128 int status;
1129
1130 if (!tp_rfk)
1131 return -ENODEV;
1132
1133 status = (tp_rfk->ops->get_status)();
1134 if (status < 0)
1135 return status;
1136
1137 rfkill_set_sw_state(tp_rfk->rfkill,
1138 (status == TPACPI_RFK_RADIO_OFF));
1139
1140 return status;
1141}
1142
1143/* Query FW and update rfkill sw state for all rfkill switches */
1144static void tpacpi_rfk_update_swstate_all(void)
1145{
1146 unsigned int i;
1147
1148 for (i = 0; i < TPACPI_RFK_SW_MAX; i++)
1149 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[i]);
1150}
1151
1152/*
1153 * Sync the HW-blocking state of all rfkill switches,
1154 * do notice it causes the rfkill core to schedule uevents
1155 */
1156static void tpacpi_rfk_update_hwblock_state(bool blocked)
1157{
1158 unsigned int i;
1159 struct tpacpi_rfk *tp_rfk;
1160
1161 for (i = 0; i < TPACPI_RFK_SW_MAX; i++) {
1162 tp_rfk = tpacpi_rfkill_switches[i];
1163 if (tp_rfk) {
1164 if (rfkill_set_hw_state(tp_rfk->rfkill,
1165 blocked)) {
1166 /* ignore -- we track sw block */
1167 }
1168 }
1169 }
1170}
1171
1172/* Call to get the WLSW state from the firmware */
1173static int hotkey_get_wlsw(void);
1174
1175/* Call to query WLSW state and update all rfkill switches */
1176static bool tpacpi_rfk_check_hwblock_state(void)
1177{
1178 int res = hotkey_get_wlsw();
1179 int hw_blocked;
1180
1181 /* When unknown or unsupported, we have to assume it is unblocked */
1182 if (res < 0)
1183 return false;
1184
1185 hw_blocked = (res == TPACPI_RFK_RADIO_OFF);
1186 tpacpi_rfk_update_hwblock_state(hw_blocked);
1187
1188 return hw_blocked;
1189}
1190
1191static int tpacpi_rfk_hook_set_block(void *data, bool blocked)
1192{
1193 struct tpacpi_rfk *tp_rfk = data;
1194 int res;
1195
1196 dbg_printk(TPACPI_DBG_RFKILL,
1197 "request to change radio state to %s\n",
1198 blocked ? "blocked" : "unblocked");
1199
1200 /* try to set radio state */
1201 res = (tp_rfk->ops->set_status)(blocked ?
1202 TPACPI_RFK_RADIO_OFF : TPACPI_RFK_RADIO_ON);
1203
1204 /* and update the rfkill core with whatever the FW really did */
1205 tpacpi_rfk_update_swstate(tp_rfk);
1206
1207 return (res < 0) ? res : 0;
1208}
1209
1210static const struct rfkill_ops tpacpi_rfk_rfkill_ops = {
1211 .set_block = tpacpi_rfk_hook_set_block,
1212};
1213
1214static int __init tpacpi_new_rfkill(const enum tpacpi_rfk_id id,
1215 const struct tpacpi_rfk_ops *tp_rfkops,
1216 const enum rfkill_type rfktype,
1217 const char *name,
1218 const bool set_default)
1219{
1220 struct tpacpi_rfk *atp_rfk;
1221 int res;
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001222 bool sw_state = false;
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001223 bool hw_state;
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001224 int sw_status;
Johannes Berg19d337d2009-06-02 13:01:37 +02001225
1226 BUG_ON(id >= TPACPI_RFK_SW_MAX || tpacpi_rfkill_switches[id]);
1227
Johannes Berg19d337d2009-06-02 13:01:37 +02001228 atp_rfk = kzalloc(sizeof(struct tpacpi_rfk), GFP_KERNEL);
1229 if (atp_rfk)
1230 atp_rfk->rfkill = rfkill_alloc(name,
1231 &tpacpi_pdev->dev,
1232 rfktype,
1233 &tpacpi_rfk_rfkill_ops,
1234 atp_rfk);
1235 if (!atp_rfk || !atp_rfk->rfkill) {
1236 printk(TPACPI_ERR
1237 "failed to allocate memory for rfkill class\n");
1238 kfree(atp_rfk);
1239 return -ENOMEM;
1240 }
1241
1242 atp_rfk->id = id;
1243 atp_rfk->ops = tp_rfkops;
1244
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001245 sw_status = (tp_rfkops->get_status)();
1246 if (sw_status < 0) {
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001247 printk(TPACPI_ERR
1248 "failed to read initial state for %s, error %d\n",
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001249 name, sw_status);
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001250 } else {
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001251 sw_state = (sw_status == TPACPI_RFK_RADIO_OFF);
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001252 if (set_default) {
1253 /* try to keep the initial state, since we ask the
1254 * firmware to preserve it across S5 in NVRAM */
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001255 rfkill_init_sw_state(atp_rfk->rfkill, sw_state);
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001256 }
1257 }
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001258 hw_state = tpacpi_rfk_check_hwblock_state();
1259 rfkill_set_hw_state(atp_rfk->rfkill, hw_state);
Johannes Berg19d337d2009-06-02 13:01:37 +02001260
1261 res = rfkill_register(atp_rfk->rfkill);
1262 if (res < 0) {
1263 printk(TPACPI_ERR
1264 "failed to register %s rfkill switch: %d\n",
1265 name, res);
1266 rfkill_destroy(atp_rfk->rfkill);
1267 kfree(atp_rfk);
1268 return res;
1269 }
1270
1271 tpacpi_rfkill_switches[id] = atp_rfk;
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001272
1273 printk(TPACPI_INFO "rfkill switch %s: radio is %sblocked\n",
1274 name, (sw_state || hw_state) ? "" : "un");
Johannes Berg19d337d2009-06-02 13:01:37 +02001275 return 0;
1276}
1277
1278static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id)
1279{
1280 struct tpacpi_rfk *tp_rfk;
1281
1282 BUG_ON(id >= TPACPI_RFK_SW_MAX);
1283
1284 tp_rfk = tpacpi_rfkill_switches[id];
1285 if (tp_rfk) {
1286 rfkill_unregister(tp_rfk->rfkill);
Corentin Chary5f0dadb2009-09-14 12:43:52 +02001287 rfkill_destroy(tp_rfk->rfkill);
Johannes Berg19d337d2009-06-02 13:01:37 +02001288 tpacpi_rfkill_switches[id] = NULL;
1289 kfree(tp_rfk);
1290 }
1291}
1292
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00001293static void printk_deprecated_rfkill_attribute(const char * const what)
1294{
1295 printk_deprecated_attribute(what,
1296 "Please switch to generic rfkill before year 2010");
1297}
1298
Johannes Berg19d337d2009-06-02 13:01:37 +02001299/* sysfs <radio> enable ------------------------------------------------ */
1300static ssize_t tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id,
1301 struct device_attribute *attr,
1302 char *buf)
1303{
1304 int status;
1305
1306 printk_deprecated_rfkill_attribute(attr->attr.name);
1307
1308 /* This is in the ABI... */
1309 if (tpacpi_rfk_check_hwblock_state()) {
1310 status = TPACPI_RFK_RADIO_OFF;
1311 } else {
1312 status = tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1313 if (status < 0)
1314 return status;
1315 }
1316
1317 return snprintf(buf, PAGE_SIZE, "%d\n",
1318 (status == TPACPI_RFK_RADIO_ON) ? 1 : 0);
1319}
1320
1321static ssize_t tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id,
1322 struct device_attribute *attr,
1323 const char *buf, size_t count)
1324{
1325 unsigned long t;
1326 int res;
1327
1328 printk_deprecated_rfkill_attribute(attr->attr.name);
1329
1330 if (parse_strtoul(buf, 1, &t))
1331 return -EINVAL;
1332
1333 tpacpi_disclose_usertask(attr->attr.name, "set to %ld\n", t);
1334
1335 /* This is in the ABI... */
1336 if (tpacpi_rfk_check_hwblock_state() && !!t)
1337 return -EPERM;
1338
1339 res = tpacpi_rfkill_switches[id]->ops->set_status((!!t) ?
1340 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF);
1341 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1342
1343 return (res < 0) ? res : count;
1344}
1345
1346/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001347static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id, struct seq_file *m)
Johannes Berg19d337d2009-06-02 13:01:37 +02001348{
Johannes Berg19d337d2009-06-02 13:01:37 +02001349 if (id >= TPACPI_RFK_SW_MAX)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001350 seq_printf(m, "status:\t\tnot supported\n");
Johannes Berg19d337d2009-06-02 13:01:37 +02001351 else {
1352 int status;
1353
1354 /* This is in the ABI... */
1355 if (tpacpi_rfk_check_hwblock_state()) {
1356 status = TPACPI_RFK_RADIO_OFF;
1357 } else {
1358 status = tpacpi_rfk_update_swstate(
1359 tpacpi_rfkill_switches[id]);
1360 if (status < 0)
1361 return status;
1362 }
1363
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001364 seq_printf(m, "status:\t\t%s\n",
Johannes Berg19d337d2009-06-02 13:01:37 +02001365 (status == TPACPI_RFK_RADIO_ON) ?
1366 "enabled" : "disabled");
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001367 seq_printf(m, "commands:\tenable, disable\n");
Johannes Berg19d337d2009-06-02 13:01:37 +02001368 }
1369
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001370 return 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02001371}
1372
1373static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id, char *buf)
1374{
1375 char *cmd;
1376 int status = -1;
1377 int res = 0;
1378
1379 if (id >= TPACPI_RFK_SW_MAX)
1380 return -ENODEV;
1381
1382 while ((cmd = next_cmd(&buf))) {
1383 if (strlencmp(cmd, "enable") == 0)
1384 status = TPACPI_RFK_RADIO_ON;
1385 else if (strlencmp(cmd, "disable") == 0)
1386 status = TPACPI_RFK_RADIO_OFF;
1387 else
1388 return -EINVAL;
1389 }
1390
1391 if (status != -1) {
1392 tpacpi_disclose_usertask("procfs", "attempt to %s %s\n",
1393 (status == TPACPI_RFK_RADIO_ON) ?
1394 "enable" : "disable",
1395 tpacpi_rfkill_names[id]);
1396 res = (tpacpi_rfkill_switches[id]->ops->set_status)(status);
1397 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1398 }
1399
1400 return res;
1401}
1402
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001403/*************************************************************************
1404 * thinkpad-acpi driver attributes
1405 */
1406
1407/* interface_version --------------------------------------------------- */
1408static ssize_t tpacpi_driver_interface_version_show(
1409 struct device_driver *drv,
1410 char *buf)
1411{
1412 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1413}
1414
1415static DRIVER_ATTR(interface_version, S_IRUGO,
1416 tpacpi_driver_interface_version_show, NULL);
1417
1418/* debug_level --------------------------------------------------------- */
1419static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1420 char *buf)
1421{
1422 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1423}
1424
1425static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1426 const char *buf, size_t count)
1427{
1428 unsigned long t;
1429
1430 if (parse_strtoul(buf, 0xffff, &t))
1431 return -EINVAL;
1432
1433 dbg_level = t;
1434
1435 return count;
1436}
1437
1438static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1439 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1440
1441/* version ------------------------------------------------------------- */
1442static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1443 char *buf)
1444{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001445 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1446 TPACPI_DESC, TPACPI_VERSION);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001447}
1448
1449static DRIVER_ATTR(version, S_IRUGO,
1450 tpacpi_driver_version_show, NULL);
1451
1452/* --------------------------------------------------------------------- */
1453
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001454#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1455
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001456/* wlsw_emulstate ------------------------------------------------------ */
1457static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1458 char *buf)
1459{
1460 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1461}
1462
1463static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1464 const char *buf, size_t count)
1465{
1466 unsigned long t;
1467
1468 if (parse_strtoul(buf, 1, &t))
1469 return -EINVAL;
1470
Johannes Berg19d337d2009-06-02 13:01:37 +02001471 if (tpacpi_wlsw_emulstate != !!t) {
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001472 tpacpi_wlsw_emulstate = !!t;
Johannes Berg19d337d2009-06-02 13:01:37 +02001473 tpacpi_rfk_update_hwblock_state(!t); /* negative logic */
1474 }
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001475
1476 return count;
1477}
1478
1479static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1480 tpacpi_driver_wlsw_emulstate_show,
1481 tpacpi_driver_wlsw_emulstate_store);
1482
1483/* bluetooth_emulstate ------------------------------------------------- */
1484static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1485 struct device_driver *drv,
1486 char *buf)
1487{
1488 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1489}
1490
1491static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1492 struct device_driver *drv,
1493 const char *buf, size_t count)
1494{
1495 unsigned long t;
1496
1497 if (parse_strtoul(buf, 1, &t))
1498 return -EINVAL;
1499
1500 tpacpi_bluetooth_emulstate = !!t;
1501
1502 return count;
1503}
1504
1505static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1506 tpacpi_driver_bluetooth_emulstate_show,
1507 tpacpi_driver_bluetooth_emulstate_store);
1508
1509/* wwan_emulstate ------------------------------------------------- */
1510static ssize_t tpacpi_driver_wwan_emulstate_show(
1511 struct device_driver *drv,
1512 char *buf)
1513{
1514 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1515}
1516
1517static ssize_t tpacpi_driver_wwan_emulstate_store(
1518 struct device_driver *drv,
1519 const char *buf, size_t count)
1520{
1521 unsigned long t;
1522
1523 if (parse_strtoul(buf, 1, &t))
1524 return -EINVAL;
1525
1526 tpacpi_wwan_emulstate = !!t;
1527
1528 return count;
1529}
1530
1531static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1532 tpacpi_driver_wwan_emulstate_show,
1533 tpacpi_driver_wwan_emulstate_store);
1534
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001535/* uwb_emulstate ------------------------------------------------- */
1536static ssize_t tpacpi_driver_uwb_emulstate_show(
1537 struct device_driver *drv,
1538 char *buf)
1539{
1540 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1541}
1542
1543static ssize_t tpacpi_driver_uwb_emulstate_store(
1544 struct device_driver *drv,
1545 const char *buf, size_t count)
1546{
1547 unsigned long t;
1548
1549 if (parse_strtoul(buf, 1, &t))
1550 return -EINVAL;
1551
1552 tpacpi_uwb_emulstate = !!t;
1553
1554 return count;
1555}
1556
1557static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
1558 tpacpi_driver_uwb_emulstate_show,
1559 tpacpi_driver_uwb_emulstate_store);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001560#endif
1561
1562/* --------------------------------------------------------------------- */
1563
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001564static struct driver_attribute *tpacpi_driver_attributes[] = {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001565 &driver_attr_debug_level, &driver_attr_version,
1566 &driver_attr_interface_version,
1567};
1568
1569static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1570{
1571 int i, res;
1572
1573 i = 0;
1574 res = 0;
1575 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1576 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1577 i++;
1578 }
1579
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001580#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1581 if (!res && dbg_wlswemul)
1582 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1583 if (!res && dbg_bluetoothemul)
1584 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1585 if (!res && dbg_wwanemul)
1586 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001587 if (!res && dbg_uwbemul)
1588 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001589#endif
1590
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001591 return res;
1592}
1593
1594static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1595{
1596 int i;
1597
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001598 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001599 driver_remove_file(drv, tpacpi_driver_attributes[i]);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001600
1601#ifdef THINKPAD_ACPI_DEBUGFACILITIES
1602 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1603 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1604 driver_remove_file(drv, &driver_attr_wwan_emulstate);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001605 driver_remove_file(drv, &driver_attr_uwb_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001606#endif
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001607}
1608
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001609/*************************************************************************
1610 * Firmware Data
1611 */
1612
1613/*
1614 * Table of recommended minimum BIOS versions
1615 *
1616 * Reasons for listing:
Uwe Kleine-König9ddc5b62010-01-20 17:02:24 +01001617 * 1. Stable BIOS, listed because the unknown amount of
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001618 * bugs and bad ACPI behaviour on older versions
1619 *
1620 * 2. BIOS or EC fw with known bugs that trigger on Linux
1621 *
1622 * 3. BIOS with known reduced functionality in older versions
1623 *
1624 * We recommend the latest BIOS and EC version.
1625 * We only support the latest BIOS and EC fw version as a rule.
1626 *
1627 * Sources: IBM ThinkPad Public Web Documents (update changelogs),
1628 * Information from users in ThinkWiki
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03001629 *
1630 * WARNING: we use this table also to detect that the machine is
1631 * a ThinkPad in some cases, so don't remove entries lightly.
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001632 */
1633
1634#define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1635 { .vendor = (__v), \
1636 .bios = TPID(__id1, __id2), \
1637 .ec = TPACPI_MATCH_ANY, \
1638 .quirks = TPACPI_MATCH_ANY << 16 \
1639 | (__bv1) << 8 | (__bv2) }
1640
1641#define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001642 __eid, __ev1, __ev2) \
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001643 { .vendor = (__v), \
1644 .bios = TPID(__bid1, __bid2), \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001645 .ec = __eid, \
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001646 .quirks = (__ev1) << 24 | (__ev2) << 16 \
1647 | (__bv1) << 8 | (__bv2) }
1648
1649#define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1650 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1651
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001652/* Outdated IBM BIOSes often lack the EC id string */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001653#define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1654 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001655 __bv1, __bv2, TPID(__id1, __id2), \
1656 __ev1, __ev2), \
1657 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1658 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1659 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001660
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001661/* Outdated IBM BIOSes often lack the EC id string */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001662#define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1663 __eid1, __eid2, __ev1, __ev2) \
1664 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001665 __bv1, __bv2, TPID(__eid1, __eid2), \
1666 __ev1, __ev2), \
1667 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1668 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1669 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001670
1671#define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1672 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1673
1674#define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1675 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001676 __bv1, __bv2, TPID(__id1, __id2), \
1677 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001678
1679#define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1680 __eid1, __eid2, __ev1, __ev2) \
1681 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001682 __bv1, __bv2, TPID(__eid1, __eid2), \
1683 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001684
1685static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
1686 /* Numeric models ------------------ */
1687 /* FW MODEL BIOS VERS */
1688 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1689 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1690 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1691 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1692 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1693 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1694 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1695 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1696 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1697
1698 /* A-series ------------------------- */
1699 /* FW MODEL BIOS VERS EC VERS */
1700 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1701 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1702 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1703 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1704 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1705 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1706 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1707 TPV_QI0('1', '3', '2', '0'), /* A22m */
1708 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1709 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1710 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1711
1712 /* G-series ------------------------- */
1713 /* FW MODEL BIOS VERS */
1714 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1715 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1716
1717 /* R-series, T-series --------------- */
1718 /* FW MODEL BIOS VERS EC VERS */
1719 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1720 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1721 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1722 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1723 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1724 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1725 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1726 T40/p, T41/p, T42/p (1) */
1727 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1728 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1729 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1730 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1731
1732 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1733 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1734 TPV_QI0('1', '6', '3', '2'), /* T22 */
1735 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1736 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1737 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1738
1739 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1740 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
Henrique de Moraes Holschuh90765c62009-12-09 01:36:23 +00001741 TPV_QL1('7', 'E', 'D', '0', '1', '5'), /* R60e, R60i */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001742
1743 /* BIOS FW BIOS VERS EC FW EC VERS */
1744 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1745 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1746
1747 /* X-series ------------------------- */
1748 /* FW MODEL BIOS VERS EC VERS */
1749 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1750 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1751 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1752 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1753 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1754 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1755 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1756
Henrique de Moraes Holschuh90765c62009-12-09 01:36:23 +00001757 TPV_QL1('7', 'B', 'D', '7', '4', '0'), /* X60/s */
1758 TPV_QL1('7', 'J', '3', '0', '1', '3'), /* X60t */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001759
1760 /* (0) - older versions lack DMI EC fw string and functionality */
1761 /* (1) - older versions known to lack functionality */
1762};
1763
1764#undef TPV_QL1
1765#undef TPV_QL0
1766#undef TPV_QI2
1767#undef TPV_QI1
1768#undef TPV_QI0
1769#undef TPV_Q_X
1770#undef TPV_Q
1771
1772static void __init tpacpi_check_outdated_fw(void)
1773{
1774 unsigned long fwvers;
1775 u16 ec_version, bios_version;
1776
1777 fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
1778 ARRAY_SIZE(tpacpi_bios_version_qtable));
1779
1780 if (!fwvers)
1781 return;
1782
1783 bios_version = fwvers & 0xffffU;
1784 ec_version = (fwvers >> 16) & 0xffffU;
1785
1786 /* note that unknown versions are set to 0x0000 and we use that */
1787 if ((bios_version > thinkpad_id.bios_release) ||
1788 (ec_version > thinkpad_id.ec_release &&
1789 ec_version != TPACPI_MATCH_ANY)) {
1790 /*
1791 * The changelogs would let us track down the exact
1792 * reason, but it is just too much of a pain to track
1793 * it. We only list BIOSes that are either really
1794 * broken, or really stable to begin with, so it is
1795 * best if the user upgrades the firmware anyway.
1796 */
1797 printk(TPACPI_WARN
1798 "WARNING: Outdated ThinkPad BIOS/EC firmware\n");
1799 printk(TPACPI_WARN
1800 "WARNING: This firmware may be missing critical bug "
1801 "fixes and/or important features\n");
1802 }
1803}
1804
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03001805static bool __init tpacpi_is_fw_known(void)
1806{
1807 return tpacpi_check_quirks(tpacpi_bios_version_qtable,
1808 ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
1809}
1810
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03001811/****************************************************************************
1812 ****************************************************************************
1813 *
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001814 * Subdrivers
1815 *
1816 ****************************************************************************
1817 ****************************************************************************/
1818
1819/*************************************************************************
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03001820 * thinkpad-acpi metadata subdriver
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001821 */
1822
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001823static int thinkpad_acpi_driver_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824{
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001825 seq_printf(m, "driver:\t\t%s\n", TPACPI_DESC);
1826 seq_printf(m, "version:\t%s\n", TPACPI_VERSION);
1827 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828}
1829
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001830static struct ibm_struct thinkpad_acpi_driver_data = {
1831 .name = "driver",
1832 .read = thinkpad_acpi_driver_read,
1833};
1834
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001835/*************************************************************************
1836 * Hotkey subdriver
1837 */
1838
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001839/*
1840 * ThinkPad firmware event model
1841 *
1842 * The ThinkPad firmware has two main event interfaces: normal ACPI
1843 * notifications (which follow the ACPI standard), and a private event
1844 * interface.
1845 *
1846 * The private event interface also issues events for the hotkeys. As
1847 * the driver gained features, the event handling code ended up being
1848 * built around the hotkey subdriver. This will need to be refactored
1849 * to a more formal event API eventually.
1850 *
1851 * Some "hotkeys" are actually supposed to be used as event reports,
1852 * such as "brightness has changed", "volume has changed", depending on
1853 * the ThinkPad model and how the firmware is operating.
1854 *
1855 * Unlike other classes, hotkey-class events have mask/unmask control on
1856 * non-ancient firmware. However, how it behaves changes a lot with the
1857 * firmware model and version.
1858 */
1859
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001860enum { /* hot key scan codes (derived from ACPI DSDT) */
1861 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1862 TP_ACPI_HOTKEYSCAN_FNF2,
1863 TP_ACPI_HOTKEYSCAN_FNF3,
1864 TP_ACPI_HOTKEYSCAN_FNF4,
1865 TP_ACPI_HOTKEYSCAN_FNF5,
1866 TP_ACPI_HOTKEYSCAN_FNF6,
1867 TP_ACPI_HOTKEYSCAN_FNF7,
1868 TP_ACPI_HOTKEYSCAN_FNF8,
1869 TP_ACPI_HOTKEYSCAN_FNF9,
1870 TP_ACPI_HOTKEYSCAN_FNF10,
1871 TP_ACPI_HOTKEYSCAN_FNF11,
1872 TP_ACPI_HOTKEYSCAN_FNF12,
1873 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1874 TP_ACPI_HOTKEYSCAN_FNINSERT,
1875 TP_ACPI_HOTKEYSCAN_FNDELETE,
1876 TP_ACPI_HOTKEYSCAN_FNHOME,
1877 TP_ACPI_HOTKEYSCAN_FNEND,
1878 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1879 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1880 TP_ACPI_HOTKEYSCAN_FNSPACE,
1881 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1882 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1883 TP_ACPI_HOTKEYSCAN_MUTE,
1884 TP_ACPI_HOTKEYSCAN_THINKPAD,
1885};
1886
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001887enum { /* Keys/events available through NVRAM polling */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001888 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1889 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1890};
1891
1892enum { /* Positions of some of the keys in hotkey masks */
1893 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1894 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1895 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1896 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1897 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1898 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1899 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1900 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1901 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1902 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1903 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1904};
1905
1906enum { /* NVRAM to ACPI HKEY group map */
1907 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1908 TP_ACPI_HKEY_ZOOM_MASK |
1909 TP_ACPI_HKEY_DISPSWTCH_MASK |
1910 TP_ACPI_HKEY_HIBERNATE_MASK,
1911 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1912 TP_ACPI_HKEY_BRGHTDWN_MASK,
1913 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1914 TP_ACPI_HKEY_VOLDWN_MASK |
1915 TP_ACPI_HKEY_MUTE_MASK,
1916};
1917
1918#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1919struct tp_nvram_state {
1920 u16 thinkpad_toggle:1;
1921 u16 zoom_toggle:1;
1922 u16 display_toggle:1;
1923 u16 thinklight_toggle:1;
1924 u16 hibernate_toggle:1;
1925 u16 displayexp_toggle:1;
1926 u16 display_state:1;
1927 u16 brightness_toggle:1;
1928 u16 volume_toggle:1;
1929 u16 mute:1;
1930
1931 u8 brightness_level;
1932 u8 volume_level;
1933};
1934
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001935/* kthread for the hotkey poller */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001936static struct task_struct *tpacpi_hotkey_task;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001937
1938/* Acquired while the poller kthread is running, use to sync start/stop */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001939static struct mutex hotkey_thread_mutex;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001940
1941/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001942 * Acquire mutex to write poller control variables as an
1943 * atomic block.
1944 *
1945 * Increment hotkey_config_change when changing them if you
1946 * want the kthread to forget old state.
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001947 *
1948 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1949 */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001950static struct mutex hotkey_thread_data_mutex;
1951static unsigned int hotkey_config_change;
1952
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001953/*
1954 * hotkey poller control variables
1955 *
1956 * Must be atomic or readers will also need to acquire mutex
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001957 *
1958 * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1959 * should be used only when the changes need to be taken as
1960 * a block, OR when one needs to force the kthread to forget
1961 * old state.
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001962 */
1963static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1964static unsigned int hotkey_poll_freq = 10; /* Hz */
1965
1966#define HOTKEY_CONFIG_CRITICAL_START \
1967 do { \
1968 mutex_lock(&hotkey_thread_data_mutex); \
1969 hotkey_config_change++; \
1970 } while (0);
1971#define HOTKEY_CONFIG_CRITICAL_END \
1972 mutex_unlock(&hotkey_thread_data_mutex);
1973
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001974#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1975
1976#define hotkey_source_mask 0U
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001977#define HOTKEY_CONFIG_CRITICAL_START
1978#define HOTKEY_CONFIG_CRITICAL_END
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001979
1980#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1981
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001982static struct mutex hotkey_mutex;
1983
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02001984static enum { /* Reasons for waking up */
1985 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1986 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1987 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1988} hotkey_wakeup_reason;
1989
1990static int hotkey_autosleep_ack;
1991
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001992static u32 hotkey_orig_mask; /* events the BIOS had enabled */
1993static u32 hotkey_all_mask; /* all events supported in fw */
1994static u32 hotkey_reserved_mask; /* events better left disabled */
1995static u32 hotkey_driver_mask; /* events needed by the driver */
1996static u32 hotkey_user_mask; /* events visible to userspace */
1997static u32 hotkey_acpi_mask; /* events enabled in firmware */
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001998
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001999static unsigned int hotkey_report_mode;
2000
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002001static u16 *hotkey_keycode_map;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002002
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03002003static struct attribute_set *hotkey_dev_attributes;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002004
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002005static void tpacpi_driver_event(const unsigned int hkey_event);
2006static void hotkey_driver_event(const unsigned int scancode);
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002007static void hotkey_poll_setup(const bool may_warn);
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002008
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002009/* HKEY.MHKG() return bits */
2010#define TP_HOTKEY_TABLET_MASK (1 << 3)
2011
Johannes Berg19d337d2009-06-02 13:01:37 +02002012static int hotkey_get_wlsw(void)
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002013{
Johannes Berg19d337d2009-06-02 13:01:37 +02002014 int status;
2015
2016 if (!tp_features.hotkey_wlsw)
2017 return -ENODEV;
2018
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02002019#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
Johannes Berg19d337d2009-06-02 13:01:37 +02002020 if (dbg_wlswemul)
2021 return (tpacpi_wlsw_emulstate) ?
2022 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02002023#endif
Johannes Berg19d337d2009-06-02 13:01:37 +02002024
2025 if (!acpi_evalf(hkey_handle, &status, "WLSW", "d"))
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002026 return -EIO;
Johannes Berg19d337d2009-06-02 13:01:37 +02002027
2028 return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002029}
2030
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002031static int hotkey_get_tablet_mode(int *status)
2032{
2033 int s;
2034
2035 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
2036 return -EIO;
2037
Henrique de Moraes Holschuhcee47f52008-03-04 14:29:21 -08002038 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
2039 return 0;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002040}
2041
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002042/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002043 * Reads current event mask from firmware, and updates
2044 * hotkey_acpi_mask accordingly. Also resets any bits
2045 * from hotkey_user_mask that are unavailable to be
2046 * delivered (shadow requirement of the userspace ABI).
2047 *
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002048 * Call with hotkey_mutex held
2049 */
2050static int hotkey_mask_get(void)
2051{
2052 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002053 u32 m = 0;
2054
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002055 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002056 return -EIO;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002057
2058 hotkey_acpi_mask = m;
2059 } else {
2060 /* no mask support doesn't mean no event support... */
2061 hotkey_acpi_mask = hotkey_all_mask;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002062 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002063
2064 /* sync userspace-visible mask */
2065 hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002066
2067 return 0;
2068}
2069
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002070void static hotkey_mask_warn_incomplete_mask(void)
2071{
2072 /* log only what the user can fix... */
2073 const u32 wantedmask = hotkey_driver_mask &
2074 ~(hotkey_acpi_mask | hotkey_source_mask) &
2075 (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
2076
2077 if (wantedmask)
2078 printk(TPACPI_NOTICE
2079 "required events 0x%08x not enabled!\n",
2080 wantedmask);
2081}
2082
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002083/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002084 * Set the firmware mask when supported
2085 *
2086 * Also calls hotkey_mask_get to update hotkey_acpi_mask.
2087 *
2088 * NOTE: does not set bits in hotkey_user_mask, but may reset them.
2089 *
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002090 * Call with hotkey_mutex held
2091 */
2092static int hotkey_mask_set(u32 mask)
2093{
2094 int i;
2095 int rc = 0;
2096
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002097 const u32 fwmask = mask & ~hotkey_source_mask;
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -03002098
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002099 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002100 for (i = 0; i < 32; i++) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002101 if (!acpi_evalf(hkey_handle,
2102 NULL, "MHKM", "vdd", i + 1,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002103 !!(mask & (1 << i)))) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002104 rc = -EIO;
2105 break;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002106 }
2107 }
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002108 }
2109
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002110 /*
2111 * We *must* make an inconditional call to hotkey_mask_get to
2112 * refresh hotkey_acpi_mask and update hotkey_user_mask
2113 *
2114 * Take the opportunity to also log when we cannot _enable_
2115 * a given event.
2116 */
2117 if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
2118 printk(TPACPI_NOTICE
2119 "asked for hotkey mask 0x%08x, but "
2120 "firmware forced it to 0x%08x\n",
2121 fwmask, hotkey_acpi_mask);
2122 }
2123
Henrique de Moraes Holschuh6b30eb72009-12-09 01:36:25 +00002124 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING)
2125 hotkey_mask_warn_incomplete_mask();
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002126
2127 return rc;
2128}
2129
2130/*
2131 * Sets hotkey_user_mask and tries to set the firmware mask
2132 *
2133 * Call with hotkey_mutex held
2134 */
2135static int hotkey_user_mask_set(const u32 mask)
2136{
2137 int rc;
2138
2139 /* Give people a chance to notice they are doing something that
2140 * is bound to go boom on their users sooner or later */
2141 if (!tp_warned.hotkey_mask_ff &&
2142 (mask == 0xffff || mask == 0xffffff ||
2143 mask == 0xffffffff)) {
2144 tp_warned.hotkey_mask_ff = 1;
2145 printk(TPACPI_NOTICE
2146 "setting the hotkey mask to 0x%08x is likely "
2147 "not the best way to go about it\n", mask);
2148 printk(TPACPI_NOTICE
2149 "please consider using the driver defaults, "
2150 "and refer to up-to-date thinkpad-acpi "
2151 "documentation\n");
2152 }
2153
2154 /* Try to enable what the user asked for, plus whatever we need.
2155 * this syncs everything but won't enable bits in hotkey_user_mask */
2156 rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
2157
2158 /* Enable the available bits in hotkey_user_mask */
2159 hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
2160
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002161 return rc;
2162}
2163
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002164/*
2165 * Sets the driver hotkey mask.
2166 *
2167 * Can be called even if the hotkey subdriver is inactive
2168 */
2169static int tpacpi_hotkey_driver_mask_set(const u32 mask)
2170{
2171 int rc;
2172
2173 /* Do the right thing if hotkey_init has not been called yet */
2174 if (!tp_features.hotkey) {
2175 hotkey_driver_mask = mask;
2176 return 0;
2177 }
2178
2179 mutex_lock(&hotkey_mutex);
2180
2181 HOTKEY_CONFIG_CRITICAL_START
2182 hotkey_driver_mask = mask;
Henrique de Moraes Holschuhb684a362009-09-26 21:42:49 -03002183#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002184 hotkey_source_mask |= (mask & ~hotkey_all_mask);
Henrique de Moraes Holschuhb684a362009-09-26 21:42:49 -03002185#endif
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002186 HOTKEY_CONFIG_CRITICAL_END
2187
2188 rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) &
2189 ~hotkey_source_mask);
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002190 hotkey_poll_setup(true);
2191
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002192 mutex_unlock(&hotkey_mutex);
2193
2194 return rc;
2195}
2196
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002197static int hotkey_status_get(int *status)
2198{
2199 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
2200 return -EIO;
2201
2202 return 0;
2203}
2204
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002205static int hotkey_status_set(bool enable)
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002206{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002207 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002208 return -EIO;
2209
2210 return 0;
2211}
2212
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002213static void tpacpi_input_send_tabletsw(void)
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002214{
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002215 int state;
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002216
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002217 if (tp_features.hotkey_tablet &&
2218 !hotkey_get_tablet_mode(&state)) {
2219 mutex_lock(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002220
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002221 input_report_switch(tpacpi_inputdev,
2222 SW_TABLET_MODE, !!state);
2223 input_sync(tpacpi_inputdev);
2224
2225 mutex_unlock(&tpacpi_inputdev_send_mutex);
2226 }
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002227}
2228
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002229/* Do NOT call without validating scancode first */
2230static void tpacpi_input_send_key(const unsigned int scancode)
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002231{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002232 const unsigned int keycode = hotkey_keycode_map[scancode];
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002233
2234 if (keycode != KEY_RESERVED) {
2235 mutex_lock(&tpacpi_inputdev_send_mutex);
2236
2237 input_report_key(tpacpi_inputdev, keycode, 1);
2238 if (keycode == KEY_UNKNOWN)
2239 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
2240 scancode);
2241 input_sync(tpacpi_inputdev);
2242
2243 input_report_key(tpacpi_inputdev, keycode, 0);
2244 if (keycode == KEY_UNKNOWN)
2245 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
2246 scancode);
2247 input_sync(tpacpi_inputdev);
2248
2249 mutex_unlock(&tpacpi_inputdev_send_mutex);
2250 }
2251}
2252
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002253/* Do NOT call without validating scancode first */
2254static void tpacpi_input_send_key_masked(const unsigned int scancode)
2255{
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002256 hotkey_driver_event(scancode);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002257 if (hotkey_user_mask & (1 << scancode))
2258 tpacpi_input_send_key(scancode);
2259}
2260
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002261#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2262static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
2263
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002264/* Do NOT call without validating scancode first */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002265static void tpacpi_hotkey_send_key(unsigned int scancode)
2266{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002267 tpacpi_input_send_key_masked(scancode);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002268 if (hotkey_report_mode < 2) {
2269 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03002270 0x80, TP_HKEY_EV_HOTKEY_BASE + scancode);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002271 }
2272}
2273
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002274static void hotkey_read_nvram(struct tp_nvram_state *n, const u32 m)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002275{
2276 u8 d;
2277
2278 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
2279 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
2280 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
2281 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
2282 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
2283 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
2284 }
2285 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
2286 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
2287 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
2288 }
2289 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
2290 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
2291 n->displayexp_toggle =
2292 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
2293 }
2294 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
2295 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
2296 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
2297 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
2298 n->brightness_toggle =
2299 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
2300 }
2301 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
2302 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
2303 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
2304 >> TP_NVRAM_POS_LEVEL_VOLUME;
2305 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2306 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2307 }
2308}
2309
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002310static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
2311 struct tp_nvram_state *newn,
2312 const u32 event_mask)
2313{
2314
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002315#define TPACPI_COMPARE_KEY(__scancode, __member) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002316 do { \
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002317 if ((event_mask & (1 << __scancode)) && \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002318 oldn->__member != newn->__member) \
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002319 tpacpi_hotkey_send_key(__scancode); \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002320 } while (0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002321
2322#define TPACPI_MAY_SEND_KEY(__scancode) \
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002323 do { \
2324 if (event_mask & (1 << __scancode)) \
2325 tpacpi_hotkey_send_key(__scancode); \
2326 } while (0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002327
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002328 void issue_volchange(const unsigned int oldvol,
2329 const unsigned int newvol)
2330 {
2331 unsigned int i = oldvol;
2332
2333 while (i > newvol) {
2334 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2335 i--;
2336 }
2337 while (i < newvol) {
2338 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2339 i++;
2340 }
2341 }
2342
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002343 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2344 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2345 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2346 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2347
2348 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2349
2350 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2351
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002352 /*
2353 * Handle volume
2354 *
2355 * This code is supposed to duplicate the IBM firmware behaviour:
2356 * - Pressing MUTE issues mute hotkey message, even when already mute
2357 * - Pressing Volume up/down issues volume up/down hotkey messages,
2358 * even when already at maximum or minumum volume
2359 * - The act of unmuting issues volume up/down notification,
2360 * depending which key was used to unmute
2361 *
2362 * We are constrained to what the NVRAM can tell us, which is not much
2363 * and certainly not enough if more than one volume hotkey was pressed
2364 * since the last poll cycle.
2365 *
2366 * Just to make our life interesting, some newer Lenovo ThinkPads have
2367 * bugs in the BIOS and may fail to update volume_toggle properly.
2368 */
2369 if (newn->mute) {
2370 /* muted */
2371 if (!oldn->mute ||
2372 oldn->volume_toggle != newn->volume_toggle ||
2373 oldn->volume_level != newn->volume_level) {
2374 /* recently muted, or repeated mute keypress, or
2375 * multiple presses ending in mute */
2376 issue_volchange(oldn->volume_level, newn->volume_level);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002377 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2378 }
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002379 } else {
2380 /* unmute */
2381 if (oldn->mute) {
2382 /* recently unmuted, issue 'unmute' keypress */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002383 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002384 }
2385 if (oldn->volume_level != newn->volume_level) {
2386 issue_volchange(oldn->volume_level, newn->volume_level);
2387 } else if (oldn->volume_toggle != newn->volume_toggle) {
2388 /* repeated vol up/down keypress at end of scale ? */
2389 if (newn->volume_level == 0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002390 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002391 else if (newn->volume_level >= TP_NVRAM_LEVEL_VOLUME_MAX)
2392 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002393 }
2394 }
2395
2396 /* handle brightness */
2397 if (oldn->brightness_toggle != newn->brightness_toggle) {
2398 if (oldn->brightness_level < newn->brightness_level) {
2399 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2400 } else if (oldn->brightness_level > newn->brightness_level) {
2401 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2402 } else {
2403 /* repeated key presses that didn't change state */
2404 if (newn->brightness_level != 0) {
2405 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2406 } else {
2407 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2408 }
2409 }
2410 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002411
2412#undef TPACPI_COMPARE_KEY
2413#undef TPACPI_MAY_SEND_KEY
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002414}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002415
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002416/*
2417 * Polling driver
2418 *
2419 * We track all events in hotkey_source_mask all the time, since
2420 * most of them are edge-based. We only issue those requested by
2421 * hotkey_user_mask or hotkey_driver_mask, though.
2422 */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002423static int hotkey_kthread(void *data)
2424{
2425 struct tp_nvram_state s[2];
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002426 u32 poll_mask, event_mask;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002427 unsigned int si, so;
2428 unsigned long t;
2429 unsigned int change_detector, must_reset;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002430 unsigned int poll_freq;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002431
2432 mutex_lock(&hotkey_thread_mutex);
2433
2434 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2435 goto exit;
2436
2437 set_freezable();
2438
2439 so = 0;
2440 si = 1;
2441 t = 0;
2442
2443 /* Initial state for compares */
2444 mutex_lock(&hotkey_thread_data_mutex);
2445 change_detector = hotkey_config_change;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002446 poll_mask = hotkey_source_mask;
2447 event_mask = hotkey_source_mask &
2448 (hotkey_driver_mask | hotkey_user_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002449 poll_freq = hotkey_poll_freq;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002450 mutex_unlock(&hotkey_thread_data_mutex);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002451 hotkey_read_nvram(&s[so], poll_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002452
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002453 while (!kthread_should_stop()) {
2454 if (t == 0) {
2455 if (likely(poll_freq))
2456 t = 1000/poll_freq;
2457 else
2458 t = 100; /* should never happen... */
2459 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002460 t = msleep_interruptible(t);
2461 if (unlikely(kthread_should_stop()))
2462 break;
2463 must_reset = try_to_freeze();
2464 if (t > 0 && !must_reset)
2465 continue;
2466
2467 mutex_lock(&hotkey_thread_data_mutex);
2468 if (must_reset || hotkey_config_change != change_detector) {
2469 /* forget old state on thaw or config change */
2470 si = so;
2471 t = 0;
2472 change_detector = hotkey_config_change;
2473 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002474 poll_mask = hotkey_source_mask;
2475 event_mask = hotkey_source_mask &
2476 (hotkey_driver_mask | hotkey_user_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002477 poll_freq = hotkey_poll_freq;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002478 mutex_unlock(&hotkey_thread_data_mutex);
2479
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002480 if (likely(poll_mask)) {
2481 hotkey_read_nvram(&s[si], poll_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002482 if (likely(si != so)) {
2483 hotkey_compare_and_issue_event(&s[so], &s[si],
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002484 event_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002485 }
2486 }
2487
2488 so = si;
2489 si ^= 1;
2490 }
2491
2492exit:
2493 mutex_unlock(&hotkey_thread_mutex);
2494 return 0;
2495}
2496
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002497/* call with hotkey_mutex held */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002498static void hotkey_poll_stop_sync(void)
2499{
2500 if (tpacpi_hotkey_task) {
2501 if (frozen(tpacpi_hotkey_task) ||
2502 freezing(tpacpi_hotkey_task))
2503 thaw_process(tpacpi_hotkey_task);
2504
2505 kthread_stop(tpacpi_hotkey_task);
2506 tpacpi_hotkey_task = NULL;
2507 mutex_lock(&hotkey_thread_mutex);
2508 /* at this point, the thread did exit */
2509 mutex_unlock(&hotkey_thread_mutex);
2510 }
2511}
2512
2513/* call with hotkey_mutex held */
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002514static void hotkey_poll_setup(const bool may_warn)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002515{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002516 const u32 poll_driver_mask = hotkey_driver_mask & hotkey_source_mask;
2517 const u32 poll_user_mask = hotkey_user_mask & hotkey_source_mask;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002518
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002519 if (hotkey_poll_freq > 0 &&
2520 (poll_driver_mask ||
2521 (poll_user_mask && tpacpi_inputdev->users > 0))) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002522 if (!tpacpi_hotkey_task) {
2523 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03002524 NULL, TPACPI_NVRAM_KTHREAD_NAME);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002525 if (IS_ERR(tpacpi_hotkey_task)) {
2526 tpacpi_hotkey_task = NULL;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002527 printk(TPACPI_ERR
2528 "could not create kernel thread "
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002529 "for hotkey polling\n");
2530 }
2531 }
2532 } else {
2533 hotkey_poll_stop_sync();
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002534 if (may_warn && (poll_driver_mask || poll_user_mask) &&
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002535 hotkey_poll_freq == 0) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002536 printk(TPACPI_NOTICE
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002537 "hot keys 0x%08x and/or events 0x%08x "
2538 "require polling, which is currently "
2539 "disabled\n",
2540 poll_user_mask, poll_driver_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002541 }
2542 }
2543}
2544
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002545static void hotkey_poll_setup_safe(const bool may_warn)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002546{
2547 mutex_lock(&hotkey_mutex);
2548 hotkey_poll_setup(may_warn);
2549 mutex_unlock(&hotkey_mutex);
2550}
2551
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002552/* call with hotkey_mutex held */
2553static void hotkey_poll_set_freq(unsigned int freq)
2554{
2555 if (!freq)
2556 hotkey_poll_stop_sync();
2557
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002558 hotkey_poll_freq = freq;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002559}
2560
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02002561#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2562
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002563static void hotkey_poll_setup(const bool __unused)
2564{
2565}
2566
2567static void hotkey_poll_setup_safe(const bool __unused)
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02002568{
2569}
2570
2571#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2572
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002573static int hotkey_inputdev_open(struct input_dev *dev)
2574{
2575 switch (tpacpi_lifecycle) {
2576 case TPACPI_LIFE_INIT:
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002577 case TPACPI_LIFE_RUNNING:
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002578 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002579 return 0;
Henrique de Moraes Holschuhb589ea42010-02-25 21:28:58 -03002580 case TPACPI_LIFE_EXITING:
2581 return -EBUSY;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002582 }
2583
2584 /* Should only happen if tpacpi_lifecycle is corrupt */
2585 BUG();
2586 return -EBUSY;
2587}
2588
2589static void hotkey_inputdev_close(struct input_dev *dev)
2590{
2591 /* disable hotkey polling when possible */
Henrique de Moraes Holschuhb589ea42010-02-25 21:28:58 -03002592 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING &&
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002593 !(hotkey_source_mask & hotkey_driver_mask))
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002594 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002595}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002596
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002597/* sysfs hotkey enable ------------------------------------------------- */
2598static ssize_t hotkey_enable_show(struct device *dev,
2599 struct device_attribute *attr,
2600 char *buf)
2601{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002602 int res, status;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002603
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002604 printk_deprecated_attribute("hotkey_enable",
2605 "Hotkey reporting is always enabled");
2606
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002607 res = hotkey_status_get(&status);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002608 if (res)
2609 return res;
2610
2611 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2612}
2613
2614static ssize_t hotkey_enable_store(struct device *dev,
2615 struct device_attribute *attr,
2616 const char *buf, size_t count)
2617{
2618 unsigned long t;
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002619
2620 printk_deprecated_attribute("hotkey_enable",
2621 "Hotkeys can be disabled through hotkey_mask");
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002622
2623 if (parse_strtoul(buf, 1, &t))
2624 return -EINVAL;
2625
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002626 if (t == 0)
2627 return -EPERM;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002628
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002629 return count;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002630}
2631
2632static struct device_attribute dev_attr_hotkey_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002633 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002634 hotkey_enable_show, hotkey_enable_store);
2635
2636/* sysfs hotkey mask --------------------------------------------------- */
2637static ssize_t hotkey_mask_show(struct device *dev,
2638 struct device_attribute *attr,
2639 char *buf)
2640{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002641 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_user_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002642}
2643
2644static ssize_t hotkey_mask_store(struct device *dev,
2645 struct device_attribute *attr,
2646 const char *buf, size_t count)
2647{
2648 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002649 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002650
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002651 if (parse_strtoul(buf, 0xffffffffUL, &t))
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002652 return -EINVAL;
2653
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002654 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002655 return -ERESTARTSYS;
2656
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002657 res = hotkey_user_mask_set(t);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002658
2659#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002660 hotkey_poll_setup(true);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002661#endif
2662
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002663 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002664
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002665 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2666
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002667 return (res) ? res : count;
2668}
2669
2670static struct device_attribute dev_attr_hotkey_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002671 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002672 hotkey_mask_show, hotkey_mask_store);
2673
2674/* sysfs hotkey bios_enabled ------------------------------------------- */
2675static ssize_t hotkey_bios_enabled_show(struct device *dev,
2676 struct device_attribute *attr,
2677 char *buf)
2678{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002679 return sprintf(buf, "0\n");
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002680}
2681
2682static struct device_attribute dev_attr_hotkey_bios_enabled =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002683 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002684
2685/* sysfs hotkey bios_mask ---------------------------------------------- */
2686static ssize_t hotkey_bios_mask_show(struct device *dev,
2687 struct device_attribute *attr,
2688 char *buf)
2689{
Henrique de Moraes Holschuh06777be2009-09-12 15:22:15 -03002690 printk_deprecated_attribute("hotkey_bios_mask",
2691 "This attribute is useless.");
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002692 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002693}
2694
2695static struct device_attribute dev_attr_hotkey_bios_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002696 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002697
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002698/* sysfs hotkey all_mask ----------------------------------------------- */
2699static ssize_t hotkey_all_mask_show(struct device *dev,
2700 struct device_attribute *attr,
2701 char *buf)
2702{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002703 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2704 hotkey_all_mask | hotkey_source_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002705}
2706
2707static struct device_attribute dev_attr_hotkey_all_mask =
2708 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
2709
2710/* sysfs hotkey recommended_mask --------------------------------------- */
2711static ssize_t hotkey_recommended_mask_show(struct device *dev,
2712 struct device_attribute *attr,
2713 char *buf)
2714{
2715 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002716 (hotkey_all_mask | hotkey_source_mask)
2717 & ~hotkey_reserved_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002718}
2719
2720static struct device_attribute dev_attr_hotkey_recommended_mask =
2721 __ATTR(hotkey_recommended_mask, S_IRUGO,
2722 hotkey_recommended_mask_show, NULL);
2723
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002724#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2725
2726/* sysfs hotkey hotkey_source_mask ------------------------------------- */
2727static ssize_t hotkey_source_mask_show(struct device *dev,
2728 struct device_attribute *attr,
2729 char *buf)
2730{
2731 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2732}
2733
2734static ssize_t hotkey_source_mask_store(struct device *dev,
2735 struct device_attribute *attr,
2736 const char *buf, size_t count)
2737{
2738 unsigned long t;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002739 u32 r_ev;
2740 int rc;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002741
2742 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2743 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2744 return -EINVAL;
2745
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002746 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002747 return -ERESTARTSYS;
2748
2749 HOTKEY_CONFIG_CRITICAL_START
2750 hotkey_source_mask = t;
2751 HOTKEY_CONFIG_CRITICAL_END
2752
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002753 rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
2754 ~hotkey_source_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002755 hotkey_poll_setup(true);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002756
2757 /* check if events needed by the driver got disabled */
2758 r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
2759 & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002760
2761 mutex_unlock(&hotkey_mutex);
2762
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002763 if (rc < 0)
2764 printk(TPACPI_ERR "hotkey_source_mask: failed to update the"
2765 "firmware event mask!\n");
2766
2767 if (r_ev)
2768 printk(TPACPI_NOTICE "hotkey_source_mask: "
2769 "some important events were disabled: "
2770 "0x%04x\n", r_ev);
2771
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002772 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2773
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002774 return (rc < 0) ? rc : count;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002775}
2776
2777static struct device_attribute dev_attr_hotkey_source_mask =
2778 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2779 hotkey_source_mask_show, hotkey_source_mask_store);
2780
2781/* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2782static ssize_t hotkey_poll_freq_show(struct device *dev,
2783 struct device_attribute *attr,
2784 char *buf)
2785{
2786 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2787}
2788
2789static ssize_t hotkey_poll_freq_store(struct device *dev,
2790 struct device_attribute *attr,
2791 const char *buf, size_t count)
2792{
2793 unsigned long t;
2794
2795 if (parse_strtoul(buf, 25, &t))
2796 return -EINVAL;
2797
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002798 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002799 return -ERESTARTSYS;
2800
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002801 hotkey_poll_set_freq(t);
2802 hotkey_poll_setup(true);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002803
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002804 mutex_unlock(&hotkey_mutex);
2805
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002806 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2807
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002808 return count;
2809}
2810
2811static struct device_attribute dev_attr_hotkey_poll_freq =
2812 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2813 hotkey_poll_freq_show, hotkey_poll_freq_store);
2814
2815#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2816
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002817/* sysfs hotkey radio_sw (pollable) ------------------------------------ */
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002818static ssize_t hotkey_radio_sw_show(struct device *dev,
2819 struct device_attribute *attr,
2820 char *buf)
2821{
Johannes Berg19d337d2009-06-02 13:01:37 +02002822 int res;
2823 res = hotkey_get_wlsw();
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002824 if (res < 0)
2825 return res;
2826
Johannes Berg19d337d2009-06-02 13:01:37 +02002827 /* Opportunistic update */
2828 tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
2829
2830 return snprintf(buf, PAGE_SIZE, "%d\n",
2831 (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002832}
2833
2834static struct device_attribute dev_attr_hotkey_radio_sw =
2835 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2836
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002837static void hotkey_radio_sw_notify_change(void)
2838{
2839 if (tp_features.hotkey_wlsw)
2840 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2841 "hotkey_radio_sw");
2842}
2843
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002844/* sysfs hotkey tablet mode (pollable) --------------------------------- */
2845static ssize_t hotkey_tablet_mode_show(struct device *dev,
2846 struct device_attribute *attr,
2847 char *buf)
2848{
2849 int res, s;
2850 res = hotkey_get_tablet_mode(&s);
2851 if (res < 0)
2852 return res;
2853
2854 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2855}
2856
2857static struct device_attribute dev_attr_hotkey_tablet_mode =
2858 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2859
2860static void hotkey_tablet_mode_notify_change(void)
2861{
2862 if (tp_features.hotkey_tablet)
2863 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2864 "hotkey_tablet_mode");
2865}
2866
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002867/* sysfs hotkey report_mode -------------------------------------------- */
2868static ssize_t hotkey_report_mode_show(struct device *dev,
2869 struct device_attribute *attr,
2870 char *buf)
2871{
2872 return snprintf(buf, PAGE_SIZE, "%d\n",
2873 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2874}
2875
2876static struct device_attribute dev_attr_hotkey_report_mode =
2877 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2878
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002879/* sysfs wakeup reason (pollable) -------------------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002880static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2881 struct device_attribute *attr,
2882 char *buf)
2883{
2884 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2885}
2886
2887static struct device_attribute dev_attr_hotkey_wakeup_reason =
2888 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2889
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02002890static void hotkey_wakeup_reason_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002891{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002892 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2893 "wakeup_reason");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002894}
2895
2896/* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002897static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2898 struct device_attribute *attr,
2899 char *buf)
2900{
2901 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2902}
2903
2904static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2905 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2906 hotkey_wakeup_hotunplug_complete_show, NULL);
2907
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02002908static void hotkey_wakeup_hotunplug_complete_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002909{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002910 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2911 "wakeup_hotunplug_complete");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002912}
2913
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002914/* --------------------------------------------------------------------- */
2915
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002916static struct attribute *hotkey_attributes[] __initdata = {
2917 &dev_attr_hotkey_enable.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002918 &dev_attr_hotkey_bios_enabled.attr,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002919 &dev_attr_hotkey_bios_mask.attr,
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002920 &dev_attr_hotkey_report_mode.attr,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002921 &dev_attr_hotkey_wakeup_reason.attr,
2922 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002923 &dev_attr_hotkey_mask.attr,
2924 &dev_attr_hotkey_all_mask.attr,
2925 &dev_attr_hotkey_recommended_mask.attr,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002926#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002927 &dev_attr_hotkey_source_mask.attr,
2928 &dev_attr_hotkey_poll_freq.attr,
2929#endif
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002930};
2931
Johannes Berg19d337d2009-06-02 13:01:37 +02002932/*
2933 * Sync both the hw and sw blocking state of all switches
2934 */
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002935static void tpacpi_send_radiosw_update(void)
2936{
2937 int wlsw;
2938
Johannes Berg19d337d2009-06-02 13:01:37 +02002939 /*
2940 * We must sync all rfkill controllers *before* issuing any
2941 * rfkill input events, or we will race the rfkill core input
2942 * handler.
2943 *
2944 * tpacpi_inputdev_send_mutex works as a syncronization point
2945 * for the above.
2946 *
2947 * We optimize to avoid numerous calls to hotkey_get_wlsw.
2948 */
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002949
Johannes Berg19d337d2009-06-02 13:01:37 +02002950 wlsw = hotkey_get_wlsw();
2951
2952 /* Sync hw blocking state first if it is hw-blocked */
2953 if (wlsw == TPACPI_RFK_RADIO_OFF)
2954 tpacpi_rfk_update_hwblock_state(true);
2955
2956 /* Sync sw blocking state */
2957 tpacpi_rfk_update_swstate_all();
2958
2959 /* Sync hw blocking state last if it is hw-unblocked */
2960 if (wlsw == TPACPI_RFK_RADIO_ON)
2961 tpacpi_rfk_update_hwblock_state(false);
2962
2963 /* Issue rfkill input event for WLSW switch */
2964 if (!(wlsw < 0)) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002965 mutex_lock(&tpacpi_inputdev_send_mutex);
2966
2967 input_report_switch(tpacpi_inputdev,
Johannes Berg19d337d2009-06-02 13:01:37 +02002968 SW_RFKILL_ALL, (wlsw > 0));
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002969 input_sync(tpacpi_inputdev);
2970
2971 mutex_unlock(&tpacpi_inputdev_send_mutex);
2972 }
Johannes Berg19d337d2009-06-02 13:01:37 +02002973
2974 /*
2975 * this can be unconditional, as we will poll state again
2976 * if userspace uses the notify to read data
2977 */
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002978 hotkey_radio_sw_notify_change();
2979}
2980
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002981static void hotkey_exit(void)
2982{
2983#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002984 mutex_lock(&hotkey_mutex);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002985 hotkey_poll_stop_sync();
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002986 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002987#endif
2988
2989 if (hotkey_dev_attributes)
2990 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2991
2992 kfree(hotkey_keycode_map);
2993
Henrique de Moraes Holschuh4be73002009-09-20 14:09:23 -03002994 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002995 "restoring original HKEY status and mask\n");
2996 /* yes, there is a bitwise or below, we want the
2997 * functions to be called even if one of them fail */
2998 if (((tp_features.hotkey_mask &&
2999 hotkey_mask_set(hotkey_orig_mask)) |
3000 hotkey_status_set(false)) != 0)
Henrique de Moraes Holschuh4be73002009-09-20 14:09:23 -03003001 printk(TPACPI_ERR
3002 "failed to restore hot key mask "
3003 "to BIOS defaults\n");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003004}
3005
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003006static void __init hotkey_unmap(const unsigned int scancode)
3007{
3008 if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
3009 clear_bit(hotkey_keycode_map[scancode],
3010 tpacpi_inputdev->keybit);
3011 hotkey_keycode_map[scancode] = KEY_RESERVED;
3012 }
3013}
3014
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003015/*
3016 * HKEY quirks:
3017 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
3018 */
3019
3020#define TPACPI_HK_Q_INIMASK 0x0001
3021
3022static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
3023 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
3024 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
3025 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
3026 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
3027 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
3028 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
3029 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
3030 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
3031 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
3032 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
3033 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
3034 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
3035 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
3036 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
3037 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
3038 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
3039 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
3040 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
3041 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
3042};
3043
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003044static int __init hotkey_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003045{
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003046 /* Requirements for changing the default keymaps:
3047 *
3048 * 1. Many of the keys are mapped to KEY_RESERVED for very
3049 * good reasons. Do not change them unless you have deep
3050 * knowledge on the IBM and Lenovo ThinkPad firmware for
3051 * the various ThinkPad models. The driver behaves
3052 * differently for KEY_RESERVED: such keys have their
3053 * hot key mask *unset* in mask_recommended, and also
3054 * in the initial hot key mask programmed into the
3055 * firmware at driver load time, which means the firm-
3056 * ware may react very differently if you change them to
3057 * something else;
3058 *
3059 * 2. You must be subscribed to the linux-thinkpad and
3060 * ibm-acpi-devel mailing lists, and you should read the
3061 * list archives since 2007 if you want to change the
3062 * keymaps. This requirement exists so that you will
3063 * know the past history of problems with the thinkpad-
3064 * acpi driver keymaps, and also that you will be
3065 * listening to any bug reports;
3066 *
3067 * 3. Do not send thinkpad-acpi specific patches directly to
3068 * for merging, *ever*. Send them to the linux-acpi
3069 * mailinglist for comments. Merging is to be done only
3070 * through acpi-test and the ACPI maintainer.
3071 *
3072 * If the above is too much to ask, don't change the keymap.
3073 * Ask the thinkpad-acpi maintainer to do it, instead.
3074 */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003075 static u16 ibm_keycode_map[] __initdata = {
3076 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3077 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
3078 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3079 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003080
3081 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003082 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3083 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3084 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003085
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003086 /* brightness: firmware always reacts to them */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003087 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003088 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003089
3090 /* Thinklight: firmware always react to it */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003091 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003092
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003093 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3094 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003095
3096 /* Volume: firmware always react to it and reprograms
3097 * the built-in *extra* mixer. Never map it to control
3098 * another mixer by default. */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003099 KEY_RESERVED, /* 0x14: VOLUME UP */
3100 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3101 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003102
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003103 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003104
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003105 /* (assignments unknown, please report if found) */
3106 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3107 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3108 };
3109 static u16 lenovo_keycode_map[] __initdata = {
3110 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3111 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
3112 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3113 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003114
3115 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003116 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3117 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3118 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003119
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003120 /* These should be enabled --only-- when ACPI video
3121 * is disabled (i.e. in "vendor" mode), and are handled
3122 * in a special way by the init code */
3123 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
3124 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003125
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003126 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003127
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003128 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3129 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003130
3131 /* Volume: z60/z61, T60 (BIOS version?): firmware always
3132 * react to it and reprograms the built-in *extra* mixer.
3133 * Never map it to control another mixer by default.
3134 *
3135 * T60?, T61, R60?, R61: firmware and EC tries to send
3136 * these over the regular keyboard, so these are no-ops,
3137 * but there are still weird bugs re. MUTE, so do not
3138 * change unless you get test reports from all Lenovo
3139 * models. May cause the BIOS to interfere with the
3140 * HDA mixer.
3141 */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003142 KEY_RESERVED, /* 0x14: VOLUME UP */
3143 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3144 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003145
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003146 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003147
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003148 /* (assignments unknown, please report if found) */
3149 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3150 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3151 };
3152
3153#define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
3154#define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
3155#define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
3156
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003157 int res, i;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003158 int status;
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03003159 int hkeyv;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003160 bool radiosw_state = false;
3161 bool tabletsw_state = false;
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03003162
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003163 unsigned long quirks;
3164
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003165 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3166 "initializing hotkey subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003167
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003168 BUG_ON(!tpacpi_inputdev);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003169 BUG_ON(tpacpi_inputdev->open != NULL ||
3170 tpacpi_inputdev->close != NULL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003171
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003172 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003173 mutex_init(&hotkey_mutex);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003174
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003175#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3176 mutex_init(&hotkey_thread_mutex);
3177 mutex_init(&hotkey_thread_data_mutex);
3178#endif
3179
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003180 /* hotkey not supported on 570 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003181 tp_features.hotkey = hkey_handle != NULL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003182
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003183 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3184 "hotkeys are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003185 str_supported(tp_features.hotkey));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003186
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003187 if (!tp_features.hotkey)
3188 return 1;
3189
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003190 quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
3191 ARRAY_SIZE(tpacpi_hotkey_qtable));
3192
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03003193 tpacpi_disable_brightness_delay();
3194
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003195 /* MUST have enough space for all attributes to be added to
3196 * hotkey_dev_attributes */
3197 hotkey_dev_attributes = create_attr_set(
3198 ARRAY_SIZE(hotkey_attributes) + 2,
3199 NULL);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003200 if (!hotkey_dev_attributes)
3201 return -ENOMEM;
3202 res = add_many_to_attr_set(hotkey_dev_attributes,
3203 hotkey_attributes,
3204 ARRAY_SIZE(hotkey_attributes));
3205 if (res)
3206 goto err_exit;
3207
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003208 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003209 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
3210 for HKEY interface version 0x100 */
3211 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
3212 if ((hkeyv >> 8) != 1) {
3213 printk(TPACPI_ERR "unknown version of the "
3214 "HKEY interface: 0x%x\n", hkeyv);
3215 printk(TPACPI_ERR "please report this to %s\n",
3216 TPACPI_MAIL);
3217 } else {
3218 /*
3219 * MHKV 0x100 in A31, R40, R40e,
3220 * T4x, X31, and later
3221 */
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003222 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3223 "firmware HKEY interface version: 0x%x\n",
3224 hkeyv);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003225
3226 /* Paranoia check AND init hotkey_all_mask */
3227 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3228 "MHKA", "qd")) {
3229 printk(TPACPI_ERR
3230 "missing MHKA handler, "
3231 "please report this to %s\n",
3232 TPACPI_MAIL);
3233 /* Fallback: pre-init for FN+F3,F4,F12 */
3234 hotkey_all_mask = 0x080cU;
3235 } else {
3236 tp_features.hotkey_mask = 1;
3237 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003238 }
3239 }
3240
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003241 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3242 "hotkey masks are %s\n",
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003243 str_supported(tp_features.hotkey_mask));
3244
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003245 /* Init hotkey_all_mask if not initialized yet */
3246 if (!tp_features.hotkey_mask && !hotkey_all_mask &&
3247 (quirks & TPACPI_HK_Q_INIMASK))
3248 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003249
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003250 /* Init hotkey_acpi_mask and hotkey_orig_mask */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003251 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003252 /* hotkey_source_mask *must* be zero for
3253 * the first hotkey_mask_get to return hotkey_orig_mask */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003254 res = hotkey_mask_get();
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003255 if (res)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003256 goto err_exit;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003257
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003258 hotkey_orig_mask = hotkey_acpi_mask;
3259 } else {
3260 hotkey_orig_mask = hotkey_all_mask;
3261 hotkey_acpi_mask = hotkey_all_mask;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003262 }
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003263
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003264#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3265 if (dbg_wlswemul) {
3266 tp_features.hotkey_wlsw = 1;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003267 radiosw_state = !!tpacpi_wlsw_emulstate;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003268 printk(TPACPI_INFO
3269 "radio switch emulation enabled\n");
3270 } else
3271#endif
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003272 /* Not all thinkpads have a hardware radio switch */
3273 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
3274 tp_features.hotkey_wlsw = 1;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003275 radiosw_state = !!status;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003276 printk(TPACPI_INFO
3277 "radio switch found; radios are %s\n",
3278 enabled(status, 0));
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003279 }
3280 if (tp_features.hotkey_wlsw)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003281 res = add_to_attr_set(hotkey_dev_attributes,
3282 &dev_attr_hotkey_radio_sw.attr);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003283
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003284 /* For X41t, X60t, X61t Tablets... */
3285 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
3286 tp_features.hotkey_tablet = 1;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003287 tabletsw_state = !!(status & TP_HOTKEY_TABLET_MASK);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003288 printk(TPACPI_INFO
3289 "possible tablet mode switch found; "
3290 "ThinkPad in %s mode\n",
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003291 (tabletsw_state) ? "tablet" : "laptop");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003292 res = add_to_attr_set(hotkey_dev_attributes,
3293 &dev_attr_hotkey_tablet_mode.attr);
3294 }
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02003295
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003296 if (!res)
3297 res = register_attr_set_with_sysfs(
3298 hotkey_dev_attributes,
3299 &tpacpi_pdev->dev.kobj);
3300 if (res)
3301 goto err_exit;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003302
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003303 /* Set up key map */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003304
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003305 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
3306 GFP_KERNEL);
3307 if (!hotkey_keycode_map) {
3308 printk(TPACPI_ERR
3309 "failed to allocate memory for key map\n");
3310 res = -ENOMEM;
3311 goto err_exit;
3312 }
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003313
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03003314 if (tpacpi_is_lenovo()) {
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003315 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003316 "using Lenovo default hot key map\n");
3317 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
3318 TPACPI_HOTKEY_MAP_SIZE);
3319 } else {
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003320 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003321 "using IBM default hot key map\n");
3322 memcpy(hotkey_keycode_map, &ibm_keycode_map,
3323 TPACPI_HOTKEY_MAP_SIZE);
3324 }
3325
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003326 input_set_capability(tpacpi_inputdev, EV_MSC, MSC_SCAN);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003327 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
3328 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3329 tpacpi_inputdev->keycode = hotkey_keycode_map;
3330 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3331 if (hotkey_keycode_map[i] != KEY_RESERVED) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003332 input_set_capability(tpacpi_inputdev, EV_KEY,
3333 hotkey_keycode_map[i]);
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003334 } else {
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003335 if (i < sizeof(hotkey_reserved_mask)*8)
3336 hotkey_reserved_mask |= 1 << i;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003337 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003338 }
3339
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003340 if (tp_features.hotkey_wlsw) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003341 input_set_capability(tpacpi_inputdev, EV_SW, SW_RFKILL_ALL);
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003342 input_report_switch(tpacpi_inputdev,
3343 SW_RFKILL_ALL, radiosw_state);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003344 }
3345 if (tp_features.hotkey_tablet) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003346 input_set_capability(tpacpi_inputdev, EV_SW, SW_TABLET_MODE);
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003347 input_report_switch(tpacpi_inputdev,
3348 SW_TABLET_MODE, tabletsw_state);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003349 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003350
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003351 /* Do not issue duplicate brightness change events to
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03003352 * userspace. tpacpi_detect_brightness_capabilities() must have
3353 * been called before this point */
Henrique de Moraes Holschuh8bf3d4c2009-05-30 13:25:09 -03003354 if (tp_features.bright_acpimode && acpi_video_backlight_support()) {
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003355 printk(TPACPI_INFO
3356 "This ThinkPad has standard ACPI backlight "
3357 "brightness control, supported by the ACPI "
3358 "video driver\n");
3359 printk(TPACPI_NOTICE
3360 "Disabling thinkpad-acpi brightness events "
3361 "by default...\n");
3362
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003363 /* Disable brightness up/down on Lenovo thinkpads when
3364 * ACPI is handling them, otherwise it is plain impossible
3365 * for userspace to do something even remotely sane */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003366 hotkey_reserved_mask |=
3367 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3368 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003369 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
3370 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003371 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003372
Henrique de Moraes Holschuh230d8cf252009-09-12 15:22:17 -03003373#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003374 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3375 & ~hotkey_all_mask
3376 & ~hotkey_reserved_mask;
Henrique de Moraes Holschuh230d8cf252009-09-12 15:22:17 -03003377
3378 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3379 "hotkey source mask 0x%08x, polling freq %u\n",
3380 hotkey_source_mask, hotkey_poll_freq);
3381#endif
3382
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003383 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3384 "enabling firmware HKEY event interface...\n");
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003385 res = hotkey_status_set(true);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003386 if (res) {
3387 hotkey_exit();
3388 return res;
3389 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003390 res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
3391 | hotkey_driver_mask)
3392 & ~hotkey_source_mask);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003393 if (res < 0 && res != -ENXIO) {
3394 hotkey_exit();
3395 return res;
3396 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003397 hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
3398 & ~hotkey_reserved_mask;
3399 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3400 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3401 hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003402
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003403 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3404 "legacy ibm/hotkey event reporting over procfs %s\n",
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003405 (hotkey_report_mode < 2) ?
3406 "enabled" : "disabled");
3407
3408 tpacpi_inputdev->open = &hotkey_inputdev_open;
3409 tpacpi_inputdev->close = &hotkey_inputdev_close;
3410
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003411 hotkey_poll_setup_safe(true);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003412
3413 return 0;
3414
3415err_exit:
3416 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3417 hotkey_dev_attributes = NULL;
3418
3419 return (res < 0)? res : 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003420}
3421
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003422static bool hotkey_notify_hotkey(const u32 hkey,
3423 bool *send_acpi_ev,
3424 bool *ignore_acpi_ev)
3425{
3426 /* 0x1000-0x1FFF: key presses */
3427 unsigned int scancode = hkey & 0xfff;
3428 *send_acpi_ev = true;
3429 *ignore_acpi_ev = false;
3430
3431 if (scancode > 0 && scancode < 0x21) {
3432 scancode--;
3433 if (!(hotkey_source_mask & (1 << scancode))) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003434 tpacpi_input_send_key_masked(scancode);
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003435 *send_acpi_ev = false;
3436 } else {
3437 *ignore_acpi_ev = true;
3438 }
3439 return true;
3440 }
3441 return false;
3442}
3443
3444static bool hotkey_notify_wakeup(const u32 hkey,
3445 bool *send_acpi_ev,
3446 bool *ignore_acpi_ev)
3447{
3448 /* 0x2000-0x2FFF: Wakeup reason */
3449 *send_acpi_ev = true;
3450 *ignore_acpi_ev = false;
3451
3452 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003453 case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */
3454 case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003455 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3456 *ignore_acpi_ev = true;
3457 break;
3458
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003459 case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */
3460 case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003461 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3462 *ignore_acpi_ev = true;
3463 break;
3464
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003465 case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */
3466 case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003467 printk(TPACPI_ALERT
3468 "EMERGENCY WAKEUP: battery almost empty\n");
3469 /* how to auto-heal: */
3470 /* 2313: woke up from S3, go to S4/S5 */
3471 /* 2413: woke up from S4, go to S5 */
3472 break;
3473
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003474 default:
3475 return false;
3476 }
3477
3478 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
3479 printk(TPACPI_INFO
3480 "woke up due to a hot-unplug "
3481 "request...\n");
3482 hotkey_wakeup_reason_notify_change();
3483 }
3484 return true;
3485}
3486
3487static bool hotkey_notify_usrevent(const u32 hkey,
3488 bool *send_acpi_ev,
3489 bool *ignore_acpi_ev)
3490{
3491 /* 0x5000-0x5FFF: human interface helpers */
3492 *send_acpi_ev = true;
3493 *ignore_acpi_ev = false;
3494
3495 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003496 case TP_HKEY_EV_PEN_INSERTED: /* X61t: tablet pen inserted into bay */
3497 case TP_HKEY_EV_PEN_REMOVED: /* X61t: tablet pen removed from bay */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003498 return true;
3499
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003500 case TP_HKEY_EV_TABLET_TABLET: /* X41t-X61t: tablet mode */
3501 case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003502 tpacpi_input_send_tabletsw();
3503 hotkey_tablet_mode_notify_change();
3504 *send_acpi_ev = false;
3505 return true;
3506
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003507 case TP_HKEY_EV_LID_CLOSE: /* Lid closed */
3508 case TP_HKEY_EV_LID_OPEN: /* Lid opened */
3509 case TP_HKEY_EV_BRGHT_CHANGED: /* brightness changed */
Henrique de Moraes Holschuh176dd982009-09-20 14:09:24 -03003510 /* do not propagate these events */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003511 *ignore_acpi_ev = true;
3512 return true;
3513
3514 default:
3515 return false;
3516 }
3517}
3518
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003519static void thermal_dump_all_sensors(void);
3520
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003521static bool hotkey_notify_thermal(const u32 hkey,
3522 bool *send_acpi_ev,
3523 bool *ignore_acpi_ev)
3524{
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003525 bool known = true;
3526
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003527 /* 0x6000-0x6FFF: thermal alarms */
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_THM_TABLE_CHANGED:
Henrique de Moraes Holschuh54926ce2009-01-11 03:01:09 -02003533 printk(TPACPI_INFO
3534 "EC reports that Thermal Table has changed\n");
3535 /* recommended action: do nothing, we don't have
3536 * Lenovo ATM information */
3537 return true;
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003538 case TP_HKEY_EV_ALARM_BAT_HOT:
3539 printk(TPACPI_CRIT
3540 "THERMAL ALARM: battery is too hot!\n");
3541 /* recommended action: warn user through gui */
3542 break;
3543 case TP_HKEY_EV_ALARM_BAT_XHOT:
3544 printk(TPACPI_ALERT
3545 "THERMAL EMERGENCY: battery is extremely hot!\n");
3546 /* recommended action: immediate sleep/hibernate */
3547 break;
3548 case TP_HKEY_EV_ALARM_SENSOR_HOT:
3549 printk(TPACPI_CRIT
3550 "THERMAL ALARM: "
3551 "a sensor reports something is too hot!\n");
3552 /* recommended action: warn user through gui, that */
3553 /* some internal component is too hot */
3554 break;
3555 case TP_HKEY_EV_ALARM_SENSOR_XHOT:
3556 printk(TPACPI_ALERT
3557 "THERMAL EMERGENCY: "
3558 "a sensor reports something is extremely hot!\n");
3559 /* recommended action: immediate sleep/hibernate */
3560 break;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003561 default:
3562 printk(TPACPI_ALERT
3563 "THERMAL ALERT: unknown thermal alarm received\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003564 known = false;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003565 }
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003566
3567 thermal_dump_all_sensors();
3568
3569 return known;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003570}
3571
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003572static void hotkey_notify(struct ibm_struct *ibm, u32 event)
3573{
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003574 u32 hkey;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003575 bool send_acpi_ev;
3576 bool ignore_acpi_ev;
3577 bool known_ev;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003578
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003579 if (event != 0x80) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003580 printk(TPACPI_ERR
3581 "unknown HKEY notification event %d\n", event);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003582 /* forward it to userspace, maybe it knows how to handle it */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003583 acpi_bus_generate_netlink_event(
3584 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02003585 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003586 event, 0);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003587 return;
3588 }
3589
3590 while (1) {
3591 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003592 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003593 return;
3594 }
3595
3596 if (hkey == 0) {
3597 /* queue empty */
3598 return;
3599 }
3600
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003601 send_acpi_ev = true;
3602 ignore_acpi_ev = false;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003603
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003604 switch (hkey >> 12) {
3605 case 1:
3606 /* 0x1000-0x1FFF: key presses */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003607 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
3608 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003609 break;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003610 case 2:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003611 /* 0x2000-0x2FFF: Wakeup reason */
3612 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
3613 &ignore_acpi_ev);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003614 break;
3615 case 3:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003616 /* 0x3000-0x3FFF: bay-related wakeups */
Henrique de Moraes Holschuhbf8b29c2010-02-25 21:28:56 -03003617 switch (hkey) {
3618 case TP_HKEY_EV_BAYEJ_ACK:
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003619 hotkey_autosleep_ack = 1;
3620 printk(TPACPI_INFO
3621 "bay ejected\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003622 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003623 known_ev = true;
Henrique de Moraes Holschuhbf8b29c2010-02-25 21:28:56 -03003624 break;
3625 case TP_HKEY_EV_OPTDRV_EJ:
3626 /* FIXME: kick libata if SATA link offline */
3627 known_ev = true;
3628 break;
3629 default:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003630 known_ev = false;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003631 }
3632 break;
3633 case 4:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003634 /* 0x4000-0x4FFF: dock-related wakeups */
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003635 if (hkey == TP_HKEY_EV_UNDOCK_ACK) {
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003636 hotkey_autosleep_ack = 1;
3637 printk(TPACPI_INFO
3638 "undocked\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003639 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003640 known_ev = true;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003641 } else {
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003642 known_ev = false;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003643 }
3644 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003645 case 5:
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02003646 /* 0x5000-0x5FFF: human interface helpers */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003647 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
3648 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003649 break;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003650 case 6:
3651 /* 0x6000-0x6FFF: thermal alarms */
3652 known_ev = hotkey_notify_thermal(hkey, &send_acpi_ev,
3653 &ignore_acpi_ev);
3654 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003655 case 7:
3656 /* 0x7000-0x7FFF: misc */
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003657 if (tp_features.hotkey_wlsw &&
3658 hkey == TP_HKEY_EV_RFKILL_CHANGED) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003659 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02003660 send_acpi_ev = 0;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003661 known_ev = true;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003662 break;
3663 }
3664 /* fallthrough to default */
3665 default:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003666 known_ev = false;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02003667 }
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003668 if (!known_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003669 printk(TPACPI_NOTICE
3670 "unhandled HKEY event 0x%04x\n", hkey);
Henrique de Moraes Holschuhcb429352009-01-11 03:01:07 -02003671 printk(TPACPI_NOTICE
3672 "please report the conditions when this "
3673 "event happened to %s\n", TPACPI_MAIL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003674 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003675
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003676 /* Legacy events */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003677 if (!ignore_acpi_ev &&
3678 (send_acpi_ev || hotkey_report_mode < 2)) {
3679 acpi_bus_generate_proc_event(ibm->acpi->device,
3680 event, hkey);
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03003681 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003682
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003683 /* netlink events */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03003684 if (!ignore_acpi_ev && send_acpi_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003685 acpi_bus_generate_netlink_event(
3686 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02003687 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003688 event, hkey);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003689 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003690 }
3691}
3692
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003693static void hotkey_suspend(pm_message_t state)
3694{
3695 /* Do these on suspend, we get the events on early resume! */
3696 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
3697 hotkey_autosleep_ack = 0;
3698}
3699
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03003700static void hotkey_resume(void)
3701{
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03003702 tpacpi_disable_brightness_delay();
3703
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003704 if (hotkey_status_set(true) < 0 ||
3705 hotkey_mask_set(hotkey_acpi_mask) < 0)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003706 printk(TPACPI_ERR
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003707 "error while attempting to reset the event "
3708 "firmware interface\n");
3709
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003710 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02003711 hotkey_tablet_mode_notify_change();
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003712 hotkey_wakeup_reason_notify_change();
3713 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003714 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03003715}
3716
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003717/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003718static int hotkey_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03003720 int res, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003722 if (!tp_features.hotkey) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003723 seq_printf(m, "status:\t\tnot supported\n");
3724 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003725 }
3726
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02003727 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02003728 return -ERESTARTSYS;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02003729 res = hotkey_status_get(&status);
3730 if (!res)
3731 res = hotkey_mask_get();
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003732 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03003733 if (res)
3734 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003736 seq_printf(m, "status:\t\t%s\n", enabled(status, 0));
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003737 if (hotkey_all_mask) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003738 seq_printf(m, "mask:\t\t0x%08x\n", hotkey_user_mask);
3739 seq_printf(m, "commands:\tenable, disable, reset, <mask>\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740 } else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003741 seq_printf(m, "mask:\t\tnot supported\n");
3742 seq_printf(m, "commands:\tenable, disable, reset\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743 }
3744
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003745 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003746}
3747
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003748static void hotkey_enabledisable_warn(bool enable)
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003749{
3750 tpacpi_log_usertask("procfs hotkey enable/disable");
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003751 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
3752 TPACPI_WARN
3753 "hotkey enable/disable functionality has been "
3754 "removed from the driver. Hotkeys are always "
3755 "enabled\n"))
3756 printk(TPACPI_ERR
3757 "Please remove the hotkey=enable module "
3758 "parameter, it is deprecated. Hotkeys are always "
3759 "enabled\n");
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003760}
3761
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003762static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003764 int res;
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03003765 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003768 if (!tp_features.hotkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769 return -ENODEV;
3770
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02003771 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02003772 return -ERESTARTSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003773
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003774 mask = hotkey_user_mask;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003775
3776 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777 while ((cmd = next_cmd(&buf))) {
3778 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003779 hotkey_enabledisable_warn(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003781 hotkey_enabledisable_warn(0);
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003782 res = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783 } else if (strlencmp(cmd, "reset") == 0) {
Henrique de Moraes Holschuh20c9aa42009-09-12 15:22:16 -03003784 mask = (hotkey_all_mask | hotkey_source_mask)
3785 & ~hotkey_reserved_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
3787 /* mask set */
3788 } else if (sscanf(cmd, "%x", &mask) == 1) {
3789 /* mask set */
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003790 } else {
3791 res = -EINVAL;
3792 goto errexit;
3793 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794 }
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003795
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003796 if (!res) {
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003797 tpacpi_disclose_usertask("procfs hotkey",
3798 "set mask to 0x%08x\n", mask);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003799 res = hotkey_user_mask_set(mask);
3800 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003802errexit:
3803 mutex_unlock(&hotkey_mutex);
3804 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003805}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003807static const struct acpi_device_id ibm_htk_device_ids[] = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003808 {TPACPI_ACPI_HKEY_HID, 0},
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003809 {"", 0},
3810};
3811
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003812static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003813 .hid = ibm_htk_device_ids,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003814 .notify = hotkey_notify,
3815 .handle = &hkey_handle,
3816 .type = ACPI_DEVICE_NOTIFY,
3817};
3818
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003819static struct ibm_struct hotkey_driver_data = {
3820 .name = "hotkey",
3821 .read = hotkey_read,
3822 .write = hotkey_write,
3823 .exit = hotkey_exit,
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03003824 .resume = hotkey_resume,
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003825 .suspend = hotkey_suspend,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003826 .acpi = &ibm_hotkey_acpidriver,
3827};
3828
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003829/*************************************************************************
3830 * Bluetooth subdriver
3831 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003833enum {
3834 /* ACPI GBDC/SBDC bits */
3835 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
3836 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003837 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03003838 0 = disable, 1 = enable */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003839};
3840
3841enum {
3842 /* ACPI \BLTH commands */
3843 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
3844 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
3845 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
3846 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
3847 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003848};
3849
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003850#define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
3851
Johannes Berg19d337d2009-06-02 13:01:37 +02003852static int bluetooth_get_status(void)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003853{
3854 int status;
3855
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003856#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3857 if (dbg_bluetoothemul)
3858 return (tpacpi_bluetooth_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02003859 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003860#endif
3861
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003862 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
3863 return -EIO;
3864
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003865 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02003866 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003867}
3868
Johannes Berg19d337d2009-06-02 13:01:37 +02003869static int bluetooth_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003870{
3871 int status;
3872
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003873 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02003874 "will attempt to %s bluetooth\n",
3875 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003876
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003877#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3878 if (dbg_bluetoothemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02003879 tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003880 return 0;
3881 }
3882#endif
3883
Johannes Berg19d337d2009-06-02 13:01:37 +02003884 if (state == TPACPI_RFK_RADIO_ON)
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03003885 status = TP_ACPI_BLUETOOTH_RADIOSSW
3886 | TP_ACPI_BLUETOOTH_RESUMECTRL;
3887 else
3888 status = 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02003889
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003890 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
3891 return -EIO;
3892
3893 return 0;
3894}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003895
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003896/* sysfs bluetooth enable ---------------------------------------------- */
3897static ssize_t bluetooth_enable_show(struct device *dev,
3898 struct device_attribute *attr,
3899 char *buf)
3900{
Johannes Berg19d337d2009-06-02 13:01:37 +02003901 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID,
3902 attr, buf);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003903}
3904
3905static ssize_t bluetooth_enable_store(struct device *dev,
3906 struct device_attribute *attr,
3907 const char *buf, size_t count)
3908{
Johannes Berg19d337d2009-06-02 13:01:37 +02003909 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID,
3910 attr, buf, count);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003911}
3912
3913static struct device_attribute dev_attr_bluetooth_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03003914 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003915 bluetooth_enable_show, bluetooth_enable_store);
3916
3917/* --------------------------------------------------------------------- */
3918
3919static struct attribute *bluetooth_attributes[] = {
3920 &dev_attr_bluetooth_enable.attr,
3921 NULL
3922};
3923
3924static const struct attribute_group bluetooth_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003925 .attrs = bluetooth_attributes,
3926};
3927
Johannes Berg19d337d2009-06-02 13:01:37 +02003928static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = {
3929 .get_status = bluetooth_get_status,
3930 .set_status = bluetooth_set_status,
3931};
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003932
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003933static void bluetooth_shutdown(void)
3934{
3935 /* Order firmware to save current state to NVRAM */
3936 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3937 TP_ACPI_BLTH_SAVE_STATE))
3938 printk(TPACPI_NOTICE
3939 "failed to save bluetooth state to NVRAM\n");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003940 else
3941 vdbg_printk(TPACPI_DBG_RFKILL,
3942 "bluestooth state saved to NVRAM\n");
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003943}
3944
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003945static void bluetooth_exit(void)
3946{
3947 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3948 &bluetooth_attr_group);
Johannes Berg19d337d2009-06-02 13:01:37 +02003949
3950 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
3951
3952 bluetooth_shutdown();
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003953}
3954
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003955static int __init bluetooth_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003957 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003958 int status = 0;
3959
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003960 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3961 "initializing bluetooth subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003962
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003963 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003964
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003965 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3966 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003967 tp_features.bluetooth = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003968 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003970 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3971 "bluetooth is %s, status 0x%02x\n",
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003972 str_supported(tp_features.bluetooth),
3973 status);
3974
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003975#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3976 if (dbg_bluetoothemul) {
3977 tp_features.bluetooth = 1;
3978 printk(TPACPI_INFO
3979 "bluetooth switch emulation enabled\n");
3980 } else
3981#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003982 if (tp_features.bluetooth &&
3983 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
3984 /* no bluetooth hardware present in system */
3985 tp_features.bluetooth = 0;
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003986 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003987 "bluetooth hardware not installed\n");
3988 }
3989
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003990 if (!tp_features.bluetooth)
3991 return 1;
3992
Johannes Berg19d337d2009-06-02 13:01:37 +02003993 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
3994 &bluetooth_tprfk_ops,
3995 RFKILL_TYPE_BLUETOOTH,
3996 TPACPI_RFK_BLUETOOTH_SW_NAME,
3997 true);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003998 if (res)
3999 return res;
4000
Johannes Berg19d337d2009-06-02 13:01:37 +02004001 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4002 &bluetooth_attr_group);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004003 if (res) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004004 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004005 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004006 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004007
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004008 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009}
4010
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004011/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004012static int bluetooth_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013{
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004014 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015}
4016
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004017static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018{
Johannes Berg19d337d2009-06-02 13:01:37 +02004019 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020}
4021
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004022static struct ibm_struct bluetooth_driver_data = {
4023 .name = "bluetooth",
4024 .read = bluetooth_read,
4025 .write = bluetooth_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004026 .exit = bluetooth_exit,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004027 .shutdown = bluetooth_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004028};
4029
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004030/*************************************************************************
4031 * Wan subdriver
4032 */
4033
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004034enum {
4035 /* ACPI GWAN/SWAN bits */
4036 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
4037 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02004038 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03004039 0 = disable, 1 = enable */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004040};
4041
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004042#define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
4043
Johannes Berg19d337d2009-06-02 13:01:37 +02004044static int wan_get_status(void)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004045{
4046 int status;
4047
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004048#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4049 if (dbg_wwanemul)
4050 return (tpacpi_wwan_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004051 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004052#endif
4053
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004054 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
4055 return -EIO;
4056
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004057 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004058 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004059}
4060
Johannes Berg19d337d2009-06-02 13:01:37 +02004061static int wan_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004062{
4063 int status;
4064
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004065 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02004066 "will attempt to %s wwan\n",
4067 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004068
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004069#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4070 if (dbg_wwanemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004071 tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004072 return 0;
4073 }
4074#endif
4075
Johannes Berg19d337d2009-06-02 13:01:37 +02004076 if (state == TPACPI_RFK_RADIO_ON)
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03004077 status = TP_ACPI_WANCARD_RADIOSSW
4078 | TP_ACPI_WANCARD_RESUMECTRL;
4079 else
4080 status = 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02004081
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004082 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
4083 return -EIO;
4084
4085 return 0;
4086}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004087
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004088/* sysfs wan enable ---------------------------------------------------- */
4089static ssize_t wan_enable_show(struct device *dev,
4090 struct device_attribute *attr,
4091 char *buf)
4092{
Johannes Berg19d337d2009-06-02 13:01:37 +02004093 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
4094 attr, buf);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004095}
4096
4097static ssize_t wan_enable_store(struct device *dev,
4098 struct device_attribute *attr,
4099 const char *buf, size_t count)
4100{
Johannes Berg19d337d2009-06-02 13:01:37 +02004101 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
4102 attr, buf, count);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004103}
4104
4105static struct device_attribute dev_attr_wan_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03004106 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004107 wan_enable_show, wan_enable_store);
4108
4109/* --------------------------------------------------------------------- */
4110
4111static struct attribute *wan_attributes[] = {
4112 &dev_attr_wan_enable.attr,
4113 NULL
4114};
4115
4116static const struct attribute_group wan_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004117 .attrs = wan_attributes,
4118};
4119
Johannes Berg19d337d2009-06-02 13:01:37 +02004120static const struct tpacpi_rfk_ops wan_tprfk_ops = {
4121 .get_status = wan_get_status,
4122 .set_status = wan_set_status,
4123};
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004124
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004125static void wan_shutdown(void)
4126{
4127 /* Order firmware to save current state to NVRAM */
4128 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
4129 TP_ACPI_WGSV_SAVE_STATE))
4130 printk(TPACPI_NOTICE
4131 "failed to save WWAN state to NVRAM\n");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004132 else
4133 vdbg_printk(TPACPI_DBG_RFKILL,
4134 "WWAN state saved to NVRAM\n");
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004135}
4136
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004137static void wan_exit(void)
4138{
4139 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4140 &wan_attr_group);
Johannes Berg19d337d2009-06-02 13:01:37 +02004141
4142 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4143
4144 wan_shutdown();
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004145}
4146
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004147static int __init wan_init(struct ibm_init_struct *iibm)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004148{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004149 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004150 int status = 0;
4151
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004152 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4153 "initializing wan subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004154
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004155 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004156
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004157 tp_features.wan = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004158 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004159
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004160 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4161 "wan is %s, status 0x%02x\n",
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004162 str_supported(tp_features.wan),
4163 status);
4164
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004165#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4166 if (dbg_wwanemul) {
4167 tp_features.wan = 1;
4168 printk(TPACPI_INFO
4169 "wwan switch emulation enabled\n");
4170 } else
4171#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004172 if (tp_features.wan &&
4173 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
4174 /* no wan hardware present in system */
4175 tp_features.wan = 0;
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004176 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004177 "wan hardware not installed\n");
4178 }
4179
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004180 if (!tp_features.wan)
4181 return 1;
4182
Johannes Berg19d337d2009-06-02 13:01:37 +02004183 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
4184 &wan_tprfk_ops,
4185 RFKILL_TYPE_WWAN,
4186 TPACPI_RFK_WWAN_SW_NAME,
4187 true);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004188 if (res)
4189 return res;
4190
Johannes Berg19d337d2009-06-02 13:01:37 +02004191 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4192 &wan_attr_group);
4193
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004194 if (res) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004195 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004196 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004197 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004198
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004199 return 0;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004200}
4201
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004202/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004203static int wan_read(struct seq_file *m)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004204{
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004205 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, m);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004206}
4207
4208static int wan_write(char *buf)
4209{
Johannes Berg19d337d2009-06-02 13:01:37 +02004210 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004211}
4212
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004213static struct ibm_struct wan_driver_data = {
4214 .name = "wan",
4215 .read = wan_read,
4216 .write = wan_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004217 .exit = wan_exit,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004218 .shutdown = wan_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004219};
4220
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004221/*************************************************************************
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004222 * UWB subdriver
4223 */
4224
4225enum {
4226 /* ACPI GUWB/SUWB bits */
4227 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4228 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4229};
4230
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004231#define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4232
Johannes Berg19d337d2009-06-02 13:01:37 +02004233static int uwb_get_status(void)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004234{
4235 int status;
4236
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004237#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4238 if (dbg_uwbemul)
4239 return (tpacpi_uwb_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004240 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004241#endif
4242
4243 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4244 return -EIO;
4245
4246 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004247 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004248}
4249
Johannes Berg19d337d2009-06-02 13:01:37 +02004250static int uwb_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004251{
4252 int status;
4253
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004254 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02004255 "will attempt to %s UWB\n",
4256 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004257
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004258#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4259 if (dbg_uwbemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004260 tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004261 return 0;
4262 }
4263#endif
4264
Johannes Berg19d337d2009-06-02 13:01:37 +02004265 if (state == TPACPI_RFK_RADIO_ON)
4266 status = TP_ACPI_UWB_RADIOSSW;
4267 else
4268 status = 0;
4269
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004270 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4271 return -EIO;
4272
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004273 return 0;
4274}
4275
4276/* --------------------------------------------------------------------- */
4277
Johannes Berg19d337d2009-06-02 13:01:37 +02004278static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
4279 .get_status = uwb_get_status,
4280 .set_status = uwb_set_status,
4281};
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004282
4283static void uwb_exit(void)
4284{
Johannes Berg19d337d2009-06-02 13:01:37 +02004285 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004286}
4287
4288static int __init uwb_init(struct ibm_init_struct *iibm)
4289{
4290 int res;
4291 int status = 0;
4292
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004293 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4294 "initializing uwb subdriver\n");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004295
4296 TPACPI_ACPIHANDLE_INIT(hkey);
4297
4298 tp_features.uwb = hkey_handle &&
4299 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4300
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004301 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4302 "uwb is %s, status 0x%02x\n",
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004303 str_supported(tp_features.uwb),
4304 status);
4305
4306#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4307 if (dbg_uwbemul) {
4308 tp_features.uwb = 1;
4309 printk(TPACPI_INFO
4310 "uwb switch emulation enabled\n");
4311 } else
4312#endif
4313 if (tp_features.uwb &&
4314 !(status & TP_ACPI_UWB_HWPRESENT)) {
4315 /* no uwb hardware present in system */
4316 tp_features.uwb = 0;
4317 dbg_printk(TPACPI_DBG_INIT,
4318 "uwb hardware not installed\n");
4319 }
4320
4321 if (!tp_features.uwb)
4322 return 1;
4323
4324 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
Johannes Berg19d337d2009-06-02 13:01:37 +02004325 &uwb_tprfk_ops,
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004326 RFKILL_TYPE_UWB,
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004327 TPACPI_RFK_UWB_SW_NAME,
Johannes Berg19d337d2009-06-02 13:01:37 +02004328 false);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004329 return res;
4330}
4331
4332static struct ibm_struct uwb_driver_data = {
4333 .name = "uwb",
4334 .exit = uwb_exit,
4335 .flags.experimental = 1,
4336};
4337
4338/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004339 * Video subdriver
4340 */
4341
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02004342#ifdef CONFIG_THINKPAD_ACPI_VIDEO
4343
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004344enum video_access_mode {
4345 TPACPI_VIDEO_NONE = 0,
4346 TPACPI_VIDEO_570, /* 570 */
4347 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4348 TPACPI_VIDEO_NEW, /* all others */
4349};
4350
4351enum { /* video status flags, based on VIDEO_570 */
4352 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4353 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4354 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4355};
4356
4357enum { /* TPACPI_VIDEO_570 constants */
4358 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4359 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4360 * video_status_flags */
4361 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4362 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4363};
4364
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02004365static enum video_access_mode video_supported;
4366static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004367
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004368static int video_autosw_get(void);
4369static int video_autosw_set(int enable);
4370
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004371TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004372
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004373static int __init video_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004375 int ivga;
4376
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004377 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4378
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004379 TPACPI_ACPIHANDLE_INIT(vid);
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03004380 if (tpacpi_is_ibm())
4381 TPACPI_ACPIHANDLE_INIT(vid2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004382
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004383 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4384 /* G41, assume IVGA doesn't change */
4385 vid_handle = vid2_handle;
4386
4387 if (!vid_handle)
4388 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004389 video_supported = TPACPI_VIDEO_NONE;
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03004390 else if (tpacpi_is_ibm() &&
4391 acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004392 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004393 video_supported = TPACPI_VIDEO_570;
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03004394 else if (tpacpi_is_ibm() &&
4395 acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004396 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004397 video_supported = TPACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004398 else
4399 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004400 video_supported = TPACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004402 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
4403 str_supported(video_supported != TPACPI_VIDEO_NONE),
4404 video_supported);
4405
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004406 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004407}
4408
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004409static void video_exit(void)
4410{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004411 dbg_printk(TPACPI_DBG_EXIT,
4412 "restoring original video autoswitch mode\n");
4413 if (video_autosw_set(video_orig_autosw))
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004414 printk(TPACPI_ERR "error while trying to restore original "
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004415 "video autoswitch mode\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004416}
4417
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004418static int video_outputsw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419{
4420 int status = 0;
4421 int i;
4422
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004423 switch (video_supported) {
4424 case TPACPI_VIDEO_570:
4425 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
4426 TP_ACPI_VIDEO_570_PHSCMD))
4427 return -EIO;
4428 status = i & TP_ACPI_VIDEO_570_PHSMASK;
4429 break;
4430 case TPACPI_VIDEO_770:
4431 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4432 return -EIO;
4433 if (i)
4434 status |= TP_ACPI_VIDEO_S_LCD;
4435 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4436 return -EIO;
4437 if (i)
4438 status |= TP_ACPI_VIDEO_S_CRT;
4439 break;
4440 case TPACPI_VIDEO_NEW:
4441 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4442 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4443 return -EIO;
4444 if (i)
4445 status |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004447 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4448 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4449 return -EIO;
4450 if (i)
4451 status |= TP_ACPI_VIDEO_S_LCD;
4452 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4453 return -EIO;
4454 if (i)
4455 status |= TP_ACPI_VIDEO_S_DVI;
4456 break;
4457 default:
4458 return -ENOSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004459 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460
4461 return status;
4462}
4463
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004464static int video_outputsw_set(int status)
4465{
4466 int autosw;
4467 int res = 0;
4468
4469 switch (video_supported) {
4470 case TPACPI_VIDEO_570:
4471 res = acpi_evalf(NULL, NULL,
4472 "\\_SB.PHS2", "vdd",
4473 TP_ACPI_VIDEO_570_PHS2CMD,
4474 status | TP_ACPI_VIDEO_570_PHS2SET);
4475 break;
4476 case TPACPI_VIDEO_770:
4477 autosw = video_autosw_get();
4478 if (autosw < 0)
4479 return autosw;
4480
4481 res = video_autosw_set(1);
4482 if (res)
4483 return res;
4484 res = acpi_evalf(vid_handle, NULL,
4485 "ASWT", "vdd", status * 0x100, 0);
4486 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004487 printk(TPACPI_ERR
4488 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004489 return -EIO;
4490 }
4491 break;
4492 case TPACPI_VIDEO_NEW:
4493 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004494 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004495 break;
4496 default:
4497 return -ENOSYS;
4498 }
4499
4500 return (res)? 0 : -EIO;
4501}
4502
4503static int video_autosw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004504{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004505 int autosw = 0;
4506
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004507 switch (video_supported) {
4508 case TPACPI_VIDEO_570:
4509 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
4510 return -EIO;
4511 break;
4512 case TPACPI_VIDEO_770:
4513 case TPACPI_VIDEO_NEW:
4514 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4515 return -EIO;
4516 break;
4517 default:
4518 return -ENOSYS;
4519 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004520
4521 return autosw & 1;
4522}
4523
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004524static int video_autosw_set(int enable)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004525{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004526 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004527 return -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004528 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004529}
4530
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004531static int video_outputsw_cycle(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004532{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004533 int autosw = video_autosw_get();
4534 int res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004535
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004536 if (autosw < 0)
4537 return autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004538
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004539 switch (video_supported) {
4540 case TPACPI_VIDEO_570:
4541 res = video_autosw_set(1);
4542 if (res)
4543 return res;
4544 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4545 break;
4546 case TPACPI_VIDEO_770:
4547 case TPACPI_VIDEO_NEW:
4548 res = video_autosw_set(1);
4549 if (res)
4550 return res;
4551 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4552 break;
4553 default:
4554 return -ENOSYS;
4555 }
4556 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004557 printk(TPACPI_ERR
4558 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004559 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004560 }
4561
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004562 return (res)? 0 : -EIO;
4563}
4564
4565static int video_expand_toggle(void)
4566{
4567 switch (video_supported) {
4568 case TPACPI_VIDEO_570:
4569 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
4570 0 : -EIO;
4571 case TPACPI_VIDEO_770:
4572 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
4573 0 : -EIO;
4574 case TPACPI_VIDEO_NEW:
4575 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
4576 0 : -EIO;
4577 default:
4578 return -ENOSYS;
4579 }
4580 /* not reached */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004581}
4582
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004583static int video_read(struct seq_file *m)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004584{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004585 int status, autosw;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004586
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004587 if (video_supported == TPACPI_VIDEO_NONE) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004588 seq_printf(m, "status:\t\tnot supported\n");
4589 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004590 }
4591
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03004592 /* Even reads can crash X.org, so... */
4593 if (!capable(CAP_SYS_ADMIN))
4594 return -EPERM;
4595
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004596 status = video_outputsw_get();
4597 if (status < 0)
4598 return status;
4599
4600 autosw = video_autosw_get();
4601 if (autosw < 0)
4602 return autosw;
4603
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004604 seq_printf(m, "status:\t\tsupported\n");
4605 seq_printf(m, "lcd:\t\t%s\n", enabled(status, 0));
4606 seq_printf(m, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004607 if (video_supported == TPACPI_VIDEO_NEW)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004608 seq_printf(m, "dvi:\t\t%s\n", enabled(status, 3));
4609 seq_printf(m, "auto:\t\t%s\n", enabled(autosw, 0));
4610 seq_printf(m, "commands:\tlcd_enable, lcd_disable\n");
4611 seq_printf(m, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004612 if (video_supported == TPACPI_VIDEO_NEW)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004613 seq_printf(m, "commands:\tdvi_enable, dvi_disable\n");
4614 seq_printf(m, "commands:\tauto_enable, auto_disable\n");
4615 seq_printf(m, "commands:\tvideo_switch, expand_toggle\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004616
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004617 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004618}
4619
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004620static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621{
4622 char *cmd;
4623 int enable, disable, status;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004624 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004625
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004626 if (video_supported == TPACPI_VIDEO_NONE)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004627 return -ENODEV;
4628
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03004629 /* Even reads can crash X.org, let alone writes... */
4630 if (!capable(CAP_SYS_ADMIN))
4631 return -EPERM;
4632
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004633 enable = 0;
4634 disable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635
4636 while ((cmd = next_cmd(&buf))) {
4637 if (strlencmp(cmd, "lcd_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004638 enable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639 } else if (strlencmp(cmd, "lcd_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004640 disable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641 } else if (strlencmp(cmd, "crt_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004642 enable |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643 } else if (strlencmp(cmd, "crt_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004644 disable |= TP_ACPI_VIDEO_S_CRT;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004645 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004646 strlencmp(cmd, "dvi_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004647 enable |= TP_ACPI_VIDEO_S_DVI;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004648 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004649 strlencmp(cmd, "dvi_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004650 disable |= TP_ACPI_VIDEO_S_DVI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651 } else if (strlencmp(cmd, "auto_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004652 res = video_autosw_set(1);
4653 if (res)
4654 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655 } else if (strlencmp(cmd, "auto_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004656 res = video_autosw_set(0);
4657 if (res)
4658 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004659 } else if (strlencmp(cmd, "video_switch") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004660 res = video_outputsw_cycle();
4661 if (res)
4662 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004664 res = video_expand_toggle();
4665 if (res)
4666 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667 } else
4668 return -EINVAL;
4669 }
4670
4671 if (enable || disable) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004672 status = video_outputsw_get();
4673 if (status < 0)
4674 return status;
4675 res = video_outputsw_set((status & ~disable) | enable);
4676 if (res)
4677 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678 }
4679
4680 return 0;
4681}
4682
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004683static struct ibm_struct video_driver_data = {
4684 .name = "video",
4685 .read = video_read,
4686 .write = video_write,
4687 .exit = video_exit,
4688};
4689
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02004690#endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4691
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004692/*************************************************************************
4693 * Light (thinklight) subdriver
4694 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004696TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4697TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004698
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004699static int light_get_status(void)
4700{
4701 int status = 0;
4702
4703 if (tp_features.light_status) {
4704 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4705 return -EIO;
4706 return (!!status);
4707 }
4708
4709 return -ENXIO;
4710}
4711
4712static int light_set_status(int status)
4713{
4714 int rc;
4715
4716 if (tp_features.light) {
4717 if (cmos_handle) {
4718 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4719 (status)?
4720 TP_CMOS_THINKLIGHT_ON :
4721 TP_CMOS_THINKLIGHT_OFF);
4722 } else {
4723 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4724 (status)? 1 : 0);
4725 }
4726 return (rc)? 0 : -EIO;
4727 }
4728
4729 return -ENXIO;
4730}
4731
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004732static void light_set_status_worker(struct work_struct *work)
4733{
4734 struct tpacpi_led_classdev *data =
4735 container_of(work, struct tpacpi_led_classdev, work);
4736
4737 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00004738 light_set_status((data->new_state != TPACPI_LED_OFF));
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004739}
4740
4741static void light_sysfs_set(struct led_classdev *led_cdev,
4742 enum led_brightness brightness)
4743{
4744 struct tpacpi_led_classdev *data =
4745 container_of(led_cdev,
4746 struct tpacpi_led_classdev,
4747 led_classdev);
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00004748 data->new_state = (brightness != LED_OFF) ?
4749 TPACPI_LED_ON : TPACPI_LED_OFF;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004750 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004751}
4752
4753static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4754{
4755 return (light_get_status() == 1)? LED_FULL : LED_OFF;
4756}
4757
4758static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4759 .led_classdev = {
4760 .name = "tpacpi::thinklight",
4761 .brightness_set = &light_sysfs_set,
4762 .brightness_get = &light_sysfs_get,
4763 }
4764};
4765
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004766static int __init light_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004767{
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004768 int rc;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004769
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004770 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4771
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03004772 if (tpacpi_is_ibm()) {
4773 TPACPI_ACPIHANDLE_INIT(ledb);
4774 TPACPI_ACPIHANDLE_INIT(lght);
4775 }
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004776 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004777 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004778
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004779 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004780 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004781
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004782 if (tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004783 /* light status not supported on
4784 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004785 tp_features.light_status =
4786 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004788 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4789 str_supported(tp_features.light),
4790 str_supported(tp_features.light_status));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004791
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004792 if (!tp_features.light)
4793 return 1;
4794
4795 rc = led_classdev_register(&tpacpi_pdev->dev,
4796 &tpacpi_led_thinklight.led_classdev);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004797
4798 if (rc < 0) {
4799 tp_features.light = 0;
4800 tp_features.light_status = 0;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004801 } else {
4802 rc = 0;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004803 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004804
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004805 return rc;
4806}
4807
4808static void light_exit(void)
4809{
4810 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
4811 if (work_pending(&tpacpi_led_thinklight.work))
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004812 flush_workqueue(tpacpi_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813}
4814
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004815static int light_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004816{
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004817 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004818
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004819 if (!tp_features.light) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004820 seq_printf(m, "status:\t\tnot supported\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004821 } else if (!tp_features.light_status) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004822 seq_printf(m, "status:\t\tunknown\n");
4823 seq_printf(m, "commands:\ton, off\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004824 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004825 status = light_get_status();
4826 if (status < 0)
4827 return status;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004828 seq_printf(m, "status:\t\t%s\n", onoff(status, 0));
4829 seq_printf(m, "commands:\ton, off\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004830 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004832 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004833}
4834
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004835static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004837 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004838 int newstatus = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004839
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004840 if (!tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004841 return -ENODEV;
4842
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843 while ((cmd = next_cmd(&buf))) {
4844 if (strlencmp(cmd, "on") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004845 newstatus = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846 } else if (strlencmp(cmd, "off") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004847 newstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004848 } else
4849 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850 }
4851
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004852 return light_set_status(newstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004853}
4854
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004855static struct ibm_struct light_driver_data = {
4856 .name = "light",
4857 .read = light_read,
4858 .write = light_write,
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004859 .exit = light_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004860};
4861
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004862/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004863 * CMOS subdriver
4864 */
4865
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004866/* sysfs cmos_command -------------------------------------------------- */
4867static ssize_t cmos_command_store(struct device *dev,
4868 struct device_attribute *attr,
4869 const char *buf, size_t count)
4870{
4871 unsigned long cmos_cmd;
4872 int res;
4873
4874 if (parse_strtoul(buf, 21, &cmos_cmd))
4875 return -EINVAL;
4876
4877 res = issue_thinkpad_cmos_command(cmos_cmd);
4878 return (res)? res : count;
4879}
4880
4881static struct device_attribute dev_attr_cmos_command =
4882 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4883
4884/* --------------------------------------------------------------------- */
4885
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004886static int __init cmos_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004887{
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004888 int res;
4889
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004890 vdbg_printk(TPACPI_DBG_INIT,
4891 "initializing cmos commands subdriver\n");
4892
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004893 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004894
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004895 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4896 str_supported(cmos_handle != NULL));
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004897
4898 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4899 if (res)
4900 return res;
4901
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004902 return (cmos_handle)? 0 : 1;
4903}
4904
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004905static void cmos_exit(void)
4906{
4907 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4908}
4909
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004910static int cmos_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004911{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004912 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4913 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914 if (!cmos_handle)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004915 seq_printf(m, "status:\t\tnot supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916 else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004917 seq_printf(m, "status:\t\tsupported\n");
4918 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004919 }
4920
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004921 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004922}
4923
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004924static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925{
4926 char *cmd;
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004927 int cmos_cmd, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004928
4929 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004930 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
4931 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932 /* cmos_cmd set */
4933 } else
4934 return -EINVAL;
4935
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004936 res = issue_thinkpad_cmos_command(cmos_cmd);
4937 if (res)
4938 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004939 }
4940
4941 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004942}
4943
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004944static struct ibm_struct cmos_driver_data = {
4945 .name = "cmos",
4946 .read = cmos_read,
4947 .write = cmos_write,
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004948 .exit = cmos_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004949};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004950
4951/*************************************************************************
4952 * LED subdriver
4953 */
4954
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004955enum led_access_mode {
4956 TPACPI_LED_NONE = 0,
4957 TPACPI_LED_570, /* 570 */
4958 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4959 TPACPI_LED_NEW, /* all others */
4960};
4961
4962enum { /* For TPACPI_LED_OLD */
4963 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
4964 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
4965 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
4966};
4967
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02004968static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004969
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004970TPACPI_HANDLE(led, ec, "SLED", /* 570 */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004971 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
4972 /* T20-22, X20-21 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004973 "LED", /* all others */
4974 ); /* R30, R31 */
4975
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03004976#define TPACPI_LED_NUMLEDS 16
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004977static struct tpacpi_led_classdev *tpacpi_leds;
4978static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
Harvey Harrisone3aa51f2008-05-29 17:51:57 -07004979static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004980 /* there's a limit of 19 chars + NULL before 2.6.26 */
4981 "tpacpi::power",
4982 "tpacpi:orange:batt",
4983 "tpacpi:green:batt",
4984 "tpacpi::dock_active",
4985 "tpacpi::bay_active",
4986 "tpacpi::dock_batt",
4987 "tpacpi::unknown_led",
4988 "tpacpi::standby",
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03004989 "tpacpi::dock_status1",
4990 "tpacpi::dock_status2",
4991 "tpacpi::unknown_led2",
4992 "tpacpi::unknown_led3",
4993 "tpacpi::thinkvantage",
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004994};
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03004995#define TPACPI_SAFE_LEDS 0x1081U
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004996
4997static inline bool tpacpi_is_led_restricted(const unsigned int led)
4998{
4999#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5000 return false;
5001#else
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005002 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005003#endif
5004}
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005005
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005006static int led_get_status(const unsigned int led)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005007{
5008 int status;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005009 enum led_status_t led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005010
5011 switch (led_supported) {
5012 case TPACPI_LED_570:
5013 if (!acpi_evalf(ec_handle,
5014 &status, "GLED", "dd", 1 << led))
5015 return -EIO;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005016 led_s = (status == 0)?
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005017 TPACPI_LED_OFF :
5018 ((status == 1)?
5019 TPACPI_LED_ON :
5020 TPACPI_LED_BLINK);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005021 tpacpi_led_state_cache[led] = led_s;
5022 return led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005023 default:
5024 return -ENXIO;
5025 }
5026
5027 /* not reached */
5028}
5029
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005030static int led_set_status(const unsigned int led,
5031 const enum led_status_t ledstatus)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005032{
5033 /* off, on, blink. Index is led_status_t */
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005034 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
5035 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005036
5037 int rc = 0;
5038
5039 switch (led_supported) {
5040 case TPACPI_LED_570:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005041 /* 570 */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005042 if (unlikely(led > 7))
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005043 return -EINVAL;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005044 if (unlikely(tpacpi_is_led_restricted(led)))
5045 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005046 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5047 (1 << led), led_sled_arg1[ledstatus]))
5048 rc = -EIO;
5049 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005050 case TPACPI_LED_OLD:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005051 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005052 if (unlikely(led > 7))
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005053 return -EINVAL;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005054 if (unlikely(tpacpi_is_led_restricted(led)))
5055 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005056 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
5057 if (rc >= 0)
5058 rc = ec_write(TPACPI_LED_EC_HLBL,
5059 (ledstatus == TPACPI_LED_BLINK) << led);
5060 if (rc >= 0)
5061 rc = ec_write(TPACPI_LED_EC_HLCL,
5062 (ledstatus != TPACPI_LED_OFF) << led);
5063 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005064 case TPACPI_LED_NEW:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005065 /* all others */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005066 if (unlikely(led >= TPACPI_LED_NUMLEDS))
5067 return -EINVAL;
5068 if (unlikely(tpacpi_is_led_restricted(led)))
5069 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005070 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5071 led, led_led_arg1[ledstatus]))
5072 rc = -EIO;
5073 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005074 default:
5075 rc = -ENXIO;
5076 }
5077
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005078 if (!rc)
5079 tpacpi_led_state_cache[led] = ledstatus;
5080
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005081 return rc;
5082}
5083
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005084static void led_set_status_worker(struct work_struct *work)
5085{
5086 struct tpacpi_led_classdev *data =
5087 container_of(work, struct tpacpi_led_classdev, work);
5088
5089 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005090 led_set_status(data->led, data->new_state);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005091}
5092
5093static void led_sysfs_set(struct led_classdev *led_cdev,
5094 enum led_brightness brightness)
5095{
5096 struct tpacpi_led_classdev *data = container_of(led_cdev,
5097 struct tpacpi_led_classdev, led_classdev);
5098
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005099 if (brightness == LED_OFF)
5100 data->new_state = TPACPI_LED_OFF;
5101 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
5102 data->new_state = TPACPI_LED_ON;
5103 else
5104 data->new_state = TPACPI_LED_BLINK;
5105
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03005106 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005107}
5108
5109static int led_sysfs_blink_set(struct led_classdev *led_cdev,
5110 unsigned long *delay_on, unsigned long *delay_off)
5111{
5112 struct tpacpi_led_classdev *data = container_of(led_cdev,
5113 struct tpacpi_led_classdev, led_classdev);
5114
5115 /* Can we choose the flash rate? */
5116 if (*delay_on == 0 && *delay_off == 0) {
5117 /* yes. set them to the hardware blink rate (1 Hz) */
5118 *delay_on = 500; /* ms */
5119 *delay_off = 500; /* ms */
5120 } else if ((*delay_on != 500) || (*delay_off != 500))
5121 return -EINVAL;
5122
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005123 data->new_state = TPACPI_LED_BLINK;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03005124 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005125
5126 return 0;
5127}
5128
5129static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
5130{
5131 int rc;
5132
5133 struct tpacpi_led_classdev *data = container_of(led_cdev,
5134 struct tpacpi_led_classdev, led_classdev);
5135
5136 rc = led_get_status(data->led);
5137
5138 if (rc == TPACPI_LED_OFF || rc < 0)
5139 rc = LED_OFF; /* no error handling in led class :( */
5140 else
5141 rc = LED_FULL;
5142
5143 return rc;
5144}
5145
5146static void led_exit(void)
5147{
5148 unsigned int i;
5149
5150 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5151 if (tpacpi_leds[i].led_classdev.name)
5152 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
5153 }
5154
5155 kfree(tpacpi_leds);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005156}
5157
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005158static int __init tpacpi_init_led(unsigned int led)
5159{
5160 int rc;
5161
5162 tpacpi_leds[led].led = led;
5163
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005164 /* LEDs with no name don't get registered */
5165 if (!tpacpi_led_names[led])
5166 return 0;
5167
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005168 tpacpi_leds[led].led_classdev.brightness_set = &led_sysfs_set;
5169 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5170 if (led_supported == TPACPI_LED_570)
5171 tpacpi_leds[led].led_classdev.brightness_get =
5172 &led_sysfs_get;
5173
5174 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5175
5176 INIT_WORK(&tpacpi_leds[led].work, led_set_status_worker);
5177
5178 rc = led_classdev_register(&tpacpi_pdev->dev,
5179 &tpacpi_leds[led].led_classdev);
5180 if (rc < 0)
5181 tpacpi_leds[led].led_classdev.name = NULL;
5182
5183 return rc;
5184}
5185
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005186static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
5187 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5188 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5189 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5190
5191 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5192 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5193 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5194 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5195 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5196 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5197 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5198 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5199
5200 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5201 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5202 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5203 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5204 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5205
5206 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5207 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5208 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5209 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5210
5211 /* (1) - may have excess leds enabled on MSB */
5212
5213 /* Defaults (order matters, keep last, don't reorder!) */
5214 { /* Lenovo */
5215 .vendor = PCI_VENDOR_ID_LENOVO,
5216 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5217 .quirks = 0x1fffU,
5218 },
5219 { /* IBM ThinkPads with no EC version string */
5220 .vendor = PCI_VENDOR_ID_IBM,
5221 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
5222 .quirks = 0x00ffU,
5223 },
5224 { /* IBM ThinkPads with EC version string */
5225 .vendor = PCI_VENDOR_ID_IBM,
5226 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5227 .quirks = 0x00bfU,
5228 },
5229};
5230
5231#undef TPACPI_LEDQ_IBM
5232#undef TPACPI_LEDQ_LNV
5233
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005234static int __init led_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005235{
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005236 unsigned int i;
5237 int rc;
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005238 unsigned long useful_leds;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005239
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005240 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
5241
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005242 TPACPI_ACPIHANDLE_INIT(led);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005243
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005244 if (!led_handle)
5245 /* led not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005246 led_supported = TPACPI_LED_NONE;
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03005247 else if (tpacpi_is_ibm() && strlencmp(led_path, "SLED") == 0)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005248 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005249 led_supported = TPACPI_LED_570;
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03005250 else if (tpacpi_is_ibm() && strlencmp(led_path, "SYSL") == 0)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005251 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005252 led_supported = TPACPI_LED_OLD;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005253 else
5254 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005255 led_supported = TPACPI_LED_NEW;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005256
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005257 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
5258 str_supported(led_supported), led_supported);
5259
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005260 if (led_supported == TPACPI_LED_NONE)
5261 return 1;
5262
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005263 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
5264 GFP_KERNEL);
5265 if (!tpacpi_leds) {
5266 printk(TPACPI_ERR "Out of memory for LED data\n");
5267 return -ENOMEM;
5268 }
5269
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005270 useful_leds = tpacpi_check_quirks(led_useful_qtable,
5271 ARRAY_SIZE(led_useful_qtable));
5272
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005273 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005274 if (!tpacpi_is_led_restricted(i) &&
5275 test_bit(i, &useful_leds)) {
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005276 rc = tpacpi_init_led(i);
5277 if (rc < 0) {
5278 led_exit();
5279 return rc;
5280 }
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005281 }
5282 }
5283
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005284#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005285 printk(TPACPI_NOTICE
5286 "warning: userspace override of important "
5287 "firmware LEDs is enabled\n");
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005288#endif
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005289 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005290}
5291
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005292#define str_led_status(s) \
5293 ((s) == TPACPI_LED_OFF ? "off" : \
5294 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005295
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005296static int led_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005297{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005298 if (!led_supported) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005299 seq_printf(m, "status:\t\tnot supported\n");
5300 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005301 }
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005302 seq_printf(m, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005303
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005304 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005305 /* 570 */
5306 int i, status;
5307 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005308 status = led_get_status(i);
5309 if (status < 0)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005310 return -EIO;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005311 seq_printf(m, "%d:\t\t%s\n",
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005312 i, str_led_status(status));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005313 }
5314 }
5315
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005316 seq_printf(m, "commands:\t"
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005317 "<led> on, <led> off, <led> blink (<led> is 0-15)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005318
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005319 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005320}
5321
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005322static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005323{
5324 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005325 int led, rc;
5326 enum led_status_t s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005327
5328 if (!led_supported)
5329 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005330
5331 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005332 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 15)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005333 return -EINVAL;
5334
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005335 if (strstr(cmd, "off")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005336 s = TPACPI_LED_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005337 } else if (strstr(cmd, "on")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005338 s = TPACPI_LED_ON;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005339 } else if (strstr(cmd, "blink")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005340 s = TPACPI_LED_BLINK;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005341 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005342 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005343 }
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005344
5345 rc = led_set_status(led, s);
5346 if (rc < 0)
5347 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005348 }
5349
5350 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005351}
5352
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005353static struct ibm_struct led_driver_data = {
5354 .name = "led",
5355 .read = led_read,
5356 .write = led_write,
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005357 .exit = led_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005358};
5359
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005360/*************************************************************************
5361 * Beep subdriver
5362 */
5363
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005364TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005365
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005366#define TPACPI_BEEP_Q1 0x0001
5367
5368static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
5369 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
5370 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
5371};
5372
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005373static int __init beep_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005374{
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005375 unsigned long quirks;
5376
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005377 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
5378
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005379 TPACPI_ACPIHANDLE_INIT(beep);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005380
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005381 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
5382 str_supported(beep_handle != NULL));
5383
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005384 quirks = tpacpi_check_quirks(beep_quirk_table,
5385 ARRAY_SIZE(beep_quirk_table));
5386
5387 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
5388
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005389 return (beep_handle)? 0 : 1;
5390}
5391
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005392static int beep_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005393{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005394 if (!beep_handle)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005395 seq_printf(m, "status:\t\tnot supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005396 else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005397 seq_printf(m, "status:\t\tsupported\n");
5398 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-17)\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005399 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005400
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005401 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005402}
5403
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005404static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005405{
5406 char *cmd;
5407 int beep_cmd;
5408
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005409 if (!beep_handle)
5410 return -ENODEV;
5411
Linus Torvalds1da177e2005-04-16 15:20:36 -07005412 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005413 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
5414 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005415 /* beep_cmd set */
5416 } else
5417 return -EINVAL;
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005418 if (tp_features.beep_needs_two_args) {
5419 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
5420 beep_cmd, 0))
5421 return -EIO;
5422 } else {
5423 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
5424 beep_cmd))
5425 return -EIO;
5426 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005427 }
5428
5429 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005430}
5431
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005432static struct ibm_struct beep_driver_data = {
5433 .name = "beep",
5434 .read = beep_read,
5435 .write = beep_write,
5436};
5437
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005438/*************************************************************************
5439 * Thermal subdriver
5440 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005441
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005442enum thermal_access_mode {
5443 TPACPI_THERMAL_NONE = 0, /* No thermal support */
5444 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
5445 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
5446 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
5447 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
5448};
5449
5450enum { /* TPACPI_THERMAL_TPEC_* */
5451 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
5452 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
5453 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005454
5455 TPACPI_THERMAL_SENSOR_NA = -128000, /* Sensor not available */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005456};
5457
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005458
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005459#define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5460struct ibm_thermal_sensors_struct {
5461 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
5462};
5463
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005464static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005465
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005466/* idx is zero-based */
5467static int thermal_get_sensor(int idx, s32 *value)
5468{
5469 int t;
5470 s8 tmp;
5471 char tmpi[5];
5472
5473 t = TP_EC_THERMAL_TMP0;
5474
5475 switch (thermal_read_mode) {
5476#if TPACPI_MAX_THERMAL_SENSORS >= 16
5477 case TPACPI_THERMAL_TPEC_16:
5478 if (idx >= 8 && idx <= 15) {
5479 t = TP_EC_THERMAL_TMP8;
5480 idx -= 8;
5481 }
5482 /* fallthrough */
5483#endif
5484 case TPACPI_THERMAL_TPEC_8:
5485 if (idx <= 7) {
5486 if (!acpi_ec_read(t + idx, &tmp))
5487 return -EIO;
5488 *value = tmp * 1000;
5489 return 0;
5490 }
5491 break;
5492
5493 case TPACPI_THERMAL_ACPI_UPDT:
5494 if (idx <= 7) {
5495 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5496 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
5497 return -EIO;
5498 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5499 return -EIO;
5500 *value = (t - 2732) * 100;
5501 return 0;
5502 }
5503 break;
5504
5505 case TPACPI_THERMAL_ACPI_TMP07:
5506 if (idx <= 7) {
5507 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5508 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5509 return -EIO;
5510 if (t > 127 || t < -127)
5511 t = TP_EC_THERMAL_TMP_NA;
5512 *value = t * 1000;
5513 return 0;
5514 }
5515 break;
5516
5517 case TPACPI_THERMAL_NONE:
5518 default:
5519 return -ENOSYS;
5520 }
5521
5522 return -EINVAL;
5523}
5524
5525static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5526{
5527 int res, i;
5528 int n;
5529
5530 n = 8;
5531 i = 0;
5532
5533 if (!s)
5534 return -EINVAL;
5535
5536 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5537 n = 16;
5538
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005539 for (i = 0 ; i < n; i++) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005540 res = thermal_get_sensor(i, &s->temp[i]);
5541 if (res)
5542 return res;
5543 }
5544
5545 return n;
5546}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005547
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005548static void thermal_dump_all_sensors(void)
5549{
5550 int n, i;
5551 struct ibm_thermal_sensors_struct t;
5552
5553 n = thermal_get_sensors(&t);
5554 if (n <= 0)
5555 return;
5556
5557 printk(TPACPI_NOTICE
5558 "temperatures (Celsius):");
5559
5560 for (i = 0; i < n; i++) {
5561 if (t.temp[i] != TPACPI_THERMAL_SENSOR_NA)
5562 printk(KERN_CONT " %d", (int)(t.temp[i] / 1000));
5563 else
5564 printk(KERN_CONT " N/A");
5565 }
5566
5567 printk(KERN_CONT "\n");
5568}
5569
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005570/* sysfs temp##_input -------------------------------------------------- */
5571
5572static ssize_t thermal_temp_input_show(struct device *dev,
5573 struct device_attribute *attr,
5574 char *buf)
5575{
5576 struct sensor_device_attribute *sensor_attr =
5577 to_sensor_dev_attr(attr);
5578 int idx = sensor_attr->index;
5579 s32 value;
5580 int res;
5581
5582 res = thermal_get_sensor(idx, &value);
5583 if (res)
5584 return res;
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005585 if (value == TPACPI_THERMAL_SENSOR_NA)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005586 return -ENXIO;
5587
5588 return snprintf(buf, PAGE_SIZE, "%d\n", value);
5589}
5590
5591#define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005592 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5593 thermal_temp_input_show, NULL, _idxB)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005594
5595static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5596 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5597 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5598 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5599 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5600 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5601 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5602 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5603 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5604 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5605 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5606 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5607 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5608 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5609 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5610 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5611 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5612};
5613
5614#define THERMAL_ATTRS(X) \
5615 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5616
5617static struct attribute *thermal_temp_input_attr[] = {
5618 THERMAL_ATTRS(8),
5619 THERMAL_ATTRS(9),
5620 THERMAL_ATTRS(10),
5621 THERMAL_ATTRS(11),
5622 THERMAL_ATTRS(12),
5623 THERMAL_ATTRS(13),
5624 THERMAL_ATTRS(14),
5625 THERMAL_ATTRS(15),
5626 THERMAL_ATTRS(0),
5627 THERMAL_ATTRS(1),
5628 THERMAL_ATTRS(2),
5629 THERMAL_ATTRS(3),
5630 THERMAL_ATTRS(4),
5631 THERMAL_ATTRS(5),
5632 THERMAL_ATTRS(6),
5633 THERMAL_ATTRS(7),
5634 NULL
5635};
5636
5637static const struct attribute_group thermal_temp_input16_group = {
5638 .attrs = thermal_temp_input_attr
5639};
5640
5641static const struct attribute_group thermal_temp_input8_group = {
5642 .attrs = &thermal_temp_input_attr[8]
5643};
5644
5645#undef THERMAL_SENSOR_ATTR_TEMP
5646#undef THERMAL_ATTRS
5647
5648/* --------------------------------------------------------------------- */
5649
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005650static int __init thermal_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005651{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005652 u8 t, ta1, ta2;
5653 int i;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005654 int acpi_tmp7;
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005655 int res;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005656
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005657 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5658
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005659 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005660
Henrique de Moraes Holschuh3d6f99c2007-07-18 23:45:46 -03005661 if (thinkpad_id.ec_model) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005662 /*
5663 * Direct EC access mode: sensors at registers
5664 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5665 * non-implemented, thermal sensors return 0x80 when
5666 * not available
5667 */
5668
5669 ta1 = ta2 = 0;
5670 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03005671 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005672 ta1 |= t;
5673 } else {
5674 ta1 = 0;
5675 break;
5676 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03005677 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005678 ta2 |= t;
5679 } else {
5680 ta1 = 0;
5681 break;
5682 }
5683 }
5684 if (ta1 == 0) {
5685 /* This is sheer paranoia, but we handle it anyway */
5686 if (acpi_tmp7) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005687 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005688 "ThinkPad ACPI EC access misbehaving, "
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005689 "falling back to ACPI TMPx access "
5690 "mode\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005691 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005692 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005693 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005694 "ThinkPad ACPI EC access misbehaving, "
5695 "disabling thermal sensors access\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005696 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005697 }
5698 } else {
5699 thermal_read_mode =
5700 (ta2 != 0) ?
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005701 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005702 }
5703 } else if (acpi_tmp7) {
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03005704 if (tpacpi_is_ibm() &&
5705 acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005706 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005707 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005708 } else {
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03005709 /* IBM/LENOVO DSDT EC.TMPx access, max 8 sensors */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005710 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005711 }
5712 } else {
5713 /* temperatures not supported on 570, G4x, R30, R31, R32 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005714 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005715 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005716
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005717 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5718 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5719 thermal_read_mode);
5720
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005721 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005722 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005723 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005724 &thermal_temp_input16_group);
5725 if (res)
5726 return res;
5727 break;
5728 case TPACPI_THERMAL_TPEC_8:
5729 case TPACPI_THERMAL_ACPI_TMP07:
5730 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005731 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005732 &thermal_temp_input8_group);
5733 if (res)
5734 return res;
5735 break;
5736 case TPACPI_THERMAL_NONE:
5737 default:
5738 return 1;
5739 }
5740
5741 return 0;
5742}
5743
5744static void thermal_exit(void)
5745{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005746 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005747 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005748 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005749 &thermal_temp_input16_group);
5750 break;
5751 case TPACPI_THERMAL_TPEC_8:
5752 case TPACPI_THERMAL_ACPI_TMP07:
5753 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005754 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Roel Kluinf04d5e02010-02-02 14:37:58 -08005755 &thermal_temp_input8_group);
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005756 break;
5757 case TPACPI_THERMAL_NONE:
5758 default:
5759 break;
5760 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005761}
5762
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005763static int thermal_read(struct seq_file *m)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005764{
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005765 int n, i;
5766 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005767
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005768 n = thermal_get_sensors(&t);
5769 if (unlikely(n < 0))
5770 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005771
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005772 seq_printf(m, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005773
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005774 if (n > 0) {
5775 for (i = 0; i < (n - 1); i++)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005776 seq_printf(m, "%d ", t.temp[i] / 1000);
5777 seq_printf(m, "%d\n", t.temp[i] / 1000);
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005778 } else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005779 seq_printf(m, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005780
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005781 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005782}
5783
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005784static struct ibm_struct thermal_driver_data = {
5785 .name = "thermal",
5786 .read = thermal_read,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005787 .exit = thermal_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005788};
5789
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005790/*************************************************************************
5791 * EC Dump subdriver
5792 */
5793
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005794static u8 ecdump_regs[256];
5795
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005796static int ecdump_read(struct seq_file *m)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005797{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005798 int i, j;
5799 u8 v;
5800
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005801 seq_printf(m, "EC "
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005802 " +00 +01 +02 +03 +04 +05 +06 +07"
5803 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
5804 for (i = 0; i < 256; i += 16) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005805 seq_printf(m, "EC 0x%02x:", i);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005806 for (j = 0; j < 16; j++) {
5807 if (!acpi_ec_read(i + j, &v))
5808 break;
5809 if (v != ecdump_regs[i + j])
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005810 seq_printf(m, " *%02x", v);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005811 else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005812 seq_printf(m, " %02x", v);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005813 ecdump_regs[i + j] = v;
5814 }
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005815 seq_putc(m, '\n');
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005816 if (j != 16)
5817 break;
5818 }
5819
5820 /* These are way too dangerous to advertise openly... */
5821#if 0
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005822 seq_printf(m, "commands:\t0x<offset> 0x<value>"
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005823 " (<offset> is 00-ff, <value> is 00-ff)\n");
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005824 seq_printf(m, "commands:\t0x<offset> <value> "
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005825 " (<offset> is 00-ff, <value> is 0-255)\n");
5826#endif
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005827 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005828}
5829
5830static int ecdump_write(char *buf)
5831{
5832 char *cmd;
5833 int i, v;
5834
5835 while ((cmd = next_cmd(&buf))) {
5836 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
5837 /* i and v set */
5838 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
5839 /* i and v set */
5840 } else
5841 return -EINVAL;
5842 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
5843 if (!acpi_ec_write(i, v))
5844 return -EIO;
5845 } else
5846 return -EINVAL;
5847 }
5848
5849 return 0;
5850}
5851
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005852static struct ibm_struct ecdump_driver_data = {
5853 .name = "ecdump",
5854 .read = ecdump_read,
5855 .write = ecdump_write,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03005856 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005857};
5858
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005859/*************************************************************************
5860 * Backlight/brightness subdriver
5861 */
Holger Macht8acb0252006-10-20 14:30:28 -07005862
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005863#define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5864
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005865/*
5866 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
5867 * CMOS NVRAM byte 0x5E, bits 0-3.
5868 *
5869 * EC HBRV (0x31) has the following layout
5870 * Bit 7: unknown function
5871 * Bit 6: unknown function
5872 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5873 * Bit 4: must be set to zero to avoid problems
5874 * Bit 3-0: backlight brightness level
5875 *
5876 * brightness_get_raw returns status data in the HBRV layout
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03005877 *
5878 * WARNING: The X61 has been verified to use HBRV for something else, so
5879 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
5880 * testing on the very early *60 Lenovo models...
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005881 */
5882
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005883enum {
5884 TP_EC_BACKLIGHT = 0x31,
5885
5886 /* TP_EC_BACKLIGHT bitmasks */
5887 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5888 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5889 TP_EC_BACKLIGHT_MAPSW = 0x20,
5890};
5891
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005892enum tpacpi_brightness_access_mode {
5893 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
5894 TPACPI_BRGHT_MODE_EC, /* EC control */
5895 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
5896 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
5897 TPACPI_BRGHT_MODE_MAX
5898};
5899
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03005900static struct backlight_device *ibm_backlight_device;
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005901
5902static enum tpacpi_brightness_access_mode brightness_mode =
5903 TPACPI_BRGHT_MODE_MAX;
5904
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005905static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5906
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005907static struct mutex brightness_mutex;
5908
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005909/* NVRAM brightness access,
5910 * call with brightness_mutex held! */
5911static unsigned int tpacpi_brightness_nvram_get(void)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005912{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005913 u8 lnvram;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005914
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005915 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5916 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5917 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03005918 lnvram &= bright_maxlvl;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005919
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005920 return lnvram;
5921}
5922
5923static void tpacpi_brightness_checkpoint_nvram(void)
5924{
5925 u8 lec = 0;
5926 u8 b_nvram;
5927
5928 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
5929 return;
5930
5931 vdbg_printk(TPACPI_DBG_BRGHT,
5932 "trying to checkpoint backlight level to NVRAM...\n");
5933
5934 if (mutex_lock_killable(&brightness_mutex) < 0)
5935 return;
5936
5937 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5938 goto unlock;
5939 lec &= TP_EC_BACKLIGHT_LVLMSK;
5940 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
5941
5942 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5943 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
5944 /* NVRAM needs update */
5945 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
5946 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
5947 b_nvram |= lec;
5948 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
5949 dbg_printk(TPACPI_DBG_BRGHT,
5950 "updated NVRAM backlight level to %u (0x%02x)\n",
5951 (unsigned int) lec, (unsigned int) b_nvram);
5952 } else
5953 vdbg_printk(TPACPI_DBG_BRGHT,
5954 "NVRAM backlight level already is %u (0x%02x)\n",
5955 (unsigned int) lec, (unsigned int) b_nvram);
5956
5957unlock:
5958 mutex_unlock(&brightness_mutex);
5959}
5960
5961
5962/* call with brightness_mutex held! */
5963static int tpacpi_brightness_get_raw(int *status)
5964{
5965 u8 lec = 0;
5966
5967 switch (brightness_mode) {
5968 case TPACPI_BRGHT_MODE_UCMS_STEP:
5969 *status = tpacpi_brightness_nvram_get();
5970 return 0;
5971 case TPACPI_BRGHT_MODE_EC:
5972 case TPACPI_BRGHT_MODE_ECNVRAM:
5973 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
Henrique de Moraes Holschuh2d5e94d2008-04-26 01:02:20 -03005974 return -EIO;
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005975 *status = lec;
5976 return 0;
5977 default:
5978 return -ENXIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005979 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005980}
5981
5982/* call with brightness_mutex held! */
5983/* do NOT call with illegal backlight level value */
5984static int tpacpi_brightness_set_ec(unsigned int value)
5985{
5986 u8 lec = 0;
5987
5988 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5989 return -EIO;
5990
5991 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
5992 (lec & TP_EC_BACKLIGHT_CMDMSK) |
5993 (value & TP_EC_BACKLIGHT_LVLMSK))))
5994 return -EIO;
5995
5996 return 0;
5997}
5998
5999/* call with brightness_mutex held! */
6000static int tpacpi_brightness_set_ucmsstep(unsigned int value)
6001{
6002 int cmos_cmd, inc;
6003 unsigned int current_value, i;
6004
6005 current_value = tpacpi_brightness_nvram_get();
6006
6007 if (value == current_value)
6008 return 0;
6009
6010 cmos_cmd = (value > current_value) ?
6011 TP_CMOS_BRIGHTNESS_UP :
6012 TP_CMOS_BRIGHTNESS_DOWN;
6013 inc = (value > current_value) ? 1 : -1;
6014
6015 for (i = current_value; i != value; i += inc)
6016 if (issue_thinkpad_cmos_command(cmos_cmd))
6017 return -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006018
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006019 return 0;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006020}
6021
6022/* May return EINTR which can always be mapped to ERESTARTSYS */
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006023static int brightness_set(unsigned int value)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006024{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006025 int res;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006026
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006027 if (value > bright_maxlvl || value < 0)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006028 return -EINVAL;
6029
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006030 vdbg_printk(TPACPI_DBG_BRGHT,
6031 "set backlight level to %d\n", value);
6032
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006033 res = mutex_lock_killable(&brightness_mutex);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006034 if (res < 0)
6035 return res;
6036
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006037 switch (brightness_mode) {
6038 case TPACPI_BRGHT_MODE_EC:
6039 case TPACPI_BRGHT_MODE_ECNVRAM:
6040 res = tpacpi_brightness_set_ec(value);
6041 break;
6042 case TPACPI_BRGHT_MODE_UCMS_STEP:
6043 res = tpacpi_brightness_set_ucmsstep(value);
6044 break;
6045 default:
6046 res = -ENXIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006047 }
6048
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006049 mutex_unlock(&brightness_mutex);
6050 return res;
6051}
6052
6053/* sysfs backlight class ----------------------------------------------- */
6054
6055static int brightness_update_status(struct backlight_device *bd)
6056{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006057 unsigned int level =
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006058 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
6059 bd->props.power == FB_BLANK_UNBLANK) ?
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006060 bd->props.brightness : 0;
6061
6062 dbg_printk(TPACPI_DBG_BRGHT,
6063 "backlight: attempt to set level to %d\n",
6064 level);
6065
6066 /* it is the backlight class's job (caller) to handle
6067 * EINTR and other errors properly */
6068 return brightness_set(level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006069}
Holger Macht8acb0252006-10-20 14:30:28 -07006070
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006071static int brightness_get(struct backlight_device *bd)
6072{
6073 int status, res;
6074
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006075 res = mutex_lock_killable(&brightness_mutex);
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006076 if (res < 0)
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006077 return 0;
6078
6079 res = tpacpi_brightness_get_raw(&status);
6080
6081 mutex_unlock(&brightness_mutex);
6082
6083 if (res < 0)
6084 return 0;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006085
6086 return status & TP_EC_BACKLIGHT_LVLMSK;
6087}
6088
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00006089static void tpacpi_brightness_notify_change(void)
6090{
6091 backlight_force_update(ibm_backlight_device,
6092 BACKLIGHT_UPDATE_HOTKEY);
6093}
6094
Richard Purdie599a52d2007-02-10 23:07:48 +00006095static struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006096 .get_brightness = brightness_get,
6097 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006098};
6099
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006100/* --------------------------------------------------------------------- */
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03006101
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006102static int __init tpacpi_query_bcl_levels(acpi_handle handle)
6103{
6104 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
6105 union acpi_object *obj;
6106 int rc;
6107
6108 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
6109 obj = (union acpi_object *)buffer.pointer;
6110 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
6111 printk(TPACPI_ERR "Unknown _BCL data, "
6112 "please report this to %s\n", TPACPI_MAIL);
6113 rc = 0;
6114 } else {
6115 rc = obj->package.count;
6116 }
6117 } else {
6118 return 0;
6119 }
6120
6121 kfree(buffer.pointer);
6122 return rc;
6123}
6124
6125static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
6126 u32 lvl, void *context, void **rv)
6127{
6128 char name[ACPI_PATH_SEGMENT_LENGTH];
6129 struct acpi_buffer buffer = { sizeof(name), &name };
6130
6131 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
6132 !strncmp("_BCL", name, sizeof(name) - 1)) {
6133 BUG_ON(!rv || !*rv);
6134 **(int **)rv = tpacpi_query_bcl_levels(handle);
6135 return AE_CTRL_TERMINATE;
6136 } else {
6137 return AE_OK;
6138 }
6139}
6140
6141/*
6142 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
6143 */
6144static unsigned int __init tpacpi_check_std_acpi_brightness_support(void)
6145{
6146 int status;
6147 int bcl_levels = 0;
6148 void *bcl_ptr = &bcl_levels;
6149
6150 if (!vid_handle)
6151 TPACPI_ACPIHANDLE_INIT(vid);
6152
6153 if (!vid_handle)
6154 return 0;
6155
6156 /*
6157 * Search for a _BCL method, and execute it. This is safe on all
6158 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
6159 * BIOS in ACPI backlight control mode. We do NOT have to care
6160 * about calling the _BCL method in an enabled video device, any
6161 * will do for our purposes.
6162 */
6163
6164 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
6165 tpacpi_acpi_walk_find_bcl, NULL, NULL,
6166 &bcl_ptr);
6167
6168 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
6169 tp_features.bright_acpimode = 1;
6170 return bcl_levels - 2;
6171 }
6172
6173 return 0;
6174}
6175
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006176/*
6177 * These are only useful for models that have only one possibility
6178 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6179 * these quirks.
6180 */
6181#define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6182#define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6183#define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6184
6185static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
6186 /* Models with ATI GPUs known to require ECNVRAM mode */
6187 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
6188
Henrique de Moraes Holschuh6da25bf2009-09-12 15:22:11 -03006189 /* Models with ATI GPUs that can use ECNVRAM */
Henrique de Moraes Holschuh7d1894d2010-02-25 21:28:56 -03006190 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC), /* R50,51 T40-42 */
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006191 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
Henrique de Moraes Holschuh7d1894d2010-02-25 21:28:56 -03006192 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_EC), /* R52 */
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006193 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6194
Henrique de Moraes Holschuh6da25bf2009-09-12 15:22:11 -03006195 /* Models with Intel Extreme Graphics 2 */
Henrique de Moraes Holschuh7d1894d2010-02-25 21:28:56 -03006196 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC), /* X40 */
Henrique de Moraes Holschuha9f8eac2009-12-09 01:36:21 +00006197 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6198 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006199
6200 /* Models with Intel GMA900 */
6201 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
6202 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
6203 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
6204};
6205
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006206/*
6207 * Returns < 0 for error, otherwise sets tp_features.bright_*
6208 * and bright_maxlvl.
6209 */
6210static void __init tpacpi_detect_brightness_capabilities(void)
6211{
6212 unsigned int b;
6213
6214 vdbg_printk(TPACPI_DBG_INIT,
6215 "detecting firmware brightness interface capabilities\n");
6216
6217 /* we could run a quirks check here (same table used by
6218 * brightness_init) if needed */
6219
6220 /*
6221 * We always attempt to detect acpi support, so as to switch
6222 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6223 * going to publish a backlight interface
6224 */
6225 b = tpacpi_check_std_acpi_brightness_support();
6226 switch (b) {
6227 case 16:
6228 bright_maxlvl = 15;
6229 printk(TPACPI_INFO
6230 "detected a 16-level brightness capable ThinkPad\n");
6231 break;
6232 case 8:
6233 case 0:
6234 bright_maxlvl = 7;
6235 printk(TPACPI_INFO
6236 "detected a 8-level brightness capable ThinkPad\n");
6237 break;
6238 default:
6239 printk(TPACPI_ERR
6240 "Unsupported brightness interface, "
6241 "please contact %s\n", TPACPI_MAIL);
6242 tp_features.bright_unkfw = 1;
6243 bright_maxlvl = b - 1;
6244 }
6245}
6246
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006247static int __init brightness_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006248{
Matthew Garretta19a6ee2010-02-17 16:39:44 -05006249 struct backlight_properties props;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006250 int b;
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006251 unsigned long quirks;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006252
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006253 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
6254
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03006255 mutex_init(&brightness_mutex);
6256
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006257 quirks = tpacpi_check_quirks(brightness_quirk_table,
6258 ARRAY_SIZE(brightness_quirk_table));
6259
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006260 /* tpacpi_detect_brightness_capabilities() must have run already */
Thomas Renninger2dba1b52008-08-01 17:38:03 +02006261
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006262 /* if it is unknown, we don't handle it: it wouldn't be safe */
6263 if (tp_features.bright_unkfw)
6264 return 1;
6265
6266 if (tp_features.bright_acpimode) {
Thomas Renninger2dba1b52008-08-01 17:38:03 +02006267 if (acpi_video_backlight_support()) {
6268 if (brightness_enable > 1) {
6269 printk(TPACPI_NOTICE
6270 "Standard ACPI backlight interface "
6271 "available, not loading native one.\n");
6272 return 1;
6273 } else if (brightness_enable == 1) {
6274 printk(TPACPI_NOTICE
6275 "Backlight control force enabled, even if standard "
6276 "ACPI backlight interface is available\n");
6277 }
6278 } else {
6279 if (brightness_enable > 1) {
6280 printk(TPACPI_NOTICE
6281 "Standard ACPI backlight interface not "
6282 "available, thinkpad_acpi native "
6283 "brightness control enabled\n");
6284 }
Henrique de Moraes Holschuhe11e2112007-10-30 17:46:22 -02006285 }
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02006286 }
6287
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03006288 if (!brightness_enable) {
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006289 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03006290 "brightness support disabled by "
6291 "module parameter\n");
6292 return 1;
6293 }
6294
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006295 /*
6296 * Check for module parameter bogosity, note that we
6297 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
6298 * able to detect "unspecified"
6299 */
6300 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03006301 return -EINVAL;
6302
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006303 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
6304 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
6305 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006306 if (quirks & TPACPI_BRGHT_Q_EC)
6307 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
6308 else
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006309 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
6310
6311 dbg_printk(TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006312 "driver auto-selected brightness_mode=%d\n",
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006313 brightness_mode);
6314 }
6315
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03006316 /* Safety */
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03006317 if (!tpacpi_is_ibm() &&
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03006318 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
6319 brightness_mode == TPACPI_BRGHT_MODE_EC))
6320 return -EINVAL;
6321
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006322 if (tpacpi_brightness_get_raw(&b) < 0)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03006323 return 1;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006324
Matthew Garretta19a6ee2010-02-17 16:39:44 -05006325 memset(&props, 0, sizeof(struct backlight_properties));
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006326 props.max_brightness = bright_maxlvl;
6327 props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
Matthew Garretta19a6ee2010-02-17 16:39:44 -05006328 ibm_backlight_device = backlight_device_register(TPACPI_BACKLIGHT_DEV_NAME,
6329 NULL, NULL,
6330 &ibm_backlight_data,
6331 &props);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006332 if (IS_ERR(ibm_backlight_device)) {
Henrique de Moraes Holschuh435c47e2009-09-20 14:09:22 -03006333 int rc = PTR_ERR(ibm_backlight_device);
6334 ibm_backlight_device = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006335 printk(TPACPI_ERR "Could not register backlight device\n");
Henrique de Moraes Holschuh435c47e2009-09-20 14:09:22 -03006336 return rc;
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006337 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006338 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6339 "brightness is supported\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006340
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006341 if (quirks & TPACPI_BRGHT_Q_ASK) {
6342 printk(TPACPI_NOTICE
6343 "brightness: will use unverified default: "
6344 "brightness_mode=%d\n", brightness_mode);
6345 printk(TPACPI_NOTICE
6346 "brightness: please report to %s whether it works well "
6347 "or not on your ThinkPad\n", TPACPI_MAIL);
6348 }
6349
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006350 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00006351
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00006352 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6353 "brightness: registering brightness hotkeys "
6354 "as change notification\n");
6355 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6356 | TP_ACPI_HKEY_BRGHTUP_MASK
6357 | TP_ACPI_HKEY_BRGHTDWN_MASK);;
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006358 return 0;
6359}
6360
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006361static void brightness_suspend(pm_message_t state)
6362{
6363 tpacpi_brightness_checkpoint_nvram();
6364}
6365
6366static void brightness_shutdown(void)
6367{
6368 tpacpi_brightness_checkpoint_nvram();
6369}
6370
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006371static void brightness_exit(void)
6372{
6373 if (ibm_backlight_device) {
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006374 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006375 "calling backlight_device_unregister()\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006376 backlight_device_unregister(ibm_backlight_device);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006377 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006378
6379 tpacpi_brightness_checkpoint_nvram();
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006380}
6381
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006382static int brightness_read(struct seq_file *m)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006383{
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006384 int level;
6385
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006386 level = brightness_get(NULL);
6387 if (level < 0) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006388 seq_printf(m, "level:\t\tunreadable\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006389 } else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006390 seq_printf(m, "level:\t\t%d\n", level);
6391 seq_printf(m, "commands:\tup, down\n");
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006392 seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n",
6393 bright_maxlvl);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006394 }
6395
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006396 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006397}
6398
6399static int brightness_write(char *buf)
6400{
6401 int level;
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006402 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006403 char *cmd;
6404
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006405 level = brightness_get(NULL);
6406 if (level < 0)
6407 return level;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006408
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006409 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006410 if (strlencmp(cmd, "up") == 0) {
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006411 if (level < bright_maxlvl)
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006412 level++;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006413 } else if (strlencmp(cmd, "down") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006414 if (level > 0)
6415 level--;
6416 } else if (sscanf(cmd, "level %d", &level) == 1 &&
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006417 level >= 0 && level <= bright_maxlvl) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006418 /* new level set */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006419 } else
6420 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006421 }
6422
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006423 tpacpi_disclose_usertask("procfs brightness",
6424 "set level to %d\n", level);
6425
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006426 /*
6427 * Now we know what the final level should be, so we try to set it.
6428 * Doing it this way makes the syscall restartable in case of EINTR
6429 */
6430 rc = brightness_set(level);
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00006431 if (!rc && ibm_backlight_device)
6432 backlight_force_update(ibm_backlight_device,
6433 BACKLIGHT_UPDATE_SYSFS);
Roel Kluin80a8d122009-11-20 19:48:23 +01006434 return (rc == -EINTR)? -ERESTARTSYS : rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006435}
6436
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006437static struct ibm_struct brightness_driver_data = {
6438 .name = "brightness",
6439 .read = brightness_read,
6440 .write = brightness_write,
6441 .exit = brightness_exit,
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006442 .suspend = brightness_suspend,
6443 .shutdown = brightness_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006444};
6445
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006446/*************************************************************************
6447 * Volume subdriver
6448 */
6449
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006450/*
6451 * IBM ThinkPads have a simple volume controller with MUTE gating.
6452 * Very early Lenovo ThinkPads follow the IBM ThinkPad spec.
6453 *
6454 * Since the *61 series (and probably also the later *60 series), Lenovo
6455 * ThinkPads only implement the MUTE gate.
6456 *
6457 * EC register 0x30
6458 * Bit 6: MUTE (1 mutes sound)
6459 * Bit 3-0: Volume
6460 * Other bits should be zero as far as we know.
6461 *
6462 * This is also stored in CMOS NVRAM, byte 0x60, bit 6 (MUTE), and
6463 * bits 3-0 (volume). Other bits in NVRAM may have other functions,
6464 * such as bit 7 which is used to detect repeated presses of MUTE,
6465 * and we leave them unchanged.
6466 */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006467
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02006468#ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
6469
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006470#define TPACPI_ALSA_DRVNAME "ThinkPad EC"
6471#define TPACPI_ALSA_SHRTNAME "ThinkPad Console Audio Control"
6472#define TPACPI_ALSA_MIXERNAME TPACPI_ALSA_SHRTNAME
6473
Henrique de Moraes Holschuhead510c2009-12-26 22:52:13 -02006474static int alsa_index = ~((1 << (SNDRV_CARDS - 3)) - 1); /* last three slots */
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006475static char *alsa_id = "ThinkPadEC";
6476static int alsa_enable = SNDRV_DEFAULT_ENABLE1;
6477
6478struct tpacpi_alsa_data {
6479 struct snd_card *card;
6480 struct snd_ctl_elem_id *ctl_mute_id;
6481 struct snd_ctl_elem_id *ctl_vol_id;
6482};
6483
6484static struct snd_card *alsa_card;
6485
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006486enum {
6487 TP_EC_AUDIO = 0x30,
6488
6489 /* TP_EC_AUDIO bits */
6490 TP_EC_AUDIO_MUTESW = 6,
6491
6492 /* TP_EC_AUDIO bitmasks */
6493 TP_EC_AUDIO_LVL_MSK = 0x0F,
6494 TP_EC_AUDIO_MUTESW_MSK = (1 << TP_EC_AUDIO_MUTESW),
6495
6496 /* Maximum volume */
6497 TP_EC_VOLUME_MAX = 14,
6498};
6499
6500enum tpacpi_volume_access_mode {
6501 TPACPI_VOL_MODE_AUTO = 0, /* Not implemented yet */
6502 TPACPI_VOL_MODE_EC, /* Pure EC control */
6503 TPACPI_VOL_MODE_UCMS_STEP, /* UCMS step-based control: N/A */
6504 TPACPI_VOL_MODE_ECNVRAM, /* EC control w/ NVRAM store */
6505 TPACPI_VOL_MODE_MAX
6506};
6507
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006508enum tpacpi_volume_capabilities {
6509 TPACPI_VOL_CAP_AUTO = 0, /* Use white/blacklist */
6510 TPACPI_VOL_CAP_VOLMUTE, /* Output vol and mute */
6511 TPACPI_VOL_CAP_MUTEONLY, /* Output mute only */
6512 TPACPI_VOL_CAP_MAX
6513};
6514
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006515static enum tpacpi_volume_access_mode volume_mode =
6516 TPACPI_VOL_MODE_MAX;
6517
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006518static enum tpacpi_volume_capabilities volume_capabilities;
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006519static int volume_control_allowed;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006520
6521/*
6522 * Used to syncronize writers to TP_EC_AUDIO and
6523 * TP_NVRAM_ADDR_MIXER, as we need to do read-modify-write
6524 */
6525static struct mutex volume_mutex;
6526
6527static void tpacpi_volume_checkpoint_nvram(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006528{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006529 u8 lec = 0;
6530 u8 b_nvram;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006531 u8 ec_mask;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006532
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006533 if (volume_mode != TPACPI_VOL_MODE_ECNVRAM)
6534 return;
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006535 if (!volume_control_allowed)
6536 return;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006537
6538 vdbg_printk(TPACPI_DBG_MIXER,
6539 "trying to checkpoint mixer state to NVRAM...\n");
6540
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006541 if (tp_features.mixer_no_level_control)
6542 ec_mask = TP_EC_AUDIO_MUTESW_MSK;
6543 else
6544 ec_mask = TP_EC_AUDIO_MUTESW_MSK | TP_EC_AUDIO_LVL_MSK;
6545
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006546 if (mutex_lock_killable(&volume_mutex) < 0)
6547 return;
6548
6549 if (unlikely(!acpi_ec_read(TP_EC_AUDIO, &lec)))
6550 goto unlock;
6551 lec &= ec_mask;
6552 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
6553
6554 if (lec != (b_nvram & ec_mask)) {
6555 /* NVRAM needs update */
6556 b_nvram &= ~ec_mask;
6557 b_nvram |= lec;
6558 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_MIXER);
6559 dbg_printk(TPACPI_DBG_MIXER,
6560 "updated NVRAM mixer status to 0x%02x (0x%02x)\n",
6561 (unsigned int) lec, (unsigned int) b_nvram);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006562 } else {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006563 vdbg_printk(TPACPI_DBG_MIXER,
6564 "NVRAM mixer status already is 0x%02x (0x%02x)\n",
6565 (unsigned int) lec, (unsigned int) b_nvram);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006566 }
6567
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006568unlock:
6569 mutex_unlock(&volume_mutex);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006570}
6571
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006572static int volume_get_status_ec(u8 *status)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006573{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006574 u8 s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006575
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006576 if (!acpi_ec_read(TP_EC_AUDIO, &s))
6577 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006578
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006579 *status = s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006580
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006581 dbg_printk(TPACPI_DBG_MIXER, "status 0x%02x\n", s);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006582
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006583 return 0;
6584}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006585
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006586static int volume_get_status(u8 *status)
6587{
6588 return volume_get_status_ec(status);
6589}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006590
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006591static int volume_set_status_ec(const u8 status)
6592{
6593 if (!acpi_ec_write(TP_EC_AUDIO, status))
6594 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006595
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006596 dbg_printk(TPACPI_DBG_MIXER, "set EC mixer to 0x%02x\n", status);
6597
6598 return 0;
6599}
6600
6601static int volume_set_status(const u8 status)
6602{
6603 return volume_set_status_ec(status);
6604}
6605
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006606/* returns < 0 on error, 0 on no change, 1 on change */
6607static int __volume_set_mute_ec(const bool mute)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006608{
6609 int rc;
6610 u8 s, n;
6611
6612 if (mutex_lock_killable(&volume_mutex) < 0)
6613 return -EINTR;
6614
6615 rc = volume_get_status_ec(&s);
6616 if (rc)
6617 goto unlock;
6618
6619 n = (mute) ? s | TP_EC_AUDIO_MUTESW_MSK :
6620 s & ~TP_EC_AUDIO_MUTESW_MSK;
6621
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006622 if (n != s) {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006623 rc = volume_set_status_ec(n);
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006624 if (!rc)
6625 rc = 1;
6626 }
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006627
6628unlock:
6629 mutex_unlock(&volume_mutex);
6630 return rc;
6631}
6632
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006633static int volume_alsa_set_mute(const bool mute)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006634{
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006635 dbg_printk(TPACPI_DBG_MIXER, "ALSA: trying to %smute\n",
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006636 (mute) ? "" : "un");
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006637 return __volume_set_mute_ec(mute);
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006638}
6639
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006640static int volume_set_mute(const bool mute)
6641{
6642 int rc;
6643
6644 dbg_printk(TPACPI_DBG_MIXER, "trying to %smute\n",
6645 (mute) ? "" : "un");
6646
6647 rc = __volume_set_mute_ec(mute);
6648 return (rc < 0) ? rc : 0;
6649}
6650
6651/* returns < 0 on error, 0 on no change, 1 on change */
6652static int __volume_set_volume_ec(const u8 vol)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006653{
6654 int rc;
6655 u8 s, n;
6656
6657 if (vol > TP_EC_VOLUME_MAX)
6658 return -EINVAL;
6659
6660 if (mutex_lock_killable(&volume_mutex) < 0)
6661 return -EINTR;
6662
6663 rc = volume_get_status_ec(&s);
6664 if (rc)
6665 goto unlock;
6666
6667 n = (s & ~TP_EC_AUDIO_LVL_MSK) | vol;
6668
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006669 if (n != s) {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006670 rc = volume_set_status_ec(n);
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006671 if (!rc)
6672 rc = 1;
6673 }
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006674
6675unlock:
6676 mutex_unlock(&volume_mutex);
6677 return rc;
6678}
6679
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006680static int volume_alsa_set_volume(const u8 vol)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006681{
6682 dbg_printk(TPACPI_DBG_MIXER,
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006683 "ALSA: trying to set volume level to %hu\n", vol);
6684 return __volume_set_volume_ec(vol);
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006685}
6686
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006687static void volume_alsa_notify_change(void)
6688{
6689 struct tpacpi_alsa_data *d;
6690
6691 if (alsa_card && alsa_card->private_data) {
6692 d = alsa_card->private_data;
6693 if (d->ctl_mute_id)
6694 snd_ctl_notify(alsa_card,
6695 SNDRV_CTL_EVENT_MASK_VALUE,
6696 d->ctl_mute_id);
6697 if (d->ctl_vol_id)
6698 snd_ctl_notify(alsa_card,
6699 SNDRV_CTL_EVENT_MASK_VALUE,
6700 d->ctl_vol_id);
6701 }
6702}
6703
6704static int volume_alsa_vol_info(struct snd_kcontrol *kcontrol,
6705 struct snd_ctl_elem_info *uinfo)
6706{
6707 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
6708 uinfo->count = 1;
6709 uinfo->value.integer.min = 0;
6710 uinfo->value.integer.max = TP_EC_VOLUME_MAX;
6711 return 0;
6712}
6713
6714static int volume_alsa_vol_get(struct snd_kcontrol *kcontrol,
6715 struct snd_ctl_elem_value *ucontrol)
6716{
6717 u8 s;
6718 int rc;
6719
6720 rc = volume_get_status(&s);
6721 if (rc < 0)
6722 return rc;
6723
6724 ucontrol->value.integer.value[0] = s & TP_EC_AUDIO_LVL_MSK;
6725 return 0;
6726}
6727
6728static int volume_alsa_vol_put(struct snd_kcontrol *kcontrol,
6729 struct snd_ctl_elem_value *ucontrol)
6730{
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006731 return volume_alsa_set_volume(ucontrol->value.integer.value[0]);
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006732}
6733
6734#define volume_alsa_mute_info snd_ctl_boolean_mono_info
6735
6736static int volume_alsa_mute_get(struct snd_kcontrol *kcontrol,
6737 struct snd_ctl_elem_value *ucontrol)
6738{
6739 u8 s;
6740 int rc;
6741
6742 rc = volume_get_status(&s);
6743 if (rc < 0)
6744 return rc;
6745
6746 ucontrol->value.integer.value[0] =
6747 (s & TP_EC_AUDIO_MUTESW_MSK) ? 0 : 1;
6748 return 0;
6749}
6750
6751static int volume_alsa_mute_put(struct snd_kcontrol *kcontrol,
6752 struct snd_ctl_elem_value *ucontrol)
6753{
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006754 return volume_alsa_set_mute(!ucontrol->value.integer.value[0]);
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006755}
6756
6757static struct snd_kcontrol_new volume_alsa_control_vol __devinitdata = {
6758 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6759 .name = "Console Playback Volume",
6760 .index = 0,
6761 .access = SNDRV_CTL_ELEM_ACCESS_READ,
6762 .info = volume_alsa_vol_info,
6763 .get = volume_alsa_vol_get,
6764};
6765
6766static struct snd_kcontrol_new volume_alsa_control_mute __devinitdata = {
6767 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6768 .name = "Console Playback Switch",
6769 .index = 0,
6770 .access = SNDRV_CTL_ELEM_ACCESS_READ,
6771 .info = volume_alsa_mute_info,
6772 .get = volume_alsa_mute_get,
6773};
6774
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006775static void volume_suspend(pm_message_t state)
6776{
6777 tpacpi_volume_checkpoint_nvram();
6778}
6779
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006780static void volume_resume(void)
6781{
6782 volume_alsa_notify_change();
6783}
6784
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006785static void volume_shutdown(void)
6786{
6787 tpacpi_volume_checkpoint_nvram();
6788}
6789
6790static void volume_exit(void)
6791{
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006792 if (alsa_card) {
6793 snd_card_free(alsa_card);
6794 alsa_card = NULL;
6795 }
6796
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006797 tpacpi_volume_checkpoint_nvram();
6798}
6799
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006800static int __init volume_create_alsa_mixer(void)
6801{
6802 struct snd_card *card;
6803 struct tpacpi_alsa_data *data;
6804 struct snd_kcontrol *ctl_vol;
6805 struct snd_kcontrol *ctl_mute;
6806 int rc;
6807
6808 rc = snd_card_create(alsa_index, alsa_id, THIS_MODULE,
6809 sizeof(struct tpacpi_alsa_data), &card);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02006810 if (rc < 0 || !card) {
6811 printk(TPACPI_ERR
6812 "Failed to create ALSA card structures: %d\n", rc);
6813 return 1;
6814 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006815
6816 BUG_ON(!card->private_data);
6817 data = card->private_data;
6818 data->card = card;
6819
6820 strlcpy(card->driver, TPACPI_ALSA_DRVNAME,
6821 sizeof(card->driver));
6822 strlcpy(card->shortname, TPACPI_ALSA_SHRTNAME,
6823 sizeof(card->shortname));
6824 snprintf(card->mixername, sizeof(card->mixername), "ThinkPad EC %s",
6825 (thinkpad_id.ec_version_str) ?
6826 thinkpad_id.ec_version_str : "(unknown)");
6827 snprintf(card->longname, sizeof(card->longname),
6828 "%s at EC reg 0x%02x, fw %s", card->shortname, TP_EC_AUDIO,
6829 (thinkpad_id.ec_version_str) ?
6830 thinkpad_id.ec_version_str : "unknown");
6831
6832 if (volume_control_allowed) {
6833 volume_alsa_control_vol.put = volume_alsa_vol_put;
6834 volume_alsa_control_vol.access =
6835 SNDRV_CTL_ELEM_ACCESS_READWRITE;
6836
6837 volume_alsa_control_mute.put = volume_alsa_mute_put;
6838 volume_alsa_control_mute.access =
6839 SNDRV_CTL_ELEM_ACCESS_READWRITE;
6840 }
6841
6842 if (!tp_features.mixer_no_level_control) {
6843 ctl_vol = snd_ctl_new1(&volume_alsa_control_vol, NULL);
6844 rc = snd_ctl_add(card, ctl_vol);
6845 if (rc < 0) {
6846 printk(TPACPI_ERR
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02006847 "Failed to create ALSA volume control: %d\n",
6848 rc);
6849 goto err_exit;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006850 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006851 data->ctl_vol_id = &ctl_vol->id;
6852 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006853
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006854 ctl_mute = snd_ctl_new1(&volume_alsa_control_mute, NULL);
6855 rc = snd_ctl_add(card, ctl_mute);
6856 if (rc < 0) {
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02006857 printk(TPACPI_ERR "Failed to create ALSA mute control: %d\n",
6858 rc);
6859 goto err_exit;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006860 }
6861 data->ctl_mute_id = &ctl_mute->id;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006862
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006863 snd_card_set_dev(card, &tpacpi_pdev->dev);
6864 rc = snd_card_register(card);
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006865 if (rc < 0) {
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02006866 printk(TPACPI_ERR "Failed to register ALSA card: %d\n", rc);
6867 goto err_exit;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006868 }
6869
6870 alsa_card = card;
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02006871 return 0;
6872
6873err_exit:
6874 snd_card_free(card);
6875 return 1;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006876}
6877
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006878#define TPACPI_VOL_Q_MUTEONLY 0x0001 /* Mute-only control available */
6879#define TPACPI_VOL_Q_LEVEL 0x0002 /* Volume control available */
6880
6881static const struct tpacpi_quirk volume_quirk_table[] __initconst = {
6882 /* Whitelist volume level on all IBM by default */
6883 { .vendor = PCI_VENDOR_ID_IBM,
6884 .bios = TPACPI_MATCH_ANY,
6885 .ec = TPACPI_MATCH_ANY,
6886 .quirks = TPACPI_VOL_Q_LEVEL },
6887
6888 /* Lenovo models with volume control (needs confirmation) */
6889 TPACPI_QEC_LNV('7', 'C', TPACPI_VOL_Q_LEVEL), /* R60/i */
6890 TPACPI_QEC_LNV('7', 'E', TPACPI_VOL_Q_LEVEL), /* R60e/i */
6891 TPACPI_QEC_LNV('7', '9', TPACPI_VOL_Q_LEVEL), /* T60/p */
6892 TPACPI_QEC_LNV('7', 'B', TPACPI_VOL_Q_LEVEL), /* X60/s */
6893 TPACPI_QEC_LNV('7', 'J', TPACPI_VOL_Q_LEVEL), /* X60t */
6894 TPACPI_QEC_LNV('7', '7', TPACPI_VOL_Q_LEVEL), /* Z60 */
6895 TPACPI_QEC_LNV('7', 'F', TPACPI_VOL_Q_LEVEL), /* Z61 */
6896
6897 /* Whitelist mute-only on all Lenovo by default */
6898 { .vendor = PCI_VENDOR_ID_LENOVO,
6899 .bios = TPACPI_MATCH_ANY,
6900 .ec = TPACPI_MATCH_ANY,
6901 .quirks = TPACPI_VOL_Q_MUTEONLY }
6902};
6903
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006904static int __init volume_init(struct ibm_init_struct *iibm)
6905{
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006906 unsigned long quirks;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006907 int rc;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006908
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006909 vdbg_printk(TPACPI_DBG_INIT, "initializing volume subdriver\n");
6910
6911 mutex_init(&volume_mutex);
6912
6913 /*
6914 * Check for module parameter bogosity, note that we
6915 * init volume_mode to TPACPI_VOL_MODE_MAX in order to be
6916 * able to detect "unspecified"
6917 */
6918 if (volume_mode > TPACPI_VOL_MODE_MAX)
6919 return -EINVAL;
6920
6921 if (volume_mode == TPACPI_VOL_MODE_UCMS_STEP) {
6922 printk(TPACPI_ERR
6923 "UCMS step volume mode not implemented, "
6924 "please contact %s\n", TPACPI_MAIL);
6925 return 1;
6926 }
6927
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006928 if (volume_capabilities >= TPACPI_VOL_CAP_MAX)
6929 return -EINVAL;
6930
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006931 /*
6932 * The ALSA mixer is our primary interface.
6933 * When disabled, don't install the subdriver at all
6934 */
6935 if (!alsa_enable) {
6936 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6937 "ALSA mixer disabled by parameter, "
6938 "not loading volume subdriver...\n");
6939 return 1;
6940 }
6941
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006942 quirks = tpacpi_check_quirks(volume_quirk_table,
6943 ARRAY_SIZE(volume_quirk_table));
6944
6945 switch (volume_capabilities) {
6946 case TPACPI_VOL_CAP_AUTO:
6947 if (quirks & TPACPI_VOL_Q_MUTEONLY)
6948 tp_features.mixer_no_level_control = 1;
6949 else if (quirks & TPACPI_VOL_Q_LEVEL)
6950 tp_features.mixer_no_level_control = 0;
6951 else
6952 return 1; /* no mixer */
6953 break;
6954 case TPACPI_VOL_CAP_VOLMUTE:
6955 tp_features.mixer_no_level_control = 0;
6956 break;
6957 case TPACPI_VOL_CAP_MUTEONLY:
6958 tp_features.mixer_no_level_control = 1;
6959 break;
6960 default:
6961 return 1;
6962 }
6963
6964 if (volume_capabilities != TPACPI_VOL_CAP_AUTO)
6965 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6966 "using user-supplied volume_capabilities=%d\n",
6967 volume_capabilities);
6968
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006969 if (volume_mode == TPACPI_VOL_MODE_AUTO ||
6970 volume_mode == TPACPI_VOL_MODE_MAX) {
6971 volume_mode = TPACPI_VOL_MODE_ECNVRAM;
6972
6973 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6974 "driver auto-selected volume_mode=%d\n",
6975 volume_mode);
6976 } else {
6977 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6978 "using user-supplied volume_mode=%d\n",
6979 volume_mode);
6980 }
6981
6982 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006983 "mute is supported, volume control is %s\n",
6984 str_supported(!tp_features.mixer_no_level_control));
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006985
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006986 rc = volume_create_alsa_mixer();
6987 if (rc) {
6988 printk(TPACPI_ERR
6989 "Could not create the ALSA mixer interface\n");
6990 return rc;
6991 }
6992
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006993 printk(TPACPI_INFO
6994 "Console audio control enabled, mode: %s\n",
6995 (volume_control_allowed) ?
6996 "override (read/write)" :
6997 "monitor (read only)");
6998
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006999 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7000 "registering volume hotkeys as change notification\n");
7001 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
7002 | TP_ACPI_HKEY_VOLUP_MASK
7003 | TP_ACPI_HKEY_VOLDWN_MASK
7004 | TP_ACPI_HKEY_MUTE_MASK);
7005
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007006 return 0;
7007}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007008
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007009static int volume_read(struct seq_file *m)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007010{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007011 u8 status;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007012
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007013 if (volume_get_status(&status) < 0) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007014 seq_printf(m, "level:\t\tunreadable\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007015 } else {
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007016 if (tp_features.mixer_no_level_control)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007017 seq_printf(m, "level:\t\tunsupported\n");
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007018 else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007019 seq_printf(m, "level:\t\t%d\n",
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007020 status & TP_EC_AUDIO_LVL_MSK);
7021
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007022 seq_printf(m, "mute:\t\t%s\n",
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007023 onoff(status, TP_EC_AUDIO_MUTESW));
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007024
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007025 if (volume_control_allowed) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007026 seq_printf(m, "commands:\tunmute, mute\n");
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007027 if (!tp_features.mixer_no_level_control) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007028 seq_printf(m,
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007029 "commands:\tup, down\n");
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007030 seq_printf(m,
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007031 "commands:\tlevel <level>"
7032 " (<level> is 0-%d)\n",
7033 TP_EC_VOLUME_MAX);
7034 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007035 }
7036 }
7037
7038 return 0;
7039}
7040
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007041static int volume_write(char *buf)
7042{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007043 u8 s;
7044 u8 new_level, new_mute;
7045 int l;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007046 char *cmd;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007047 int rc;
7048
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007049 /*
7050 * We do allow volume control at driver startup, so that the
7051 * user can set initial state through the volume=... parameter hack.
7052 */
7053 if (!volume_control_allowed && tpacpi_lifecycle != TPACPI_LIFE_INIT) {
7054 if (unlikely(!tp_warned.volume_ctrl_forbidden)) {
7055 tp_warned.volume_ctrl_forbidden = 1;
7056 printk(TPACPI_NOTICE
7057 "Console audio control in monitor mode, "
7058 "changes are not allowed.\n");
7059 printk(TPACPI_NOTICE
7060 "Use the volume_control=1 module parameter "
7061 "to enable volume control\n");
7062 }
7063 return -EPERM;
7064 }
7065
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007066 rc = volume_get_status(&s);
7067 if (rc < 0)
7068 return rc;
7069
7070 new_level = s & TP_EC_AUDIO_LVL_MSK;
7071 new_mute = s & TP_EC_AUDIO_MUTESW_MSK;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007072
7073 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007074 if (!tp_features.mixer_no_level_control) {
7075 if (strlencmp(cmd, "up") == 0) {
7076 if (new_mute)
7077 new_mute = 0;
7078 else if (new_level < TP_EC_VOLUME_MAX)
7079 new_level++;
7080 continue;
7081 } else if (strlencmp(cmd, "down") == 0) {
7082 if (new_mute)
7083 new_mute = 0;
7084 else if (new_level > 0)
7085 new_level--;
7086 continue;
7087 } else if (sscanf(cmd, "level %u", &l) == 1 &&
7088 l >= 0 && l <= TP_EC_VOLUME_MAX) {
7089 new_level = l;
7090 continue;
7091 }
7092 }
7093 if (strlencmp(cmd, "mute") == 0)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007094 new_mute = TP_EC_AUDIO_MUTESW_MSK;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007095 else if (strlencmp(cmd, "unmute") == 0)
7096 new_mute = 0;
7097 else
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007098 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007099 }
7100
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007101 if (tp_features.mixer_no_level_control) {
7102 tpacpi_disclose_usertask("procfs volume", "%smute\n",
7103 new_mute ? "" : "un");
7104 rc = volume_set_mute(!!new_mute);
7105 } else {
7106 tpacpi_disclose_usertask("procfs volume",
7107 "%smute and set level to %d\n",
7108 new_mute ? "" : "un", new_level);
7109 rc = volume_set_status(new_mute | new_level);
7110 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007111 volume_alsa_notify_change();
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007112
7113 return (rc == -EINTR) ? -ERESTARTSYS : rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007114}
7115
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007116static struct ibm_struct volume_driver_data = {
7117 .name = "volume",
7118 .read = volume_read,
7119 .write = volume_write,
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007120 .exit = volume_exit,
7121 .suspend = volume_suspend,
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007122 .resume = volume_resume,
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007123 .shutdown = volume_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007124};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007125
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02007126#else /* !CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7127
7128#define alsa_card NULL
7129
7130static void inline volume_alsa_notify_change(void)
7131{
7132}
7133
7134static int __init volume_init(struct ibm_init_struct *iibm)
7135{
7136 printk(TPACPI_INFO
7137 "volume: disabled as there is no ALSA support in this kernel\n");
7138
7139 return 1;
7140}
7141
7142static struct ibm_struct volume_driver_data = {
7143 .name = "volume",
7144};
7145
7146#endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7147
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007148/*************************************************************************
7149 * Fan subdriver
7150 */
7151
7152/*
7153 * FAN ACCESS MODES
7154 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007155 * TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007156 * ACPI GFAN method: returns fan level
7157 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007158 * see TPACPI_FAN_WR_ACPI_SFAN
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007159 * EC 0x2f (HFSP) not available if GFAN exists
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007160 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007161 * TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007162 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
7163 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007164 * EC 0x2f (HFSP) might be available *for reading*, but do not use
7165 * it for writing.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007166 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007167 * TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007168 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
7169 * Supported on almost all ThinkPads
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007170 *
7171 * Fan speed changes of any sort (including those caused by the
7172 * disengaged mode) are usually done slowly by the firmware as the
Uwe Kleine-König9ddc5b62010-01-20 17:02:24 +01007173 * maximum amount of fan duty cycle change per second seems to be
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007174 * limited.
7175 *
7176 * Reading is not available if GFAN exists.
7177 * Writing is not available if SFAN exists.
7178 *
7179 * Bits
7180 * 7 automatic mode engaged;
7181 * (default operation mode of the ThinkPad)
7182 * fan level is ignored in this mode.
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007183 * 6 full speed mode (takes precedence over bit 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007184 * not available on all thinkpads. May disable
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007185 * the tachometer while the fan controller ramps up
7186 * the speed (which can take up to a few *minutes*).
7187 * Speeds up fan to 100% duty-cycle, which is far above
7188 * the standard RPM levels. It is not impossible that
7189 * it could cause hardware damage.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007190 * 5-3 unused in some models. Extra bits for fan level
7191 * in others, but still useless as all values above
7192 * 7 map to the same speed as level 7 in these models.
7193 * 2-0 fan level (0..7 usually)
7194 * 0x00 = stop
7195 * 0x07 = max (set when temperatures critical)
7196 * Some ThinkPads may have other levels, see
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007197 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007198 *
7199 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
7200 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
7201 * does so, its initial value is meaningless (0x07).
7202 *
7203 * For firmware bugs, refer to:
7204 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7205 *
7206 * ----
7207 *
7208 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
7209 * Main fan tachometer reading (in RPM)
7210 *
7211 * This register is present on all ThinkPads with a new-style EC, and
7212 * it is known not to be present on the A21m/e, and T22, as there is
7213 * something else in offset 0x84 according to the ACPI DSDT. Other
7214 * ThinkPads from this same time period (and earlier) probably lack the
7215 * tachometer as well.
7216 *
Nick Andrew877d0312009-01-26 11:06:57 +01007217 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007218 * was never fixed by IBM to report the EC firmware version string
7219 * probably support the tachometer (like the early X models), so
7220 * detecting it is quite hard. We need more data to know for sure.
7221 *
7222 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
7223 * might result.
7224 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007225 * FIRMWARE BUG: may go stale while the EC is switching to full speed
7226 * mode.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007227 *
7228 * For firmware bugs, refer to:
7229 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7230 *
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007231 * ----
7232 *
7233 * ThinkPad EC register 0x31 bit 0 (only on select models)
7234 *
7235 * When bit 0 of EC register 0x31 is zero, the tachometer registers
7236 * show the speed of the main fan. When bit 0 of EC register 0x31
7237 * is one, the tachometer registers show the speed of the auxiliary
7238 * fan.
7239 *
7240 * Fan control seems to affect both fans, regardless of the state
7241 * of this bit.
7242 *
7243 * So far, only the firmware for the X60/X61 non-tablet versions
7244 * seem to support this (firmware TP-7M).
7245 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007246 * TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007247 * ThinkPad X31, X40, X41. Not available in the X60.
7248 *
7249 * FANS ACPI handle: takes three arguments: low speed, medium speed,
7250 * high speed. ACPI DSDT seems to map these three speeds to levels
7251 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
7252 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
7253 *
7254 * The speeds are stored on handles
7255 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
7256 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007257 * There are three default speed sets, accessible as handles:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007258 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
7259 *
7260 * ACPI DSDT switches which set is in use depending on various
7261 * factors.
7262 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007263 * TPACPI_FAN_WR_TPEC is also available and should be used to
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007264 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
7265 * but the ACPI tables just mention level 7.
7266 */
7267
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007268enum { /* Fan control constants */
7269 fan_status_offset = 0x2f, /* EC register 0x2f */
7270 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
7271 * 0x84 must be read before 0x85 */
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007272 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
7273 bit 0 selects which fan is active */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007274
7275 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
7276 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
7277
7278 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
7279};
7280
7281enum fan_status_access_mode {
7282 TPACPI_FAN_NONE = 0, /* No fan status or control */
7283 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
7284 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
7285};
7286
7287enum fan_control_access_mode {
7288 TPACPI_FAN_WR_NONE = 0, /* No fan control */
7289 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
7290 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
7291 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
7292};
7293
7294enum fan_control_commands {
7295 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
7296 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
7297 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
7298 * and also watchdog cmd */
7299};
7300
7301static int fan_control_allowed;
7302
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007303static enum fan_status_access_mode fan_status_access_mode;
7304static enum fan_control_access_mode fan_control_access_mode;
7305static enum fan_control_commands fan_control_commands;
7306
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02007307static u8 fan_control_initial_status;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007308static u8 fan_control_desired_level;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007309static u8 fan_control_resume_level;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007310static int fan_watchdog_maxinterval;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007311
7312static struct mutex fan_mutex;
7313
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007314static void fan_watchdog_fire(struct work_struct *ignored);
Len Brown25c68a32006-12-08 04:43:41 -05007315static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007316
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007317TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
7318TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007319 "\\FSPD", /* 600e/x, 770e, 770x */
7320 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007321TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007322 "JFNS", /* 770x-JL */
7323 ); /* all others */
7324
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007325/*
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007326 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
7327 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
7328 * be in auto mode (0x80).
7329 *
7330 * This is corrected by any write to HFSP either by the driver, or
7331 * by the firmware.
7332 *
7333 * We assume 0x07 really means auto mode while this quirk is active,
7334 * as this is far more likely than the ThinkPad being in level 7,
7335 * which is only used by the firmware during thermal emergencies.
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007336 *
7337 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
7338 * TP-70 (T43, R52), which are known to be buggy.
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007339 */
7340
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007341static void fan_quirk1_setup(void)
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007342{
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007343 if (fan_control_initial_status == 0x07) {
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007344 printk(TPACPI_NOTICE
7345 "fan_init: initial fan status is unknown, "
7346 "assuming it is in auto mode\n");
7347 tp_features.fan_ctrl_status_undef = 1;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007348 }
7349}
7350
7351static void fan_quirk1_handle(u8 *fan_status)
7352{
7353 if (unlikely(tp_features.fan_ctrl_status_undef)) {
7354 if (*fan_status != fan_control_initial_status) {
7355 /* something changed the HFSP regisnter since
7356 * driver init time, so it is not undefined
7357 * anymore */
7358 tp_features.fan_ctrl_status_undef = 0;
7359 } else {
7360 /* Return most likely status. In fact, it
7361 * might be the only possible status */
7362 *fan_status = TP_EC_FAN_AUTO;
7363 }
7364 }
7365}
7366
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007367/* Select main fan on X60/X61, NOOP on others */
7368static bool fan_select_fan1(void)
7369{
7370 if (tp_features.second_fan) {
7371 u8 val;
7372
7373 if (ec_read(fan_select_offset, &val) < 0)
7374 return false;
7375 val &= 0xFEU;
7376 if (ec_write(fan_select_offset, val) < 0)
7377 return false;
7378 }
7379 return true;
7380}
7381
7382/* Select secondary fan on X60/X61 */
7383static bool fan_select_fan2(void)
7384{
7385 u8 val;
7386
7387 if (!tp_features.second_fan)
7388 return false;
7389
7390 if (ec_read(fan_select_offset, &val) < 0)
7391 return false;
7392 val |= 0x01U;
7393 if (ec_write(fan_select_offset, val) < 0)
7394 return false;
7395
7396 return true;
7397}
7398
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007399/*
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007400 * Call with fan_mutex held
7401 */
7402static void fan_update_desired_level(u8 status)
7403{
7404 if ((status &
7405 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7406 if (status > 7)
7407 fan_control_desired_level = 7;
7408 else
7409 fan_control_desired_level = status;
7410 }
7411}
7412
7413static int fan_get_status(u8 *status)
7414{
7415 u8 s;
7416
7417 /* TODO:
7418 * Add TPACPI_FAN_RD_ACPI_FANS ? */
7419
7420 switch (fan_status_access_mode) {
7421 case TPACPI_FAN_RD_ACPI_GFAN:
7422 /* 570, 600e/x, 770e, 770x */
7423
7424 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
7425 return -EIO;
7426
7427 if (likely(status))
7428 *status = s & 0x07;
7429
7430 break;
7431
7432 case TPACPI_FAN_RD_TPEC:
7433 /* all except 570, 600e/x, 770e, 770x */
7434 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
7435 return -EIO;
7436
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007437 if (likely(status)) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007438 *status = s;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007439 fan_quirk1_handle(status);
7440 }
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007441
7442 break;
7443
7444 default:
7445 return -ENXIO;
7446 }
7447
7448 return 0;
7449}
7450
7451static int fan_get_status_safe(u8 *status)
7452{
7453 int rc;
7454 u8 s;
7455
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007456 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007457 return -ERESTARTSYS;
7458 rc = fan_get_status(&s);
7459 if (!rc)
7460 fan_update_desired_level(s);
7461 mutex_unlock(&fan_mutex);
7462
7463 if (status)
7464 *status = s;
7465
7466 return rc;
7467}
7468
7469static int fan_get_speed(unsigned int *speed)
7470{
7471 u8 hi, lo;
7472
7473 switch (fan_status_access_mode) {
7474 case TPACPI_FAN_RD_TPEC:
7475 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007476 if (unlikely(!fan_select_fan1()))
7477 return -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007478 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
7479 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
7480 return -EIO;
7481
7482 if (likely(speed))
7483 *speed = (hi << 8) | lo;
7484
7485 break;
7486
7487 default:
7488 return -ENXIO;
7489 }
7490
7491 return 0;
7492}
7493
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007494static int fan2_get_speed(unsigned int *speed)
7495{
7496 u8 hi, lo;
7497 bool rc;
7498
7499 switch (fan_status_access_mode) {
7500 case TPACPI_FAN_RD_TPEC:
7501 /* all except 570, 600e/x, 770e, 770x */
7502 if (unlikely(!fan_select_fan2()))
7503 return -EIO;
7504 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
7505 !acpi_ec_read(fan_rpm_offset + 1, &hi);
7506 fan_select_fan1(); /* play it safe */
7507 if (rc)
7508 return -EIO;
7509
7510 if (likely(speed))
7511 *speed = (hi << 8) | lo;
7512
7513 break;
7514
7515 default:
7516 return -ENXIO;
7517 }
7518
7519 return 0;
7520}
7521
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007522static int fan_set_level(int level)
7523{
7524 if (!fan_control_allowed)
7525 return -EPERM;
7526
7527 switch (fan_control_access_mode) {
7528 case TPACPI_FAN_WR_ACPI_SFAN:
7529 if (level >= 0 && level <= 7) {
7530 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
7531 return -EIO;
7532 } else
7533 return -EINVAL;
7534 break;
7535
7536 case TPACPI_FAN_WR_ACPI_FANS:
7537 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007538 if (!(level & TP_EC_FAN_AUTO) &&
7539 !(level & TP_EC_FAN_FULLSPEED) &&
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007540 ((level < 0) || (level > 7)))
7541 return -EINVAL;
7542
7543 /* safety net should the EC not support AUTO
7544 * or FULLSPEED mode bits and just ignore them */
7545 if (level & TP_EC_FAN_FULLSPEED)
7546 level |= 7; /* safety min speed 7 */
Roel Kluin547266e2008-02-05 00:24:56 +01007547 else if (level & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007548 level |= 4; /* safety min speed 4 */
7549
7550 if (!acpi_ec_write(fan_status_offset, level))
7551 return -EIO;
7552 else
7553 tp_features.fan_ctrl_status_undef = 0;
7554 break;
7555
7556 default:
7557 return -ENXIO;
7558 }
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007559
7560 vdbg_printk(TPACPI_DBG_FAN,
7561 "fan control: set fan control register to 0x%02x\n", level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007562 return 0;
7563}
7564
7565static int fan_set_level_safe(int level)
7566{
7567 int rc;
7568
7569 if (!fan_control_allowed)
7570 return -EPERM;
7571
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007572 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007573 return -ERESTARTSYS;
7574
7575 if (level == TPACPI_FAN_LAST_LEVEL)
7576 level = fan_control_desired_level;
7577
7578 rc = fan_set_level(level);
7579 if (!rc)
7580 fan_update_desired_level(level);
7581
7582 mutex_unlock(&fan_mutex);
7583 return rc;
7584}
7585
7586static int fan_set_enable(void)
7587{
7588 u8 s;
7589 int rc;
7590
7591 if (!fan_control_allowed)
7592 return -EPERM;
7593
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007594 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007595 return -ERESTARTSYS;
7596
7597 switch (fan_control_access_mode) {
7598 case TPACPI_FAN_WR_ACPI_FANS:
7599 case TPACPI_FAN_WR_TPEC:
7600 rc = fan_get_status(&s);
7601 if (rc < 0)
7602 break;
7603
7604 /* Don't go out of emergency fan mode */
7605 if (s != 7) {
7606 s &= 0x07;
7607 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
7608 }
7609
7610 if (!acpi_ec_write(fan_status_offset, s))
7611 rc = -EIO;
7612 else {
7613 tp_features.fan_ctrl_status_undef = 0;
7614 rc = 0;
7615 }
7616 break;
7617
7618 case TPACPI_FAN_WR_ACPI_SFAN:
7619 rc = fan_get_status(&s);
7620 if (rc < 0)
7621 break;
7622
7623 s &= 0x07;
7624
7625 /* Set fan to at least level 4 */
7626 s |= 4;
7627
7628 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007629 rc = -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007630 else
7631 rc = 0;
7632 break;
7633
7634 default:
7635 rc = -ENXIO;
7636 }
7637
7638 mutex_unlock(&fan_mutex);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007639
7640 if (!rc)
7641 vdbg_printk(TPACPI_DBG_FAN,
7642 "fan control: set fan control register to 0x%02x\n",
7643 s);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007644 return rc;
7645}
7646
7647static int fan_set_disable(void)
7648{
7649 int rc;
7650
7651 if (!fan_control_allowed)
7652 return -EPERM;
7653
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007654 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007655 return -ERESTARTSYS;
7656
7657 rc = 0;
7658 switch (fan_control_access_mode) {
7659 case TPACPI_FAN_WR_ACPI_FANS:
7660 case TPACPI_FAN_WR_TPEC:
7661 if (!acpi_ec_write(fan_status_offset, 0x00))
7662 rc = -EIO;
7663 else {
7664 fan_control_desired_level = 0;
7665 tp_features.fan_ctrl_status_undef = 0;
7666 }
7667 break;
7668
7669 case TPACPI_FAN_WR_ACPI_SFAN:
7670 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
7671 rc = -EIO;
7672 else
7673 fan_control_desired_level = 0;
7674 break;
7675
7676 default:
7677 rc = -ENXIO;
7678 }
7679
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007680 if (!rc)
7681 vdbg_printk(TPACPI_DBG_FAN,
7682 "fan control: set fan control register to 0\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007683
7684 mutex_unlock(&fan_mutex);
7685 return rc;
7686}
7687
7688static int fan_set_speed(int speed)
7689{
7690 int rc;
7691
7692 if (!fan_control_allowed)
7693 return -EPERM;
7694
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007695 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007696 return -ERESTARTSYS;
7697
7698 rc = 0;
7699 switch (fan_control_access_mode) {
7700 case TPACPI_FAN_WR_ACPI_FANS:
7701 if (speed >= 0 && speed <= 65535) {
7702 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
7703 speed, speed, speed))
7704 rc = -EIO;
7705 } else
7706 rc = -EINVAL;
7707 break;
7708
7709 default:
7710 rc = -ENXIO;
7711 }
7712
7713 mutex_unlock(&fan_mutex);
7714 return rc;
7715}
7716
7717static void fan_watchdog_reset(void)
7718{
7719 static int fan_watchdog_active;
7720
7721 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
7722 return;
7723
7724 if (fan_watchdog_active)
7725 cancel_delayed_work(&fan_watchdog_task);
7726
7727 if (fan_watchdog_maxinterval > 0 &&
7728 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
7729 fan_watchdog_active = 1;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03007730 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007731 msecs_to_jiffies(fan_watchdog_maxinterval
7732 * 1000))) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007733 printk(TPACPI_ERR
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03007734 "failed to queue the fan watchdog, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007735 "watchdog will not trigger\n");
7736 }
7737 } else
7738 fan_watchdog_active = 0;
7739}
7740
7741static void fan_watchdog_fire(struct work_struct *ignored)
7742{
7743 int rc;
7744
7745 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
7746 return;
7747
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007748 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007749 rc = fan_set_enable();
7750 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007751 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007752 "will try again later...\n", -rc);
7753 /* reschedule for later */
7754 fan_watchdog_reset();
7755 }
7756}
7757
7758/*
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007759 * SYSFS fan layout: hwmon compatible (device)
7760 *
7761 * pwm*_enable:
7762 * 0: "disengaged" mode
7763 * 1: manual mode
7764 * 2: native EC "auto" mode (recommended, hardware default)
7765 *
7766 * pwm*: set speed in manual mode, ignored otherwise.
7767 * 0 is level 0; 255 is level 7. Intermediate points done with linear
7768 * interpolation.
7769 *
7770 * fan*_input: tachometer reading, RPM
7771 *
7772 *
7773 * SYSFS fan layout: extensions
7774 *
7775 * fan_watchdog (driver):
7776 * fan watchdog interval in seconds, 0 disables (default), max 120
7777 */
7778
7779/* sysfs fan pwm1_enable ----------------------------------------------- */
7780static ssize_t fan_pwm1_enable_show(struct device *dev,
7781 struct device_attribute *attr,
7782 char *buf)
7783{
7784 int res, mode;
7785 u8 status;
7786
7787 res = fan_get_status_safe(&status);
7788 if (res)
7789 return res;
7790
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007791 if (status & TP_EC_FAN_FULLSPEED) {
7792 mode = 0;
7793 } else if (status & TP_EC_FAN_AUTO) {
7794 mode = 2;
7795 } else
7796 mode = 1;
7797
7798 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
7799}
7800
7801static ssize_t fan_pwm1_enable_store(struct device *dev,
7802 struct device_attribute *attr,
7803 const char *buf, size_t count)
7804{
7805 unsigned long t;
7806 int res, level;
7807
7808 if (parse_strtoul(buf, 2, &t))
7809 return -EINVAL;
7810
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007811 tpacpi_disclose_usertask("hwmon pwm1_enable",
7812 "set fan mode to %lu\n", t);
7813
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007814 switch (t) {
7815 case 0:
7816 level = TP_EC_FAN_FULLSPEED;
7817 break;
7818 case 1:
7819 level = TPACPI_FAN_LAST_LEVEL;
7820 break;
7821 case 2:
7822 level = TP_EC_FAN_AUTO;
7823 break;
7824 case 3:
7825 /* reserved for software-controlled auto mode */
7826 return -ENOSYS;
7827 default:
7828 return -EINVAL;
7829 }
7830
7831 res = fan_set_level_safe(level);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03007832 if (res == -ENXIO)
7833 return -EINVAL;
7834 else if (res < 0)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007835 return res;
7836
7837 fan_watchdog_reset();
7838
7839 return count;
7840}
7841
7842static struct device_attribute dev_attr_fan_pwm1_enable =
7843 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
7844 fan_pwm1_enable_show, fan_pwm1_enable_store);
7845
7846/* sysfs fan pwm1 ------------------------------------------------------ */
7847static ssize_t fan_pwm1_show(struct device *dev,
7848 struct device_attribute *attr,
7849 char *buf)
7850{
7851 int res;
7852 u8 status;
7853
7854 res = fan_get_status_safe(&status);
7855 if (res)
7856 return res;
7857
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007858 if ((status &
7859 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
7860 status = fan_control_desired_level;
7861
7862 if (status > 7)
7863 status = 7;
7864
7865 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
7866}
7867
7868static ssize_t fan_pwm1_store(struct device *dev,
7869 struct device_attribute *attr,
7870 const char *buf, size_t count)
7871{
7872 unsigned long s;
7873 int rc;
7874 u8 status, newlevel;
7875
7876 if (parse_strtoul(buf, 255, &s))
7877 return -EINVAL;
7878
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007879 tpacpi_disclose_usertask("hwmon pwm1",
7880 "set fan speed to %lu\n", s);
7881
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007882 /* scale down from 0-255 to 0-7 */
7883 newlevel = (s >> 5) & 0x07;
7884
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007885 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02007886 return -ERESTARTSYS;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007887
7888 rc = fan_get_status(&status);
7889 if (!rc && (status &
7890 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7891 rc = fan_set_level(newlevel);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03007892 if (rc == -ENXIO)
7893 rc = -EINVAL;
7894 else if (!rc) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007895 fan_update_desired_level(newlevel);
Henrique de Moraes Holschuhca4ac2f2007-04-27 22:00:11 -03007896 fan_watchdog_reset();
7897 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007898 }
7899
7900 mutex_unlock(&fan_mutex);
7901 return (rc)? rc : count;
7902}
7903
7904static struct device_attribute dev_attr_fan_pwm1 =
7905 __ATTR(pwm1, S_IWUSR | S_IRUGO,
7906 fan_pwm1_show, fan_pwm1_store);
7907
7908/* sysfs fan fan1_input ------------------------------------------------ */
7909static ssize_t fan_fan1_input_show(struct device *dev,
7910 struct device_attribute *attr,
7911 char *buf)
7912{
7913 int res;
7914 unsigned int speed;
7915
7916 res = fan_get_speed(&speed);
7917 if (res < 0)
7918 return res;
7919
7920 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7921}
7922
7923static struct device_attribute dev_attr_fan_fan1_input =
7924 __ATTR(fan1_input, S_IRUGO,
7925 fan_fan1_input_show, NULL);
7926
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007927/* sysfs fan fan2_input ------------------------------------------------ */
7928static ssize_t fan_fan2_input_show(struct device *dev,
7929 struct device_attribute *attr,
7930 char *buf)
7931{
7932 int res;
7933 unsigned int speed;
7934
7935 res = fan2_get_speed(&speed);
7936 if (res < 0)
7937 return res;
7938
7939 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7940}
7941
7942static struct device_attribute dev_attr_fan_fan2_input =
7943 __ATTR(fan2_input, S_IRUGO,
7944 fan_fan2_input_show, NULL);
7945
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007946/* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007947static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
7948 char *buf)
7949{
7950 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
7951}
7952
7953static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
7954 const char *buf, size_t count)
7955{
7956 unsigned long t;
7957
7958 if (parse_strtoul(buf, 120, &t))
7959 return -EINVAL;
7960
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03007961 if (!fan_control_allowed)
7962 return -EPERM;
7963
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007964 fan_watchdog_maxinterval = t;
7965 fan_watchdog_reset();
7966
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007967 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
7968
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007969 return count;
7970}
7971
7972static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
7973 fan_fan_watchdog_show, fan_fan_watchdog_store);
7974
7975/* --------------------------------------------------------------------- */
7976static struct attribute *fan_attributes[] = {
7977 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
7978 &dev_attr_fan_fan1_input.attr,
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007979 NULL, /* for fan2_input */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007980 NULL
7981};
7982
7983static const struct attribute_group fan_attr_group = {
7984 .attrs = fan_attributes,
7985};
7986
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007987#define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
7988#define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007989
7990#define TPACPI_FAN_QI(__id1, __id2, __quirks) \
7991 { .vendor = PCI_VENDOR_ID_IBM, \
7992 .bios = TPACPI_MATCH_ANY, \
7993 .ec = TPID(__id1, __id2), \
7994 .quirks = __quirks }
7995
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007996#define TPACPI_FAN_QL(__id1, __id2, __quirks) \
7997 { .vendor = PCI_VENDOR_ID_LENOVO, \
7998 .bios = TPACPI_MATCH_ANY, \
7999 .ec = TPID(__id1, __id2), \
8000 .quirks = __quirks }
8001
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008002static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
8003 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
8004 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
8005 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
8006 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008007 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008008};
8009
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008010#undef TPACPI_FAN_QL
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008011#undef TPACPI_FAN_QI
8012
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008013static int __init fan_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008014{
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008015 int rc;
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008016 unsigned long quirks;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008017
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008018 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8019 "initializing fan subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008020
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03008021 mutex_init(&fan_mutex);
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008022 fan_status_access_mode = TPACPI_FAN_NONE;
8023 fan_control_access_mode = TPACPI_FAN_WR_NONE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008024 fan_control_commands = 0;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008025 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03008026 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008027 tp_features.second_fan = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008028 fan_control_desired_level = 7;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008029
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03008030 if (tpacpi_is_ibm()) {
8031 TPACPI_ACPIHANDLE_INIT(fans);
8032 TPACPI_ACPIHANDLE_INIT(gfan);
8033 TPACPI_ACPIHANDLE_INIT(sfan);
8034 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008035
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008036 quirks = tpacpi_check_quirks(fan_quirk_table,
8037 ARRAY_SIZE(fan_quirk_table));
8038
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008039 if (gfan_handle) {
8040 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008041 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008042 } else {
8043 /* all other ThinkPads: note that even old-style
8044 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02008045 if (likely(acpi_ec_read(fan_status_offset,
8046 &fan_control_initial_status))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008047 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008048 if (quirks & TPACPI_FAN_Q1)
8049 fan_quirk1_setup();
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008050 if (quirks & TPACPI_FAN_2FAN) {
8051 tp_features.second_fan = 1;
8052 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8053 "secondary fan support enabled\n");
8054 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008055 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008056 printk(TPACPI_ERR
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008057 "ThinkPad ACPI EC access misbehaving, "
8058 "fan status and control unavailable\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008059 return 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008060 }
8061 }
8062
8063 if (sfan_handle) {
8064 /* 570, 770x-JL */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008065 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02008066 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008067 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008068 } else {
8069 if (!gfan_handle) {
8070 /* gfan without sfan means no fan control */
8071 /* all other models implement TP EC 0x2f control */
8072
8073 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02008074 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008075 fan_control_access_mode =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008076 TPACPI_FAN_WR_ACPI_FANS;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008077 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008078 TPACPI_FAN_CMD_SPEED |
8079 TPACPI_FAN_CMD_LEVEL |
8080 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008081 } else {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008082 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02008083 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008084 TPACPI_FAN_CMD_LEVEL |
8085 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008086 }
8087 }
8088 }
8089
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008090 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8091 "fan is %s, modes %d, %d\n",
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008092 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
8093 fan_control_access_mode != TPACPI_FAN_WR_NONE),
8094 fan_status_access_mode, fan_control_access_mode);
8095
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008096 /* fan control master switch */
8097 if (!fan_control_allowed) {
8098 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8099 fan_control_commands = 0;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008100 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008101 "fan control features disabled by parameter\n");
8102 }
8103
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008104 /* update fan_control_desired_level */
8105 if (fan_status_access_mode != TPACPI_FAN_NONE)
8106 fan_get_status_safe(NULL);
8107
8108 if (fan_status_access_mode != TPACPI_FAN_NONE ||
8109 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008110 if (tp_features.second_fan) {
8111 /* attach second fan tachometer */
8112 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
8113 &dev_attr_fan_fan2_input.attr;
8114 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008115 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008116 &fan_attr_group);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008117 if (rc < 0)
8118 return rc;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03008119
8120 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
8121 &driver_attr_fan_watchdog);
8122 if (rc < 0) {
8123 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
8124 &fan_attr_group);
8125 return rc;
8126 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008127 return 0;
8128 } else
8129 return 1;
8130}
8131
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008132static void fan_exit(void)
8133{
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008134 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008135 "cancelling any pending fan watchdog tasks\n");
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008136
8137 /* FIXME: can we really do this unconditionally? */
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008138 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008139 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
8140 &driver_attr_fan_watchdog);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008141
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008142 cancel_delayed_work(&fan_watchdog_task);
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03008143 flush_workqueue(tpacpi_wq);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008144}
8145
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008146static void fan_suspend(pm_message_t state)
8147{
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008148 int rc;
8149
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008150 if (!fan_control_allowed)
8151 return;
8152
8153 /* Store fan status in cache */
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008154 fan_control_resume_level = 0;
8155 rc = fan_get_status_safe(&fan_control_resume_level);
8156 if (rc < 0)
8157 printk(TPACPI_NOTICE
8158 "failed to read fan level for later "
8159 "restore during resume: %d\n", rc);
8160
8161 /* if it is undefined, don't attempt to restore it.
8162 * KEEP THIS LAST */
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008163 if (tp_features.fan_ctrl_status_undef)
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008164 fan_control_resume_level = 0;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008165}
8166
8167static void fan_resume(void)
8168{
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008169 u8 current_level = 7;
8170 bool do_set = false;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008171 int rc;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008172
8173 /* DSDT *always* updates status on resume */
8174 tp_features.fan_ctrl_status_undef = 0;
8175
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008176 if (!fan_control_allowed ||
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008177 !fan_control_resume_level ||
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008178 (fan_get_status_safe(&current_level) < 0))
8179 return;
8180
8181 switch (fan_control_access_mode) {
8182 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008183 /* never decrease fan level */
8184 do_set = (fan_control_resume_level > current_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008185 break;
8186 case TPACPI_FAN_WR_ACPI_FANS:
8187 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008188 /* never decrease fan level, scale is:
8189 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
8190 *
8191 * We expect the firmware to set either 7 or AUTO, but we
8192 * handle FULLSPEED out of paranoia.
8193 *
8194 * So, we can safely only restore FULLSPEED or 7, anything
8195 * else could slow the fan. Restoring AUTO is useless, at
8196 * best that's exactly what the DSDT already set (it is the
8197 * slower it uses).
8198 *
8199 * Always keep in mind that the DSDT *will* have set the
8200 * fans to what the vendor supposes is the best level. We
8201 * muck with it only to speed the fan up.
8202 */
8203 if (fan_control_resume_level != 7 &&
8204 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
8205 return;
8206 else
8207 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
8208 (current_level != fan_control_resume_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008209 break;
8210 default:
8211 return;
8212 }
8213 if (do_set) {
8214 printk(TPACPI_NOTICE
8215 "restoring fan level to 0x%02x\n",
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008216 fan_control_resume_level);
8217 rc = fan_set_level_safe(fan_control_resume_level);
8218 if (rc < 0)
8219 printk(TPACPI_NOTICE
8220 "failed to restore fan level: %d\n", rc);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008221 }
8222}
8223
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008224static int fan_read(struct seq_file *m)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008225{
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008226 int rc;
8227 u8 status;
8228 unsigned int speed = 0;
8229
8230 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008231 case TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008232 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008233 rc = fan_get_status_safe(&status);
8234 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008235 return rc;
8236
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008237 seq_printf(m, "status:\t\t%s\n"
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008238 "level:\t\t%d\n",
8239 (status != 0) ? "enabled" : "disabled", status);
8240 break;
8241
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008242 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008243 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008244 rc = fan_get_status_safe(&status);
8245 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008246 return rc;
8247
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008248 seq_printf(m, "status:\t\t%s\n",
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008249 (status != 0) ? "enabled" : "disabled");
8250
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008251 rc = fan_get_speed(&speed);
8252 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008253 return rc;
8254
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008255 seq_printf(m, "speed:\t\t%d\n", speed);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008256
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008257 if (status & TP_EC_FAN_FULLSPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008258 /* Disengaged mode takes precedence */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008259 seq_printf(m, "level:\t\tdisengaged\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008260 else if (status & TP_EC_FAN_AUTO)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008261 seq_printf(m, "level:\t\tauto\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008262 else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008263 seq_printf(m, "level:\t\t%d\n", status);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008264 break;
8265
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008266 case TPACPI_FAN_NONE:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008267 default:
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008268 seq_printf(m, "status:\t\tnot supported\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008269 }
8270
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008271 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008272 seq_printf(m, "commands:\tlevel <level>");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008273
8274 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008275 case TPACPI_FAN_WR_ACPI_SFAN:
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008276 seq_printf(m, " (<level> is 0-7)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008277 break;
8278
8279 default:
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008280 seq_printf(m, " (<level> is 0-7, "
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008281 "auto, disengaged, full-speed)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008282 break;
8283 }
8284 }
8285
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008286 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008287 seq_printf(m, "commands:\tenable, disable\n"
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008288 "commands:\twatchdog <timeout> (<timeout> "
8289 "is 0 (off), 1-120 (seconds))\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008290
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008291 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008292 seq_printf(m, "commands:\tspeed <speed>"
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008293 " (<speed> is 0-65535)\n");
8294
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008295 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008296}
8297
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008298static int fan_write_cmd_level(const char *cmd, int *rc)
8299{
8300 int level;
8301
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02008302 if (strlencmp(cmd, "level auto") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008303 level = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008304 else if ((strlencmp(cmd, "level disengaged") == 0) |
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008305 (strlencmp(cmd, "level full-speed") == 0))
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008306 level = TP_EC_FAN_FULLSPEED;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02008307 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008308 return 0;
8309
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008310 *rc = fan_set_level_safe(level);
8311 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008312 printk(TPACPI_ERR "level command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008313 "access mode %d", fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008314 else if (!*rc)
8315 tpacpi_disclose_usertask("procfs fan",
8316 "set level to %d\n", level);
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008317
8318 return 1;
8319}
8320
8321static int fan_write_cmd_enable(const char *cmd, int *rc)
8322{
8323 if (strlencmp(cmd, "enable") != 0)
8324 return 0;
8325
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008326 *rc = fan_set_enable();
8327 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008328 printk(TPACPI_ERR "enable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008329 "access mode %d", fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008330 else if (!*rc)
8331 tpacpi_disclose_usertask("procfs fan", "enable\n");
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008332
8333 return 1;
8334}
8335
8336static int fan_write_cmd_disable(const char *cmd, int *rc)
8337{
8338 if (strlencmp(cmd, "disable") != 0)
8339 return 0;
8340
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008341 *rc = fan_set_disable();
8342 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008343 printk(TPACPI_ERR "disable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008344 "access mode %d", fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008345 else if (!*rc)
8346 tpacpi_disclose_usertask("procfs fan", "disable\n");
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008347
8348 return 1;
8349}
8350
8351static int fan_write_cmd_speed(const char *cmd, int *rc)
8352{
8353 int speed;
8354
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02008355 /* TODO:
8356 * Support speed <low> <medium> <high> ? */
8357
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008358 if (sscanf(cmd, "speed %d", &speed) != 1)
8359 return 0;
8360
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008361 *rc = fan_set_speed(speed);
8362 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008363 printk(TPACPI_ERR "speed command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008364 "access mode %d", fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008365 else if (!*rc)
8366 tpacpi_disclose_usertask("procfs fan",
8367 "set speed to %d\n", speed);
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008368
8369 return 1;
8370}
8371
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008372static int fan_write_cmd_watchdog(const char *cmd, int *rc)
8373{
8374 int interval;
8375
8376 if (sscanf(cmd, "watchdog %d", &interval) != 1)
8377 return 0;
8378
8379 if (interval < 0 || interval > 120)
8380 *rc = -EINVAL;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008381 else {
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008382 fan_watchdog_maxinterval = interval;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008383 tpacpi_disclose_usertask("procfs fan",
8384 "set watchdog timer to %d\n",
8385 interval);
8386 }
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008387
8388 return 1;
8389}
8390
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008391static int fan_write(char *buf)
8392{
8393 char *cmd;
8394 int rc = 0;
8395
8396 while (!rc && (cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008397 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008398 fan_write_cmd_level(cmd, &rc)) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008399 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008400 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008401 fan_write_cmd_disable(cmd, &rc) ||
8402 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008403 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008404 fan_write_cmd_speed(cmd, &rc))
8405 )
8406 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008407 else if (!rc)
8408 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008409 }
8410
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008411 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008412}
8413
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008414static struct ibm_struct fan_driver_data = {
8415 .name = "fan",
8416 .read = fan_read,
8417 .write = fan_write,
8418 .exit = fan_exit,
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008419 .suspend = fan_suspend,
8420 .resume = fan_resume,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008421};
8422
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008423/****************************************************************************
8424 ****************************************************************************
8425 *
8426 * Infrastructure
8427 *
8428 ****************************************************************************
8429 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07008430
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03008431/*
8432 * HKEY event callout for other subdrivers go here
8433 * (yes, it is ugly, but it is quick, safe, and gets the job done
8434 */
8435static void tpacpi_driver_event(const unsigned int hkey_event)
8436{
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00008437 if (ibm_backlight_device) {
8438 switch (hkey_event) {
8439 case TP_HKEY_EV_BRGHT_UP:
8440 case TP_HKEY_EV_BRGHT_DOWN:
8441 tpacpi_brightness_notify_change();
8442 }
8443 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02008444 if (alsa_card) {
8445 switch (hkey_event) {
8446 case TP_HKEY_EV_VOL_UP:
8447 case TP_HKEY_EV_VOL_DOWN:
8448 case TP_HKEY_EV_VOL_MUTE:
8449 volume_alsa_notify_change();
8450 }
8451 }
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03008452}
8453
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03008454static void hotkey_driver_event(const unsigned int scancode)
8455{
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03008456 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode);
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03008457}
8458
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008459/* sysfs name ---------------------------------------------------------- */
8460static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
8461 struct device_attribute *attr,
8462 char *buf)
8463{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008464 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008465}
8466
8467static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
8468 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
8469
8470/* --------------------------------------------------------------------- */
8471
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008472/* /proc support */
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03008473static struct proc_dir_entry *proc_dir;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008474
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008475/*
8476 * Module and infrastructure proble, init and exit handling
8477 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008478
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008479static int force_load;
8480
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008481#ifdef CONFIG_THINKPAD_ACPI_DEBUG
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008482static const char * __init str_supported(int is_supported)
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008483{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008484 static char text_unsupported[] __initdata = "not supported";
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008485
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008486 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008487}
8488#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
8489
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008490static void ibm_exit(struct ibm_struct *ibm)
8491{
8492 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
8493
8494 list_del_init(&ibm->all_drivers);
8495
8496 if (ibm->flags.acpi_notify_installed) {
8497 dbg_printk(TPACPI_DBG_EXIT,
8498 "%s: acpi_remove_notify_handler\n", ibm->name);
8499 BUG_ON(!ibm->acpi);
8500 acpi_remove_notify_handler(*ibm->acpi->handle,
8501 ibm->acpi->type,
8502 dispatch_acpi_notify);
8503 ibm->flags.acpi_notify_installed = 0;
8504 ibm->flags.acpi_notify_installed = 0;
8505 }
8506
8507 if (ibm->flags.proc_created) {
8508 dbg_printk(TPACPI_DBG_EXIT,
8509 "%s: remove_proc_entry\n", ibm->name);
8510 remove_proc_entry(ibm->name, proc_dir);
8511 ibm->flags.proc_created = 0;
8512 }
8513
8514 if (ibm->flags.acpi_driver_registered) {
8515 dbg_printk(TPACPI_DBG_EXIT,
8516 "%s: acpi_bus_unregister_driver\n", ibm->name);
8517 BUG_ON(!ibm->acpi);
8518 acpi_bus_unregister_driver(ibm->acpi->driver);
8519 kfree(ibm->acpi->driver);
8520 ibm->acpi->driver = NULL;
8521 ibm->flags.acpi_driver_registered = 0;
8522 }
8523
8524 if (ibm->flags.init_called && ibm->exit) {
8525 ibm->exit();
8526 ibm->flags.init_called = 0;
8527 }
8528
8529 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
8530}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02008531
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008532static int __init ibm_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008533{
8534 int ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008535 struct ibm_struct *ibm = iibm->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008536 struct proc_dir_entry *entry;
8537
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008538 BUG_ON(ibm == NULL);
8539
8540 INIT_LIST_HEAD(&ibm->all_drivers);
8541
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03008542 if (ibm->flags.experimental && !experimental)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008543 return 0;
8544
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008545 dbg_printk(TPACPI_DBG_INIT,
8546 "probing for %s\n", ibm->name);
8547
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008548 if (iibm->init) {
8549 ret = iibm->init(iibm);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008550 if (ret > 0)
8551 return 0; /* probe failed */
8552 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008553 return ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008554
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03008555 ibm->flags.init_called = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008556 }
8557
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03008558 if (ibm->acpi) {
8559 if (ibm->acpi->hid) {
8560 ret = register_tpacpi_subdriver(ibm);
8561 if (ret)
8562 goto err_out;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008563 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03008564
8565 if (ibm->acpi->notify) {
8566 ret = setup_acpi_notify(ibm);
8567 if (ret == -ENODEV) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008568 printk(TPACPI_NOTICE "disabling subdriver %s\n",
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03008569 ibm->name);
8570 ret = 0;
8571 goto err_out;
8572 }
8573 if (ret < 0)
8574 goto err_out;
8575 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008576 }
8577
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008578 dbg_printk(TPACPI_DBG_INIT,
8579 "%s installed\n", ibm->name);
8580
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008581 if (ibm->read) {
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03008582 mode_t mode = iibm->base_procfs_mode;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008583
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03008584 if (!mode)
8585 mode = S_IRUGO;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008586 if (ibm->write)
8587 mode |= S_IWUSR;
8588 entry = proc_create_data(ibm->name, mode, proc_dir,
8589 &dispatch_proc_fops, ibm);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008590 if (!entry) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008591 printk(TPACPI_ERR "unable to create proc entry %s\n",
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008592 ibm->name);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008593 ret = -ENODEV;
8594 goto err_out;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008595 }
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03008596 ibm->flags.proc_created = 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008597 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008598
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008599 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
8600
Linus Torvalds1da177e2005-04-16 15:20:36 -07008601 return 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008602
8603err_out:
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008604 dbg_printk(TPACPI_DBG_INIT,
8605 "%s: at error exit path with result %d\n",
8606 ibm->name, ret);
8607
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008608 ibm_exit(ibm);
8609 return (ret < 0)? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008610}
8611
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008612/* Probing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008613
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008614static bool __pure __init tpacpi_is_fw_digit(const char c)
8615{
8616 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
8617}
8618
8619/* Most models: xxyTkkWW (#.##c); Ancient 570/600 and -SL lacks (#.##c) */
8620static bool __pure __init tpacpi_is_valid_fw_id(const char* const s,
8621 const char t)
8622{
8623 return s && strlen(s) >= 8 &&
8624 tpacpi_is_fw_digit(s[0]) &&
8625 tpacpi_is_fw_digit(s[1]) &&
8626 s[2] == t && s[3] == 'T' &&
8627 tpacpi_is_fw_digit(s[4]) &&
8628 tpacpi_is_fw_digit(s[5]) &&
8629 s[6] == 'W' && s[7] == 'W';
8630}
8631
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008632/* returns 0 - probe ok, or < 0 - probe error.
8633 * Probe ok doesn't mean thinkpad found.
8634 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
8635static int __must_check __init get_thinkpad_model_data(
8636 struct thinkpad_id_data *tp)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02008637{
Jeff Garzik18552562007-10-03 15:15:40 -04008638 const struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02008639 char ec_fw_string[18];
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008640 char const *s;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02008641
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008642 if (!tp)
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008643 return -EINVAL;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008644
8645 memset(tp, 0, sizeof(*tp));
8646
8647 if (dmi_name_in_vendors("IBM"))
8648 tp->vendor = PCI_VENDOR_ID_IBM;
8649 else if (dmi_name_in_vendors("LENOVO"))
8650 tp->vendor = PCI_VENDOR_ID_LENOVO;
8651 else
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008652 return 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008653
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008654 s = dmi_get_system_info(DMI_BIOS_VERSION);
8655 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
8656 if (s && !tp->bios_version_str)
8657 return -ENOMEM;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008658
8659 /* Really ancient ThinkPad 240X will fail this, which is fine */
8660 if (!tpacpi_is_valid_fw_id(tp->bios_version_str, 'E'))
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008661 return 0;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008662
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008663 tp->bios_model = tp->bios_version_str[0]
8664 | (tp->bios_version_str[1] << 8);
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008665 tp->bios_release = (tp->bios_version_str[4] << 8)
8666 | tp->bios_version_str[5];
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008667
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02008668 /*
8669 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
8670 * X32 or newer, all Z series; Some models must have an
8671 * up-to-date BIOS or they will not be detected.
8672 *
8673 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
8674 */
8675 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02008676 if (sscanf(dev->name,
8677 "IBM ThinkPad Embedded Controller -[%17c",
8678 ec_fw_string) == 1) {
8679 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
8680 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008681
8682 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008683 if (!tp->ec_version_str)
8684 return -ENOMEM;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008685
8686 if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) {
8687 tp->ec_model = ec_fw_string[0]
8688 | (ec_fw_string[1] << 8);
8689 tp->ec_release = (ec_fw_string[4] << 8)
8690 | ec_fw_string[5];
8691 } else {
8692 printk(TPACPI_NOTICE
8693 "ThinkPad firmware release %s "
8694 "doesn't match the known patterns\n",
8695 ec_fw_string);
8696 printk(TPACPI_NOTICE
8697 "please report this to %s\n",
8698 TPACPI_MAIL);
8699 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008700 break;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02008701 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02008702 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008703
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008704 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
8705 if (s && !strnicmp(s, "ThinkPad", 8)) {
8706 tp->model_str = kstrdup(s, GFP_KERNEL);
8707 if (!tp->model_str)
8708 return -ENOMEM;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008709 }
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03008710
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008711 s = dmi_get_system_info(DMI_PRODUCT_NAME);
8712 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
8713 if (s && !tp->nummodel_str)
8714 return -ENOMEM;
8715
8716 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008717}
8718
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008719static int __init probe_for_thinkpad(void)
8720{
8721 int is_thinkpad;
8722
8723 if (acpi_disabled)
8724 return -ENODEV;
8725
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03008726 /* It would be dangerous to run the driver in this case */
8727 if (!tpacpi_is_ibm() && !tpacpi_is_lenovo())
8728 return -ENODEV;
8729
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008730 /*
8731 * Non-ancient models have better DMI tagging, but very old models
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03008732 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008733 */
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03008734 is_thinkpad = (thinkpad_id.model_str != NULL) ||
8735 (thinkpad_id.ec_model != 0) ||
8736 tpacpi_is_fw_known();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008737
8738 /* ec is required because many other handles are relative to it */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008739 TPACPI_ACPIHANDLE_INIT(ec);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008740 if (!ec_handle) {
8741 if (is_thinkpad)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008742 printk(TPACPI_ERR
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008743 "Not yet supported ThinkPad detected!\n");
8744 return -ENODEV;
8745 }
8746
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03008747 if (!is_thinkpad && !force_load)
8748 return -ENODEV;
8749
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008750 return 0;
8751}
8752
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03008753static void __init thinkpad_acpi_init_banner(void)
8754{
8755 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
8756 printk(TPACPI_INFO "%s\n", TPACPI_URL);
8757
8758 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
8759 (thinkpad_id.bios_version_str) ?
8760 thinkpad_id.bios_version_str : "unknown",
8761 (thinkpad_id.ec_version_str) ?
8762 thinkpad_id.ec_version_str : "unknown");
8763
8764 BUG_ON(!thinkpad_id.vendor);
8765
8766 if (thinkpad_id.model_str)
8767 printk(TPACPI_INFO "%s %s, model %s\n",
8768 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
8769 "IBM" : ((thinkpad_id.vendor ==
8770 PCI_VENDOR_ID_LENOVO) ?
8771 "Lenovo" : "Unknown vendor"),
8772 thinkpad_id.model_str,
8773 (thinkpad_id.nummodel_str) ?
8774 thinkpad_id.nummodel_str : "unknown");
8775}
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008776
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008777/* Module init, exit, parameters */
8778
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008779static struct ibm_init_struct ibms_init[] __initdata = {
8780 {
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008781 .data = &thinkpad_acpi_driver_data,
8782 },
8783 {
8784 .init = hotkey_init,
8785 .data = &hotkey_driver_data,
8786 },
8787 {
8788 .init = bluetooth_init,
8789 .data = &bluetooth_driver_data,
8790 },
8791 {
8792 .init = wan_init,
8793 .data = &wan_driver_data,
8794 },
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02008795 {
8796 .init = uwb_init,
8797 .data = &uwb_driver_data,
8798 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02008799#ifdef CONFIG_THINKPAD_ACPI_VIDEO
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008800 {
8801 .init = video_init,
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03008802 .base_procfs_mode = S_IRUSR,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008803 .data = &video_driver_data,
8804 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02008805#endif
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008806 {
8807 .init = light_init,
8808 .data = &light_driver_data,
8809 },
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008810 {
8811 .init = cmos_init,
8812 .data = &cmos_driver_data,
8813 },
8814 {
8815 .init = led_init,
8816 .data = &led_driver_data,
8817 },
8818 {
8819 .init = beep_init,
8820 .data = &beep_driver_data,
8821 },
8822 {
8823 .init = thermal_init,
8824 .data = &thermal_driver_data,
8825 },
8826 {
8827 .data = &ecdump_driver_data,
8828 },
8829 {
8830 .init = brightness_init,
8831 .data = &brightness_driver_data,
8832 },
8833 {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02008834 .init = volume_init,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008835 .data = &volume_driver_data,
8836 },
8837 {
8838 .init = fan_init,
8839 .data = &fan_driver_data,
8840 },
8841};
8842
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008843static int __init set_ibm_param(const char *val, struct kernel_param *kp)
8844{
8845 unsigned int i;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008846 struct ibm_struct *ibm;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008847
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02008848 if (!kp || !kp->name || !val)
8849 return -EINVAL;
8850
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008851 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
8852 ibm = ibms_init[i].data;
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02008853 WARN_ON(ibm == NULL);
8854
8855 if (!ibm || !ibm->name)
8856 continue;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008857
8858 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
8859 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008860 return -ENOSPC;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008861 strcpy(ibms_init[i].param, val);
8862 strcat(ibms_init[i].param, ",");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008863 return 0;
8864 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008865 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008866
8867 return -EINVAL;
8868}
8869
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008870module_param(experimental, int, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008871MODULE_PARM_DESC(experimental,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008872 "Enables experimental features when non-zero");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008873
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03008874module_param_named(debug, dbg_level, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008875MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03008876
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008877module_param(force_load, bool, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008878MODULE_PARM_DESC(force_load,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008879 "Attempts to load the driver even on a "
8880 "mis-identified ThinkPad when true");
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03008881
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008882module_param_named(fan_control, fan_control_allowed, bool, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008883MODULE_PARM_DESC(fan_control,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008884 "Enables setting fan parameters features when true");
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008885
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008886module_param_named(brightness_mode, brightness_mode, uint, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008887MODULE_PARM_DESC(brightness_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008888 "Selects brightness control strategy: "
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00008889 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03008890
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008891module_param(brightness_enable, uint, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008892MODULE_PARM_DESC(brightness_enable,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008893 "Enables backlight control when 1, disables when 0");
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02008894
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008895module_param(hotkey_report_mode, uint, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008896MODULE_PARM_DESC(hotkey_report_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008897 "used for backwards compatibility with userspace, "
8898 "see documentation");
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03008899
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02008900#ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02008901module_param_named(volume_mode, volume_mode, uint, 0444);
8902MODULE_PARM_DESC(volume_mode,
8903 "Selects volume control strategy: "
8904 "0=auto, 1=EC, 2=N/A, 3=EC+NVRAM");
8905
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02008906module_param_named(volume_capabilities, volume_capabilities, uint, 0444);
8907MODULE_PARM_DESC(volume_capabilities,
8908 "Selects the mixer capabilites: "
8909 "0=auto, 1=volume and mute, 2=mute only");
8910
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02008911module_param_named(volume_control, volume_control_allowed, bool, 0444);
8912MODULE_PARM_DESC(volume_control,
8913 "Enables software override for the console audio "
8914 "control when true");
8915
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02008916/* ALSA module API parameters */
8917module_param_named(index, alsa_index, int, 0444);
8918MODULE_PARM_DESC(index, "ALSA index for the ACPI EC Mixer");
8919module_param_named(id, alsa_id, charp, 0444);
8920MODULE_PARM_DESC(id, "ALSA id for the ACPI EC Mixer");
8921module_param_named(enable, alsa_enable, bool, 0444);
8922MODULE_PARM_DESC(enable, "Enable the ALSA interface for the ACPI EC Mixer");
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02008923#endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02008924
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008925#define TPACPI_PARAM(feature) \
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008926 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
Henrique de Moraes Holschuhcbb14842008-02-16 02:17:50 -02008927 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008928 "at module load, see documentation")
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008929
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008930TPACPI_PARAM(hotkey);
8931TPACPI_PARAM(bluetooth);
8932TPACPI_PARAM(video);
8933TPACPI_PARAM(light);
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008934TPACPI_PARAM(cmos);
8935TPACPI_PARAM(led);
8936TPACPI_PARAM(beep);
8937TPACPI_PARAM(ecdump);
8938TPACPI_PARAM(brightness);
8939TPACPI_PARAM(volume);
8940TPACPI_PARAM(fan);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008941
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02008942#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008943module_param(dbg_wlswemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02008944MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
8945module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
8946MODULE_PARM_DESC(wlsw_state,
8947 "Initial state of the emulated WLSW switch");
8948
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008949module_param(dbg_bluetoothemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02008950MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
8951module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
8952MODULE_PARM_DESC(bluetooth_state,
8953 "Initial state of the emulated bluetooth switch");
8954
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008955module_param(dbg_wwanemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02008956MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
8957module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
8958MODULE_PARM_DESC(wwan_state,
8959 "Initial state of the emulated WWAN switch");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02008960
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008961module_param(dbg_uwbemul, uint, 0444);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02008962MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
8963module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
8964MODULE_PARM_DESC(uwb_state,
8965 "Initial state of the emulated UWB switch");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02008966#endif
8967
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008968static void thinkpad_acpi_module_exit(void)
8969{
8970 struct ibm_struct *ibm, *itmp;
8971
8972 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
8973
8974 list_for_each_entry_safe_reverse(ibm, itmp,
8975 &tpacpi_all_drivers,
8976 all_drivers) {
8977 ibm_exit(ibm);
8978 }
8979
8980 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
8981
8982 if (tpacpi_inputdev) {
8983 if (tp_features.input_device_registered)
8984 input_unregister_device(tpacpi_inputdev);
8985 else
8986 input_free_device(tpacpi_inputdev);
8987 }
8988
8989 if (tpacpi_hwmon)
8990 hwmon_device_unregister(tpacpi_hwmon);
8991
8992 if (tp_features.sensors_pdev_attrs_registered)
8993 device_remove_file(&tpacpi_sensors_pdev->dev,
8994 &dev_attr_thinkpad_acpi_pdev_name);
8995 if (tpacpi_sensors_pdev)
8996 platform_device_unregister(tpacpi_sensors_pdev);
8997 if (tpacpi_pdev)
8998 platform_device_unregister(tpacpi_pdev);
8999
9000 if (tp_features.sensors_pdrv_attrs_registered)
9001 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
9002 if (tp_features.platform_drv_attrs_registered)
9003 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
9004
9005 if (tp_features.sensors_pdrv_registered)
9006 platform_driver_unregister(&tpacpi_hwmon_pdriver);
9007
9008 if (tp_features.platform_drv_registered)
9009 platform_driver_unregister(&tpacpi_pdriver);
9010
9011 if (proc_dir)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009012 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009013
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03009014 if (tpacpi_wq)
9015 destroy_workqueue(tpacpi_wq);
9016
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009017 kfree(thinkpad_id.bios_version_str);
9018 kfree(thinkpad_id.ec_version_str);
9019 kfree(thinkpad_id.model_str);
9020}
9021
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02009022
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03009023static int __init thinkpad_acpi_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009024{
9025 int ret, i;
9026
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03009027 tpacpi_lifecycle = TPACPI_LIFE_INIT;
9028
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03009029 /* Parameter checking */
9030 if (hotkey_report_mode > 2)
9031 return -EINVAL;
9032
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009033 /* Driver-level probe */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009034
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009035 ret = get_thinkpad_model_data(&thinkpad_id);
9036 if (ret) {
9037 printk(TPACPI_ERR
9038 "unable to get DMI data: %d\n", ret);
9039 thinkpad_acpi_module_exit();
9040 return ret;
9041 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009042 ret = probe_for_thinkpad();
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009043 if (ret) {
9044 thinkpad_acpi_module_exit();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009045 return ret;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009046 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009047
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009048 /* Driver initialization */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009049
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03009050 thinkpad_acpi_init_banner();
9051 tpacpi_check_outdated_fw();
9052
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009053 TPACPI_ACPIHANDLE_INIT(ecrd);
9054 TPACPI_ACPIHANDLE_INIT(ecwr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009055
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03009056 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
9057 if (!tpacpi_wq) {
9058 thinkpad_acpi_module_exit();
9059 return -ENOMEM;
9060 }
9061
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009062 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009063 if (!proc_dir) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009064 printk(TPACPI_ERR
9065 "unable to create proc dir " TPACPI_PROC_DIR);
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03009066 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07009067 return -ENODEV;
9068 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04009069
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009070 ret = platform_driver_register(&tpacpi_pdriver);
9071 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009072 printk(TPACPI_ERR
9073 "unable to register main platform driver\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009074 thinkpad_acpi_module_exit();
9075 return ret;
9076 }
Henrique de Moraes Holschuhac363932007-07-27 17:04:40 -03009077 tp_features.platform_drv_registered = 1;
9078
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009079 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
9080 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009081 printk(TPACPI_ERR
9082 "unable to register hwmon platform driver\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009083 thinkpad_acpi_module_exit();
9084 return ret;
9085 }
9086 tp_features.sensors_pdrv_registered = 1;
9087
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03009088 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03009089 if (!ret) {
9090 tp_features.platform_drv_attrs_registered = 1;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009091 ret = tpacpi_create_driver_attributes(
9092 &tpacpi_hwmon_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03009093 }
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03009094 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009095 printk(TPACPI_ERR
9096 "unable to create sysfs driver attributes\n");
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03009097 thinkpad_acpi_module_exit();
9098 return ret;
9099 }
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03009100 tp_features.sensors_pdrv_attrs_registered = 1;
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03009101
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009102
9103 /* Device initialization */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009104 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009105 NULL, 0);
9106 if (IS_ERR(tpacpi_pdev)) {
9107 ret = PTR_ERR(tpacpi_pdev);
9108 tpacpi_pdev = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009109 printk(TPACPI_ERR "unable to register platform device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009110 thinkpad_acpi_module_exit();
9111 return ret;
9112 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009113 tpacpi_sensors_pdev = platform_device_register_simple(
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009114 TPACPI_HWMON_DRVR_NAME,
9115 -1, NULL, 0);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009116 if (IS_ERR(tpacpi_sensors_pdev)) {
9117 ret = PTR_ERR(tpacpi_sensors_pdev);
9118 tpacpi_sensors_pdev = NULL;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009119 printk(TPACPI_ERR
9120 "unable to register hwmon platform device\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009121 thinkpad_acpi_module_exit();
9122 return ret;
9123 }
9124 ret = device_create_file(&tpacpi_sensors_pdev->dev,
9125 &dev_attr_thinkpad_acpi_pdev_name);
9126 if (ret) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009127 printk(TPACPI_ERR
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009128 "unable to create sysfs hwmon device attributes\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009129 thinkpad_acpi_module_exit();
9130 return ret;
9131 }
9132 tp_features.sensors_pdev_attrs_registered = 1;
9133 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009134 if (IS_ERR(tpacpi_hwmon)) {
9135 ret = PTR_ERR(tpacpi_hwmon);
9136 tpacpi_hwmon = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009137 printk(TPACPI_ERR "unable to register hwmon device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009138 thinkpad_acpi_module_exit();
9139 return ret;
9140 }
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -03009141 mutex_init(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009142 tpacpi_inputdev = input_allocate_device();
9143 if (!tpacpi_inputdev) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009144 printk(TPACPI_ERR "unable to allocate input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009145 thinkpad_acpi_module_exit();
9146 return -ENOMEM;
9147 } else {
9148 /* Prepare input device, but don't register */
9149 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009150 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009151 tpacpi_inputdev->id.bustype = BUS_HOST;
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03009152 tpacpi_inputdev->id.vendor = thinkpad_id.vendor;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009153 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
9154 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
Henrique de Moraes Holschuhd112ef92009-12-09 01:36:26 +00009155 tpacpi_inputdev->dev.parent = &tpacpi_pdev->dev;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009156 }
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03009157
9158 /* Init subdriver dependencies */
9159 tpacpi_detect_brightness_capabilities();
9160
9161 /* Init subdrivers */
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009162 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
9163 ret = ibm_init(&ibms_init[i]);
9164 if (ret >= 0 && *ibms_init[i].param)
9165 ret = ibms_init[i].data->write(ibms_init[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009166 if (ret < 0) {
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03009167 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07009168 return ret;
9169 }
9170 }
Henrique de Moraes Holschuhb589ea42010-02-25 21:28:58 -03009171
9172 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
9173
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009174 ret = input_register_device(tpacpi_inputdev);
9175 if (ret < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009176 printk(TPACPI_ERR "unable to register input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009177 thinkpad_acpi_module_exit();
9178 return ret;
9179 } else {
9180 tp_features.input_device_registered = 1;
9181 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009182
9183 return 0;
9184}
9185
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03009186MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
9187
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009188/*
Henrique de Moraes Holschuh922fe092009-04-14 02:44:13 +00009189 * This will autoload the driver in almost every ThinkPad
9190 * in widespread use.
9191 *
9192 * Only _VERY_ old models, like the 240, 240x and 570 lack
9193 * the HKEY event interface.
9194 */
9195MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
9196
9197/*
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009198 * DMI matching for module autoloading
9199 *
9200 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
9201 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
9202 *
9203 * Only models listed in thinkwiki will be supported, so add yours
9204 * if it is not there yet.
9205 */
9206#define IBM_BIOS_MODULE_ALIAS(__type) \
Mathieu Chouquet-Stringerb36a50f2009-03-14 16:35:26 +01009207 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009208
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009209/* Ancient thinkpad BIOSes have to be identified by
9210 * BIOS type or model number, and there are far less
9211 * BIOS types than model numbers... */
Henrique de Moraes Holschuh922fe092009-04-14 02:44:13 +00009212IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009213
Henrique de Moraes Holschuhf68f53a2009-04-14 02:44:12 +00009214MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
9215MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009216MODULE_DESCRIPTION(TPACPI_DESC);
9217MODULE_VERSION(TPACPI_VERSION);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009218MODULE_LICENSE("GPL");
9219
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03009220module_init(thinkpad_acpi_module_init);
9221module_exit(thinkpad_acpi_module_exit);