blob: 1f27b350ae01197c226ea6660a20e333923c5f02 [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 Holschuhbee4cd9b2009-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;
307 u32 bright_16levels:1;
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -0300308 u32 bright_acpimode: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 Holschuha73f3092009-01-11 03:01:00 -0200371#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
372static int dbg_wlswemul;
373static int tpacpi_wlsw_emulstate;
374static int dbg_bluetoothemul;
375static int tpacpi_bluetooth_emulstate;
376static int dbg_wwanemul;
377static int tpacpi_wwan_emulstate;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -0200378static int dbg_uwbemul;
379static int tpacpi_uwb_emulstate;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200380#endif
381
382
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000383/*************************************************************************
384 * Debugging helpers
385 */
386
387#define dbg_printk(a_dbg_level, format, arg...) \
388 do { if (dbg_level & (a_dbg_level)) \
389 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
390 } while (0)
391
392#ifdef CONFIG_THINKPAD_ACPI_DEBUG
393#define vdbg_printk dbg_printk
394static const char *str_supported(int is_supported);
395#else
396#define vdbg_printk(a_dbg_level, format, arg...) \
397 do { } while (0)
398#endif
399
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +0000400static void tpacpi_log_usertask(const char * const what)
401{
402 printk(TPACPI_DEBUG "%s: access by process with PID %d\n",
403 what, task_tgid_vnr(current));
404}
405
406#define tpacpi_disclose_usertask(what, format, arg...) \
407 do { \
408 if (unlikely( \
409 (dbg_level & TPACPI_DBG_DISCLOSETASK) && \
410 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
411 printk(TPACPI_DEBUG "%s: PID %d: " format, \
412 what, task_tgid_vnr(current), ## arg); \
413 } \
414 } while (0)
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000415
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -0300416/*
417 * Quirk handling helpers
418 *
419 * ThinkPad IDs and versions seen in the field so far
420 * are two-characters from the set [0-9A-Z], i.e. base 36.
421 *
422 * We use values well outside that range as specials.
423 */
424
425#define TPACPI_MATCH_ANY 0xffffU
426#define TPACPI_MATCH_UNKNOWN 0U
427
428/* TPID('1', 'Y') == 0x5931 */
429#define TPID(__c1, __c2) (((__c2) << 8) | (__c1))
430
431#define TPACPI_Q_IBM(__id1, __id2, __quirk) \
432 { .vendor = PCI_VENDOR_ID_IBM, \
433 .bios = TPID(__id1, __id2), \
434 .ec = TPACPI_MATCH_ANY, \
435 .quirks = (__quirk) }
436
437#define TPACPI_Q_LNV(__id1, __id2, __quirk) \
438 { .vendor = PCI_VENDOR_ID_LENOVO, \
439 .bios = TPID(__id1, __id2), \
440 .ec = TPACPI_MATCH_ANY, \
441 .quirks = (__quirk) }
442
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -0200443#define TPACPI_QEC_LNV(__id1, __id2, __quirk) \
444 { .vendor = PCI_VENDOR_ID_LENOVO, \
445 .bios = TPACPI_MATCH_ANY, \
446 .ec = TPID(__id1, __id2), \
447 .quirks = (__quirk) }
448
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -0300449struct tpacpi_quirk {
450 unsigned int vendor;
451 u16 bios;
452 u16 ec;
453 unsigned long quirks;
454};
455
456/**
457 * tpacpi_check_quirks() - search BIOS/EC version on a list
458 * @qlist: array of &struct tpacpi_quirk
459 * @qlist_size: number of elements in @qlist
460 *
461 * Iterates over a quirks list until one is found that matches the
462 * ThinkPad's vendor, BIOS and EC model.
463 *
464 * Returns 0 if nothing matches, otherwise returns the quirks field of
465 * the matching &struct tpacpi_quirk entry.
466 *
467 * The match criteria is: vendor, ec and bios much match.
468 */
469static unsigned long __init tpacpi_check_quirks(
470 const struct tpacpi_quirk *qlist,
471 unsigned int qlist_size)
472{
473 while (qlist_size) {
474 if ((qlist->vendor == thinkpad_id.vendor ||
475 qlist->vendor == TPACPI_MATCH_ANY) &&
476 (qlist->bios == thinkpad_id.bios_model ||
477 qlist->bios == TPACPI_MATCH_ANY) &&
478 (qlist->ec == thinkpad_id.ec_model ||
479 qlist->ec == TPACPI_MATCH_ANY))
480 return qlist->quirks;
481
482 qlist_size--;
483 qlist++;
484 }
485 return 0;
486}
487
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -0300488static inline bool __pure __init tpacpi_is_lenovo(void)
489{
490 return thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO;
491}
492
493static inline bool __pure __init tpacpi_is_ibm(void)
494{
495 return thinkpad_id.vendor == PCI_VENDOR_ID_IBM;
496}
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -0300497
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300498/****************************************************************************
499 ****************************************************************************
500 *
501 * ACPI Helpers and device model
502 *
503 ****************************************************************************
504 ****************************************************************************/
505
506/*************************************************************************
507 * ACPI basic handles
508 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -0300510static acpi_handle root_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200512#define TPACPI_HANDLE(object, parent, paths...) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 static acpi_handle object##_handle; \
514 static acpi_handle *object##_parent = &parent##_handle; \
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400515 static char *object##_path; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 static char *object##_paths[] = { paths }
517
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200518TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400519 "\\_SB.PCI.ISA.EC", /* 570 */
520 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
521 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
522 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
523 "\\_SB.PCI0.ICH3.EC0", /* R31 */
Henrique de Moraes Holschuha3189302010-05-16 19:45:26 -0300524 "\\_SB.PCI0.LPC0.EC", /* X100e and a few others */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400525 "\\_SB.PCI0.LPC.EC", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300526 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200528TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
529TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200531TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
532 /* T4x, X31, X40 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400533 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
534 "\\CMS", /* R40, R40e */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300535 ); /* all others */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400536
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200537TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400538 "^HKEY", /* R30, R31 */
539 "HKEY", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300540 ); /* 570 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400541
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -0200542TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
543 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
544 "\\_SB.PCI0.VID0", /* 770e */
545 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
Henrique de Moraes Holschuha3189302010-05-16 19:45:26 -0300546 "\\_SB.PCI0.AGP.VGA", /* X100e and a few others */
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -0200547 "\\_SB.PCI0.AGP.VID", /* all others */
548 ); /* R30, R31 */
549
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400550
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300551/*************************************************************************
552 * ACPI helpers
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -0200553 */
554
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555static int acpi_evalf(acpi_handle handle,
556 void *res, char *method, char *fmt, ...)
557{
558 char *fmt0 = fmt;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400559 struct acpi_object_list params;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200560 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400561 struct acpi_buffer result, *resultp;
562 union acpi_object out_obj;
563 acpi_status status;
564 va_list ap;
565 char res_type;
566 int success;
567 int quiet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
569 if (!*fmt) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200570 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 return 0;
572 }
573
574 if (*fmt == 'q') {
575 quiet = 1;
576 fmt++;
577 } else
578 quiet = 0;
579
580 res_type = *(fmt++);
581
582 params.count = 0;
583 params.pointer = &in_objs[0];
584
585 va_start(ap, fmt);
586 while (*fmt) {
587 char c = *(fmt++);
588 switch (c) {
589 case 'd': /* int */
590 in_objs[params.count].integer.value = va_arg(ap, int);
591 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
592 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400593 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 default:
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200595 printk(TPACPI_ERR "acpi_evalf() called "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 "with invalid format character '%c'\n", c);
597 return 0;
598 }
599 }
600 va_end(ap);
601
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400602 if (res_type != 'v') {
603 result.length = sizeof(out_obj);
604 result.pointer = &out_obj;
605 resultp = &result;
606 } else
607 resultp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400609 status = acpi_evaluate_object(handle, method, &params, resultp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
611 switch (res_type) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400612 case 'd': /* int */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 if (res)
614 *(int *)res = out_obj.integer.value;
615 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
616 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400617 case 'v': /* void */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 success = status == AE_OK;
619 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400620 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 default:
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200622 printk(TPACPI_ERR "acpi_evalf() called "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 "with invalid format character '%c'\n", res_type);
624 return 0;
625 }
626
627 if (!success && !quiet)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200628 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 method, fmt0, status);
630
631 return success;
632}
633
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200634static int acpi_ec_read(int i, u8 *p)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300635{
636 int v;
637
638 if (ecrd_handle) {
639 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
640 return 0;
641 *p = v;
642 } else {
643 if (ec_read(i, p) < 0)
644 return 0;
645 }
646
647 return 1;
648}
649
650static int acpi_ec_write(int i, u8 v)
651{
652 if (ecwr_handle) {
653 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
654 return 0;
655 } else {
656 if (ec_write(i, v) < 0)
657 return 0;
658 }
659
660 return 1;
661}
662
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -0300663static int issue_thinkpad_cmos_command(int cmos_cmd)
664{
665 if (!cmos_handle)
666 return -ENXIO;
667
668 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
669 return -EIO;
670
671 return 0;
672}
673
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300674/*************************************************************************
675 * ACPI device model
676 */
677
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200678#define TPACPI_ACPIHANDLE_INIT(object) \
679 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200680 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
681
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300682static void drv_acpi_handle_init(char *name,
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300683 acpi_handle *handle, acpi_handle parent,
684 char **paths, int num_paths, char **path)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300685{
686 int i;
687 acpi_status status;
688
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300689 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
690 name);
691
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300692 for (i = 0; i < num_paths; i++) {
693 status = acpi_get_handle(parent, paths[i], handle);
694 if (ACPI_SUCCESS(status)) {
695 *path = paths[i];
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300696 dbg_printk(TPACPI_DBG_INIT,
697 "Found ACPI handle %s for %s\n",
698 *path, name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300699 return;
700 }
701 }
702
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300703 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
704 name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300705 *handle = NULL;
706}
707
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300708static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300709{
710 struct ibm_struct *ibm = data;
711
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300712 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
713 return;
714
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300715 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300716 return;
717
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300718 ibm->acpi->notify(ibm, event);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300719}
720
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300721static int __init setup_acpi_notify(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300722{
723 acpi_status status;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300724 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300725
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300726 BUG_ON(!ibm->acpi);
727
728 if (!*ibm->acpi->handle)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300729 return 0;
730
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300731 vdbg_printk(TPACPI_DBG_INIT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300732 "setting up ACPI notify for %s\n", ibm->name);
733
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300734 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
735 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200736 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300737 ibm->name, rc);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300738 return -ENODEV;
739 }
740
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700741 ibm->acpi->device->driver_data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300742 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200743 TPACPI_ACPI_EVENT_PREFIX,
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -0300744 ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300745
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300746 status = acpi_install_notify_handler(*ibm->acpi->handle,
747 ibm->acpi->type, dispatch_acpi_notify, ibm);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300748 if (ACPI_FAILURE(status)) {
749 if (status == AE_ALREADY_EXISTS) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200750 printk(TPACPI_NOTICE
751 "another device driver is already "
752 "handling %s events\n", ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300753 } else {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200754 printk(TPACPI_ERR
755 "acpi_install_notify_handler(%s) failed: %d\n",
756 ibm->name, status);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300757 }
758 return -ENODEV;
759 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300760 ibm->flags.acpi_notify_installed = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300761 return 0;
762}
763
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300764static int __init tpacpi_device_add(struct acpi_device *device)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300765{
766 return 0;
767}
768
Henrique de Moraes Holschuh67001212007-04-21 11:08:25 -0300769static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300770{
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300771 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300772
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300773 dbg_printk(TPACPI_DBG_INIT,
774 "registering %s as an ACPI driver\n", ibm->name);
775
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300776 BUG_ON(!ibm->acpi);
777
778 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
779 if (!ibm->acpi->driver) {
Mariusz Kozlowski4f778b92008-10-18 14:23:53 -0300780 printk(TPACPI_ERR
781 "failed to allocate memory for ibm->acpi->driver\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300782 return -ENOMEM;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300783 }
784
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200785 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300786 ibm->acpi->driver->ids = ibm->acpi->hid;
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200787
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300788 ibm->acpi->driver->ops.add = &tpacpi_device_add;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300789
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300790 rc = acpi_bus_register_driver(ibm->acpi->driver);
791 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200792 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200793 ibm->name, rc);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300794 kfree(ibm->acpi->driver);
795 ibm->acpi->driver = NULL;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300796 } else if (!rc)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300797 ibm->flags.acpi_driver_registered = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300798
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300799 return rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300800}
801
802
803/****************************************************************************
804 ****************************************************************************
805 *
806 * Procfs Helpers
807 *
808 ****************************************************************************
809 ****************************************************************************/
810
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200811static int dispatch_proc_show(struct seq_file *m, void *v)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300812{
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200813 struct ibm_struct *ibm = m->private;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300814
815 if (!ibm || !ibm->read)
816 return -EINVAL;
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200817 return ibm->read(m);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300818}
819
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200820static int dispatch_proc_open(struct inode *inode, struct file *file)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300821{
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200822 return single_open(file, dispatch_proc_show, PDE(inode)->data);
823}
824
825static ssize_t dispatch_proc_write(struct file *file,
826 const char __user *userbuf,
827 size_t count, loff_t *pos)
828{
829 struct ibm_struct *ibm = PDE(file->f_path.dentry->d_inode)->data;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300830 char *kernbuf;
831 int ret;
832
833 if (!ibm || !ibm->write)
834 return -EINVAL;
Michael Buesch5b05d462009-08-01 12:04:19 -0300835 if (count > PAGE_SIZE - 2)
836 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300837
838 kernbuf = kmalloc(count + 2, GFP_KERNEL);
839 if (!kernbuf)
840 return -ENOMEM;
841
842 if (copy_from_user(kernbuf, userbuf, count)) {
843 kfree(kernbuf);
844 return -EFAULT;
845 }
846
847 kernbuf[count] = 0;
848 strcat(kernbuf, ",");
849 ret = ibm->write(kernbuf);
850 if (ret == 0)
851 ret = count;
852
853 kfree(kernbuf);
854
855 return ret;
856}
857
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200858static const struct file_operations dispatch_proc_fops = {
859 .owner = THIS_MODULE,
860 .open = dispatch_proc_open,
861 .read = seq_read,
862 .llseek = seq_lseek,
863 .release = single_release,
864 .write = dispatch_proc_write,
865};
866
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867static char *next_cmd(char **cmds)
868{
869 char *start = *cmds;
870 char *end;
871
872 while ((end = strchr(start, ',')) && end == start)
873 start = end + 1;
874
875 if (!end)
876 return NULL;
877
878 *end = 0;
879 *cmds = end + 1;
880 return start;
881}
882
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300883
884/****************************************************************************
885 ****************************************************************************
886 *
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300887 * Device model: input, hwmon and platform
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300888 *
889 ****************************************************************************
890 ****************************************************************************/
891
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -0300892static struct platform_device *tpacpi_pdev;
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300893static struct platform_device *tpacpi_sensors_pdev;
Tony Jones1beeffe2007-08-20 13:46:20 -0700894static struct device *tpacpi_hwmon;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300895static struct input_dev *tpacpi_inputdev;
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -0300896static struct mutex tpacpi_inputdev_send_mutex;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200897static LIST_HEAD(tpacpi_all_drivers);
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300898
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200899static int tpacpi_suspend_handler(struct platform_device *pdev,
900 pm_message_t state)
901{
902 struct ibm_struct *ibm, *itmp;
903
904 list_for_each_entry_safe(ibm, itmp,
905 &tpacpi_all_drivers,
906 all_drivers) {
907 if (ibm->suspend)
908 (ibm->suspend)(state);
909 }
910
911 return 0;
912}
913
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300914static int tpacpi_resume_handler(struct platform_device *pdev)
915{
916 struct ibm_struct *ibm, *itmp;
917
918 list_for_each_entry_safe(ibm, itmp,
919 &tpacpi_all_drivers,
920 all_drivers) {
921 if (ibm->resume)
922 (ibm->resume)();
923 }
924
925 return 0;
926}
927
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200928static void tpacpi_shutdown_handler(struct platform_device *pdev)
929{
930 struct ibm_struct *ibm, *itmp;
931
932 list_for_each_entry_safe(ibm, itmp,
933 &tpacpi_all_drivers,
934 all_drivers) {
935 if (ibm->shutdown)
936 (ibm->shutdown)();
937 }
938}
939
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300940static struct platform_driver tpacpi_pdriver = {
941 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200942 .name = TPACPI_DRVR_NAME,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300943 .owner = THIS_MODULE,
944 },
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200945 .suspend = tpacpi_suspend_handler,
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300946 .resume = tpacpi_resume_handler,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200947 .shutdown = tpacpi_shutdown_handler,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300948};
949
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300950static struct platform_driver tpacpi_hwmon_pdriver = {
951 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200952 .name = TPACPI_HWMON_DRVR_NAME,
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300953 .owner = THIS_MODULE,
954 },
955};
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300956
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -0300957/*************************************************************************
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300958 * sysfs support helpers
959 */
960
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200961struct attribute_set {
962 unsigned int members, max_members;
963 struct attribute_group group;
964};
965
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300966struct attribute_set_obj {
967 struct attribute_set s;
968 struct attribute *a;
969} __attribute__((packed));
970
971static struct attribute_set *create_attr_set(unsigned int max_members,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200972 const char *name)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300973{
974 struct attribute_set_obj *sobj;
975
976 if (max_members == 0)
977 return NULL;
978
979 /* Allocates space for implicit NULL at the end too */
980 sobj = kzalloc(sizeof(struct attribute_set_obj) +
981 max_members * sizeof(struct attribute *),
982 GFP_KERNEL);
983 if (!sobj)
984 return NULL;
985 sobj->s.max_members = max_members;
986 sobj->s.group.attrs = &sobj->a;
987 sobj->s.group.name = name;
988
989 return &sobj->s;
990}
991
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200992#define destroy_attr_set(_set) \
993 kfree(_set);
994
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300995/* not multi-threaded safe, use it in a single thread per set */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200996static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300997{
998 if (!s || !attr)
999 return -EINVAL;
1000
1001 if (s->members >= s->max_members)
1002 return -ENOMEM;
1003
1004 s->group.attrs[s->members] = attr;
1005 s->members++;
1006
1007 return 0;
1008}
1009
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001010static int add_many_to_attr_set(struct attribute_set *s,
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001011 struct attribute **attr,
1012 unsigned int count)
1013{
1014 int i, res;
1015
1016 for (i = 0; i < count; i++) {
1017 res = add_to_attr_set(s, attr[i]);
1018 if (res)
1019 return res;
1020 }
1021
1022 return 0;
1023}
1024
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001025static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001026{
1027 sysfs_remove_group(kobj, &s->group);
1028 destroy_attr_set(s);
1029}
1030
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001031#define register_attr_set_with_sysfs(_attr_set, _kobj) \
1032 sysfs_create_group(_kobj, &_attr_set->group)
1033
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001034static int parse_strtoul(const char *buf,
1035 unsigned long max, unsigned long *value)
1036{
1037 char *endp;
1038
André Goddard Rosae7d28602009-12-14 18:01:06 -08001039 *value = simple_strtoul(skip_spaces(buf), &endp, 0);
1040 endp = skip_spaces(endp);
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001041 if (*endp || *value > max)
1042 return -EINVAL;
1043
1044 return 0;
1045}
1046
Henrique de Moraes Holschuhd64c81c42008-10-18 14:23:55 -03001047static void tpacpi_disable_brightness_delay(void)
1048{
1049 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
1050 printk(TPACPI_NOTICE
1051 "ACPI backlight control delay disabled\n");
1052}
1053
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03001054static int __init tpacpi_query_bcl_levels(acpi_handle handle)
1055{
1056 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1057 union acpi_object *obj;
1058 int rc;
1059
1060 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
1061 obj = (union acpi_object *)buffer.pointer;
1062 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
1063 printk(TPACPI_ERR "Unknown _BCL data, "
1064 "please report this to %s\n", TPACPI_MAIL);
1065 rc = 0;
1066 } else {
1067 rc = obj->package.count;
1068 }
1069 } else {
1070 return 0;
1071 }
1072
1073 kfree(buffer.pointer);
1074 return rc;
1075}
1076
1077static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
1078 u32 lvl, void *context, void **rv)
1079{
1080 char name[ACPI_PATH_SEGMENT_LENGTH];
1081 struct acpi_buffer buffer = { sizeof(name), &name };
1082
1083 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
1084 !strncmp("_BCL", name, sizeof(name) - 1)) {
1085 BUG_ON(!rv || !*rv);
1086 **(int **)rv = tpacpi_query_bcl_levels(handle);
1087 return AE_CTRL_TERMINATE;
1088 } else {
1089 return AE_OK;
1090 }
1091}
1092
1093/*
1094 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
1095 */
1096static int __init tpacpi_check_std_acpi_brightness_support(void)
1097{
1098 int status;
1099 int bcl_levels = 0;
1100 void *bcl_ptr = &bcl_levels;
1101
1102 if (!vid_handle) {
1103 TPACPI_ACPIHANDLE_INIT(vid);
1104 }
1105 if (!vid_handle)
1106 return 0;
1107
1108 /*
1109 * Search for a _BCL method, and execute it. This is safe on all
1110 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
1111 * BIOS in ACPI backlight control mode. We do NOT have to care
1112 * about calling the _BCL method in an enabled video device, any
1113 * will do for our purposes.
1114 */
1115
1116 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
Lin Ming22635762009-11-13 10:06:08 +08001117 tpacpi_acpi_walk_find_bcl, NULL, NULL,
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03001118 &bcl_ptr);
1119
1120 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
1121 tp_features.bright_acpimode = 1;
1122 return (bcl_levels - 2);
1123 }
1124
1125 return 0;
1126}
1127
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00001128static void printk_deprecated_attribute(const char * const what,
1129 const char * const details)
1130{
1131 tpacpi_log_usertask("deprecated sysfs attribute");
1132 printk(TPACPI_WARN "WARNING: sysfs attribute %s is deprecated and "
1133 "will be removed. %s\n",
1134 what, details);
1135}
1136
Johannes Berg19d337d2009-06-02 13:01:37 +02001137/*************************************************************************
1138 * rfkill and radio control support helpers
1139 */
1140
1141/*
1142 * ThinkPad-ACPI firmware handling model:
1143 *
1144 * WLSW (master wireless switch) is event-driven, and is common to all
1145 * firmware-controlled radios. It cannot be controlled, just monitored,
1146 * as expected. It overrides all radio state in firmware
1147 *
1148 * The kernel, a masked-off hotkey, and WLSW can change the radio state
1149 * (TODO: verify how WLSW interacts with the returned radio state).
1150 *
1151 * The only time there are shadow radio state changes, is when
1152 * masked-off hotkeys are used.
1153 */
1154
1155/*
1156 * Internal driver API for radio state:
1157 *
1158 * int: < 0 = error, otherwise enum tpacpi_rfkill_state
1159 * bool: true means radio blocked (off)
1160 */
1161enum tpacpi_rfkill_state {
1162 TPACPI_RFK_RADIO_OFF = 0,
1163 TPACPI_RFK_RADIO_ON
1164};
1165
1166/* rfkill switches */
1167enum tpacpi_rfk_id {
1168 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
1169 TPACPI_RFK_WWAN_SW_ID,
1170 TPACPI_RFK_UWB_SW_ID,
1171 TPACPI_RFK_SW_MAX
1172};
1173
1174static const char *tpacpi_rfkill_names[] = {
1175 [TPACPI_RFK_BLUETOOTH_SW_ID] = "bluetooth",
1176 [TPACPI_RFK_WWAN_SW_ID] = "wwan",
1177 [TPACPI_RFK_UWB_SW_ID] = "uwb",
1178 [TPACPI_RFK_SW_MAX] = NULL
1179};
1180
1181/* ThinkPad-ACPI rfkill subdriver */
1182struct tpacpi_rfk {
1183 struct rfkill *rfkill;
1184 enum tpacpi_rfk_id id;
1185 const struct tpacpi_rfk_ops *ops;
1186};
1187
1188struct tpacpi_rfk_ops {
1189 /* firmware interface */
1190 int (*get_status)(void);
1191 int (*set_status)(const enum tpacpi_rfkill_state);
1192};
1193
1194static struct tpacpi_rfk *tpacpi_rfkill_switches[TPACPI_RFK_SW_MAX];
1195
1196/* Query FW and update rfkill sw state for a given rfkill switch */
1197static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk *tp_rfk)
1198{
1199 int status;
1200
1201 if (!tp_rfk)
1202 return -ENODEV;
1203
1204 status = (tp_rfk->ops->get_status)();
1205 if (status < 0)
1206 return status;
1207
1208 rfkill_set_sw_state(tp_rfk->rfkill,
1209 (status == TPACPI_RFK_RADIO_OFF));
1210
1211 return status;
1212}
1213
1214/* Query FW and update rfkill sw state for all rfkill switches */
1215static void tpacpi_rfk_update_swstate_all(void)
1216{
1217 unsigned int i;
1218
1219 for (i = 0; i < TPACPI_RFK_SW_MAX; i++)
1220 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[i]);
1221}
1222
1223/*
1224 * Sync the HW-blocking state of all rfkill switches,
1225 * do notice it causes the rfkill core to schedule uevents
1226 */
1227static void tpacpi_rfk_update_hwblock_state(bool blocked)
1228{
1229 unsigned int i;
1230 struct tpacpi_rfk *tp_rfk;
1231
1232 for (i = 0; i < TPACPI_RFK_SW_MAX; i++) {
1233 tp_rfk = tpacpi_rfkill_switches[i];
1234 if (tp_rfk) {
1235 if (rfkill_set_hw_state(tp_rfk->rfkill,
1236 blocked)) {
1237 /* ignore -- we track sw block */
1238 }
1239 }
1240 }
1241}
1242
1243/* Call to get the WLSW state from the firmware */
1244static int hotkey_get_wlsw(void);
1245
1246/* Call to query WLSW state and update all rfkill switches */
1247static bool tpacpi_rfk_check_hwblock_state(void)
1248{
1249 int res = hotkey_get_wlsw();
1250 int hw_blocked;
1251
1252 /* When unknown or unsupported, we have to assume it is unblocked */
1253 if (res < 0)
1254 return false;
1255
1256 hw_blocked = (res == TPACPI_RFK_RADIO_OFF);
1257 tpacpi_rfk_update_hwblock_state(hw_blocked);
1258
1259 return hw_blocked;
1260}
1261
1262static int tpacpi_rfk_hook_set_block(void *data, bool blocked)
1263{
1264 struct tpacpi_rfk *tp_rfk = data;
1265 int res;
1266
1267 dbg_printk(TPACPI_DBG_RFKILL,
1268 "request to change radio state to %s\n",
1269 blocked ? "blocked" : "unblocked");
1270
1271 /* try to set radio state */
1272 res = (tp_rfk->ops->set_status)(blocked ?
1273 TPACPI_RFK_RADIO_OFF : TPACPI_RFK_RADIO_ON);
1274
1275 /* and update the rfkill core with whatever the FW really did */
1276 tpacpi_rfk_update_swstate(tp_rfk);
1277
1278 return (res < 0) ? res : 0;
1279}
1280
1281static const struct rfkill_ops tpacpi_rfk_rfkill_ops = {
1282 .set_block = tpacpi_rfk_hook_set_block,
1283};
1284
1285static int __init tpacpi_new_rfkill(const enum tpacpi_rfk_id id,
1286 const struct tpacpi_rfk_ops *tp_rfkops,
1287 const enum rfkill_type rfktype,
1288 const char *name,
1289 const bool set_default)
1290{
1291 struct tpacpi_rfk *atp_rfk;
1292 int res;
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001293 bool sw_state = false;
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001294 bool hw_state;
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001295 int sw_status;
Johannes Berg19d337d2009-06-02 13:01:37 +02001296
1297 BUG_ON(id >= TPACPI_RFK_SW_MAX || tpacpi_rfkill_switches[id]);
1298
Johannes Berg19d337d2009-06-02 13:01:37 +02001299 atp_rfk = kzalloc(sizeof(struct tpacpi_rfk), GFP_KERNEL);
1300 if (atp_rfk)
1301 atp_rfk->rfkill = rfkill_alloc(name,
1302 &tpacpi_pdev->dev,
1303 rfktype,
1304 &tpacpi_rfk_rfkill_ops,
1305 atp_rfk);
1306 if (!atp_rfk || !atp_rfk->rfkill) {
1307 printk(TPACPI_ERR
1308 "failed to allocate memory for rfkill class\n");
1309 kfree(atp_rfk);
1310 return -ENOMEM;
1311 }
1312
1313 atp_rfk->id = id;
1314 atp_rfk->ops = tp_rfkops;
1315
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001316 sw_status = (tp_rfkops->get_status)();
1317 if (sw_status < 0) {
Alan Jenkinsb3fa13292009-06-08 13:27:27 +01001318 printk(TPACPI_ERR
1319 "failed to read initial state for %s, error %d\n",
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001320 name, sw_status);
Alan Jenkinsb3fa13292009-06-08 13:27:27 +01001321 } else {
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001322 sw_state = (sw_status == TPACPI_RFK_RADIO_OFF);
Alan Jenkinsb3fa13292009-06-08 13:27:27 +01001323 if (set_default) {
1324 /* try to keep the initial state, since we ask the
1325 * firmware to preserve it across S5 in NVRAM */
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001326 rfkill_init_sw_state(atp_rfk->rfkill, sw_state);
Alan Jenkinsb3fa13292009-06-08 13:27:27 +01001327 }
1328 }
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001329 hw_state = tpacpi_rfk_check_hwblock_state();
1330 rfkill_set_hw_state(atp_rfk->rfkill, hw_state);
Johannes Berg19d337d2009-06-02 13:01:37 +02001331
1332 res = rfkill_register(atp_rfk->rfkill);
1333 if (res < 0) {
1334 printk(TPACPI_ERR
1335 "failed to register %s rfkill switch: %d\n",
1336 name, res);
1337 rfkill_destroy(atp_rfk->rfkill);
1338 kfree(atp_rfk);
1339 return res;
1340 }
1341
1342 tpacpi_rfkill_switches[id] = atp_rfk;
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001343
1344 printk(TPACPI_INFO "rfkill switch %s: radio is %sblocked\n",
1345 name, (sw_state || hw_state) ? "" : "un");
Johannes Berg19d337d2009-06-02 13:01:37 +02001346 return 0;
1347}
1348
1349static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id)
1350{
1351 struct tpacpi_rfk *tp_rfk;
1352
1353 BUG_ON(id >= TPACPI_RFK_SW_MAX);
1354
1355 tp_rfk = tpacpi_rfkill_switches[id];
1356 if (tp_rfk) {
1357 rfkill_unregister(tp_rfk->rfkill);
Corentin Chary5f0dadb2009-09-14 12:43:52 +02001358 rfkill_destroy(tp_rfk->rfkill);
Johannes Berg19d337d2009-06-02 13:01:37 +02001359 tpacpi_rfkill_switches[id] = NULL;
1360 kfree(tp_rfk);
1361 }
1362}
1363
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00001364static void printk_deprecated_rfkill_attribute(const char * const what)
1365{
1366 printk_deprecated_attribute(what,
1367 "Please switch to generic rfkill before year 2010");
1368}
1369
Johannes Berg19d337d2009-06-02 13:01:37 +02001370/* sysfs <radio> enable ------------------------------------------------ */
1371static ssize_t tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id,
1372 struct device_attribute *attr,
1373 char *buf)
1374{
1375 int status;
1376
1377 printk_deprecated_rfkill_attribute(attr->attr.name);
1378
1379 /* This is in the ABI... */
1380 if (tpacpi_rfk_check_hwblock_state()) {
1381 status = TPACPI_RFK_RADIO_OFF;
1382 } else {
1383 status = tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1384 if (status < 0)
1385 return status;
1386 }
1387
1388 return snprintf(buf, PAGE_SIZE, "%d\n",
1389 (status == TPACPI_RFK_RADIO_ON) ? 1 : 0);
1390}
1391
1392static ssize_t tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id,
1393 struct device_attribute *attr,
1394 const char *buf, size_t count)
1395{
1396 unsigned long t;
1397 int res;
1398
1399 printk_deprecated_rfkill_attribute(attr->attr.name);
1400
1401 if (parse_strtoul(buf, 1, &t))
1402 return -EINVAL;
1403
1404 tpacpi_disclose_usertask(attr->attr.name, "set to %ld\n", t);
1405
1406 /* This is in the ABI... */
1407 if (tpacpi_rfk_check_hwblock_state() && !!t)
1408 return -EPERM;
1409
1410 res = tpacpi_rfkill_switches[id]->ops->set_status((!!t) ?
1411 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF);
1412 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1413
1414 return (res < 0) ? res : count;
1415}
1416
1417/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001418static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id, struct seq_file *m)
Johannes Berg19d337d2009-06-02 13:01:37 +02001419{
Johannes Berg19d337d2009-06-02 13:01:37 +02001420 if (id >= TPACPI_RFK_SW_MAX)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001421 seq_printf(m, "status:\t\tnot supported\n");
Johannes Berg19d337d2009-06-02 13:01:37 +02001422 else {
1423 int status;
1424
1425 /* This is in the ABI... */
1426 if (tpacpi_rfk_check_hwblock_state()) {
1427 status = TPACPI_RFK_RADIO_OFF;
1428 } else {
1429 status = tpacpi_rfk_update_swstate(
1430 tpacpi_rfkill_switches[id]);
1431 if (status < 0)
1432 return status;
1433 }
1434
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001435 seq_printf(m, "status:\t\t%s\n",
Johannes Berg19d337d2009-06-02 13:01:37 +02001436 (status == TPACPI_RFK_RADIO_ON) ?
1437 "enabled" : "disabled");
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001438 seq_printf(m, "commands:\tenable, disable\n");
Johannes Berg19d337d2009-06-02 13:01:37 +02001439 }
1440
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001441 return 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02001442}
1443
1444static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id, char *buf)
1445{
1446 char *cmd;
1447 int status = -1;
1448 int res = 0;
1449
1450 if (id >= TPACPI_RFK_SW_MAX)
1451 return -ENODEV;
1452
1453 while ((cmd = next_cmd(&buf))) {
1454 if (strlencmp(cmd, "enable") == 0)
1455 status = TPACPI_RFK_RADIO_ON;
1456 else if (strlencmp(cmd, "disable") == 0)
1457 status = TPACPI_RFK_RADIO_OFF;
1458 else
1459 return -EINVAL;
1460 }
1461
1462 if (status != -1) {
1463 tpacpi_disclose_usertask("procfs", "attempt to %s %s\n",
1464 (status == TPACPI_RFK_RADIO_ON) ?
1465 "enable" : "disable",
1466 tpacpi_rfkill_names[id]);
1467 res = (tpacpi_rfkill_switches[id]->ops->set_status)(status);
1468 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1469 }
1470
1471 return res;
1472}
1473
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001474/*************************************************************************
1475 * thinkpad-acpi driver attributes
1476 */
1477
1478/* interface_version --------------------------------------------------- */
1479static ssize_t tpacpi_driver_interface_version_show(
1480 struct device_driver *drv,
1481 char *buf)
1482{
1483 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1484}
1485
1486static DRIVER_ATTR(interface_version, S_IRUGO,
1487 tpacpi_driver_interface_version_show, NULL);
1488
1489/* debug_level --------------------------------------------------------- */
1490static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1491 char *buf)
1492{
1493 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1494}
1495
1496static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1497 const char *buf, size_t count)
1498{
1499 unsigned long t;
1500
1501 if (parse_strtoul(buf, 0xffff, &t))
1502 return -EINVAL;
1503
1504 dbg_level = t;
1505
1506 return count;
1507}
1508
1509static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1510 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1511
1512/* version ------------------------------------------------------------- */
1513static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1514 char *buf)
1515{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001516 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1517 TPACPI_DESC, TPACPI_VERSION);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001518}
1519
1520static DRIVER_ATTR(version, S_IRUGO,
1521 tpacpi_driver_version_show, NULL);
1522
1523/* --------------------------------------------------------------------- */
1524
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001525#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1526
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001527/* wlsw_emulstate ------------------------------------------------------ */
1528static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1529 char *buf)
1530{
1531 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1532}
1533
1534static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1535 const char *buf, size_t count)
1536{
1537 unsigned long t;
1538
1539 if (parse_strtoul(buf, 1, &t))
1540 return -EINVAL;
1541
Johannes Berg19d337d2009-06-02 13:01:37 +02001542 if (tpacpi_wlsw_emulstate != !!t) {
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001543 tpacpi_wlsw_emulstate = !!t;
Johannes Berg19d337d2009-06-02 13:01:37 +02001544 tpacpi_rfk_update_hwblock_state(!t); /* negative logic */
1545 }
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001546
1547 return count;
1548}
1549
1550static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1551 tpacpi_driver_wlsw_emulstate_show,
1552 tpacpi_driver_wlsw_emulstate_store);
1553
1554/* bluetooth_emulstate ------------------------------------------------- */
1555static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1556 struct device_driver *drv,
1557 char *buf)
1558{
1559 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1560}
1561
1562static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1563 struct device_driver *drv,
1564 const char *buf, size_t count)
1565{
1566 unsigned long t;
1567
1568 if (parse_strtoul(buf, 1, &t))
1569 return -EINVAL;
1570
1571 tpacpi_bluetooth_emulstate = !!t;
1572
1573 return count;
1574}
1575
1576static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1577 tpacpi_driver_bluetooth_emulstate_show,
1578 tpacpi_driver_bluetooth_emulstate_store);
1579
1580/* wwan_emulstate ------------------------------------------------- */
1581static ssize_t tpacpi_driver_wwan_emulstate_show(
1582 struct device_driver *drv,
1583 char *buf)
1584{
1585 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1586}
1587
1588static ssize_t tpacpi_driver_wwan_emulstate_store(
1589 struct device_driver *drv,
1590 const char *buf, size_t count)
1591{
1592 unsigned long t;
1593
1594 if (parse_strtoul(buf, 1, &t))
1595 return -EINVAL;
1596
1597 tpacpi_wwan_emulstate = !!t;
1598
1599 return count;
1600}
1601
1602static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1603 tpacpi_driver_wwan_emulstate_show,
1604 tpacpi_driver_wwan_emulstate_store);
1605
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001606/* uwb_emulstate ------------------------------------------------- */
1607static ssize_t tpacpi_driver_uwb_emulstate_show(
1608 struct device_driver *drv,
1609 char *buf)
1610{
1611 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1612}
1613
1614static ssize_t tpacpi_driver_uwb_emulstate_store(
1615 struct device_driver *drv,
1616 const char *buf, size_t count)
1617{
1618 unsigned long t;
1619
1620 if (parse_strtoul(buf, 1, &t))
1621 return -EINVAL;
1622
1623 tpacpi_uwb_emulstate = !!t;
1624
1625 return count;
1626}
1627
1628static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
1629 tpacpi_driver_uwb_emulstate_show,
1630 tpacpi_driver_uwb_emulstate_store);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001631#endif
1632
1633/* --------------------------------------------------------------------- */
1634
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001635static struct driver_attribute *tpacpi_driver_attributes[] = {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001636 &driver_attr_debug_level, &driver_attr_version,
1637 &driver_attr_interface_version,
1638};
1639
1640static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1641{
1642 int i, res;
1643
1644 i = 0;
1645 res = 0;
1646 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1647 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1648 i++;
1649 }
1650
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001651#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1652 if (!res && dbg_wlswemul)
1653 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1654 if (!res && dbg_bluetoothemul)
1655 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1656 if (!res && dbg_wwanemul)
1657 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001658 if (!res && dbg_uwbemul)
1659 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001660#endif
1661
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001662 return res;
1663}
1664
1665static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1666{
1667 int i;
1668
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001669 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001670 driver_remove_file(drv, tpacpi_driver_attributes[i]);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001671
1672#ifdef THINKPAD_ACPI_DEBUGFACILITIES
1673 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1674 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1675 driver_remove_file(drv, &driver_attr_wwan_emulstate);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001676 driver_remove_file(drv, &driver_attr_uwb_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001677#endif
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001678}
1679
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001680/*************************************************************************
1681 * Firmware Data
1682 */
1683
1684/*
1685 * Table of recommended minimum BIOS versions
1686 *
1687 * Reasons for listing:
Uwe Kleine-König9ddc5b62010-01-20 17:02:24 +01001688 * 1. Stable BIOS, listed because the unknown amount of
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001689 * bugs and bad ACPI behaviour on older versions
1690 *
1691 * 2. BIOS or EC fw with known bugs that trigger on Linux
1692 *
1693 * 3. BIOS with known reduced functionality in older versions
1694 *
1695 * We recommend the latest BIOS and EC version.
1696 * We only support the latest BIOS and EC fw version as a rule.
1697 *
1698 * Sources: IBM ThinkPad Public Web Documents (update changelogs),
1699 * Information from users in ThinkWiki
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03001700 *
1701 * WARNING: we use this table also to detect that the machine is
1702 * a ThinkPad in some cases, so don't remove entries lightly.
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001703 */
1704
1705#define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1706 { .vendor = (__v), \
1707 .bios = TPID(__id1, __id2), \
1708 .ec = TPACPI_MATCH_ANY, \
1709 .quirks = TPACPI_MATCH_ANY << 16 \
1710 | (__bv1) << 8 | (__bv2) }
1711
1712#define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001713 __eid, __ev1, __ev2) \
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001714 { .vendor = (__v), \
1715 .bios = TPID(__bid1, __bid2), \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001716 .ec = __eid, \
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001717 .quirks = (__ev1) << 24 | (__ev2) << 16 \
1718 | (__bv1) << 8 | (__bv2) }
1719
1720#define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1721 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1722
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001723/* Outdated IBM BIOSes often lack the EC id string */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001724#define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1725 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001726 __bv1, __bv2, TPID(__id1, __id2), \
1727 __ev1, __ev2), \
1728 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1729 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1730 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001731
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001732/* Outdated IBM BIOSes often lack the EC id string */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001733#define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1734 __eid1, __eid2, __ev1, __ev2) \
1735 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001736 __bv1, __bv2, TPID(__eid1, __eid2), \
1737 __ev1, __ev2), \
1738 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1739 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1740 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001741
1742#define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1743 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1744
1745#define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1746 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001747 __bv1, __bv2, TPID(__id1, __id2), \
1748 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001749
1750#define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1751 __eid1, __eid2, __ev1, __ev2) \
1752 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001753 __bv1, __bv2, TPID(__eid1, __eid2), \
1754 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001755
1756static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
1757 /* Numeric models ------------------ */
1758 /* FW MODEL BIOS VERS */
1759 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1760 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1761 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1762 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1763 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1764 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1765 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1766 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1767 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1768
1769 /* A-series ------------------------- */
1770 /* FW MODEL BIOS VERS EC VERS */
1771 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1772 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1773 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1774 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1775 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1776 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1777 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1778 TPV_QI0('1', '3', '2', '0'), /* A22m */
1779 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1780 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1781 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1782
1783 /* G-series ------------------------- */
1784 /* FW MODEL BIOS VERS */
1785 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1786 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1787
1788 /* R-series, T-series --------------- */
1789 /* FW MODEL BIOS VERS EC VERS */
1790 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1791 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1792 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1793 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1794 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1795 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1796 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1797 T40/p, T41/p, T42/p (1) */
1798 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1799 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1800 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1801 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1802
1803 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1804 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1805 TPV_QI0('1', '6', '3', '2'), /* T22 */
1806 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1807 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1808 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1809
1810 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1811 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
Henrique de Moraes Holschuh90765c62009-12-09 01:36:23 +00001812 TPV_QL1('7', 'E', 'D', '0', '1', '5'), /* R60e, R60i */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001813
1814 /* BIOS FW BIOS VERS EC FW EC VERS */
1815 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1816 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1817
1818 /* X-series ------------------------- */
1819 /* FW MODEL BIOS VERS EC VERS */
1820 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1821 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1822 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1823 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1824 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1825 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1826 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1827
Henrique de Moraes Holschuh90765c62009-12-09 01:36:23 +00001828 TPV_QL1('7', 'B', 'D', '7', '4', '0'), /* X60/s */
1829 TPV_QL1('7', 'J', '3', '0', '1', '3'), /* X60t */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001830
1831 /* (0) - older versions lack DMI EC fw string and functionality */
1832 /* (1) - older versions known to lack functionality */
1833};
1834
1835#undef TPV_QL1
1836#undef TPV_QL0
1837#undef TPV_QI2
1838#undef TPV_QI1
1839#undef TPV_QI0
1840#undef TPV_Q_X
1841#undef TPV_Q
1842
1843static void __init tpacpi_check_outdated_fw(void)
1844{
1845 unsigned long fwvers;
1846 u16 ec_version, bios_version;
1847
1848 fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
1849 ARRAY_SIZE(tpacpi_bios_version_qtable));
1850
1851 if (!fwvers)
1852 return;
1853
1854 bios_version = fwvers & 0xffffU;
1855 ec_version = (fwvers >> 16) & 0xffffU;
1856
1857 /* note that unknown versions are set to 0x0000 and we use that */
1858 if ((bios_version > thinkpad_id.bios_release) ||
1859 (ec_version > thinkpad_id.ec_release &&
1860 ec_version != TPACPI_MATCH_ANY)) {
1861 /*
1862 * The changelogs would let us track down the exact
1863 * reason, but it is just too much of a pain to track
1864 * it. We only list BIOSes that are either really
1865 * broken, or really stable to begin with, so it is
1866 * best if the user upgrades the firmware anyway.
1867 */
1868 printk(TPACPI_WARN
1869 "WARNING: Outdated ThinkPad BIOS/EC firmware\n");
1870 printk(TPACPI_WARN
1871 "WARNING: This firmware may be missing critical bug "
1872 "fixes and/or important features\n");
1873 }
1874}
1875
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03001876static bool __init tpacpi_is_fw_known(void)
1877{
1878 return tpacpi_check_quirks(tpacpi_bios_version_qtable,
1879 ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
1880}
1881
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03001882/****************************************************************************
1883 ****************************************************************************
1884 *
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001885 * Subdrivers
1886 *
1887 ****************************************************************************
1888 ****************************************************************************/
1889
1890/*************************************************************************
Henrique de Moraes Holschuh142cfc92007-04-21 11:08:26 -03001891 * thinkpad-acpi init subdriver
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001892 */
1893
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001894static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001896 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
1897 printk(TPACPI_INFO "%s\n", TPACPI_URL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001899 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03001900 (thinkpad_id.bios_version_str) ?
1901 thinkpad_id.bios_version_str : "unknown",
1902 (thinkpad_id.ec_version_str) ?
1903 thinkpad_id.ec_version_str : "unknown");
1904
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03001905 BUG_ON(!thinkpad_id.vendor);
1906
1907 if (thinkpad_id.model_str)
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03001908 printk(TPACPI_INFO "%s %s, model %s\n",
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03001909 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
1910 "IBM" : ((thinkpad_id.vendor ==
1911 PCI_VENDOR_ID_LENOVO) ?
1912 "Lenovo" : "Unknown vendor"),
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03001913 thinkpad_id.model_str,
1914 (thinkpad_id.nummodel_str) ?
1915 thinkpad_id.nummodel_str : "unknown");
Henrique de Moraes Holschuh3945ac32007-02-06 19:13:44 -02001916
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001917 tpacpi_check_outdated_fw();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 return 0;
1919}
1920
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001921static int thinkpad_acpi_driver_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922{
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001923 seq_printf(m, "driver:\t\t%s\n", TPACPI_DESC);
1924 seq_printf(m, "version:\t%s\n", TPACPI_VERSION);
1925 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926}
1927
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001928static struct ibm_struct thinkpad_acpi_driver_data = {
1929 .name = "driver",
1930 .read = thinkpad_acpi_driver_read,
1931};
1932
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001933/*************************************************************************
1934 * Hotkey subdriver
1935 */
1936
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001937/*
1938 * ThinkPad firmware event model
1939 *
1940 * The ThinkPad firmware has two main event interfaces: normal ACPI
1941 * notifications (which follow the ACPI standard), and a private event
1942 * interface.
1943 *
1944 * The private event interface also issues events for the hotkeys. As
1945 * the driver gained features, the event handling code ended up being
1946 * built around the hotkey subdriver. This will need to be refactored
1947 * to a more formal event API eventually.
1948 *
1949 * Some "hotkeys" are actually supposed to be used as event reports,
1950 * such as "brightness has changed", "volume has changed", depending on
1951 * the ThinkPad model and how the firmware is operating.
1952 *
1953 * Unlike other classes, hotkey-class events have mask/unmask control on
1954 * non-ancient firmware. However, how it behaves changes a lot with the
1955 * firmware model and version.
1956 */
1957
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001958enum { /* hot key scan codes (derived from ACPI DSDT) */
1959 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1960 TP_ACPI_HOTKEYSCAN_FNF2,
1961 TP_ACPI_HOTKEYSCAN_FNF3,
1962 TP_ACPI_HOTKEYSCAN_FNF4,
1963 TP_ACPI_HOTKEYSCAN_FNF5,
1964 TP_ACPI_HOTKEYSCAN_FNF6,
1965 TP_ACPI_HOTKEYSCAN_FNF7,
1966 TP_ACPI_HOTKEYSCAN_FNF8,
1967 TP_ACPI_HOTKEYSCAN_FNF9,
1968 TP_ACPI_HOTKEYSCAN_FNF10,
1969 TP_ACPI_HOTKEYSCAN_FNF11,
1970 TP_ACPI_HOTKEYSCAN_FNF12,
1971 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1972 TP_ACPI_HOTKEYSCAN_FNINSERT,
1973 TP_ACPI_HOTKEYSCAN_FNDELETE,
1974 TP_ACPI_HOTKEYSCAN_FNHOME,
1975 TP_ACPI_HOTKEYSCAN_FNEND,
1976 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1977 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1978 TP_ACPI_HOTKEYSCAN_FNSPACE,
1979 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1980 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1981 TP_ACPI_HOTKEYSCAN_MUTE,
1982 TP_ACPI_HOTKEYSCAN_THINKPAD,
1983};
1984
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001985enum { /* Keys/events available through NVRAM polling */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001986 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1987 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1988};
1989
1990enum { /* Positions of some of the keys in hotkey masks */
1991 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1992 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1993 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1994 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1995 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1996 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1997 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1998 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1999 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
2000 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
2001 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
2002};
2003
2004enum { /* NVRAM to ACPI HKEY group map */
2005 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
2006 TP_ACPI_HKEY_ZOOM_MASK |
2007 TP_ACPI_HKEY_DISPSWTCH_MASK |
2008 TP_ACPI_HKEY_HIBERNATE_MASK,
2009 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
2010 TP_ACPI_HKEY_BRGHTDWN_MASK,
2011 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
2012 TP_ACPI_HKEY_VOLDWN_MASK |
2013 TP_ACPI_HKEY_MUTE_MASK,
2014};
2015
2016#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2017struct tp_nvram_state {
2018 u16 thinkpad_toggle:1;
2019 u16 zoom_toggle:1;
2020 u16 display_toggle:1;
2021 u16 thinklight_toggle:1;
2022 u16 hibernate_toggle:1;
2023 u16 displayexp_toggle:1;
2024 u16 display_state:1;
2025 u16 brightness_toggle:1;
2026 u16 volume_toggle:1;
2027 u16 mute:1;
2028
2029 u8 brightness_level;
2030 u8 volume_level;
2031};
2032
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002033/* kthread for the hotkey poller */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002034static struct task_struct *tpacpi_hotkey_task;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002035
2036/* Acquired while the poller kthread is running, use to sync start/stop */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002037static struct mutex hotkey_thread_mutex;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002038
2039/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002040 * Acquire mutex to write poller control variables as an
2041 * atomic block.
2042 *
2043 * Increment hotkey_config_change when changing them if you
2044 * want the kthread to forget old state.
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002045 *
2046 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
2047 */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002048static struct mutex hotkey_thread_data_mutex;
2049static unsigned int hotkey_config_change;
2050
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002051/*
2052 * hotkey poller control variables
2053 *
2054 * Must be atomic or readers will also need to acquire mutex
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002055 *
2056 * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
2057 * should be used only when the changes need to be taken as
2058 * a block, OR when one needs to force the kthread to forget
2059 * old state.
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002060 */
2061static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
2062static unsigned int hotkey_poll_freq = 10; /* Hz */
2063
2064#define HOTKEY_CONFIG_CRITICAL_START \
2065 do { \
2066 mutex_lock(&hotkey_thread_data_mutex); \
2067 hotkey_config_change++; \
2068 } while (0);
2069#define HOTKEY_CONFIG_CRITICAL_END \
2070 mutex_unlock(&hotkey_thread_data_mutex);
2071
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002072#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2073
2074#define hotkey_source_mask 0U
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002075#define HOTKEY_CONFIG_CRITICAL_START
2076#define HOTKEY_CONFIG_CRITICAL_END
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002077
2078#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2079
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002080static struct mutex hotkey_mutex;
2081
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002082static enum { /* Reasons for waking up */
2083 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
2084 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
2085 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
2086} hotkey_wakeup_reason;
2087
2088static int hotkey_autosleep_ack;
2089
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002090static u32 hotkey_orig_mask; /* events the BIOS had enabled */
2091static u32 hotkey_all_mask; /* all events supported in fw */
2092static u32 hotkey_reserved_mask; /* events better left disabled */
2093static u32 hotkey_driver_mask; /* events needed by the driver */
2094static u32 hotkey_user_mask; /* events visible to userspace */
2095static u32 hotkey_acpi_mask; /* events enabled in firmware */
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002096
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02002097static unsigned int hotkey_report_mode;
2098
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002099static u16 *hotkey_keycode_map;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002100
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03002101static struct attribute_set *hotkey_dev_attributes;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002102
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002103static void tpacpi_driver_event(const unsigned int hkey_event);
2104static void hotkey_driver_event(const unsigned int scancode);
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002105static void hotkey_poll_setup(const bool may_warn);
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002106
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002107/* HKEY.MHKG() return bits */
2108#define TP_HOTKEY_TABLET_MASK (1 << 3)
2109
Johannes Berg19d337d2009-06-02 13:01:37 +02002110static int hotkey_get_wlsw(void)
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002111{
Johannes Berg19d337d2009-06-02 13:01:37 +02002112 int status;
2113
2114 if (!tp_features.hotkey_wlsw)
2115 return -ENODEV;
2116
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02002117#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
Johannes Berg19d337d2009-06-02 13:01:37 +02002118 if (dbg_wlswemul)
2119 return (tpacpi_wlsw_emulstate) ?
2120 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02002121#endif
Johannes Berg19d337d2009-06-02 13:01:37 +02002122
2123 if (!acpi_evalf(hkey_handle, &status, "WLSW", "d"))
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002124 return -EIO;
Johannes Berg19d337d2009-06-02 13:01:37 +02002125
2126 return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002127}
2128
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002129static int hotkey_get_tablet_mode(int *status)
2130{
2131 int s;
2132
2133 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
2134 return -EIO;
2135
Henrique de Moraes Holschuhcee47f52008-03-04 14:29:21 -08002136 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
2137 return 0;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002138}
2139
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002140/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002141 * Reads current event mask from firmware, and updates
2142 * hotkey_acpi_mask accordingly. Also resets any bits
2143 * from hotkey_user_mask that are unavailable to be
2144 * delivered (shadow requirement of the userspace ABI).
2145 *
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002146 * Call with hotkey_mutex held
2147 */
2148static int hotkey_mask_get(void)
2149{
2150 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002151 u32 m = 0;
2152
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002153 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002154 return -EIO;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002155
2156 hotkey_acpi_mask = m;
2157 } else {
2158 /* no mask support doesn't mean no event support... */
2159 hotkey_acpi_mask = hotkey_all_mask;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002160 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002161
2162 /* sync userspace-visible mask */
2163 hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002164
2165 return 0;
2166}
2167
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002168void static hotkey_mask_warn_incomplete_mask(void)
2169{
2170 /* log only what the user can fix... */
2171 const u32 wantedmask = hotkey_driver_mask &
2172 ~(hotkey_acpi_mask | hotkey_source_mask) &
2173 (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
2174
2175 if (wantedmask)
2176 printk(TPACPI_NOTICE
2177 "required events 0x%08x not enabled!\n",
2178 wantedmask);
2179}
2180
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002181/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002182 * Set the firmware mask when supported
2183 *
2184 * Also calls hotkey_mask_get to update hotkey_acpi_mask.
2185 *
2186 * NOTE: does not set bits in hotkey_user_mask, but may reset them.
2187 *
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002188 * Call with hotkey_mutex held
2189 */
2190static int hotkey_mask_set(u32 mask)
2191{
2192 int i;
2193 int rc = 0;
2194
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002195 const u32 fwmask = mask & ~hotkey_source_mask;
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -03002196
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002197 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002198 for (i = 0; i < 32; i++) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002199 if (!acpi_evalf(hkey_handle,
2200 NULL, "MHKM", "vdd", i + 1,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002201 !!(mask & (1 << i)))) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002202 rc = -EIO;
2203 break;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002204 }
2205 }
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002206 }
2207
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002208 /*
2209 * We *must* make an inconditional call to hotkey_mask_get to
2210 * refresh hotkey_acpi_mask and update hotkey_user_mask
2211 *
2212 * Take the opportunity to also log when we cannot _enable_
2213 * a given event.
2214 */
2215 if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
2216 printk(TPACPI_NOTICE
2217 "asked for hotkey mask 0x%08x, but "
2218 "firmware forced it to 0x%08x\n",
2219 fwmask, hotkey_acpi_mask);
2220 }
2221
Henrique de Moraes Holschuh6b30eb72009-12-09 01:36:25 +00002222 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING)
2223 hotkey_mask_warn_incomplete_mask();
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002224
2225 return rc;
2226}
2227
2228/*
2229 * Sets hotkey_user_mask and tries to set the firmware mask
2230 *
2231 * Call with hotkey_mutex held
2232 */
2233static int hotkey_user_mask_set(const u32 mask)
2234{
2235 int rc;
2236
2237 /* Give people a chance to notice they are doing something that
2238 * is bound to go boom on their users sooner or later */
2239 if (!tp_warned.hotkey_mask_ff &&
2240 (mask == 0xffff || mask == 0xffffff ||
2241 mask == 0xffffffff)) {
2242 tp_warned.hotkey_mask_ff = 1;
2243 printk(TPACPI_NOTICE
2244 "setting the hotkey mask to 0x%08x is likely "
2245 "not the best way to go about it\n", mask);
2246 printk(TPACPI_NOTICE
2247 "please consider using the driver defaults, "
2248 "and refer to up-to-date thinkpad-acpi "
2249 "documentation\n");
2250 }
2251
2252 /* Try to enable what the user asked for, plus whatever we need.
2253 * this syncs everything but won't enable bits in hotkey_user_mask */
2254 rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
2255
2256 /* Enable the available bits in hotkey_user_mask */
2257 hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
2258
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002259 return rc;
2260}
2261
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002262/*
2263 * Sets the driver hotkey mask.
2264 *
2265 * Can be called even if the hotkey subdriver is inactive
2266 */
2267static int tpacpi_hotkey_driver_mask_set(const u32 mask)
2268{
2269 int rc;
2270
2271 /* Do the right thing if hotkey_init has not been called yet */
2272 if (!tp_features.hotkey) {
2273 hotkey_driver_mask = mask;
2274 return 0;
2275 }
2276
2277 mutex_lock(&hotkey_mutex);
2278
2279 HOTKEY_CONFIG_CRITICAL_START
2280 hotkey_driver_mask = mask;
Henrique de Moraes Holschuhb684a362009-09-26 21:42:49 -03002281#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002282 hotkey_source_mask |= (mask & ~hotkey_all_mask);
Henrique de Moraes Holschuhb684a362009-09-26 21:42:49 -03002283#endif
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002284 HOTKEY_CONFIG_CRITICAL_END
2285
2286 rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) &
2287 ~hotkey_source_mask);
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002288 hotkey_poll_setup(true);
2289
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002290 mutex_unlock(&hotkey_mutex);
2291
2292 return rc;
2293}
2294
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002295static int hotkey_status_get(int *status)
2296{
2297 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
2298 return -EIO;
2299
2300 return 0;
2301}
2302
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002303static int hotkey_status_set(bool enable)
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002304{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002305 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002306 return -EIO;
2307
2308 return 0;
2309}
2310
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002311static void tpacpi_input_send_tabletsw(void)
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002312{
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002313 int state;
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002314
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002315 if (tp_features.hotkey_tablet &&
2316 !hotkey_get_tablet_mode(&state)) {
2317 mutex_lock(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002318
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002319 input_report_switch(tpacpi_inputdev,
2320 SW_TABLET_MODE, !!state);
2321 input_sync(tpacpi_inputdev);
2322
2323 mutex_unlock(&tpacpi_inputdev_send_mutex);
2324 }
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002325}
2326
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002327/* Do NOT call without validating scancode first */
2328static void tpacpi_input_send_key(const unsigned int scancode)
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002329{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002330 const unsigned int keycode = hotkey_keycode_map[scancode];
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002331
2332 if (keycode != KEY_RESERVED) {
2333 mutex_lock(&tpacpi_inputdev_send_mutex);
2334
2335 input_report_key(tpacpi_inputdev, keycode, 1);
2336 if (keycode == KEY_UNKNOWN)
2337 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
2338 scancode);
2339 input_sync(tpacpi_inputdev);
2340
2341 input_report_key(tpacpi_inputdev, keycode, 0);
2342 if (keycode == KEY_UNKNOWN)
2343 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
2344 scancode);
2345 input_sync(tpacpi_inputdev);
2346
2347 mutex_unlock(&tpacpi_inputdev_send_mutex);
2348 }
2349}
2350
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002351/* Do NOT call without validating scancode first */
2352static void tpacpi_input_send_key_masked(const unsigned int scancode)
2353{
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002354 hotkey_driver_event(scancode);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002355 if (hotkey_user_mask & (1 << scancode))
2356 tpacpi_input_send_key(scancode);
2357}
2358
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002359#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2360static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
2361
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002362/* Do NOT call without validating scancode first */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002363static void tpacpi_hotkey_send_key(unsigned int scancode)
2364{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002365 tpacpi_input_send_key_masked(scancode);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002366 if (hotkey_report_mode < 2) {
2367 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03002368 0x80, TP_HKEY_EV_HOTKEY_BASE + scancode);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002369 }
2370}
2371
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002372static void hotkey_read_nvram(struct tp_nvram_state *n, const u32 m)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002373{
2374 u8 d;
2375
2376 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
2377 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
2378 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
2379 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
2380 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
2381 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
2382 }
2383 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
2384 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
2385 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
2386 }
2387 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
2388 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
2389 n->displayexp_toggle =
2390 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
2391 }
2392 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
2393 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
2394 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
2395 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
2396 n->brightness_toggle =
2397 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
2398 }
2399 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
2400 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
2401 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
2402 >> TP_NVRAM_POS_LEVEL_VOLUME;
2403 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2404 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2405 }
2406}
2407
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002408static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
2409 struct tp_nvram_state *newn,
2410 const u32 event_mask)
2411{
2412
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002413#define TPACPI_COMPARE_KEY(__scancode, __member) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002414 do { \
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002415 if ((event_mask & (1 << __scancode)) && \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002416 oldn->__member != newn->__member) \
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002417 tpacpi_hotkey_send_key(__scancode); \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002418 } while (0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002419
2420#define TPACPI_MAY_SEND_KEY(__scancode) \
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002421 do { \
2422 if (event_mask & (1 << __scancode)) \
2423 tpacpi_hotkey_send_key(__scancode); \
2424 } while (0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002425
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002426 void issue_volchange(const unsigned int oldvol,
2427 const unsigned int newvol)
2428 {
2429 unsigned int i = oldvol;
2430
2431 while (i > newvol) {
2432 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2433 i--;
2434 }
2435 while (i < newvol) {
2436 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2437 i++;
2438 }
2439 }
2440
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002441 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2442 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2443 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2444 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2445
2446 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2447
2448 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2449
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002450 /*
2451 * Handle volume
2452 *
2453 * This code is supposed to duplicate the IBM firmware behaviour:
2454 * - Pressing MUTE issues mute hotkey message, even when already mute
2455 * - Pressing Volume up/down issues volume up/down hotkey messages,
2456 * even when already at maximum or minumum volume
2457 * - The act of unmuting issues volume up/down notification,
2458 * depending which key was used to unmute
2459 *
2460 * We are constrained to what the NVRAM can tell us, which is not much
2461 * and certainly not enough if more than one volume hotkey was pressed
2462 * since the last poll cycle.
2463 *
2464 * Just to make our life interesting, some newer Lenovo ThinkPads have
2465 * bugs in the BIOS and may fail to update volume_toggle properly.
2466 */
2467 if (newn->mute) {
2468 /* muted */
2469 if (!oldn->mute ||
2470 oldn->volume_toggle != newn->volume_toggle ||
2471 oldn->volume_level != newn->volume_level) {
2472 /* recently muted, or repeated mute keypress, or
2473 * multiple presses ending in mute */
2474 issue_volchange(oldn->volume_level, newn->volume_level);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002475 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2476 }
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002477 } else {
2478 /* unmute */
2479 if (oldn->mute) {
2480 /* recently unmuted, issue 'unmute' keypress */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002481 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002482 }
2483 if (oldn->volume_level != newn->volume_level) {
2484 issue_volchange(oldn->volume_level, newn->volume_level);
2485 } else if (oldn->volume_toggle != newn->volume_toggle) {
2486 /* repeated vol up/down keypress at end of scale ? */
2487 if (newn->volume_level == 0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002488 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002489 else if (newn->volume_level >= TP_NVRAM_LEVEL_VOLUME_MAX)
2490 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002491 }
2492 }
2493
2494 /* handle brightness */
2495 if (oldn->brightness_toggle != newn->brightness_toggle) {
2496 if (oldn->brightness_level < newn->brightness_level) {
2497 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2498 } else if (oldn->brightness_level > newn->brightness_level) {
2499 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2500 } else {
2501 /* repeated key presses that didn't change state */
2502 if (newn->brightness_level != 0) {
2503 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2504 } else {
2505 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2506 }
2507 }
2508 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002509
2510#undef TPACPI_COMPARE_KEY
2511#undef TPACPI_MAY_SEND_KEY
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002512}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002513
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002514/*
2515 * Polling driver
2516 *
2517 * We track all events in hotkey_source_mask all the time, since
2518 * most of them are edge-based. We only issue those requested by
2519 * hotkey_user_mask or hotkey_driver_mask, though.
2520 */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002521static int hotkey_kthread(void *data)
2522{
2523 struct tp_nvram_state s[2];
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002524 u32 poll_mask, event_mask;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002525 unsigned int si, so;
2526 unsigned long t;
2527 unsigned int change_detector, must_reset;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002528 unsigned int poll_freq;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002529
2530 mutex_lock(&hotkey_thread_mutex);
2531
2532 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2533 goto exit;
2534
2535 set_freezable();
2536
2537 so = 0;
2538 si = 1;
2539 t = 0;
2540
2541 /* Initial state for compares */
2542 mutex_lock(&hotkey_thread_data_mutex);
2543 change_detector = hotkey_config_change;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002544 poll_mask = hotkey_source_mask;
2545 event_mask = hotkey_source_mask &
2546 (hotkey_driver_mask | hotkey_user_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002547 poll_freq = hotkey_poll_freq;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002548 mutex_unlock(&hotkey_thread_data_mutex);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002549 hotkey_read_nvram(&s[so], poll_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002550
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002551 while (!kthread_should_stop()) {
2552 if (t == 0) {
2553 if (likely(poll_freq))
2554 t = 1000/poll_freq;
2555 else
2556 t = 100; /* should never happen... */
2557 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002558 t = msleep_interruptible(t);
2559 if (unlikely(kthread_should_stop()))
2560 break;
2561 must_reset = try_to_freeze();
2562 if (t > 0 && !must_reset)
2563 continue;
2564
2565 mutex_lock(&hotkey_thread_data_mutex);
2566 if (must_reset || hotkey_config_change != change_detector) {
2567 /* forget old state on thaw or config change */
2568 si = so;
2569 t = 0;
2570 change_detector = hotkey_config_change;
2571 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002572 poll_mask = hotkey_source_mask;
2573 event_mask = hotkey_source_mask &
2574 (hotkey_driver_mask | hotkey_user_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002575 poll_freq = hotkey_poll_freq;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002576 mutex_unlock(&hotkey_thread_data_mutex);
2577
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002578 if (likely(poll_mask)) {
2579 hotkey_read_nvram(&s[si], poll_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002580 if (likely(si != so)) {
2581 hotkey_compare_and_issue_event(&s[so], &s[si],
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002582 event_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002583 }
2584 }
2585
2586 so = si;
2587 si ^= 1;
2588 }
2589
2590exit:
2591 mutex_unlock(&hotkey_thread_mutex);
2592 return 0;
2593}
2594
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002595/* call with hotkey_mutex held */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002596static void hotkey_poll_stop_sync(void)
2597{
2598 if (tpacpi_hotkey_task) {
2599 if (frozen(tpacpi_hotkey_task) ||
2600 freezing(tpacpi_hotkey_task))
2601 thaw_process(tpacpi_hotkey_task);
2602
2603 kthread_stop(tpacpi_hotkey_task);
2604 tpacpi_hotkey_task = NULL;
2605 mutex_lock(&hotkey_thread_mutex);
2606 /* at this point, the thread did exit */
2607 mutex_unlock(&hotkey_thread_mutex);
2608 }
2609}
2610
2611/* call with hotkey_mutex held */
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002612static void hotkey_poll_setup(const bool may_warn)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002613{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002614 const u32 poll_driver_mask = hotkey_driver_mask & hotkey_source_mask;
2615 const u32 poll_user_mask = hotkey_user_mask & hotkey_source_mask;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002616
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002617 if (hotkey_poll_freq > 0 &&
2618 (poll_driver_mask ||
2619 (poll_user_mask && tpacpi_inputdev->users > 0))) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002620 if (!tpacpi_hotkey_task) {
2621 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03002622 NULL, TPACPI_NVRAM_KTHREAD_NAME);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002623 if (IS_ERR(tpacpi_hotkey_task)) {
2624 tpacpi_hotkey_task = NULL;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002625 printk(TPACPI_ERR
2626 "could not create kernel thread "
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002627 "for hotkey polling\n");
2628 }
2629 }
2630 } else {
2631 hotkey_poll_stop_sync();
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002632 if (may_warn && (poll_driver_mask || poll_user_mask) &&
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002633 hotkey_poll_freq == 0) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002634 printk(TPACPI_NOTICE
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002635 "hot keys 0x%08x and/or events 0x%08x "
2636 "require polling, which is currently "
2637 "disabled\n",
2638 poll_user_mask, poll_driver_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002639 }
2640 }
2641}
2642
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002643static void hotkey_poll_setup_safe(const bool may_warn)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002644{
2645 mutex_lock(&hotkey_mutex);
2646 hotkey_poll_setup(may_warn);
2647 mutex_unlock(&hotkey_mutex);
2648}
2649
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002650/* call with hotkey_mutex held */
2651static void hotkey_poll_set_freq(unsigned int freq)
2652{
2653 if (!freq)
2654 hotkey_poll_stop_sync();
2655
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002656 hotkey_poll_freq = freq;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002657}
2658
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02002659#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2660
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002661static void hotkey_poll_setup(const bool __unused)
2662{
2663}
2664
2665static void hotkey_poll_setup_safe(const bool __unused)
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02002666{
2667}
2668
2669#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2670
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002671static int hotkey_inputdev_open(struct input_dev *dev)
2672{
2673 switch (tpacpi_lifecycle) {
2674 case TPACPI_LIFE_INIT:
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002675 case TPACPI_LIFE_RUNNING:
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002676 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002677 return 0;
Henrique de Moraes Holschuhb589ea42010-02-25 21:28:58 -03002678 case TPACPI_LIFE_EXITING:
2679 return -EBUSY;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002680 }
2681
2682 /* Should only happen if tpacpi_lifecycle is corrupt */
2683 BUG();
2684 return -EBUSY;
2685}
2686
2687static void hotkey_inputdev_close(struct input_dev *dev)
2688{
2689 /* disable hotkey polling when possible */
Henrique de Moraes Holschuhb589ea42010-02-25 21:28:58 -03002690 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING &&
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002691 !(hotkey_source_mask & hotkey_driver_mask))
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002692 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002693}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002694
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002695/* sysfs hotkey enable ------------------------------------------------- */
2696static ssize_t hotkey_enable_show(struct device *dev,
2697 struct device_attribute *attr,
2698 char *buf)
2699{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002700 int res, status;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002701
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002702 printk_deprecated_attribute("hotkey_enable",
2703 "Hotkey reporting is always enabled");
2704
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002705 res = hotkey_status_get(&status);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002706 if (res)
2707 return res;
2708
2709 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2710}
2711
2712static ssize_t hotkey_enable_store(struct device *dev,
2713 struct device_attribute *attr,
2714 const char *buf, size_t count)
2715{
2716 unsigned long t;
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002717
2718 printk_deprecated_attribute("hotkey_enable",
2719 "Hotkeys can be disabled through hotkey_mask");
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002720
2721 if (parse_strtoul(buf, 1, &t))
2722 return -EINVAL;
2723
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002724 if (t == 0)
2725 return -EPERM;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002726
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002727 return count;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002728}
2729
2730static struct device_attribute dev_attr_hotkey_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002731 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002732 hotkey_enable_show, hotkey_enable_store);
2733
2734/* sysfs hotkey mask --------------------------------------------------- */
2735static ssize_t hotkey_mask_show(struct device *dev,
2736 struct device_attribute *attr,
2737 char *buf)
2738{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002739 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_user_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002740}
2741
2742static ssize_t hotkey_mask_store(struct device *dev,
2743 struct device_attribute *attr,
2744 const char *buf, size_t count)
2745{
2746 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002747 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002748
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002749 if (parse_strtoul(buf, 0xffffffffUL, &t))
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002750 return -EINVAL;
2751
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002752 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002753 return -ERESTARTSYS;
2754
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002755 res = hotkey_user_mask_set(t);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002756
2757#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002758 hotkey_poll_setup(true);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002759#endif
2760
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002761 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002762
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002763 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2764
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002765 return (res) ? res : count;
2766}
2767
2768static struct device_attribute dev_attr_hotkey_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002769 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002770 hotkey_mask_show, hotkey_mask_store);
2771
2772/* sysfs hotkey bios_enabled ------------------------------------------- */
2773static ssize_t hotkey_bios_enabled_show(struct device *dev,
2774 struct device_attribute *attr,
2775 char *buf)
2776{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002777 return sprintf(buf, "0\n");
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002778}
2779
2780static struct device_attribute dev_attr_hotkey_bios_enabled =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002781 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002782
2783/* sysfs hotkey bios_mask ---------------------------------------------- */
2784static ssize_t hotkey_bios_mask_show(struct device *dev,
2785 struct device_attribute *attr,
2786 char *buf)
2787{
Henrique de Moraes Holschuh06777be2009-09-12 15:22:15 -03002788 printk_deprecated_attribute("hotkey_bios_mask",
2789 "This attribute is useless.");
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002790 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002791}
2792
2793static struct device_attribute dev_attr_hotkey_bios_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002794 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002795
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002796/* sysfs hotkey all_mask ----------------------------------------------- */
2797static ssize_t hotkey_all_mask_show(struct device *dev,
2798 struct device_attribute *attr,
2799 char *buf)
2800{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002801 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2802 hotkey_all_mask | hotkey_source_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002803}
2804
2805static struct device_attribute dev_attr_hotkey_all_mask =
2806 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
2807
2808/* sysfs hotkey recommended_mask --------------------------------------- */
2809static ssize_t hotkey_recommended_mask_show(struct device *dev,
2810 struct device_attribute *attr,
2811 char *buf)
2812{
2813 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002814 (hotkey_all_mask | hotkey_source_mask)
2815 & ~hotkey_reserved_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002816}
2817
2818static struct device_attribute dev_attr_hotkey_recommended_mask =
2819 __ATTR(hotkey_recommended_mask, S_IRUGO,
2820 hotkey_recommended_mask_show, NULL);
2821
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002822#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2823
2824/* sysfs hotkey hotkey_source_mask ------------------------------------- */
2825static ssize_t hotkey_source_mask_show(struct device *dev,
2826 struct device_attribute *attr,
2827 char *buf)
2828{
2829 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2830}
2831
2832static ssize_t hotkey_source_mask_store(struct device *dev,
2833 struct device_attribute *attr,
2834 const char *buf, size_t count)
2835{
2836 unsigned long t;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002837 u32 r_ev;
2838 int rc;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002839
2840 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2841 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2842 return -EINVAL;
2843
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002844 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002845 return -ERESTARTSYS;
2846
2847 HOTKEY_CONFIG_CRITICAL_START
2848 hotkey_source_mask = t;
2849 HOTKEY_CONFIG_CRITICAL_END
2850
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002851 rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
2852 ~hotkey_source_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002853 hotkey_poll_setup(true);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002854
2855 /* check if events needed by the driver got disabled */
2856 r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
2857 & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002858
2859 mutex_unlock(&hotkey_mutex);
2860
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002861 if (rc < 0)
2862 printk(TPACPI_ERR "hotkey_source_mask: failed to update the"
2863 "firmware event mask!\n");
2864
2865 if (r_ev)
2866 printk(TPACPI_NOTICE "hotkey_source_mask: "
2867 "some important events were disabled: "
2868 "0x%04x\n", r_ev);
2869
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002870 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2871
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002872 return (rc < 0) ? rc : count;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002873}
2874
2875static struct device_attribute dev_attr_hotkey_source_mask =
2876 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2877 hotkey_source_mask_show, hotkey_source_mask_store);
2878
2879/* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2880static ssize_t hotkey_poll_freq_show(struct device *dev,
2881 struct device_attribute *attr,
2882 char *buf)
2883{
2884 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2885}
2886
2887static ssize_t hotkey_poll_freq_store(struct device *dev,
2888 struct device_attribute *attr,
2889 const char *buf, size_t count)
2890{
2891 unsigned long t;
2892
2893 if (parse_strtoul(buf, 25, &t))
2894 return -EINVAL;
2895
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002896 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002897 return -ERESTARTSYS;
2898
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002899 hotkey_poll_set_freq(t);
2900 hotkey_poll_setup(true);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002901
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002902 mutex_unlock(&hotkey_mutex);
2903
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002904 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2905
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002906 return count;
2907}
2908
2909static struct device_attribute dev_attr_hotkey_poll_freq =
2910 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2911 hotkey_poll_freq_show, hotkey_poll_freq_store);
2912
2913#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2914
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002915/* sysfs hotkey radio_sw (pollable) ------------------------------------ */
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002916static ssize_t hotkey_radio_sw_show(struct device *dev,
2917 struct device_attribute *attr,
2918 char *buf)
2919{
Johannes Berg19d337d2009-06-02 13:01:37 +02002920 int res;
2921 res = hotkey_get_wlsw();
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002922 if (res < 0)
2923 return res;
2924
Johannes Berg19d337d2009-06-02 13:01:37 +02002925 /* Opportunistic update */
2926 tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
2927
2928 return snprintf(buf, PAGE_SIZE, "%d\n",
2929 (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002930}
2931
2932static struct device_attribute dev_attr_hotkey_radio_sw =
2933 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2934
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002935static void hotkey_radio_sw_notify_change(void)
2936{
2937 if (tp_features.hotkey_wlsw)
2938 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2939 "hotkey_radio_sw");
2940}
2941
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002942/* sysfs hotkey tablet mode (pollable) --------------------------------- */
2943static ssize_t hotkey_tablet_mode_show(struct device *dev,
2944 struct device_attribute *attr,
2945 char *buf)
2946{
2947 int res, s;
2948 res = hotkey_get_tablet_mode(&s);
2949 if (res < 0)
2950 return res;
2951
2952 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2953}
2954
2955static struct device_attribute dev_attr_hotkey_tablet_mode =
2956 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2957
2958static void hotkey_tablet_mode_notify_change(void)
2959{
2960 if (tp_features.hotkey_tablet)
2961 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2962 "hotkey_tablet_mode");
2963}
2964
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002965/* sysfs hotkey report_mode -------------------------------------------- */
2966static ssize_t hotkey_report_mode_show(struct device *dev,
2967 struct device_attribute *attr,
2968 char *buf)
2969{
2970 return snprintf(buf, PAGE_SIZE, "%d\n",
2971 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2972}
2973
2974static struct device_attribute dev_attr_hotkey_report_mode =
2975 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2976
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002977/* sysfs wakeup reason (pollable) -------------------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002978static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2979 struct device_attribute *attr,
2980 char *buf)
2981{
2982 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2983}
2984
2985static struct device_attribute dev_attr_hotkey_wakeup_reason =
2986 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2987
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02002988static void hotkey_wakeup_reason_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002989{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002990 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2991 "wakeup_reason");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002992}
2993
2994/* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002995static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2996 struct device_attribute *attr,
2997 char *buf)
2998{
2999 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
3000}
3001
3002static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
3003 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
3004 hotkey_wakeup_hotunplug_complete_show, NULL);
3005
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02003006static void hotkey_wakeup_hotunplug_complete_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003007{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003008 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
3009 "wakeup_hotunplug_complete");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003010}
3011
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003012/* --------------------------------------------------------------------- */
3013
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003014static struct attribute *hotkey_attributes[] __initdata = {
3015 &dev_attr_hotkey_enable.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003016 &dev_attr_hotkey_bios_enabled.attr,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003017 &dev_attr_hotkey_bios_mask.attr,
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003018 &dev_attr_hotkey_report_mode.attr,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003019 &dev_attr_hotkey_wakeup_reason.attr,
3020 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003021 &dev_attr_hotkey_mask.attr,
3022 &dev_attr_hotkey_all_mask.attr,
3023 &dev_attr_hotkey_recommended_mask.attr,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003024#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003025 &dev_attr_hotkey_source_mask.attr,
3026 &dev_attr_hotkey_poll_freq.attr,
3027#endif
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003028};
3029
Johannes Berg19d337d2009-06-02 13:01:37 +02003030/*
3031 * Sync both the hw and sw blocking state of all switches
3032 */
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003033static void tpacpi_send_radiosw_update(void)
3034{
3035 int wlsw;
3036
Johannes Berg19d337d2009-06-02 13:01:37 +02003037 /*
3038 * We must sync all rfkill controllers *before* issuing any
3039 * rfkill input events, or we will race the rfkill core input
3040 * handler.
3041 *
3042 * tpacpi_inputdev_send_mutex works as a syncronization point
3043 * for the above.
3044 *
3045 * We optimize to avoid numerous calls to hotkey_get_wlsw.
3046 */
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003047
Johannes Berg19d337d2009-06-02 13:01:37 +02003048 wlsw = hotkey_get_wlsw();
3049
3050 /* Sync hw blocking state first if it is hw-blocked */
3051 if (wlsw == TPACPI_RFK_RADIO_OFF)
3052 tpacpi_rfk_update_hwblock_state(true);
3053
3054 /* Sync sw blocking state */
3055 tpacpi_rfk_update_swstate_all();
3056
3057 /* Sync hw blocking state last if it is hw-unblocked */
3058 if (wlsw == TPACPI_RFK_RADIO_ON)
3059 tpacpi_rfk_update_hwblock_state(false);
3060
3061 /* Issue rfkill input event for WLSW switch */
3062 if (!(wlsw < 0)) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003063 mutex_lock(&tpacpi_inputdev_send_mutex);
3064
3065 input_report_switch(tpacpi_inputdev,
Johannes Berg19d337d2009-06-02 13:01:37 +02003066 SW_RFKILL_ALL, (wlsw > 0));
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003067 input_sync(tpacpi_inputdev);
3068
3069 mutex_unlock(&tpacpi_inputdev_send_mutex);
3070 }
Johannes Berg19d337d2009-06-02 13:01:37 +02003071
3072 /*
3073 * this can be unconditional, as we will poll state again
3074 * if userspace uses the notify to read data
3075 */
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003076 hotkey_radio_sw_notify_change();
3077}
3078
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003079static void hotkey_exit(void)
3080{
3081#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003082 mutex_lock(&hotkey_mutex);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003083 hotkey_poll_stop_sync();
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003084 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003085#endif
3086
3087 if (hotkey_dev_attributes)
3088 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3089
3090 kfree(hotkey_keycode_map);
3091
Henrique de Moraes Holschuh4be73002009-09-20 14:09:23 -03003092 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003093 "restoring original HKEY status and mask\n");
3094 /* yes, there is a bitwise or below, we want the
3095 * functions to be called even if one of them fail */
3096 if (((tp_features.hotkey_mask &&
3097 hotkey_mask_set(hotkey_orig_mask)) |
3098 hotkey_status_set(false)) != 0)
Henrique de Moraes Holschuh4be73002009-09-20 14:09:23 -03003099 printk(TPACPI_ERR
3100 "failed to restore hot key mask "
3101 "to BIOS defaults\n");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003102}
3103
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003104static void __init hotkey_unmap(const unsigned int scancode)
3105{
3106 if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
3107 clear_bit(hotkey_keycode_map[scancode],
3108 tpacpi_inputdev->keybit);
3109 hotkey_keycode_map[scancode] = KEY_RESERVED;
3110 }
3111}
3112
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003113/*
3114 * HKEY quirks:
3115 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
3116 */
3117
3118#define TPACPI_HK_Q_INIMASK 0x0001
3119
3120static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
3121 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
3122 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
3123 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
3124 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
3125 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
3126 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
3127 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
3128 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
3129 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
3130 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
3131 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
3132 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
3133 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
3134 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
3135 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
3136 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
3137 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
3138 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
3139 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
3140};
3141
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003142static int __init hotkey_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003143{
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003144 /* Requirements for changing the default keymaps:
3145 *
3146 * 1. Many of the keys are mapped to KEY_RESERVED for very
3147 * good reasons. Do not change them unless you have deep
3148 * knowledge on the IBM and Lenovo ThinkPad firmware for
3149 * the various ThinkPad models. The driver behaves
3150 * differently for KEY_RESERVED: such keys have their
3151 * hot key mask *unset* in mask_recommended, and also
3152 * in the initial hot key mask programmed into the
3153 * firmware at driver load time, which means the firm-
3154 * ware may react very differently if you change them to
3155 * something else;
3156 *
3157 * 2. You must be subscribed to the linux-thinkpad and
3158 * ibm-acpi-devel mailing lists, and you should read the
3159 * list archives since 2007 if you want to change the
3160 * keymaps. This requirement exists so that you will
3161 * know the past history of problems with the thinkpad-
3162 * acpi driver keymaps, and also that you will be
3163 * listening to any bug reports;
3164 *
3165 * 3. Do not send thinkpad-acpi specific patches directly to
3166 * for merging, *ever*. Send them to the linux-acpi
3167 * mailinglist for comments. Merging is to be done only
3168 * through acpi-test and the ACPI maintainer.
3169 *
3170 * If the above is too much to ask, don't change the keymap.
3171 * Ask the thinkpad-acpi maintainer to do it, instead.
3172 */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003173 static u16 ibm_keycode_map[] __initdata = {
3174 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3175 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
3176 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3177 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003178
3179 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003180 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3181 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3182 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003183
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003184 /* brightness: firmware always reacts to them */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003185 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003186 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003187
3188 /* Thinklight: firmware always react to it */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003189 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003190
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003191 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3192 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003193
3194 /* Volume: firmware always react to it and reprograms
3195 * the built-in *extra* mixer. Never map it to control
3196 * another mixer by default. */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003197 KEY_RESERVED, /* 0x14: VOLUME UP */
3198 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3199 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003200
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003201 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003202
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003203 /* (assignments unknown, please report if found) */
3204 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3205 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3206 };
3207 static u16 lenovo_keycode_map[] __initdata = {
3208 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3209 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
3210 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3211 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003212
3213 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003214 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3215 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3216 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003217
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003218 /* These should be enabled --only-- when ACPI video
3219 * is disabled (i.e. in "vendor" mode), and are handled
3220 * in a special way by the init code */
3221 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
3222 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003223
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003224 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003225
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003226 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3227 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003228
3229 /* Volume: z60/z61, T60 (BIOS version?): firmware always
3230 * react to it and reprograms the built-in *extra* mixer.
3231 * Never map it to control another mixer by default.
3232 *
3233 * T60?, T61, R60?, R61: firmware and EC tries to send
3234 * these over the regular keyboard, so these are no-ops,
3235 * but there are still weird bugs re. MUTE, so do not
3236 * change unless you get test reports from all Lenovo
3237 * models. May cause the BIOS to interfere with the
3238 * HDA mixer.
3239 */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003240 KEY_RESERVED, /* 0x14: VOLUME UP */
3241 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3242 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003243
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003244 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003245
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003246 /* (assignments unknown, please report if found) */
3247 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3248 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3249 };
3250
3251#define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
3252#define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
3253#define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
3254
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003255 int res, i;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003256 int status;
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03003257 int hkeyv;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003258 bool radiosw_state = false;
3259 bool tabletsw_state = false;
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03003260
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003261 unsigned long quirks;
3262
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003263 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3264 "initializing hotkey subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003265
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003266 BUG_ON(!tpacpi_inputdev);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003267 BUG_ON(tpacpi_inputdev->open != NULL ||
3268 tpacpi_inputdev->close != NULL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003269
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003270 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003271 mutex_init(&hotkey_mutex);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003272
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003273#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3274 mutex_init(&hotkey_thread_mutex);
3275 mutex_init(&hotkey_thread_data_mutex);
3276#endif
3277
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003278 /* hotkey not supported on 570 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003279 tp_features.hotkey = hkey_handle != NULL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003280
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003281 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3282 "hotkeys are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003283 str_supported(tp_features.hotkey));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003284
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003285 if (!tp_features.hotkey)
3286 return 1;
3287
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003288 quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
3289 ARRAY_SIZE(tpacpi_hotkey_qtable));
3290
Henrique de Moraes Holschuhd64c81c42008-10-18 14:23:55 -03003291 tpacpi_disable_brightness_delay();
3292
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003293 /* MUST have enough space for all attributes to be added to
3294 * hotkey_dev_attributes */
3295 hotkey_dev_attributes = create_attr_set(
3296 ARRAY_SIZE(hotkey_attributes) + 2,
3297 NULL);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003298 if (!hotkey_dev_attributes)
3299 return -ENOMEM;
3300 res = add_many_to_attr_set(hotkey_dev_attributes,
3301 hotkey_attributes,
3302 ARRAY_SIZE(hotkey_attributes));
3303 if (res)
3304 goto err_exit;
3305
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003306 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003307 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
3308 for HKEY interface version 0x100 */
3309 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
3310 if ((hkeyv >> 8) != 1) {
3311 printk(TPACPI_ERR "unknown version of the "
3312 "HKEY interface: 0x%x\n", hkeyv);
3313 printk(TPACPI_ERR "please report this to %s\n",
3314 TPACPI_MAIL);
3315 } else {
3316 /*
3317 * MHKV 0x100 in A31, R40, R40e,
3318 * T4x, X31, and later
3319 */
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003320 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3321 "firmware HKEY interface version: 0x%x\n",
3322 hkeyv);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003323
3324 /* Paranoia check AND init hotkey_all_mask */
3325 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3326 "MHKA", "qd")) {
3327 printk(TPACPI_ERR
3328 "missing MHKA handler, "
3329 "please report this to %s\n",
3330 TPACPI_MAIL);
3331 /* Fallback: pre-init for FN+F3,F4,F12 */
3332 hotkey_all_mask = 0x080cU;
3333 } else {
3334 tp_features.hotkey_mask = 1;
3335 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003336 }
3337 }
3338
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003339 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3340 "hotkey masks are %s\n",
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003341 str_supported(tp_features.hotkey_mask));
3342
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003343 /* Init hotkey_all_mask if not initialized yet */
3344 if (!tp_features.hotkey_mask && !hotkey_all_mask &&
3345 (quirks & TPACPI_HK_Q_INIMASK))
3346 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003347
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003348 /* Init hotkey_acpi_mask and hotkey_orig_mask */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003349 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003350 /* hotkey_source_mask *must* be zero for
3351 * the first hotkey_mask_get to return hotkey_orig_mask */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003352 res = hotkey_mask_get();
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003353 if (res)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003354 goto err_exit;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003355
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003356 hotkey_orig_mask = hotkey_acpi_mask;
3357 } else {
3358 hotkey_orig_mask = hotkey_all_mask;
3359 hotkey_acpi_mask = hotkey_all_mask;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003360 }
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003361
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003362#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3363 if (dbg_wlswemul) {
3364 tp_features.hotkey_wlsw = 1;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003365 radiosw_state = !!tpacpi_wlsw_emulstate;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003366 printk(TPACPI_INFO
3367 "radio switch emulation enabled\n");
3368 } else
3369#endif
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003370 /* Not all thinkpads have a hardware radio switch */
3371 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
3372 tp_features.hotkey_wlsw = 1;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003373 radiosw_state = !!status;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003374 printk(TPACPI_INFO
3375 "radio switch found; radios are %s\n",
3376 enabled(status, 0));
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003377 }
3378 if (tp_features.hotkey_wlsw)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003379 res = add_to_attr_set(hotkey_dev_attributes,
3380 &dev_attr_hotkey_radio_sw.attr);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003381
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003382 /* For X41t, X60t, X61t Tablets... */
3383 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
3384 tp_features.hotkey_tablet = 1;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003385 tabletsw_state = !!(status & TP_HOTKEY_TABLET_MASK);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003386 printk(TPACPI_INFO
3387 "possible tablet mode switch found; "
3388 "ThinkPad in %s mode\n",
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003389 (tabletsw_state) ? "tablet" : "laptop");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003390 res = add_to_attr_set(hotkey_dev_attributes,
3391 &dev_attr_hotkey_tablet_mode.attr);
3392 }
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02003393
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003394 if (!res)
3395 res = register_attr_set_with_sysfs(
3396 hotkey_dev_attributes,
3397 &tpacpi_pdev->dev.kobj);
3398 if (res)
3399 goto err_exit;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003400
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003401 /* Set up key map */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003402
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003403 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
3404 GFP_KERNEL);
3405 if (!hotkey_keycode_map) {
3406 printk(TPACPI_ERR
3407 "failed to allocate memory for key map\n");
3408 res = -ENOMEM;
3409 goto err_exit;
3410 }
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003411
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03003412 if (tpacpi_is_lenovo()) {
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003413 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003414 "using Lenovo default hot key map\n");
3415 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
3416 TPACPI_HOTKEY_MAP_SIZE);
3417 } else {
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003418 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003419 "using IBM default hot key map\n");
3420 memcpy(hotkey_keycode_map, &ibm_keycode_map,
3421 TPACPI_HOTKEY_MAP_SIZE);
3422 }
3423
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003424 input_set_capability(tpacpi_inputdev, EV_MSC, MSC_SCAN);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003425 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
3426 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3427 tpacpi_inputdev->keycode = hotkey_keycode_map;
3428 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3429 if (hotkey_keycode_map[i] != KEY_RESERVED) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003430 input_set_capability(tpacpi_inputdev, EV_KEY,
3431 hotkey_keycode_map[i]);
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003432 } else {
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003433 if (i < sizeof(hotkey_reserved_mask)*8)
3434 hotkey_reserved_mask |= 1 << i;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003435 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003436 }
3437
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003438 if (tp_features.hotkey_wlsw) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003439 input_set_capability(tpacpi_inputdev, EV_SW, SW_RFKILL_ALL);
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003440 input_report_switch(tpacpi_inputdev,
3441 SW_RFKILL_ALL, radiosw_state);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003442 }
3443 if (tp_features.hotkey_tablet) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003444 input_set_capability(tpacpi_inputdev, EV_SW, SW_TABLET_MODE);
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003445 input_report_switch(tpacpi_inputdev,
3446 SW_TABLET_MODE, tabletsw_state);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003447 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003448
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003449 /* Do not issue duplicate brightness change events to
3450 * userspace */
3451 if (!tp_features.bright_acpimode)
3452 /* update bright_acpimode... */
3453 tpacpi_check_std_acpi_brightness_support();
3454
Henrique de Moraes Holschuh8bf3d4c2009-05-30 13:25:09 -03003455 if (tp_features.bright_acpimode && acpi_video_backlight_support()) {
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003456 printk(TPACPI_INFO
3457 "This ThinkPad has standard ACPI backlight "
3458 "brightness control, supported by the ACPI "
3459 "video driver\n");
3460 printk(TPACPI_NOTICE
3461 "Disabling thinkpad-acpi brightness events "
3462 "by default...\n");
3463
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003464 /* Disable brightness up/down on Lenovo thinkpads when
3465 * ACPI is handling them, otherwise it is plain impossible
3466 * for userspace to do something even remotely sane */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003467 hotkey_reserved_mask |=
3468 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3469 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003470 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
3471 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003472 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003473
Henrique de Moraes Holschuh230d8cf252009-09-12 15:22:17 -03003474#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003475 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3476 & ~hotkey_all_mask
3477 & ~hotkey_reserved_mask;
Henrique de Moraes Holschuh230d8cf252009-09-12 15:22:17 -03003478
3479 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3480 "hotkey source mask 0x%08x, polling freq %u\n",
3481 hotkey_source_mask, hotkey_poll_freq);
3482#endif
3483
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003484 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3485 "enabling firmware HKEY event interface...\n");
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003486 res = hotkey_status_set(true);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003487 if (res) {
3488 hotkey_exit();
3489 return res;
3490 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003491 res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
3492 | hotkey_driver_mask)
3493 & ~hotkey_source_mask);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003494 if (res < 0 && res != -ENXIO) {
3495 hotkey_exit();
3496 return res;
3497 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003498 hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
3499 & ~hotkey_reserved_mask;
3500 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3501 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3502 hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003503
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003504 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3505 "legacy ibm/hotkey event reporting over procfs %s\n",
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003506 (hotkey_report_mode < 2) ?
3507 "enabled" : "disabled");
3508
3509 tpacpi_inputdev->open = &hotkey_inputdev_open;
3510 tpacpi_inputdev->close = &hotkey_inputdev_close;
3511
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003512 hotkey_poll_setup_safe(true);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003513
3514 return 0;
3515
3516err_exit:
3517 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3518 hotkey_dev_attributes = NULL;
3519
3520 return (res < 0)? res : 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003521}
3522
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003523static bool hotkey_notify_hotkey(const u32 hkey,
3524 bool *send_acpi_ev,
3525 bool *ignore_acpi_ev)
3526{
3527 /* 0x1000-0x1FFF: key presses */
3528 unsigned int scancode = hkey & 0xfff;
3529 *send_acpi_ev = true;
3530 *ignore_acpi_ev = false;
3531
3532 if (scancode > 0 && scancode < 0x21) {
3533 scancode--;
3534 if (!(hotkey_source_mask & (1 << scancode))) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003535 tpacpi_input_send_key_masked(scancode);
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003536 *send_acpi_ev = false;
3537 } else {
3538 *ignore_acpi_ev = true;
3539 }
3540 return true;
3541 }
3542 return false;
3543}
3544
3545static bool hotkey_notify_wakeup(const u32 hkey,
3546 bool *send_acpi_ev,
3547 bool *ignore_acpi_ev)
3548{
3549 /* 0x2000-0x2FFF: Wakeup reason */
3550 *send_acpi_ev = true;
3551 *ignore_acpi_ev = false;
3552
3553 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003554 case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */
3555 case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003556 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3557 *ignore_acpi_ev = true;
3558 break;
3559
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003560 case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */
3561 case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003562 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3563 *ignore_acpi_ev = true;
3564 break;
3565
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003566 case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */
3567 case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003568 printk(TPACPI_ALERT
3569 "EMERGENCY WAKEUP: battery almost empty\n");
3570 /* how to auto-heal: */
3571 /* 2313: woke up from S3, go to S4/S5 */
3572 /* 2413: woke up from S4, go to S5 */
3573 break;
3574
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003575 default:
3576 return false;
3577 }
3578
3579 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
3580 printk(TPACPI_INFO
3581 "woke up due to a hot-unplug "
3582 "request...\n");
3583 hotkey_wakeup_reason_notify_change();
3584 }
3585 return true;
3586}
3587
3588static bool hotkey_notify_usrevent(const u32 hkey,
3589 bool *send_acpi_ev,
3590 bool *ignore_acpi_ev)
3591{
3592 /* 0x5000-0x5FFF: human interface helpers */
3593 *send_acpi_ev = true;
3594 *ignore_acpi_ev = false;
3595
3596 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003597 case TP_HKEY_EV_PEN_INSERTED: /* X61t: tablet pen inserted into bay */
3598 case TP_HKEY_EV_PEN_REMOVED: /* X61t: tablet pen removed from bay */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003599 return true;
3600
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003601 case TP_HKEY_EV_TABLET_TABLET: /* X41t-X61t: tablet mode */
3602 case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003603 tpacpi_input_send_tabletsw();
3604 hotkey_tablet_mode_notify_change();
3605 *send_acpi_ev = false;
3606 return true;
3607
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003608 case TP_HKEY_EV_LID_CLOSE: /* Lid closed */
3609 case TP_HKEY_EV_LID_OPEN: /* Lid opened */
3610 case TP_HKEY_EV_BRGHT_CHANGED: /* brightness changed */
Henrique de Moraes Holschuh176dd982009-09-20 14:09:24 -03003611 /* do not propagate these events */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003612 *ignore_acpi_ev = true;
3613 return true;
3614
3615 default:
3616 return false;
3617 }
3618}
3619
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003620static void thermal_dump_all_sensors(void);
3621
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003622static bool hotkey_notify_thermal(const u32 hkey,
3623 bool *send_acpi_ev,
3624 bool *ignore_acpi_ev)
3625{
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003626 bool known = true;
3627
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003628 /* 0x6000-0x6FFF: thermal alarms */
3629 *send_acpi_ev = true;
3630 *ignore_acpi_ev = false;
3631
3632 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003633 case TP_HKEY_EV_THM_TABLE_CHANGED:
Henrique de Moraes Holschuh54926ce2009-01-11 03:01:09 -02003634 printk(TPACPI_INFO
3635 "EC reports that Thermal Table has changed\n");
3636 /* recommended action: do nothing, we don't have
3637 * Lenovo ATM information */
3638 return true;
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003639 case TP_HKEY_EV_ALARM_BAT_HOT:
3640 printk(TPACPI_CRIT
3641 "THERMAL ALARM: battery is too hot!\n");
3642 /* recommended action: warn user through gui */
3643 break;
3644 case TP_HKEY_EV_ALARM_BAT_XHOT:
3645 printk(TPACPI_ALERT
3646 "THERMAL EMERGENCY: battery is extremely hot!\n");
3647 /* recommended action: immediate sleep/hibernate */
3648 break;
3649 case TP_HKEY_EV_ALARM_SENSOR_HOT:
3650 printk(TPACPI_CRIT
3651 "THERMAL ALARM: "
3652 "a sensor reports something is too hot!\n");
3653 /* recommended action: warn user through gui, that */
3654 /* some internal component is too hot */
3655 break;
3656 case TP_HKEY_EV_ALARM_SENSOR_XHOT:
3657 printk(TPACPI_ALERT
3658 "THERMAL EMERGENCY: "
3659 "a sensor reports something is extremely hot!\n");
3660 /* recommended action: immediate sleep/hibernate */
3661 break;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003662 default:
3663 printk(TPACPI_ALERT
3664 "THERMAL ALERT: unknown thermal alarm received\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003665 known = false;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003666 }
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003667
3668 thermal_dump_all_sensors();
3669
3670 return known;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003671}
3672
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003673static void hotkey_notify(struct ibm_struct *ibm, u32 event)
3674{
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003675 u32 hkey;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003676 bool send_acpi_ev;
3677 bool ignore_acpi_ev;
3678 bool known_ev;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003679
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003680 if (event != 0x80) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003681 printk(TPACPI_ERR
3682 "unknown HKEY notification event %d\n", event);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003683 /* forward it to userspace, maybe it knows how to handle it */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003684 acpi_bus_generate_netlink_event(
3685 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02003686 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003687 event, 0);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003688 return;
3689 }
3690
3691 while (1) {
3692 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003693 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003694 return;
3695 }
3696
3697 if (hkey == 0) {
3698 /* queue empty */
3699 return;
3700 }
3701
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003702 send_acpi_ev = true;
3703 ignore_acpi_ev = false;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003704
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003705 switch (hkey >> 12) {
3706 case 1:
3707 /* 0x1000-0x1FFF: key presses */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003708 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
3709 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003710 break;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003711 case 2:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003712 /* 0x2000-0x2FFF: Wakeup reason */
3713 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
3714 &ignore_acpi_ev);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003715 break;
3716 case 3:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003717 /* 0x3000-0x3FFF: bay-related wakeups */
Henrique de Moraes Holschuhbf8b29c2010-02-25 21:28:56 -03003718 switch (hkey) {
3719 case TP_HKEY_EV_BAYEJ_ACK:
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003720 hotkey_autosleep_ack = 1;
3721 printk(TPACPI_INFO
3722 "bay ejected\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003723 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003724 known_ev = true;
Henrique de Moraes Holschuhbf8b29c2010-02-25 21:28:56 -03003725 break;
3726 case TP_HKEY_EV_OPTDRV_EJ:
3727 /* FIXME: kick libata if SATA link offline */
3728 known_ev = true;
3729 break;
3730 default:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003731 known_ev = false;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003732 }
3733 break;
3734 case 4:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003735 /* 0x4000-0x4FFF: dock-related wakeups */
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003736 if (hkey == TP_HKEY_EV_UNDOCK_ACK) {
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003737 hotkey_autosleep_ack = 1;
3738 printk(TPACPI_INFO
3739 "undocked\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003740 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003741 known_ev = true;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003742 } else {
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003743 known_ev = false;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003744 }
3745 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003746 case 5:
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02003747 /* 0x5000-0x5FFF: human interface helpers */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003748 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
3749 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003750 break;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003751 case 6:
3752 /* 0x6000-0x6FFF: thermal alarms */
3753 known_ev = hotkey_notify_thermal(hkey, &send_acpi_ev,
3754 &ignore_acpi_ev);
3755 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003756 case 7:
3757 /* 0x7000-0x7FFF: misc */
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003758 if (tp_features.hotkey_wlsw &&
3759 hkey == TP_HKEY_EV_RFKILL_CHANGED) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003760 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02003761 send_acpi_ev = 0;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003762 known_ev = true;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003763 break;
3764 }
3765 /* fallthrough to default */
3766 default:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003767 known_ev = false;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02003768 }
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003769 if (!known_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003770 printk(TPACPI_NOTICE
3771 "unhandled HKEY event 0x%04x\n", hkey);
Henrique de Moraes Holschuhcb429352009-01-11 03:01:07 -02003772 printk(TPACPI_NOTICE
3773 "please report the conditions when this "
3774 "event happened to %s\n", TPACPI_MAIL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003775 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003776
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003777 /* Legacy events */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003778 if (!ignore_acpi_ev &&
3779 (send_acpi_ev || hotkey_report_mode < 2)) {
3780 acpi_bus_generate_proc_event(ibm->acpi->device,
3781 event, hkey);
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03003782 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003783
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003784 /* netlink events */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03003785 if (!ignore_acpi_ev && send_acpi_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003786 acpi_bus_generate_netlink_event(
3787 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02003788 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003789 event, hkey);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003790 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003791 }
3792}
3793
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003794static void hotkey_suspend(pm_message_t state)
3795{
3796 /* Do these on suspend, we get the events on early resume! */
3797 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
3798 hotkey_autosleep_ack = 0;
3799}
3800
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03003801static void hotkey_resume(void)
3802{
Henrique de Moraes Holschuhd64c81c42008-10-18 14:23:55 -03003803 tpacpi_disable_brightness_delay();
3804
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003805 if (hotkey_status_set(true) < 0 ||
3806 hotkey_mask_set(hotkey_acpi_mask) < 0)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003807 printk(TPACPI_ERR
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003808 "error while attempting to reset the event "
3809 "firmware interface\n");
3810
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003811 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02003812 hotkey_tablet_mode_notify_change();
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003813 hotkey_wakeup_reason_notify_change();
3814 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003815 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03003816}
3817
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003818/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003819static int hotkey_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03003821 int res, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003823 if (!tp_features.hotkey) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003824 seq_printf(m, "status:\t\tnot supported\n");
3825 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003826 }
3827
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02003828 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02003829 return -ERESTARTSYS;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02003830 res = hotkey_status_get(&status);
3831 if (!res)
3832 res = hotkey_mask_get();
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003833 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03003834 if (res)
3835 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003837 seq_printf(m, "status:\t\t%s\n", enabled(status, 0));
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003838 if (hotkey_all_mask) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003839 seq_printf(m, "mask:\t\t0x%08x\n", hotkey_user_mask);
3840 seq_printf(m, "commands:\tenable, disable, reset, <mask>\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003841 } else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003842 seq_printf(m, "mask:\t\tnot supported\n");
3843 seq_printf(m, "commands:\tenable, disable, reset\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844 }
3845
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003846 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847}
3848
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003849static void hotkey_enabledisable_warn(bool enable)
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003850{
3851 tpacpi_log_usertask("procfs hotkey enable/disable");
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003852 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
3853 TPACPI_WARN
3854 "hotkey enable/disable functionality has been "
3855 "removed from the driver. Hotkeys are always "
3856 "enabled\n"))
3857 printk(TPACPI_ERR
3858 "Please remove the hotkey=enable module "
3859 "parameter, it is deprecated. Hotkeys are always "
3860 "enabled\n");
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003861}
3862
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003863static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003865 int res;
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03003866 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003868
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003869 if (!tp_features.hotkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870 return -ENODEV;
3871
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02003872 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02003873 return -ERESTARTSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003874
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003875 mask = hotkey_user_mask;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003876
3877 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878 while ((cmd = next_cmd(&buf))) {
3879 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003880 hotkey_enabledisable_warn(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003882 hotkey_enabledisable_warn(0);
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003883 res = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884 } else if (strlencmp(cmd, "reset") == 0) {
Henrique de Moraes Holschuh20c9aa462009-09-12 15:22:16 -03003885 mask = (hotkey_all_mask | hotkey_source_mask)
3886 & ~hotkey_reserved_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003887 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
3888 /* mask set */
3889 } else if (sscanf(cmd, "%x", &mask) == 1) {
3890 /* mask set */
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003891 } else {
3892 res = -EINVAL;
3893 goto errexit;
3894 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895 }
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003896
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003897 if (!res) {
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003898 tpacpi_disclose_usertask("procfs hotkey",
3899 "set mask to 0x%08x\n", mask);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003900 res = hotkey_user_mask_set(mask);
3901 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003903errexit:
3904 mutex_unlock(&hotkey_mutex);
3905 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003906}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003908static const struct acpi_device_id ibm_htk_device_ids[] = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003909 {TPACPI_ACPI_HKEY_HID, 0},
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003910 {"", 0},
3911};
3912
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003913static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003914 .hid = ibm_htk_device_ids,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003915 .notify = hotkey_notify,
3916 .handle = &hkey_handle,
3917 .type = ACPI_DEVICE_NOTIFY,
3918};
3919
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003920static struct ibm_struct hotkey_driver_data = {
3921 .name = "hotkey",
3922 .read = hotkey_read,
3923 .write = hotkey_write,
3924 .exit = hotkey_exit,
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03003925 .resume = hotkey_resume,
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003926 .suspend = hotkey_suspend,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003927 .acpi = &ibm_hotkey_acpidriver,
3928};
3929
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003930/*************************************************************************
3931 * Bluetooth subdriver
3932 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003934enum {
3935 /* ACPI GBDC/SBDC bits */
3936 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
3937 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003938 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03003939 0 = disable, 1 = enable */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003940};
3941
3942enum {
3943 /* ACPI \BLTH commands */
3944 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
3945 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
3946 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
3947 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
3948 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003949};
3950
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00003951#define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
3952
Johannes Berg19d337d2009-06-02 13:01:37 +02003953static int bluetooth_get_status(void)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003954{
3955 int status;
3956
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003957#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3958 if (dbg_bluetoothemul)
3959 return (tpacpi_bluetooth_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02003960 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003961#endif
3962
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003963 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
3964 return -EIO;
3965
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003966 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02003967 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003968}
3969
Johannes Berg19d337d2009-06-02 13:01:37 +02003970static int bluetooth_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003971{
3972 int status;
3973
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00003974 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02003975 "will attempt to %s bluetooth\n",
3976 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00003977
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003978#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3979 if (dbg_bluetoothemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02003980 tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003981 return 0;
3982 }
3983#endif
3984
Johannes Berg19d337d2009-06-02 13:01:37 +02003985 if (state == TPACPI_RFK_RADIO_ON)
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03003986 status = TP_ACPI_BLUETOOTH_RADIOSSW
3987 | TP_ACPI_BLUETOOTH_RESUMECTRL;
3988 else
3989 status = 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02003990
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003991 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
3992 return -EIO;
3993
3994 return 0;
3995}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003996
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003997/* sysfs bluetooth enable ---------------------------------------------- */
3998static ssize_t bluetooth_enable_show(struct device *dev,
3999 struct device_attribute *attr,
4000 char *buf)
4001{
Johannes Berg19d337d2009-06-02 13:01:37 +02004002 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID,
4003 attr, buf);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004004}
4005
4006static ssize_t bluetooth_enable_store(struct device *dev,
4007 struct device_attribute *attr,
4008 const char *buf, size_t count)
4009{
Johannes Berg19d337d2009-06-02 13:01:37 +02004010 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID,
4011 attr, buf, count);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004012}
4013
4014static struct device_attribute dev_attr_bluetooth_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03004015 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004016 bluetooth_enable_show, bluetooth_enable_store);
4017
4018/* --------------------------------------------------------------------- */
4019
4020static struct attribute *bluetooth_attributes[] = {
4021 &dev_attr_bluetooth_enable.attr,
4022 NULL
4023};
4024
4025static const struct attribute_group bluetooth_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004026 .attrs = bluetooth_attributes,
4027};
4028
Johannes Berg19d337d2009-06-02 13:01:37 +02004029static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = {
4030 .get_status = bluetooth_get_status,
4031 .set_status = bluetooth_set_status,
4032};
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004033
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004034static void bluetooth_shutdown(void)
4035{
4036 /* Order firmware to save current state to NVRAM */
4037 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
4038 TP_ACPI_BLTH_SAVE_STATE))
4039 printk(TPACPI_NOTICE
4040 "failed to save bluetooth state to NVRAM\n");
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004041 else
4042 vdbg_printk(TPACPI_DBG_RFKILL,
4043 "bluestooth state saved to NVRAM\n");
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004044}
4045
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004046static void bluetooth_exit(void)
4047{
4048 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4049 &bluetooth_attr_group);
Johannes Berg19d337d2009-06-02 13:01:37 +02004050
4051 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
4052
4053 bluetooth_shutdown();
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004054}
4055
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004056static int __init bluetooth_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004058 int res;
Henrique de Moraes Holschuhd6fdd1e92007-04-21 11:08:40 -03004059 int status = 0;
4060
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004061 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4062 "initializing bluetooth subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004063
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004064 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004065
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004066 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4067 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004068 tp_features.bluetooth = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e92007-04-21 11:08:40 -03004069 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004071 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4072 "bluetooth is %s, status 0x%02x\n",
Henrique de Moraes Holschuhd6fdd1e92007-04-21 11:08:40 -03004073 str_supported(tp_features.bluetooth),
4074 status);
4075
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004076#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4077 if (dbg_bluetoothemul) {
4078 tp_features.bluetooth = 1;
4079 printk(TPACPI_INFO
4080 "bluetooth switch emulation enabled\n");
4081 } else
4082#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004083 if (tp_features.bluetooth &&
4084 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
4085 /* no bluetooth hardware present in system */
4086 tp_features.bluetooth = 0;
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004087 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004088 "bluetooth hardware not installed\n");
4089 }
4090
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004091 if (!tp_features.bluetooth)
4092 return 1;
4093
Johannes Berg19d337d2009-06-02 13:01:37 +02004094 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
4095 &bluetooth_tprfk_ops,
4096 RFKILL_TYPE_BLUETOOTH,
4097 TPACPI_RFK_BLUETOOTH_SW_NAME,
4098 true);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004099 if (res)
4100 return res;
4101
Johannes Berg19d337d2009-06-02 13:01:37 +02004102 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4103 &bluetooth_attr_group);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004104 if (res) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004105 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004106 return res;
Henrique de Moraes Holschuhd6fdd1e92007-04-21 11:08:40 -03004107 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004108
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004109 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004110}
4111
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004112/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004113static int bluetooth_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114{
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004115 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116}
4117
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004118static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119{
Johannes Berg19d337d2009-06-02 13:01:37 +02004120 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121}
4122
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004123static struct ibm_struct bluetooth_driver_data = {
4124 .name = "bluetooth",
4125 .read = bluetooth_read,
4126 .write = bluetooth_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004127 .exit = bluetooth_exit,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004128 .shutdown = bluetooth_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004129};
4130
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004131/*************************************************************************
4132 * Wan subdriver
4133 */
4134
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004135enum {
4136 /* ACPI GWAN/SWAN bits */
4137 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
4138 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02004139 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03004140 0 = disable, 1 = enable */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004141};
4142
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004143#define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
4144
Johannes Berg19d337d2009-06-02 13:01:37 +02004145static int wan_get_status(void)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004146{
4147 int status;
4148
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004149#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4150 if (dbg_wwanemul)
4151 return (tpacpi_wwan_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004152 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004153#endif
4154
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004155 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
4156 return -EIO;
4157
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004158 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004159 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004160}
4161
Johannes Berg19d337d2009-06-02 13:01:37 +02004162static int wan_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004163{
4164 int status;
4165
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004166 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02004167 "will attempt to %s wwan\n",
4168 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004169
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004170#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4171 if (dbg_wwanemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004172 tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004173 return 0;
4174 }
4175#endif
4176
Johannes Berg19d337d2009-06-02 13:01:37 +02004177 if (state == TPACPI_RFK_RADIO_ON)
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03004178 status = TP_ACPI_WANCARD_RADIOSSW
4179 | TP_ACPI_WANCARD_RESUMECTRL;
4180 else
4181 status = 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02004182
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004183 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
4184 return -EIO;
4185
4186 return 0;
4187}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004188
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004189/* sysfs wan enable ---------------------------------------------------- */
4190static ssize_t wan_enable_show(struct device *dev,
4191 struct device_attribute *attr,
4192 char *buf)
4193{
Johannes Berg19d337d2009-06-02 13:01:37 +02004194 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
4195 attr, buf);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004196}
4197
4198static ssize_t wan_enable_store(struct device *dev,
4199 struct device_attribute *attr,
4200 const char *buf, size_t count)
4201{
Johannes Berg19d337d2009-06-02 13:01:37 +02004202 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
4203 attr, buf, count);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004204}
4205
4206static struct device_attribute dev_attr_wan_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03004207 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004208 wan_enable_show, wan_enable_store);
4209
4210/* --------------------------------------------------------------------- */
4211
4212static struct attribute *wan_attributes[] = {
4213 &dev_attr_wan_enable.attr,
4214 NULL
4215};
4216
4217static const struct attribute_group wan_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004218 .attrs = wan_attributes,
4219};
4220
Johannes Berg19d337d2009-06-02 13:01:37 +02004221static const struct tpacpi_rfk_ops wan_tprfk_ops = {
4222 .get_status = wan_get_status,
4223 .set_status = wan_set_status,
4224};
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004225
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004226static void wan_shutdown(void)
4227{
4228 /* Order firmware to save current state to NVRAM */
4229 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
4230 TP_ACPI_WGSV_SAVE_STATE))
4231 printk(TPACPI_NOTICE
4232 "failed to save WWAN state to NVRAM\n");
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004233 else
4234 vdbg_printk(TPACPI_DBG_RFKILL,
4235 "WWAN state saved to NVRAM\n");
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004236}
4237
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004238static void wan_exit(void)
4239{
4240 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4241 &wan_attr_group);
Johannes Berg19d337d2009-06-02 13:01:37 +02004242
4243 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4244
4245 wan_shutdown();
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004246}
4247
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004248static int __init wan_init(struct ibm_init_struct *iibm)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004249{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004250 int res;
Henrique de Moraes Holschuhd6fdd1e92007-04-21 11:08:40 -03004251 int status = 0;
4252
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004253 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4254 "initializing wan subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004255
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004256 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004257
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004258 tp_features.wan = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e92007-04-21 11:08:40 -03004259 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004260
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004261 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4262 "wan is %s, status 0x%02x\n",
Henrique de Moraes Holschuhd6fdd1e92007-04-21 11:08:40 -03004263 str_supported(tp_features.wan),
4264 status);
4265
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004266#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4267 if (dbg_wwanemul) {
4268 tp_features.wan = 1;
4269 printk(TPACPI_INFO
4270 "wwan switch emulation enabled\n");
4271 } else
4272#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004273 if (tp_features.wan &&
4274 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
4275 /* no wan hardware present in system */
4276 tp_features.wan = 0;
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004277 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004278 "wan hardware not installed\n");
4279 }
4280
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004281 if (!tp_features.wan)
4282 return 1;
4283
Johannes Berg19d337d2009-06-02 13:01:37 +02004284 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
4285 &wan_tprfk_ops,
4286 RFKILL_TYPE_WWAN,
4287 TPACPI_RFK_WWAN_SW_NAME,
4288 true);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004289 if (res)
4290 return res;
4291
Johannes Berg19d337d2009-06-02 13:01:37 +02004292 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4293 &wan_attr_group);
4294
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004295 if (res) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004296 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004297 return res;
Henrique de Moraes Holschuhd6fdd1e92007-04-21 11:08:40 -03004298 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004299
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004300 return 0;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004301}
4302
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004303/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004304static int wan_read(struct seq_file *m)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004305{
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004306 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, m);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004307}
4308
4309static int wan_write(char *buf)
4310{
Johannes Berg19d337d2009-06-02 13:01:37 +02004311 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004312}
4313
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004314static struct ibm_struct wan_driver_data = {
4315 .name = "wan",
4316 .read = wan_read,
4317 .write = wan_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004318 .exit = wan_exit,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004319 .shutdown = wan_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004320};
4321
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004322/*************************************************************************
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004323 * UWB subdriver
4324 */
4325
4326enum {
4327 /* ACPI GUWB/SUWB bits */
4328 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4329 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4330};
4331
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004332#define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4333
Johannes Berg19d337d2009-06-02 13:01:37 +02004334static int uwb_get_status(void)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004335{
4336 int status;
4337
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004338#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4339 if (dbg_uwbemul)
4340 return (tpacpi_uwb_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004341 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004342#endif
4343
4344 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4345 return -EIO;
4346
4347 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004348 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004349}
4350
Johannes Berg19d337d2009-06-02 13:01:37 +02004351static int uwb_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004352{
4353 int status;
4354
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004355 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02004356 "will attempt to %s UWB\n",
4357 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004358
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004359#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4360 if (dbg_uwbemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004361 tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004362 return 0;
4363 }
4364#endif
4365
Johannes Berg19d337d2009-06-02 13:01:37 +02004366 if (state == TPACPI_RFK_RADIO_ON)
4367 status = TP_ACPI_UWB_RADIOSSW;
4368 else
4369 status = 0;
4370
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004371 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4372 return -EIO;
4373
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004374 return 0;
4375}
4376
4377/* --------------------------------------------------------------------- */
4378
Johannes Berg19d337d2009-06-02 13:01:37 +02004379static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
4380 .get_status = uwb_get_status,
4381 .set_status = uwb_set_status,
4382};
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004383
4384static void uwb_exit(void)
4385{
Johannes Berg19d337d2009-06-02 13:01:37 +02004386 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004387}
4388
4389static int __init uwb_init(struct ibm_init_struct *iibm)
4390{
4391 int res;
4392 int status = 0;
4393
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004394 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4395 "initializing uwb subdriver\n");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004396
4397 TPACPI_ACPIHANDLE_INIT(hkey);
4398
4399 tp_features.uwb = hkey_handle &&
4400 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4401
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004402 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4403 "uwb is %s, status 0x%02x\n",
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004404 str_supported(tp_features.uwb),
4405 status);
4406
4407#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4408 if (dbg_uwbemul) {
4409 tp_features.uwb = 1;
4410 printk(TPACPI_INFO
4411 "uwb switch emulation enabled\n");
4412 } else
4413#endif
4414 if (tp_features.uwb &&
4415 !(status & TP_ACPI_UWB_HWPRESENT)) {
4416 /* no uwb hardware present in system */
4417 tp_features.uwb = 0;
4418 dbg_printk(TPACPI_DBG_INIT,
4419 "uwb hardware not installed\n");
4420 }
4421
4422 if (!tp_features.uwb)
4423 return 1;
4424
4425 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
Johannes Berg19d337d2009-06-02 13:01:37 +02004426 &uwb_tprfk_ops,
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004427 RFKILL_TYPE_UWB,
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004428 TPACPI_RFK_UWB_SW_NAME,
Johannes Berg19d337d2009-06-02 13:01:37 +02004429 false);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004430 return res;
4431}
4432
4433static struct ibm_struct uwb_driver_data = {
4434 .name = "uwb",
4435 .exit = uwb_exit,
4436 .flags.experimental = 1,
4437};
4438
4439/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004440 * Video subdriver
4441 */
4442
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02004443#ifdef CONFIG_THINKPAD_ACPI_VIDEO
4444
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004445enum video_access_mode {
4446 TPACPI_VIDEO_NONE = 0,
4447 TPACPI_VIDEO_570, /* 570 */
4448 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4449 TPACPI_VIDEO_NEW, /* all others */
4450};
4451
4452enum { /* video status flags, based on VIDEO_570 */
4453 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4454 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4455 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4456};
4457
4458enum { /* TPACPI_VIDEO_570 constants */
4459 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4460 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4461 * video_status_flags */
4462 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4463 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4464};
4465
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02004466static enum video_access_mode video_supported;
4467static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004468
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004469static int video_autosw_get(void);
4470static int video_autosw_set(int enable);
4471
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004472TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004473
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004474static int __init video_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004476 int ivga;
4477
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004478 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4479
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004480 TPACPI_ACPIHANDLE_INIT(vid);
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03004481 if (tpacpi_is_ibm())
4482 TPACPI_ACPIHANDLE_INIT(vid2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004483
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004484 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4485 /* G41, assume IVGA doesn't change */
4486 vid_handle = vid2_handle;
4487
4488 if (!vid_handle)
4489 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004490 video_supported = TPACPI_VIDEO_NONE;
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03004491 else if (tpacpi_is_ibm() &&
4492 acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004493 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004494 video_supported = TPACPI_VIDEO_570;
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03004495 else if (tpacpi_is_ibm() &&
4496 acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004497 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004498 video_supported = TPACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004499 else
4500 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004501 video_supported = TPACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004503 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
4504 str_supported(video_supported != TPACPI_VIDEO_NONE),
4505 video_supported);
4506
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004507 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508}
4509
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004510static void video_exit(void)
4511{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004512 dbg_printk(TPACPI_DBG_EXIT,
4513 "restoring original video autoswitch mode\n");
4514 if (video_autosw_set(video_orig_autosw))
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004515 printk(TPACPI_ERR "error while trying to restore original "
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004516 "video autoswitch mode\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004517}
4518
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004519static int video_outputsw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520{
4521 int status = 0;
4522 int i;
4523
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004524 switch (video_supported) {
4525 case TPACPI_VIDEO_570:
4526 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
4527 TP_ACPI_VIDEO_570_PHSCMD))
4528 return -EIO;
4529 status = i & TP_ACPI_VIDEO_570_PHSMASK;
4530 break;
4531 case TPACPI_VIDEO_770:
4532 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4533 return -EIO;
4534 if (i)
4535 status |= TP_ACPI_VIDEO_S_LCD;
4536 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4537 return -EIO;
4538 if (i)
4539 status |= TP_ACPI_VIDEO_S_CRT;
4540 break;
4541 case TPACPI_VIDEO_NEW:
4542 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4543 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4544 return -EIO;
4545 if (i)
4546 status |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004548 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4549 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4550 return -EIO;
4551 if (i)
4552 status |= TP_ACPI_VIDEO_S_LCD;
4553 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4554 return -EIO;
4555 if (i)
4556 status |= TP_ACPI_VIDEO_S_DVI;
4557 break;
4558 default:
4559 return -ENOSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004560 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561
4562 return status;
4563}
4564
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004565static int video_outputsw_set(int status)
4566{
4567 int autosw;
4568 int res = 0;
4569
4570 switch (video_supported) {
4571 case TPACPI_VIDEO_570:
4572 res = acpi_evalf(NULL, NULL,
4573 "\\_SB.PHS2", "vdd",
4574 TP_ACPI_VIDEO_570_PHS2CMD,
4575 status | TP_ACPI_VIDEO_570_PHS2SET);
4576 break;
4577 case TPACPI_VIDEO_770:
4578 autosw = video_autosw_get();
4579 if (autosw < 0)
4580 return autosw;
4581
4582 res = video_autosw_set(1);
4583 if (res)
4584 return res;
4585 res = acpi_evalf(vid_handle, NULL,
4586 "ASWT", "vdd", status * 0x100, 0);
4587 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004588 printk(TPACPI_ERR
4589 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004590 return -EIO;
4591 }
4592 break;
4593 case TPACPI_VIDEO_NEW:
4594 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004595 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004596 break;
4597 default:
4598 return -ENOSYS;
4599 }
4600
4601 return (res)? 0 : -EIO;
4602}
4603
4604static int video_autosw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004605{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004606 int autosw = 0;
4607
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004608 switch (video_supported) {
4609 case TPACPI_VIDEO_570:
4610 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
4611 return -EIO;
4612 break;
4613 case TPACPI_VIDEO_770:
4614 case TPACPI_VIDEO_NEW:
4615 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4616 return -EIO;
4617 break;
4618 default:
4619 return -ENOSYS;
4620 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004621
4622 return autosw & 1;
4623}
4624
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004625static int video_autosw_set(int enable)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004626{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004627 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004628 return -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004629 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004630}
4631
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004632static int video_outputsw_cycle(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004633{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004634 int autosw = video_autosw_get();
4635 int res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004636
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004637 if (autosw < 0)
4638 return autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004639
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004640 switch (video_supported) {
4641 case TPACPI_VIDEO_570:
4642 res = video_autosw_set(1);
4643 if (res)
4644 return res;
4645 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4646 break;
4647 case TPACPI_VIDEO_770:
4648 case TPACPI_VIDEO_NEW:
4649 res = video_autosw_set(1);
4650 if (res)
4651 return res;
4652 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4653 break;
4654 default:
4655 return -ENOSYS;
4656 }
4657 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004658 printk(TPACPI_ERR
4659 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004660 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004661 }
4662
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004663 return (res)? 0 : -EIO;
4664}
4665
4666static int video_expand_toggle(void)
4667{
4668 switch (video_supported) {
4669 case TPACPI_VIDEO_570:
4670 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
4671 0 : -EIO;
4672 case TPACPI_VIDEO_770:
4673 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
4674 0 : -EIO;
4675 case TPACPI_VIDEO_NEW:
4676 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
4677 0 : -EIO;
4678 default:
4679 return -ENOSYS;
4680 }
4681 /* not reached */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004682}
4683
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004684static int video_read(struct seq_file *m)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004685{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004686 int status, autosw;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004687
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004688 if (video_supported == TPACPI_VIDEO_NONE) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004689 seq_printf(m, "status:\t\tnot supported\n");
4690 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004691 }
4692
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03004693 /* Even reads can crash X.org, so... */
4694 if (!capable(CAP_SYS_ADMIN))
4695 return -EPERM;
4696
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004697 status = video_outputsw_get();
4698 if (status < 0)
4699 return status;
4700
4701 autosw = video_autosw_get();
4702 if (autosw < 0)
4703 return autosw;
4704
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004705 seq_printf(m, "status:\t\tsupported\n");
4706 seq_printf(m, "lcd:\t\t%s\n", enabled(status, 0));
4707 seq_printf(m, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004708 if (video_supported == TPACPI_VIDEO_NEW)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004709 seq_printf(m, "dvi:\t\t%s\n", enabled(status, 3));
4710 seq_printf(m, "auto:\t\t%s\n", enabled(autosw, 0));
4711 seq_printf(m, "commands:\tlcd_enable, lcd_disable\n");
4712 seq_printf(m, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004713 if (video_supported == TPACPI_VIDEO_NEW)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004714 seq_printf(m, "commands:\tdvi_enable, dvi_disable\n");
4715 seq_printf(m, "commands:\tauto_enable, auto_disable\n");
4716 seq_printf(m, "commands:\tvideo_switch, expand_toggle\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004717
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004718 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004719}
4720
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004721static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722{
4723 char *cmd;
4724 int enable, disable, status;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004725 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004726
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004727 if (video_supported == TPACPI_VIDEO_NONE)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004728 return -ENODEV;
4729
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03004730 /* Even reads can crash X.org, let alone writes... */
4731 if (!capable(CAP_SYS_ADMIN))
4732 return -EPERM;
4733
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004734 enable = 0;
4735 disable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736
4737 while ((cmd = next_cmd(&buf))) {
4738 if (strlencmp(cmd, "lcd_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004739 enable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740 } else if (strlencmp(cmd, "lcd_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004741 disable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004742 } else if (strlencmp(cmd, "crt_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004743 enable |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744 } else if (strlencmp(cmd, "crt_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004745 disable |= TP_ACPI_VIDEO_S_CRT;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004746 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004747 strlencmp(cmd, "dvi_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004748 enable |= TP_ACPI_VIDEO_S_DVI;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004749 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004750 strlencmp(cmd, "dvi_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004751 disable |= TP_ACPI_VIDEO_S_DVI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004752 } else if (strlencmp(cmd, "auto_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004753 res = video_autosw_set(1);
4754 if (res)
4755 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004756 } else if (strlencmp(cmd, "auto_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004757 res = video_autosw_set(0);
4758 if (res)
4759 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760 } else if (strlencmp(cmd, "video_switch") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004761 res = video_outputsw_cycle();
4762 if (res)
4763 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004765 res = video_expand_toggle();
4766 if (res)
4767 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004768 } else
4769 return -EINVAL;
4770 }
4771
4772 if (enable || disable) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004773 status = video_outputsw_get();
4774 if (status < 0)
4775 return status;
4776 res = video_outputsw_set((status & ~disable) | enable);
4777 if (res)
4778 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779 }
4780
4781 return 0;
4782}
4783
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004784static struct ibm_struct video_driver_data = {
4785 .name = "video",
4786 .read = video_read,
4787 .write = video_write,
4788 .exit = video_exit,
4789};
4790
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02004791#endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4792
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004793/*************************************************************************
4794 * Light (thinklight) subdriver
4795 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004797TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4798TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004799
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004800static int light_get_status(void)
4801{
4802 int status = 0;
4803
4804 if (tp_features.light_status) {
4805 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4806 return -EIO;
4807 return (!!status);
4808 }
4809
4810 return -ENXIO;
4811}
4812
4813static int light_set_status(int status)
4814{
4815 int rc;
4816
4817 if (tp_features.light) {
4818 if (cmos_handle) {
4819 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4820 (status)?
4821 TP_CMOS_THINKLIGHT_ON :
4822 TP_CMOS_THINKLIGHT_OFF);
4823 } else {
4824 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4825 (status)? 1 : 0);
4826 }
4827 return (rc)? 0 : -EIO;
4828 }
4829
4830 return -ENXIO;
4831}
4832
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004833static void light_set_status_worker(struct work_struct *work)
4834{
4835 struct tpacpi_led_classdev *data =
4836 container_of(work, struct tpacpi_led_classdev, work);
4837
4838 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00004839 light_set_status((data->new_state != TPACPI_LED_OFF));
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004840}
4841
4842static void light_sysfs_set(struct led_classdev *led_cdev,
4843 enum led_brightness brightness)
4844{
4845 struct tpacpi_led_classdev *data =
4846 container_of(led_cdev,
4847 struct tpacpi_led_classdev,
4848 led_classdev);
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00004849 data->new_state = (brightness != LED_OFF) ?
4850 TPACPI_LED_ON : TPACPI_LED_OFF;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004851 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004852}
4853
4854static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4855{
4856 return (light_get_status() == 1)? LED_FULL : LED_OFF;
4857}
4858
4859static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4860 .led_classdev = {
4861 .name = "tpacpi::thinklight",
4862 .brightness_set = &light_sysfs_set,
4863 .brightness_get = &light_sysfs_get,
4864 }
4865};
4866
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004867static int __init light_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004868{
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004869 int rc;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004870
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004871 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4872
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03004873 if (tpacpi_is_ibm()) {
4874 TPACPI_ACPIHANDLE_INIT(ledb);
4875 TPACPI_ACPIHANDLE_INIT(lght);
4876 }
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004877 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004878 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004879
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004880 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004881 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004882
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004883 if (tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004884 /* light status not supported on
4885 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004886 tp_features.light_status =
4887 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004888
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004889 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4890 str_supported(tp_features.light),
4891 str_supported(tp_features.light_status));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004892
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004893 if (!tp_features.light)
4894 return 1;
4895
4896 rc = led_classdev_register(&tpacpi_pdev->dev,
4897 &tpacpi_led_thinklight.led_classdev);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004898
4899 if (rc < 0) {
4900 tp_features.light = 0;
4901 tp_features.light_status = 0;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004902 } else {
4903 rc = 0;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004904 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004905
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004906 return rc;
4907}
4908
4909static void light_exit(void)
4910{
4911 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
4912 if (work_pending(&tpacpi_led_thinklight.work))
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004913 flush_workqueue(tpacpi_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914}
4915
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004916static int light_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004917{
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004918 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004919
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004920 if (!tp_features.light) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004921 seq_printf(m, "status:\t\tnot supported\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004922 } else if (!tp_features.light_status) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004923 seq_printf(m, "status:\t\tunknown\n");
4924 seq_printf(m, "commands:\ton, off\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004925 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004926 status = light_get_status();
4927 if (status < 0)
4928 return status;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004929 seq_printf(m, "status:\t\t%s\n", onoff(status, 0));
4930 seq_printf(m, "commands:\ton, off\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004931 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004933 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004934}
4935
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004936static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004938 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004939 int newstatus = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004940
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004941 if (!tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004942 return -ENODEV;
4943
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944 while ((cmd = next_cmd(&buf))) {
4945 if (strlencmp(cmd, "on") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004946 newstatus = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004947 } else if (strlencmp(cmd, "off") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004948 newstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949 } else
4950 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951 }
4952
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004953 return light_set_status(newstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004954}
4955
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004956static struct ibm_struct light_driver_data = {
4957 .name = "light",
4958 .read = light_read,
4959 .write = light_write,
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004960 .exit = light_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004961};
4962
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004963/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004964 * CMOS subdriver
4965 */
4966
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004967/* sysfs cmos_command -------------------------------------------------- */
4968static ssize_t cmos_command_store(struct device *dev,
4969 struct device_attribute *attr,
4970 const char *buf, size_t count)
4971{
4972 unsigned long cmos_cmd;
4973 int res;
4974
4975 if (parse_strtoul(buf, 21, &cmos_cmd))
4976 return -EINVAL;
4977
4978 res = issue_thinkpad_cmos_command(cmos_cmd);
4979 return (res)? res : count;
4980}
4981
4982static struct device_attribute dev_attr_cmos_command =
4983 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4984
4985/* --------------------------------------------------------------------- */
4986
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004987static int __init cmos_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004988{
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004989 int res;
4990
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004991 vdbg_printk(TPACPI_DBG_INIT,
4992 "initializing cmos commands subdriver\n");
4993
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004994 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004995
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004996 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4997 str_supported(cmos_handle != NULL));
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004998
4999 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
5000 if (res)
5001 return res;
5002
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005003 return (cmos_handle)? 0 : 1;
5004}
5005
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005006static void cmos_exit(void)
5007{
5008 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
5009}
5010
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005011static int cmos_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005013 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
5014 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005015 if (!cmos_handle)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005016 seq_printf(m, "status:\t\tnot supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005017 else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005018 seq_printf(m, "status:\t\tsupported\n");
5019 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005020 }
5021
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005022 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005023}
5024
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005025static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005026{
5027 char *cmd;
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03005028 int cmos_cmd, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005029
5030 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005031 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
5032 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005033 /* cmos_cmd set */
5034 } else
5035 return -EINVAL;
5036
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03005037 res = issue_thinkpad_cmos_command(cmos_cmd);
5038 if (res)
5039 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005040 }
5041
5042 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005043}
5044
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005045static struct ibm_struct cmos_driver_data = {
5046 .name = "cmos",
5047 .read = cmos_read,
5048 .write = cmos_write,
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005049 .exit = cmos_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005050};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005051
5052/*************************************************************************
5053 * LED subdriver
5054 */
5055
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005056enum led_access_mode {
5057 TPACPI_LED_NONE = 0,
5058 TPACPI_LED_570, /* 570 */
5059 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5060 TPACPI_LED_NEW, /* all others */
5061};
5062
5063enum { /* For TPACPI_LED_OLD */
5064 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
5065 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
5066 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
5067};
5068
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02005069static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005070
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005071TPACPI_HANDLE(led, ec, "SLED", /* 570 */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005072 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
5073 /* T20-22, X20-21 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005074 "LED", /* all others */
5075 ); /* R30, R31 */
5076
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005077#define TPACPI_LED_NUMLEDS 16
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005078static struct tpacpi_led_classdev *tpacpi_leds;
5079static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
Harvey Harrisone3aa51f2008-05-29 17:51:57 -07005080static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005081 /* there's a limit of 19 chars + NULL before 2.6.26 */
5082 "tpacpi::power",
5083 "tpacpi:orange:batt",
5084 "tpacpi:green:batt",
5085 "tpacpi::dock_active",
5086 "tpacpi::bay_active",
5087 "tpacpi::dock_batt",
5088 "tpacpi::unknown_led",
5089 "tpacpi::standby",
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005090 "tpacpi::dock_status1",
5091 "tpacpi::dock_status2",
5092 "tpacpi::unknown_led2",
5093 "tpacpi::unknown_led3",
5094 "tpacpi::thinkvantage",
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005095};
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005096#define TPACPI_SAFE_LEDS 0x1081U
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005097
5098static inline bool tpacpi_is_led_restricted(const unsigned int led)
5099{
5100#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5101 return false;
5102#else
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005103 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005104#endif
5105}
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005106
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005107static int led_get_status(const unsigned int led)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005108{
5109 int status;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005110 enum led_status_t led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005111
5112 switch (led_supported) {
5113 case TPACPI_LED_570:
5114 if (!acpi_evalf(ec_handle,
5115 &status, "GLED", "dd", 1 << led))
5116 return -EIO;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005117 led_s = (status == 0)?
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005118 TPACPI_LED_OFF :
5119 ((status == 1)?
5120 TPACPI_LED_ON :
5121 TPACPI_LED_BLINK);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005122 tpacpi_led_state_cache[led] = led_s;
5123 return led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005124 default:
5125 return -ENXIO;
5126 }
5127
5128 /* not reached */
5129}
5130
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005131static int led_set_status(const unsigned int led,
5132 const enum led_status_t ledstatus)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005133{
5134 /* off, on, blink. Index is led_status_t */
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005135 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
5136 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005137
5138 int rc = 0;
5139
5140 switch (led_supported) {
5141 case TPACPI_LED_570:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005142 /* 570 */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005143 if (unlikely(led > 7))
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005144 return -EINVAL;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005145 if (unlikely(tpacpi_is_led_restricted(led)))
5146 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005147 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5148 (1 << led), led_sled_arg1[ledstatus]))
5149 rc = -EIO;
5150 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005151 case TPACPI_LED_OLD:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005152 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005153 if (unlikely(led > 7))
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005154 return -EINVAL;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005155 if (unlikely(tpacpi_is_led_restricted(led)))
5156 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005157 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
5158 if (rc >= 0)
5159 rc = ec_write(TPACPI_LED_EC_HLBL,
5160 (ledstatus == TPACPI_LED_BLINK) << led);
5161 if (rc >= 0)
5162 rc = ec_write(TPACPI_LED_EC_HLCL,
5163 (ledstatus != TPACPI_LED_OFF) << led);
5164 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005165 case TPACPI_LED_NEW:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005166 /* all others */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005167 if (unlikely(led >= TPACPI_LED_NUMLEDS))
5168 return -EINVAL;
5169 if (unlikely(tpacpi_is_led_restricted(led)))
5170 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005171 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5172 led, led_led_arg1[ledstatus]))
5173 rc = -EIO;
5174 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005175 default:
5176 rc = -ENXIO;
5177 }
5178
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005179 if (!rc)
5180 tpacpi_led_state_cache[led] = ledstatus;
5181
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005182 return rc;
5183}
5184
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005185static void led_set_status_worker(struct work_struct *work)
5186{
5187 struct tpacpi_led_classdev *data =
5188 container_of(work, struct tpacpi_led_classdev, work);
5189
5190 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005191 led_set_status(data->led, data->new_state);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005192}
5193
5194static void led_sysfs_set(struct led_classdev *led_cdev,
5195 enum led_brightness brightness)
5196{
5197 struct tpacpi_led_classdev *data = container_of(led_cdev,
5198 struct tpacpi_led_classdev, led_classdev);
5199
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005200 if (brightness == LED_OFF)
5201 data->new_state = TPACPI_LED_OFF;
5202 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
5203 data->new_state = TPACPI_LED_ON;
5204 else
5205 data->new_state = TPACPI_LED_BLINK;
5206
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03005207 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005208}
5209
5210static int led_sysfs_blink_set(struct led_classdev *led_cdev,
5211 unsigned long *delay_on, unsigned long *delay_off)
5212{
5213 struct tpacpi_led_classdev *data = container_of(led_cdev,
5214 struct tpacpi_led_classdev, led_classdev);
5215
5216 /* Can we choose the flash rate? */
5217 if (*delay_on == 0 && *delay_off == 0) {
5218 /* yes. set them to the hardware blink rate (1 Hz) */
5219 *delay_on = 500; /* ms */
5220 *delay_off = 500; /* ms */
5221 } else if ((*delay_on != 500) || (*delay_off != 500))
5222 return -EINVAL;
5223
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005224 data->new_state = TPACPI_LED_BLINK;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03005225 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005226
5227 return 0;
5228}
5229
5230static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
5231{
5232 int rc;
5233
5234 struct tpacpi_led_classdev *data = container_of(led_cdev,
5235 struct tpacpi_led_classdev, led_classdev);
5236
5237 rc = led_get_status(data->led);
5238
5239 if (rc == TPACPI_LED_OFF || rc < 0)
5240 rc = LED_OFF; /* no error handling in led class :( */
5241 else
5242 rc = LED_FULL;
5243
5244 return rc;
5245}
5246
5247static void led_exit(void)
5248{
5249 unsigned int i;
5250
5251 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5252 if (tpacpi_leds[i].led_classdev.name)
5253 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
5254 }
5255
5256 kfree(tpacpi_leds);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005257}
5258
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005259static int __init tpacpi_init_led(unsigned int led)
5260{
5261 int rc;
5262
5263 tpacpi_leds[led].led = led;
5264
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005265 /* LEDs with no name don't get registered */
5266 if (!tpacpi_led_names[led])
5267 return 0;
5268
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005269 tpacpi_leds[led].led_classdev.brightness_set = &led_sysfs_set;
5270 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5271 if (led_supported == TPACPI_LED_570)
5272 tpacpi_leds[led].led_classdev.brightness_get =
5273 &led_sysfs_get;
5274
5275 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5276
5277 INIT_WORK(&tpacpi_leds[led].work, led_set_status_worker);
5278
5279 rc = led_classdev_register(&tpacpi_pdev->dev,
5280 &tpacpi_leds[led].led_classdev);
5281 if (rc < 0)
5282 tpacpi_leds[led].led_classdev.name = NULL;
5283
5284 return rc;
5285}
5286
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005287static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
5288 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5289 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5290 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5291
5292 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5293 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5294 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5295 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5296 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5297 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5298 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5299 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5300
5301 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5302 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5303 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5304 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5305 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5306
5307 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5308 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5309 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5310 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5311
5312 /* (1) - may have excess leds enabled on MSB */
5313
5314 /* Defaults (order matters, keep last, don't reorder!) */
5315 { /* Lenovo */
5316 .vendor = PCI_VENDOR_ID_LENOVO,
5317 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5318 .quirks = 0x1fffU,
5319 },
5320 { /* IBM ThinkPads with no EC version string */
5321 .vendor = PCI_VENDOR_ID_IBM,
5322 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
5323 .quirks = 0x00ffU,
5324 },
5325 { /* IBM ThinkPads with EC version string */
5326 .vendor = PCI_VENDOR_ID_IBM,
5327 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5328 .quirks = 0x00bfU,
5329 },
5330};
5331
5332#undef TPACPI_LEDQ_IBM
5333#undef TPACPI_LEDQ_LNV
5334
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005335static int __init led_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005336{
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005337 unsigned int i;
5338 int rc;
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005339 unsigned long useful_leds;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005340
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005341 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
5342
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005343 TPACPI_ACPIHANDLE_INIT(led);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005344
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005345 if (!led_handle)
5346 /* led not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005347 led_supported = TPACPI_LED_NONE;
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03005348 else if (tpacpi_is_ibm() && strlencmp(led_path, "SLED") == 0)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005349 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005350 led_supported = TPACPI_LED_570;
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03005351 else if (tpacpi_is_ibm() && strlencmp(led_path, "SYSL") == 0)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005352 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005353 led_supported = TPACPI_LED_OLD;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005354 else
5355 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005356 led_supported = TPACPI_LED_NEW;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005357
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005358 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
5359 str_supported(led_supported), led_supported);
5360
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005361 if (led_supported == TPACPI_LED_NONE)
5362 return 1;
5363
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005364 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
5365 GFP_KERNEL);
5366 if (!tpacpi_leds) {
5367 printk(TPACPI_ERR "Out of memory for LED data\n");
5368 return -ENOMEM;
5369 }
5370
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005371 useful_leds = tpacpi_check_quirks(led_useful_qtable,
5372 ARRAY_SIZE(led_useful_qtable));
5373
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005374 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005375 if (!tpacpi_is_led_restricted(i) &&
5376 test_bit(i, &useful_leds)) {
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005377 rc = tpacpi_init_led(i);
5378 if (rc < 0) {
5379 led_exit();
5380 return rc;
5381 }
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005382 }
5383 }
5384
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005385#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005386 printk(TPACPI_NOTICE
5387 "warning: userspace override of important "
5388 "firmware LEDs is enabled\n");
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005389#endif
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005390 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005391}
5392
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005393#define str_led_status(s) \
5394 ((s) == TPACPI_LED_OFF ? "off" : \
5395 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005396
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005397static int led_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005398{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005399 if (!led_supported) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005400 seq_printf(m, "status:\t\tnot supported\n");
5401 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005402 }
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005403 seq_printf(m, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005404
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005405 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005406 /* 570 */
5407 int i, status;
5408 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005409 status = led_get_status(i);
5410 if (status < 0)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005411 return -EIO;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005412 seq_printf(m, "%d:\t\t%s\n",
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005413 i, str_led_status(status));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005414 }
5415 }
5416
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005417 seq_printf(m, "commands:\t"
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005418 "<led> on, <led> off, <led> blink (<led> is 0-15)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005419
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005420 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005421}
5422
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005423static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005424{
5425 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005426 int led, rc;
5427 enum led_status_t s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005428
5429 if (!led_supported)
5430 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005431
5432 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005433 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 15)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005434 return -EINVAL;
5435
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005436 if (strstr(cmd, "off")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005437 s = TPACPI_LED_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005438 } else if (strstr(cmd, "on")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005439 s = TPACPI_LED_ON;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005440 } else if (strstr(cmd, "blink")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005441 s = TPACPI_LED_BLINK;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005442 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005443 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005444 }
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005445
5446 rc = led_set_status(led, s);
5447 if (rc < 0)
5448 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005449 }
5450
5451 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005452}
5453
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005454static struct ibm_struct led_driver_data = {
5455 .name = "led",
5456 .read = led_read,
5457 .write = led_write,
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005458 .exit = led_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005459};
5460
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005461/*************************************************************************
5462 * Beep subdriver
5463 */
5464
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005465TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005466
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005467#define TPACPI_BEEP_Q1 0x0001
5468
5469static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
5470 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
5471 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
5472};
5473
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005474static int __init beep_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005475{
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005476 unsigned long quirks;
5477
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005478 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
5479
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005480 TPACPI_ACPIHANDLE_INIT(beep);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005481
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005482 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
5483 str_supported(beep_handle != NULL));
5484
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005485 quirks = tpacpi_check_quirks(beep_quirk_table,
5486 ARRAY_SIZE(beep_quirk_table));
5487
5488 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
5489
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005490 return (beep_handle)? 0 : 1;
5491}
5492
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005493static int beep_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005494{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005495 if (!beep_handle)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005496 seq_printf(m, "status:\t\tnot supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005497 else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005498 seq_printf(m, "status:\t\tsupported\n");
5499 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-17)\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005500 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005501
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005502 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005503}
5504
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005505static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005506{
5507 char *cmd;
5508 int beep_cmd;
5509
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005510 if (!beep_handle)
5511 return -ENODEV;
5512
Linus Torvalds1da177e2005-04-16 15:20:36 -07005513 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005514 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
5515 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005516 /* beep_cmd set */
5517 } else
5518 return -EINVAL;
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005519 if (tp_features.beep_needs_two_args) {
5520 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
5521 beep_cmd, 0))
5522 return -EIO;
5523 } else {
5524 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
5525 beep_cmd))
5526 return -EIO;
5527 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005528 }
5529
5530 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005531}
5532
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005533static struct ibm_struct beep_driver_data = {
5534 .name = "beep",
5535 .read = beep_read,
5536 .write = beep_write,
5537};
5538
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005539/*************************************************************************
5540 * Thermal subdriver
5541 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005542
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005543enum thermal_access_mode {
5544 TPACPI_THERMAL_NONE = 0, /* No thermal support */
5545 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
5546 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
5547 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
5548 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
5549};
5550
5551enum { /* TPACPI_THERMAL_TPEC_* */
5552 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
5553 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
5554 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005555
5556 TPACPI_THERMAL_SENSOR_NA = -128000, /* Sensor not available */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005557};
5558
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005559
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005560#define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5561struct ibm_thermal_sensors_struct {
5562 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
5563};
5564
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005565static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005566
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005567/* idx is zero-based */
5568static int thermal_get_sensor(int idx, s32 *value)
5569{
5570 int t;
5571 s8 tmp;
5572 char tmpi[5];
5573
5574 t = TP_EC_THERMAL_TMP0;
5575
5576 switch (thermal_read_mode) {
5577#if TPACPI_MAX_THERMAL_SENSORS >= 16
5578 case TPACPI_THERMAL_TPEC_16:
5579 if (idx >= 8 && idx <= 15) {
5580 t = TP_EC_THERMAL_TMP8;
5581 idx -= 8;
5582 }
5583 /* fallthrough */
5584#endif
5585 case TPACPI_THERMAL_TPEC_8:
5586 if (idx <= 7) {
5587 if (!acpi_ec_read(t + idx, &tmp))
5588 return -EIO;
5589 *value = tmp * 1000;
5590 return 0;
5591 }
5592 break;
5593
5594 case TPACPI_THERMAL_ACPI_UPDT:
5595 if (idx <= 7) {
5596 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5597 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
5598 return -EIO;
5599 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5600 return -EIO;
5601 *value = (t - 2732) * 100;
5602 return 0;
5603 }
5604 break;
5605
5606 case TPACPI_THERMAL_ACPI_TMP07:
5607 if (idx <= 7) {
5608 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5609 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5610 return -EIO;
5611 if (t > 127 || t < -127)
5612 t = TP_EC_THERMAL_TMP_NA;
5613 *value = t * 1000;
5614 return 0;
5615 }
5616 break;
5617
5618 case TPACPI_THERMAL_NONE:
5619 default:
5620 return -ENOSYS;
5621 }
5622
5623 return -EINVAL;
5624}
5625
5626static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5627{
5628 int res, i;
5629 int n;
5630
5631 n = 8;
5632 i = 0;
5633
5634 if (!s)
5635 return -EINVAL;
5636
5637 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5638 n = 16;
5639
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005640 for (i = 0 ; i < n; i++) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005641 res = thermal_get_sensor(i, &s->temp[i]);
5642 if (res)
5643 return res;
5644 }
5645
5646 return n;
5647}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005648
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005649static void thermal_dump_all_sensors(void)
5650{
5651 int n, i;
5652 struct ibm_thermal_sensors_struct t;
5653
5654 n = thermal_get_sensors(&t);
5655 if (n <= 0)
5656 return;
5657
5658 printk(TPACPI_NOTICE
5659 "temperatures (Celsius):");
5660
5661 for (i = 0; i < n; i++) {
5662 if (t.temp[i] != TPACPI_THERMAL_SENSOR_NA)
5663 printk(KERN_CONT " %d", (int)(t.temp[i] / 1000));
5664 else
5665 printk(KERN_CONT " N/A");
5666 }
5667
5668 printk(KERN_CONT "\n");
5669}
5670
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005671/* sysfs temp##_input -------------------------------------------------- */
5672
5673static ssize_t thermal_temp_input_show(struct device *dev,
5674 struct device_attribute *attr,
5675 char *buf)
5676{
5677 struct sensor_device_attribute *sensor_attr =
5678 to_sensor_dev_attr(attr);
5679 int idx = sensor_attr->index;
5680 s32 value;
5681 int res;
5682
5683 res = thermal_get_sensor(idx, &value);
5684 if (res)
5685 return res;
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005686 if (value == TPACPI_THERMAL_SENSOR_NA)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005687 return -ENXIO;
5688
5689 return snprintf(buf, PAGE_SIZE, "%d\n", value);
5690}
5691
5692#define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005693 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5694 thermal_temp_input_show, NULL, _idxB)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005695
5696static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5697 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5698 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5699 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5700 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5701 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5702 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5703 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5704 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5705 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5706 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5707 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5708 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5709 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5710 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5711 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5712 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5713};
5714
5715#define THERMAL_ATTRS(X) \
5716 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5717
5718static struct attribute *thermal_temp_input_attr[] = {
5719 THERMAL_ATTRS(8),
5720 THERMAL_ATTRS(9),
5721 THERMAL_ATTRS(10),
5722 THERMAL_ATTRS(11),
5723 THERMAL_ATTRS(12),
5724 THERMAL_ATTRS(13),
5725 THERMAL_ATTRS(14),
5726 THERMAL_ATTRS(15),
5727 THERMAL_ATTRS(0),
5728 THERMAL_ATTRS(1),
5729 THERMAL_ATTRS(2),
5730 THERMAL_ATTRS(3),
5731 THERMAL_ATTRS(4),
5732 THERMAL_ATTRS(5),
5733 THERMAL_ATTRS(6),
5734 THERMAL_ATTRS(7),
5735 NULL
5736};
5737
5738static const struct attribute_group thermal_temp_input16_group = {
5739 .attrs = thermal_temp_input_attr
5740};
5741
5742static const struct attribute_group thermal_temp_input8_group = {
5743 .attrs = &thermal_temp_input_attr[8]
5744};
5745
5746#undef THERMAL_SENSOR_ATTR_TEMP
5747#undef THERMAL_ATTRS
5748
5749/* --------------------------------------------------------------------- */
5750
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005751static int __init thermal_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005752{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005753 u8 t, ta1, ta2;
5754 int i;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005755 int acpi_tmp7;
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005756 int res;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005757
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005758 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5759
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005760 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005761
Henrique de Moraes Holschuh3d6f99c2007-07-18 23:45:46 -03005762 if (thinkpad_id.ec_model) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005763 /*
5764 * Direct EC access mode: sensors at registers
5765 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5766 * non-implemented, thermal sensors return 0x80 when
5767 * not available
5768 */
5769
5770 ta1 = ta2 = 0;
5771 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03005772 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005773 ta1 |= t;
5774 } else {
5775 ta1 = 0;
5776 break;
5777 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03005778 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005779 ta2 |= t;
5780 } else {
5781 ta1 = 0;
5782 break;
5783 }
5784 }
5785 if (ta1 == 0) {
5786 /* This is sheer paranoia, but we handle it anyway */
5787 if (acpi_tmp7) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005788 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005789 "ThinkPad ACPI EC access misbehaving, "
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005790 "falling back to ACPI TMPx access "
5791 "mode\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005792 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005793 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005794 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005795 "ThinkPad ACPI EC access misbehaving, "
5796 "disabling thermal sensors access\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005797 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005798 }
5799 } else {
5800 thermal_read_mode =
5801 (ta2 != 0) ?
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005802 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005803 }
5804 } else if (acpi_tmp7) {
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03005805 if (tpacpi_is_ibm() &&
5806 acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005807 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005808 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005809 } else {
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03005810 /* IBM/LENOVO DSDT EC.TMPx access, max 8 sensors */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005811 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005812 }
5813 } else {
5814 /* temperatures not supported on 570, G4x, R30, R31, R32 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005815 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005816 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005817
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005818 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5819 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5820 thermal_read_mode);
5821
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005822 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005823 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005824 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005825 &thermal_temp_input16_group);
5826 if (res)
5827 return res;
5828 break;
5829 case TPACPI_THERMAL_TPEC_8:
5830 case TPACPI_THERMAL_ACPI_TMP07:
5831 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005832 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005833 &thermal_temp_input8_group);
5834 if (res)
5835 return res;
5836 break;
5837 case TPACPI_THERMAL_NONE:
5838 default:
5839 return 1;
5840 }
5841
5842 return 0;
5843}
5844
5845static void thermal_exit(void)
5846{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005847 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005848 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005849 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005850 &thermal_temp_input16_group);
5851 break;
5852 case TPACPI_THERMAL_TPEC_8:
5853 case TPACPI_THERMAL_ACPI_TMP07:
5854 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005855 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Roel Kluinf04d5e02010-02-02 14:37:58 -08005856 &thermal_temp_input8_group);
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005857 break;
5858 case TPACPI_THERMAL_NONE:
5859 default:
5860 break;
5861 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005862}
5863
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005864static int thermal_read(struct seq_file *m)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005865{
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005866 int n, i;
5867 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005868
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005869 n = thermal_get_sensors(&t);
5870 if (unlikely(n < 0))
5871 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005872
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005873 seq_printf(m, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005874
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005875 if (n > 0) {
5876 for (i = 0; i < (n - 1); i++)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005877 seq_printf(m, "%d ", t.temp[i] / 1000);
5878 seq_printf(m, "%d\n", t.temp[i] / 1000);
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005879 } else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005880 seq_printf(m, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005881
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005882 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005883}
5884
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005885static struct ibm_struct thermal_driver_data = {
5886 .name = "thermal",
5887 .read = thermal_read,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005888 .exit = thermal_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005889};
5890
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005891/*************************************************************************
5892 * EC Dump subdriver
5893 */
5894
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005895static u8 ecdump_regs[256];
5896
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005897static int ecdump_read(struct seq_file *m)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005898{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005899 int i, j;
5900 u8 v;
5901
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005902 seq_printf(m, "EC "
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005903 " +00 +01 +02 +03 +04 +05 +06 +07"
5904 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
5905 for (i = 0; i < 256; i += 16) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005906 seq_printf(m, "EC 0x%02x:", i);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005907 for (j = 0; j < 16; j++) {
5908 if (!acpi_ec_read(i + j, &v))
5909 break;
5910 if (v != ecdump_regs[i + j])
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005911 seq_printf(m, " *%02x", v);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005912 else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005913 seq_printf(m, " %02x", v);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005914 ecdump_regs[i + j] = v;
5915 }
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005916 seq_putc(m, '\n');
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005917 if (j != 16)
5918 break;
5919 }
5920
5921 /* These are way too dangerous to advertise openly... */
5922#if 0
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005923 seq_printf(m, "commands:\t0x<offset> 0x<value>"
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005924 " (<offset> is 00-ff, <value> is 00-ff)\n");
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005925 seq_printf(m, "commands:\t0x<offset> <value> "
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005926 " (<offset> is 00-ff, <value> is 0-255)\n");
5927#endif
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005928 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005929}
5930
5931static int ecdump_write(char *buf)
5932{
5933 char *cmd;
5934 int i, v;
5935
5936 while ((cmd = next_cmd(&buf))) {
5937 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
5938 /* i and v set */
5939 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
5940 /* i and v set */
5941 } else
5942 return -EINVAL;
5943 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
5944 if (!acpi_ec_write(i, v))
5945 return -EIO;
5946 } else
5947 return -EINVAL;
5948 }
5949
5950 return 0;
5951}
5952
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005953static struct ibm_struct ecdump_driver_data = {
5954 .name = "ecdump",
5955 .read = ecdump_read,
5956 .write = ecdump_write,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03005957 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005958};
5959
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005960/*************************************************************************
5961 * Backlight/brightness subdriver
5962 */
Holger Macht8acb0252006-10-20 14:30:28 -07005963
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005964#define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5965
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005966/*
5967 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
5968 * CMOS NVRAM byte 0x5E, bits 0-3.
5969 *
5970 * EC HBRV (0x31) has the following layout
5971 * Bit 7: unknown function
5972 * Bit 6: unknown function
5973 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5974 * Bit 4: must be set to zero to avoid problems
5975 * Bit 3-0: backlight brightness level
5976 *
5977 * brightness_get_raw returns status data in the HBRV layout
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03005978 *
5979 * WARNING: The X61 has been verified to use HBRV for something else, so
5980 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
5981 * testing on the very early *60 Lenovo models...
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005982 */
5983
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005984enum {
5985 TP_EC_BACKLIGHT = 0x31,
5986
5987 /* TP_EC_BACKLIGHT bitmasks */
5988 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5989 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5990 TP_EC_BACKLIGHT_MAPSW = 0x20,
5991};
5992
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005993enum tpacpi_brightness_access_mode {
5994 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
5995 TPACPI_BRGHT_MODE_EC, /* EC control */
5996 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
5997 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
5998 TPACPI_BRGHT_MODE_MAX
5999};
6000
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03006001static struct backlight_device *ibm_backlight_device;
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006002
6003static enum tpacpi_brightness_access_mode brightness_mode =
6004 TPACPI_BRGHT_MODE_MAX;
6005
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006006static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
6007
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006008static struct mutex brightness_mutex;
6009
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006010/* NVRAM brightness access,
6011 * call with brightness_mutex held! */
6012static unsigned int tpacpi_brightness_nvram_get(void)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006013{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006014 u8 lnvram;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006015
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006016 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
6017 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
6018 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
6019 lnvram &= (tp_features.bright_16levels) ? 0x0f : 0x07;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006020
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006021 return lnvram;
6022}
6023
6024static void tpacpi_brightness_checkpoint_nvram(void)
6025{
6026 u8 lec = 0;
6027 u8 b_nvram;
6028
6029 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
6030 return;
6031
6032 vdbg_printk(TPACPI_DBG_BRGHT,
6033 "trying to checkpoint backlight level to NVRAM...\n");
6034
6035 if (mutex_lock_killable(&brightness_mutex) < 0)
6036 return;
6037
6038 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6039 goto unlock;
6040 lec &= TP_EC_BACKLIGHT_LVLMSK;
6041 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
6042
6043 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
6044 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
6045 /* NVRAM needs update */
6046 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
6047 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
6048 b_nvram |= lec;
6049 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
6050 dbg_printk(TPACPI_DBG_BRGHT,
6051 "updated NVRAM backlight level to %u (0x%02x)\n",
6052 (unsigned int) lec, (unsigned int) b_nvram);
6053 } else
6054 vdbg_printk(TPACPI_DBG_BRGHT,
6055 "NVRAM backlight level already is %u (0x%02x)\n",
6056 (unsigned int) lec, (unsigned int) b_nvram);
6057
6058unlock:
6059 mutex_unlock(&brightness_mutex);
6060}
6061
6062
6063/* call with brightness_mutex held! */
6064static int tpacpi_brightness_get_raw(int *status)
6065{
6066 u8 lec = 0;
6067
6068 switch (brightness_mode) {
6069 case TPACPI_BRGHT_MODE_UCMS_STEP:
6070 *status = tpacpi_brightness_nvram_get();
6071 return 0;
6072 case TPACPI_BRGHT_MODE_EC:
6073 case TPACPI_BRGHT_MODE_ECNVRAM:
6074 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
Henrique de Moraes Holschuh2d5e94d2008-04-26 01:02:20 -03006075 return -EIO;
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006076 *status = lec;
6077 return 0;
6078 default:
6079 return -ENXIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006080 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006081}
6082
6083/* call with brightness_mutex held! */
6084/* do NOT call with illegal backlight level value */
6085static int tpacpi_brightness_set_ec(unsigned int value)
6086{
6087 u8 lec = 0;
6088
6089 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6090 return -EIO;
6091
6092 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
6093 (lec & TP_EC_BACKLIGHT_CMDMSK) |
6094 (value & TP_EC_BACKLIGHT_LVLMSK))))
6095 return -EIO;
6096
6097 return 0;
6098}
6099
6100/* call with brightness_mutex held! */
6101static int tpacpi_brightness_set_ucmsstep(unsigned int value)
6102{
6103 int cmos_cmd, inc;
6104 unsigned int current_value, i;
6105
6106 current_value = tpacpi_brightness_nvram_get();
6107
6108 if (value == current_value)
6109 return 0;
6110
6111 cmos_cmd = (value > current_value) ?
6112 TP_CMOS_BRIGHTNESS_UP :
6113 TP_CMOS_BRIGHTNESS_DOWN;
6114 inc = (value > current_value) ? 1 : -1;
6115
6116 for (i = current_value; i != value; i += inc)
6117 if (issue_thinkpad_cmos_command(cmos_cmd))
6118 return -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006119
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006120 return 0;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006121}
6122
6123/* May return EINTR which can always be mapped to ERESTARTSYS */
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006124static int brightness_set(unsigned int value)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006125{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006126 int res;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006127
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006128 if (value > ((tp_features.bright_16levels)? 15 : 7) ||
6129 value < 0)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006130 return -EINVAL;
6131
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006132 vdbg_printk(TPACPI_DBG_BRGHT,
6133 "set backlight level to %d\n", value);
6134
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006135 res = mutex_lock_killable(&brightness_mutex);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006136 if (res < 0)
6137 return res;
6138
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006139 switch (brightness_mode) {
6140 case TPACPI_BRGHT_MODE_EC:
6141 case TPACPI_BRGHT_MODE_ECNVRAM:
6142 res = tpacpi_brightness_set_ec(value);
6143 break;
6144 case TPACPI_BRGHT_MODE_UCMS_STEP:
6145 res = tpacpi_brightness_set_ucmsstep(value);
6146 break;
6147 default:
6148 res = -ENXIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006149 }
6150
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006151 mutex_unlock(&brightness_mutex);
6152 return res;
6153}
6154
6155/* sysfs backlight class ----------------------------------------------- */
6156
6157static int brightness_update_status(struct backlight_device *bd)
6158{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006159 unsigned int level =
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006160 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
6161 bd->props.power == FB_BLANK_UNBLANK) ?
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006162 bd->props.brightness : 0;
6163
6164 dbg_printk(TPACPI_DBG_BRGHT,
6165 "backlight: attempt to set level to %d\n",
6166 level);
6167
6168 /* it is the backlight class's job (caller) to handle
6169 * EINTR and other errors properly */
6170 return brightness_set(level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006171}
Holger Macht8acb0252006-10-20 14:30:28 -07006172
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006173static int brightness_get(struct backlight_device *bd)
6174{
6175 int status, res;
6176
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006177 res = mutex_lock_killable(&brightness_mutex);
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006178 if (res < 0)
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006179 return 0;
6180
6181 res = tpacpi_brightness_get_raw(&status);
6182
6183 mutex_unlock(&brightness_mutex);
6184
6185 if (res < 0)
6186 return 0;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006187
6188 return status & TP_EC_BACKLIGHT_LVLMSK;
6189}
6190
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00006191static void tpacpi_brightness_notify_change(void)
6192{
6193 backlight_force_update(ibm_backlight_device,
6194 BACKLIGHT_UPDATE_HOTKEY);
6195}
6196
Richard Purdie599a52d2007-02-10 23:07:48 +00006197static struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006198 .get_brightness = brightness_get,
6199 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006200};
6201
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006202/* --------------------------------------------------------------------- */
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03006203
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006204/*
6205 * These are only useful for models that have only one possibility
6206 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6207 * these quirks.
6208 */
6209#define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6210#define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6211#define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6212
6213static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
6214 /* Models with ATI GPUs known to require ECNVRAM mode */
6215 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
6216
Henrique de Moraes Holschuh6da25bf2009-09-12 15:22:11 -03006217 /* Models with ATI GPUs that can use ECNVRAM */
Henrique de Moraes Holschuh7d1894d2010-02-25 21:28:56 -03006218 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC), /* R50,51 T40-42 */
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006219 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
Henrique de Moraes Holschuh7d1894d2010-02-25 21:28:56 -03006220 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_EC), /* R52 */
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006221 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6222
Henrique de Moraes Holschuh6da25bf2009-09-12 15:22:11 -03006223 /* Models with Intel Extreme Graphics 2 */
Henrique de Moraes Holschuh7d1894d2010-02-25 21:28:56 -03006224 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC), /* X40 */
Henrique de Moraes Holschuha9f8eac2009-12-09 01:36:21 +00006225 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6226 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006227
6228 /* Models with Intel GMA900 */
6229 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
6230 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
6231 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
6232};
6233
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006234static int __init brightness_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006235{
Matthew Garretta19a6ee2010-02-17 16:39:44 -05006236 struct backlight_properties props;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006237 int b;
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006238 unsigned long quirks;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006239
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006240 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
6241
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03006242 mutex_init(&brightness_mutex);
6243
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006244 quirks = tpacpi_check_quirks(brightness_quirk_table,
6245 ARRAY_SIZE(brightness_quirk_table));
6246
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03006247 /*
6248 * We always attempt to detect acpi support, so as to switch
6249 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6250 * going to publish a backlight interface
6251 */
6252 b = tpacpi_check_std_acpi_brightness_support();
6253 if (b > 0) {
Thomas Renninger2dba1b52008-08-01 17:38:03 +02006254
6255 if (acpi_video_backlight_support()) {
6256 if (brightness_enable > 1) {
6257 printk(TPACPI_NOTICE
6258 "Standard ACPI backlight interface "
6259 "available, not loading native one.\n");
6260 return 1;
6261 } else if (brightness_enable == 1) {
6262 printk(TPACPI_NOTICE
6263 "Backlight control force enabled, even if standard "
6264 "ACPI backlight interface is available\n");
6265 }
6266 } else {
6267 if (brightness_enable > 1) {
6268 printk(TPACPI_NOTICE
6269 "Standard ACPI backlight interface not "
6270 "available, thinkpad_acpi native "
6271 "brightness control enabled\n");
6272 }
Henrique de Moraes Holschuhe11e2112007-10-30 17:46:22 -02006273 }
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02006274 }
6275
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03006276 if (!brightness_enable) {
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006277 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03006278 "brightness support disabled by "
6279 "module parameter\n");
6280 return 1;
6281 }
6282
6283 if (b > 16) {
6284 printk(TPACPI_ERR
6285 "Unsupported brightness interface, "
6286 "please contact %s\n", TPACPI_MAIL);
6287 return 1;
6288 }
6289 if (b == 16)
6290 tp_features.bright_16levels = 1;
6291
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006292 /*
6293 * Check for module parameter bogosity, note that we
6294 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
6295 * able to detect "unspecified"
6296 */
6297 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03006298 return -EINVAL;
6299
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006300 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
6301 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
6302 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006303 if (quirks & TPACPI_BRGHT_Q_EC)
6304 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
6305 else
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006306 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
6307
6308 dbg_printk(TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006309 "driver auto-selected brightness_mode=%d\n",
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006310 brightness_mode);
6311 }
6312
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03006313 /* Safety */
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03006314 if (!tpacpi_is_ibm() &&
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03006315 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
6316 brightness_mode == TPACPI_BRGHT_MODE_EC))
6317 return -EINVAL;
6318
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006319 if (tpacpi_brightness_get_raw(&b) < 0)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03006320 return 1;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006321
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02006322 if (tp_features.bright_16levels)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006323 printk(TPACPI_INFO
6324 "detected a 16-level brightness capable ThinkPad\n");
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02006325
Matthew Garretta19a6ee2010-02-17 16:39:44 -05006326 memset(&props, 0, sizeof(struct backlight_properties));
6327 props.max_brightness = (tp_features.bright_16levels) ? 15 : 7;
6328 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 Holschuhe11aecf2008-04-26 01:02:21 -03006350 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006351 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00006352
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00006353 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6354 "brightness: registering brightness hotkeys "
6355 "as change notification\n");
6356 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6357 | TP_ACPI_HKEY_BRGHTUP_MASK
6358 | TP_ACPI_HKEY_BRGHTDWN_MASK);;
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006359 return 0;
6360}
6361
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006362static void brightness_suspend(pm_message_t state)
6363{
6364 tpacpi_brightness_checkpoint_nvram();
6365}
6366
6367static void brightness_shutdown(void)
6368{
6369 tpacpi_brightness_checkpoint_nvram();
6370}
6371
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006372static void brightness_exit(void)
6373{
6374 if (ibm_backlight_device) {
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006375 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006376 "calling backlight_device_unregister()\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006377 backlight_device_unregister(ibm_backlight_device);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006378 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006379
6380 tpacpi_brightness_checkpoint_nvram();
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006381}
6382
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006383static int brightness_read(struct seq_file *m)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006384{
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006385 int level;
6386
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006387 level = brightness_get(NULL);
6388 if (level < 0) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006389 seq_printf(m, "level:\t\tunreadable\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006390 } else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006391 seq_printf(m, "level:\t\t%d\n", level);
6392 seq_printf(m, "commands:\tup, down\n");
6393 seq_printf(m, "commands:\tlevel <level>"
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02006394 " (<level> is 0-%d)\n",
6395 (tp_features.bright_16levels) ? 15 : 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006396 }
6397
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006398 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006399}
6400
6401static int brightness_write(char *buf)
6402{
6403 int level;
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006404 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006405 char *cmd;
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02006406 int max_level = (tp_features.bright_16levels) ? 15 : 7;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006407
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006408 level = brightness_get(NULL);
6409 if (level < 0)
6410 return level;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006411
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006412 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006413 if (strlencmp(cmd, "up") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006414 if (level < max_level)
6415 level++;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006416 } else if (strlencmp(cmd, "down") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006417 if (level > 0)
6418 level--;
6419 } else if (sscanf(cmd, "level %d", &level) == 1 &&
6420 level >= 0 && level <= max_level) {
6421 /* new level set */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006422 } else
6423 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006424 }
6425
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006426 tpacpi_disclose_usertask("procfs brightness",
6427 "set level to %d\n", level);
6428
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006429 /*
6430 * Now we know what the final level should be, so we try to set it.
6431 * Doing it this way makes the syscall restartable in case of EINTR
6432 */
6433 rc = brightness_set(level);
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00006434 if (!rc && ibm_backlight_device)
6435 backlight_force_update(ibm_backlight_device,
6436 BACKLIGHT_UPDATE_SYSFS);
Roel Kluin80a8d122009-11-20 19:48:23 +01006437 return (rc == -EINTR)? -ERESTARTSYS : rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006438}
6439
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006440static struct ibm_struct brightness_driver_data = {
6441 .name = "brightness",
6442 .read = brightness_read,
6443 .write = brightness_write,
6444 .exit = brightness_exit,
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006445 .suspend = brightness_suspend,
6446 .shutdown = brightness_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006447};
6448
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006449/*************************************************************************
6450 * Volume subdriver
6451 */
6452
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006453/*
6454 * IBM ThinkPads have a simple volume controller with MUTE gating.
6455 * Very early Lenovo ThinkPads follow the IBM ThinkPad spec.
6456 *
6457 * Since the *61 series (and probably also the later *60 series), Lenovo
6458 * ThinkPads only implement the MUTE gate.
6459 *
6460 * EC register 0x30
6461 * Bit 6: MUTE (1 mutes sound)
6462 * Bit 3-0: Volume
6463 * Other bits should be zero as far as we know.
6464 *
6465 * This is also stored in CMOS NVRAM, byte 0x60, bit 6 (MUTE), and
6466 * bits 3-0 (volume). Other bits in NVRAM may have other functions,
6467 * such as bit 7 which is used to detect repeated presses of MUTE,
6468 * and we leave them unchanged.
6469 */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006470
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02006471#ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
6472
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006473#define TPACPI_ALSA_DRVNAME "ThinkPad EC"
6474#define TPACPI_ALSA_SHRTNAME "ThinkPad Console Audio Control"
6475#define TPACPI_ALSA_MIXERNAME TPACPI_ALSA_SHRTNAME
6476
Henrique de Moraes Holschuhead510c2009-12-26 22:52:13 -02006477static int alsa_index = ~((1 << (SNDRV_CARDS - 3)) - 1); /* last three slots */
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006478static char *alsa_id = "ThinkPadEC";
6479static int alsa_enable = SNDRV_DEFAULT_ENABLE1;
6480
6481struct tpacpi_alsa_data {
6482 struct snd_card *card;
6483 struct snd_ctl_elem_id *ctl_mute_id;
6484 struct snd_ctl_elem_id *ctl_vol_id;
6485};
6486
6487static struct snd_card *alsa_card;
6488
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006489enum {
6490 TP_EC_AUDIO = 0x30,
6491
6492 /* TP_EC_AUDIO bits */
6493 TP_EC_AUDIO_MUTESW = 6,
6494
6495 /* TP_EC_AUDIO bitmasks */
6496 TP_EC_AUDIO_LVL_MSK = 0x0F,
6497 TP_EC_AUDIO_MUTESW_MSK = (1 << TP_EC_AUDIO_MUTESW),
6498
6499 /* Maximum volume */
6500 TP_EC_VOLUME_MAX = 14,
6501};
6502
6503enum tpacpi_volume_access_mode {
6504 TPACPI_VOL_MODE_AUTO = 0, /* Not implemented yet */
6505 TPACPI_VOL_MODE_EC, /* Pure EC control */
6506 TPACPI_VOL_MODE_UCMS_STEP, /* UCMS step-based control: N/A */
6507 TPACPI_VOL_MODE_ECNVRAM, /* EC control w/ NVRAM store */
6508 TPACPI_VOL_MODE_MAX
6509};
6510
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006511enum tpacpi_volume_capabilities {
6512 TPACPI_VOL_CAP_AUTO = 0, /* Use white/blacklist */
6513 TPACPI_VOL_CAP_VOLMUTE, /* Output vol and mute */
6514 TPACPI_VOL_CAP_MUTEONLY, /* Output mute only */
6515 TPACPI_VOL_CAP_MAX
6516};
6517
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006518static enum tpacpi_volume_access_mode volume_mode =
6519 TPACPI_VOL_MODE_MAX;
6520
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006521static enum tpacpi_volume_capabilities volume_capabilities;
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006522static int volume_control_allowed;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006523
6524/*
6525 * Used to syncronize writers to TP_EC_AUDIO and
6526 * TP_NVRAM_ADDR_MIXER, as we need to do read-modify-write
6527 */
6528static struct mutex volume_mutex;
6529
6530static void tpacpi_volume_checkpoint_nvram(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006531{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006532 u8 lec = 0;
6533 u8 b_nvram;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006534 u8 ec_mask;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006535
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006536 if (volume_mode != TPACPI_VOL_MODE_ECNVRAM)
6537 return;
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006538 if (!volume_control_allowed)
6539 return;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006540
6541 vdbg_printk(TPACPI_DBG_MIXER,
6542 "trying to checkpoint mixer state to NVRAM...\n");
6543
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006544 if (tp_features.mixer_no_level_control)
6545 ec_mask = TP_EC_AUDIO_MUTESW_MSK;
6546 else
6547 ec_mask = TP_EC_AUDIO_MUTESW_MSK | TP_EC_AUDIO_LVL_MSK;
6548
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006549 if (mutex_lock_killable(&volume_mutex) < 0)
6550 return;
6551
6552 if (unlikely(!acpi_ec_read(TP_EC_AUDIO, &lec)))
6553 goto unlock;
6554 lec &= ec_mask;
6555 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
6556
6557 if (lec != (b_nvram & ec_mask)) {
6558 /* NVRAM needs update */
6559 b_nvram &= ~ec_mask;
6560 b_nvram |= lec;
6561 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_MIXER);
6562 dbg_printk(TPACPI_DBG_MIXER,
6563 "updated NVRAM mixer status to 0x%02x (0x%02x)\n",
6564 (unsigned int) lec, (unsigned int) b_nvram);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006565 } else {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006566 vdbg_printk(TPACPI_DBG_MIXER,
6567 "NVRAM mixer status already is 0x%02x (0x%02x)\n",
6568 (unsigned int) lec, (unsigned int) b_nvram);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006569 }
6570
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006571unlock:
6572 mutex_unlock(&volume_mutex);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006573}
6574
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006575static int volume_get_status_ec(u8 *status)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006576{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006577 u8 s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006578
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006579 if (!acpi_ec_read(TP_EC_AUDIO, &s))
6580 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006581
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006582 *status = s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006583
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006584 dbg_printk(TPACPI_DBG_MIXER, "status 0x%02x\n", s);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006585
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006586 return 0;
6587}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006588
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006589static int volume_get_status(u8 *status)
6590{
6591 return volume_get_status_ec(status);
6592}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006593
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006594static int volume_set_status_ec(const u8 status)
6595{
6596 if (!acpi_ec_write(TP_EC_AUDIO, status))
6597 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006598
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006599 dbg_printk(TPACPI_DBG_MIXER, "set EC mixer to 0x%02x\n", status);
6600
6601 return 0;
6602}
6603
6604static int volume_set_status(const u8 status)
6605{
6606 return volume_set_status_ec(status);
6607}
6608
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006609/* returns < 0 on error, 0 on no change, 1 on change */
6610static int __volume_set_mute_ec(const bool mute)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006611{
6612 int rc;
6613 u8 s, n;
6614
6615 if (mutex_lock_killable(&volume_mutex) < 0)
6616 return -EINTR;
6617
6618 rc = volume_get_status_ec(&s);
6619 if (rc)
6620 goto unlock;
6621
6622 n = (mute) ? s | TP_EC_AUDIO_MUTESW_MSK :
6623 s & ~TP_EC_AUDIO_MUTESW_MSK;
6624
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006625 if (n != s) {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006626 rc = volume_set_status_ec(n);
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006627 if (!rc)
6628 rc = 1;
6629 }
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006630
6631unlock:
6632 mutex_unlock(&volume_mutex);
6633 return rc;
6634}
6635
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006636static int volume_alsa_set_mute(const bool mute)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006637{
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006638 dbg_printk(TPACPI_DBG_MIXER, "ALSA: trying to %smute\n",
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006639 (mute) ? "" : "un");
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006640 return __volume_set_mute_ec(mute);
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006641}
6642
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006643static int volume_set_mute(const bool mute)
6644{
6645 int rc;
6646
6647 dbg_printk(TPACPI_DBG_MIXER, "trying to %smute\n",
6648 (mute) ? "" : "un");
6649
6650 rc = __volume_set_mute_ec(mute);
6651 return (rc < 0) ? rc : 0;
6652}
6653
6654/* returns < 0 on error, 0 on no change, 1 on change */
6655static int __volume_set_volume_ec(const u8 vol)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006656{
6657 int rc;
6658 u8 s, n;
6659
6660 if (vol > TP_EC_VOLUME_MAX)
6661 return -EINVAL;
6662
6663 if (mutex_lock_killable(&volume_mutex) < 0)
6664 return -EINTR;
6665
6666 rc = volume_get_status_ec(&s);
6667 if (rc)
6668 goto unlock;
6669
6670 n = (s & ~TP_EC_AUDIO_LVL_MSK) | vol;
6671
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006672 if (n != s) {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006673 rc = volume_set_status_ec(n);
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006674 if (!rc)
6675 rc = 1;
6676 }
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006677
6678unlock:
6679 mutex_unlock(&volume_mutex);
6680 return rc;
6681}
6682
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006683static int volume_alsa_set_volume(const u8 vol)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006684{
6685 dbg_printk(TPACPI_DBG_MIXER,
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006686 "ALSA: trying to set volume level to %hu\n", vol);
6687 return __volume_set_volume_ec(vol);
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006688}
6689
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006690static void volume_alsa_notify_change(void)
6691{
6692 struct tpacpi_alsa_data *d;
6693
6694 if (alsa_card && alsa_card->private_data) {
6695 d = alsa_card->private_data;
6696 if (d->ctl_mute_id)
6697 snd_ctl_notify(alsa_card,
6698 SNDRV_CTL_EVENT_MASK_VALUE,
6699 d->ctl_mute_id);
6700 if (d->ctl_vol_id)
6701 snd_ctl_notify(alsa_card,
6702 SNDRV_CTL_EVENT_MASK_VALUE,
6703 d->ctl_vol_id);
6704 }
6705}
6706
6707static int volume_alsa_vol_info(struct snd_kcontrol *kcontrol,
6708 struct snd_ctl_elem_info *uinfo)
6709{
6710 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
6711 uinfo->count = 1;
6712 uinfo->value.integer.min = 0;
6713 uinfo->value.integer.max = TP_EC_VOLUME_MAX;
6714 return 0;
6715}
6716
6717static int volume_alsa_vol_get(struct snd_kcontrol *kcontrol,
6718 struct snd_ctl_elem_value *ucontrol)
6719{
6720 u8 s;
6721 int rc;
6722
6723 rc = volume_get_status(&s);
6724 if (rc < 0)
6725 return rc;
6726
6727 ucontrol->value.integer.value[0] = s & TP_EC_AUDIO_LVL_MSK;
6728 return 0;
6729}
6730
6731static int volume_alsa_vol_put(struct snd_kcontrol *kcontrol,
6732 struct snd_ctl_elem_value *ucontrol)
6733{
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006734 return volume_alsa_set_volume(ucontrol->value.integer.value[0]);
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006735}
6736
6737#define volume_alsa_mute_info snd_ctl_boolean_mono_info
6738
6739static int volume_alsa_mute_get(struct snd_kcontrol *kcontrol,
6740 struct snd_ctl_elem_value *ucontrol)
6741{
6742 u8 s;
6743 int rc;
6744
6745 rc = volume_get_status(&s);
6746 if (rc < 0)
6747 return rc;
6748
6749 ucontrol->value.integer.value[0] =
6750 (s & TP_EC_AUDIO_MUTESW_MSK) ? 0 : 1;
6751 return 0;
6752}
6753
6754static int volume_alsa_mute_put(struct snd_kcontrol *kcontrol,
6755 struct snd_ctl_elem_value *ucontrol)
6756{
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006757 return volume_alsa_set_mute(!ucontrol->value.integer.value[0]);
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006758}
6759
6760static struct snd_kcontrol_new volume_alsa_control_vol __devinitdata = {
6761 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6762 .name = "Console Playback Volume",
6763 .index = 0,
6764 .access = SNDRV_CTL_ELEM_ACCESS_READ,
6765 .info = volume_alsa_vol_info,
6766 .get = volume_alsa_vol_get,
6767};
6768
6769static struct snd_kcontrol_new volume_alsa_control_mute __devinitdata = {
6770 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6771 .name = "Console Playback Switch",
6772 .index = 0,
6773 .access = SNDRV_CTL_ELEM_ACCESS_READ,
6774 .info = volume_alsa_mute_info,
6775 .get = volume_alsa_mute_get,
6776};
6777
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006778static void volume_suspend(pm_message_t state)
6779{
6780 tpacpi_volume_checkpoint_nvram();
6781}
6782
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006783static void volume_resume(void)
6784{
6785 volume_alsa_notify_change();
6786}
6787
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006788static void volume_shutdown(void)
6789{
6790 tpacpi_volume_checkpoint_nvram();
6791}
6792
6793static void volume_exit(void)
6794{
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006795 if (alsa_card) {
6796 snd_card_free(alsa_card);
6797 alsa_card = NULL;
6798 }
6799
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006800 tpacpi_volume_checkpoint_nvram();
6801}
6802
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006803static int __init volume_create_alsa_mixer(void)
6804{
6805 struct snd_card *card;
6806 struct tpacpi_alsa_data *data;
6807 struct snd_kcontrol *ctl_vol;
6808 struct snd_kcontrol *ctl_mute;
6809 int rc;
6810
6811 rc = snd_card_create(alsa_index, alsa_id, THIS_MODULE,
6812 sizeof(struct tpacpi_alsa_data), &card);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02006813 if (rc < 0 || !card) {
6814 printk(TPACPI_ERR
6815 "Failed to create ALSA card structures: %d\n", rc);
6816 return 1;
6817 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006818
6819 BUG_ON(!card->private_data);
6820 data = card->private_data;
6821 data->card = card;
6822
6823 strlcpy(card->driver, TPACPI_ALSA_DRVNAME,
6824 sizeof(card->driver));
6825 strlcpy(card->shortname, TPACPI_ALSA_SHRTNAME,
6826 sizeof(card->shortname));
6827 snprintf(card->mixername, sizeof(card->mixername), "ThinkPad EC %s",
6828 (thinkpad_id.ec_version_str) ?
6829 thinkpad_id.ec_version_str : "(unknown)");
6830 snprintf(card->longname, sizeof(card->longname),
6831 "%s at EC reg 0x%02x, fw %s", card->shortname, TP_EC_AUDIO,
6832 (thinkpad_id.ec_version_str) ?
6833 thinkpad_id.ec_version_str : "unknown");
6834
6835 if (volume_control_allowed) {
6836 volume_alsa_control_vol.put = volume_alsa_vol_put;
6837 volume_alsa_control_vol.access =
6838 SNDRV_CTL_ELEM_ACCESS_READWRITE;
6839
6840 volume_alsa_control_mute.put = volume_alsa_mute_put;
6841 volume_alsa_control_mute.access =
6842 SNDRV_CTL_ELEM_ACCESS_READWRITE;
6843 }
6844
6845 if (!tp_features.mixer_no_level_control) {
6846 ctl_vol = snd_ctl_new1(&volume_alsa_control_vol, NULL);
6847 rc = snd_ctl_add(card, ctl_vol);
6848 if (rc < 0) {
6849 printk(TPACPI_ERR
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02006850 "Failed to create ALSA volume control: %d\n",
6851 rc);
6852 goto err_exit;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006853 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006854 data->ctl_vol_id = &ctl_vol->id;
6855 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006856
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006857 ctl_mute = snd_ctl_new1(&volume_alsa_control_mute, NULL);
6858 rc = snd_ctl_add(card, ctl_mute);
6859 if (rc < 0) {
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02006860 printk(TPACPI_ERR "Failed to create ALSA mute control: %d\n",
6861 rc);
6862 goto err_exit;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006863 }
6864 data->ctl_mute_id = &ctl_mute->id;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006865
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006866 snd_card_set_dev(card, &tpacpi_pdev->dev);
6867 rc = snd_card_register(card);
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006868 if (rc < 0) {
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02006869 printk(TPACPI_ERR "Failed to register ALSA card: %d\n", rc);
6870 goto err_exit;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006871 }
6872
6873 alsa_card = card;
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02006874 return 0;
6875
6876err_exit:
6877 snd_card_free(card);
6878 return 1;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006879}
6880
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006881#define TPACPI_VOL_Q_MUTEONLY 0x0001 /* Mute-only control available */
6882#define TPACPI_VOL_Q_LEVEL 0x0002 /* Volume control available */
6883
6884static const struct tpacpi_quirk volume_quirk_table[] __initconst = {
6885 /* Whitelist volume level on all IBM by default */
6886 { .vendor = PCI_VENDOR_ID_IBM,
6887 .bios = TPACPI_MATCH_ANY,
6888 .ec = TPACPI_MATCH_ANY,
6889 .quirks = TPACPI_VOL_Q_LEVEL },
6890
6891 /* Lenovo models with volume control (needs confirmation) */
6892 TPACPI_QEC_LNV('7', 'C', TPACPI_VOL_Q_LEVEL), /* R60/i */
6893 TPACPI_QEC_LNV('7', 'E', TPACPI_VOL_Q_LEVEL), /* R60e/i */
6894 TPACPI_QEC_LNV('7', '9', TPACPI_VOL_Q_LEVEL), /* T60/p */
6895 TPACPI_QEC_LNV('7', 'B', TPACPI_VOL_Q_LEVEL), /* X60/s */
6896 TPACPI_QEC_LNV('7', 'J', TPACPI_VOL_Q_LEVEL), /* X60t */
6897 TPACPI_QEC_LNV('7', '7', TPACPI_VOL_Q_LEVEL), /* Z60 */
6898 TPACPI_QEC_LNV('7', 'F', TPACPI_VOL_Q_LEVEL), /* Z61 */
6899
6900 /* Whitelist mute-only on all Lenovo by default */
6901 { .vendor = PCI_VENDOR_ID_LENOVO,
6902 .bios = TPACPI_MATCH_ANY,
6903 .ec = TPACPI_MATCH_ANY,
6904 .quirks = TPACPI_VOL_Q_MUTEONLY }
6905};
6906
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006907static int __init volume_init(struct ibm_init_struct *iibm)
6908{
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006909 unsigned long quirks;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006910 int rc;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006911
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006912 vdbg_printk(TPACPI_DBG_INIT, "initializing volume subdriver\n");
6913
6914 mutex_init(&volume_mutex);
6915
6916 /*
6917 * Check for module parameter bogosity, note that we
6918 * init volume_mode to TPACPI_VOL_MODE_MAX in order to be
6919 * able to detect "unspecified"
6920 */
6921 if (volume_mode > TPACPI_VOL_MODE_MAX)
6922 return -EINVAL;
6923
6924 if (volume_mode == TPACPI_VOL_MODE_UCMS_STEP) {
6925 printk(TPACPI_ERR
6926 "UCMS step volume mode not implemented, "
6927 "please contact %s\n", TPACPI_MAIL);
6928 return 1;
6929 }
6930
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006931 if (volume_capabilities >= TPACPI_VOL_CAP_MAX)
6932 return -EINVAL;
6933
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006934 /*
6935 * The ALSA mixer is our primary interface.
6936 * When disabled, don't install the subdriver at all
6937 */
6938 if (!alsa_enable) {
6939 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6940 "ALSA mixer disabled by parameter, "
6941 "not loading volume subdriver...\n");
6942 return 1;
6943 }
6944
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006945 quirks = tpacpi_check_quirks(volume_quirk_table,
6946 ARRAY_SIZE(volume_quirk_table));
6947
6948 switch (volume_capabilities) {
6949 case TPACPI_VOL_CAP_AUTO:
6950 if (quirks & TPACPI_VOL_Q_MUTEONLY)
6951 tp_features.mixer_no_level_control = 1;
6952 else if (quirks & TPACPI_VOL_Q_LEVEL)
6953 tp_features.mixer_no_level_control = 0;
6954 else
6955 return 1; /* no mixer */
6956 break;
6957 case TPACPI_VOL_CAP_VOLMUTE:
6958 tp_features.mixer_no_level_control = 0;
6959 break;
6960 case TPACPI_VOL_CAP_MUTEONLY:
6961 tp_features.mixer_no_level_control = 1;
6962 break;
6963 default:
6964 return 1;
6965 }
6966
6967 if (volume_capabilities != TPACPI_VOL_CAP_AUTO)
6968 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6969 "using user-supplied volume_capabilities=%d\n",
6970 volume_capabilities);
6971
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006972 if (volume_mode == TPACPI_VOL_MODE_AUTO ||
6973 volume_mode == TPACPI_VOL_MODE_MAX) {
6974 volume_mode = TPACPI_VOL_MODE_ECNVRAM;
6975
6976 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6977 "driver auto-selected volume_mode=%d\n",
6978 volume_mode);
6979 } else {
6980 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6981 "using user-supplied volume_mode=%d\n",
6982 volume_mode);
6983 }
6984
6985 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006986 "mute is supported, volume control is %s\n",
6987 str_supported(!tp_features.mixer_no_level_control));
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006988
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006989 rc = volume_create_alsa_mixer();
6990 if (rc) {
6991 printk(TPACPI_ERR
6992 "Could not create the ALSA mixer interface\n");
6993 return rc;
6994 }
6995
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006996 printk(TPACPI_INFO
6997 "Console audio control enabled, mode: %s\n",
6998 (volume_control_allowed) ?
6999 "override (read/write)" :
7000 "monitor (read only)");
7001
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007002 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7003 "registering volume hotkeys as change notification\n");
7004 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
7005 | TP_ACPI_HKEY_VOLUP_MASK
7006 | TP_ACPI_HKEY_VOLDWN_MASK
7007 | TP_ACPI_HKEY_MUTE_MASK);
7008
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007009 return 0;
7010}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007011
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007012static int volume_read(struct seq_file *m)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007013{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007014 u8 status;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007015
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007016 if (volume_get_status(&status) < 0) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007017 seq_printf(m, "level:\t\tunreadable\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007018 } else {
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007019 if (tp_features.mixer_no_level_control)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007020 seq_printf(m, "level:\t\tunsupported\n");
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007021 else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007022 seq_printf(m, "level:\t\t%d\n",
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007023 status & TP_EC_AUDIO_LVL_MSK);
7024
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007025 seq_printf(m, "mute:\t\t%s\n",
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007026 onoff(status, TP_EC_AUDIO_MUTESW));
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007027
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007028 if (volume_control_allowed) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007029 seq_printf(m, "commands:\tunmute, mute\n");
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007030 if (!tp_features.mixer_no_level_control) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007031 seq_printf(m,
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007032 "commands:\tup, down\n");
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007033 seq_printf(m,
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007034 "commands:\tlevel <level>"
7035 " (<level> is 0-%d)\n",
7036 TP_EC_VOLUME_MAX);
7037 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007038 }
7039 }
7040
7041 return 0;
7042}
7043
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007044static int volume_write(char *buf)
7045{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007046 u8 s;
7047 u8 new_level, new_mute;
7048 int l;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007049 char *cmd;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007050 int rc;
7051
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007052 /*
7053 * We do allow volume control at driver startup, so that the
7054 * user can set initial state through the volume=... parameter hack.
7055 */
7056 if (!volume_control_allowed && tpacpi_lifecycle != TPACPI_LIFE_INIT) {
7057 if (unlikely(!tp_warned.volume_ctrl_forbidden)) {
7058 tp_warned.volume_ctrl_forbidden = 1;
7059 printk(TPACPI_NOTICE
7060 "Console audio control in monitor mode, "
7061 "changes are not allowed.\n");
7062 printk(TPACPI_NOTICE
7063 "Use the volume_control=1 module parameter "
7064 "to enable volume control\n");
7065 }
7066 return -EPERM;
7067 }
7068
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007069 rc = volume_get_status(&s);
7070 if (rc < 0)
7071 return rc;
7072
7073 new_level = s & TP_EC_AUDIO_LVL_MSK;
7074 new_mute = s & TP_EC_AUDIO_MUTESW_MSK;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007075
7076 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007077 if (!tp_features.mixer_no_level_control) {
7078 if (strlencmp(cmd, "up") == 0) {
7079 if (new_mute)
7080 new_mute = 0;
7081 else if (new_level < TP_EC_VOLUME_MAX)
7082 new_level++;
7083 continue;
7084 } else if (strlencmp(cmd, "down") == 0) {
7085 if (new_mute)
7086 new_mute = 0;
7087 else if (new_level > 0)
7088 new_level--;
7089 continue;
7090 } else if (sscanf(cmd, "level %u", &l) == 1 &&
7091 l >= 0 && l <= TP_EC_VOLUME_MAX) {
7092 new_level = l;
7093 continue;
7094 }
7095 }
7096 if (strlencmp(cmd, "mute") == 0)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007097 new_mute = TP_EC_AUDIO_MUTESW_MSK;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007098 else if (strlencmp(cmd, "unmute") == 0)
7099 new_mute = 0;
7100 else
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007101 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007102 }
7103
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007104 if (tp_features.mixer_no_level_control) {
7105 tpacpi_disclose_usertask("procfs volume", "%smute\n",
7106 new_mute ? "" : "un");
7107 rc = volume_set_mute(!!new_mute);
7108 } else {
7109 tpacpi_disclose_usertask("procfs volume",
7110 "%smute and set level to %d\n",
7111 new_mute ? "" : "un", new_level);
7112 rc = volume_set_status(new_mute | new_level);
7113 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007114 volume_alsa_notify_change();
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007115
7116 return (rc == -EINTR) ? -ERESTARTSYS : rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007117}
7118
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007119static struct ibm_struct volume_driver_data = {
7120 .name = "volume",
7121 .read = volume_read,
7122 .write = volume_write,
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007123 .exit = volume_exit,
7124 .suspend = volume_suspend,
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007125 .resume = volume_resume,
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007126 .shutdown = volume_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007127};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007128
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02007129#else /* !CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7130
7131#define alsa_card NULL
7132
7133static void inline volume_alsa_notify_change(void)
7134{
7135}
7136
7137static int __init volume_init(struct ibm_init_struct *iibm)
7138{
7139 printk(TPACPI_INFO
7140 "volume: disabled as there is no ALSA support in this kernel\n");
7141
7142 return 1;
7143}
7144
7145static struct ibm_struct volume_driver_data = {
7146 .name = "volume",
7147};
7148
7149#endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7150
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007151/*************************************************************************
7152 * Fan subdriver
7153 */
7154
7155/*
7156 * FAN ACCESS MODES
7157 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007158 * TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007159 * ACPI GFAN method: returns fan level
7160 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007161 * see TPACPI_FAN_WR_ACPI_SFAN
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007162 * EC 0x2f (HFSP) not available if GFAN exists
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007163 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007164 * TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007165 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
7166 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007167 * EC 0x2f (HFSP) might be available *for reading*, but do not use
7168 * it for writing.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007169 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007170 * TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007171 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
7172 * Supported on almost all ThinkPads
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007173 *
7174 * Fan speed changes of any sort (including those caused by the
7175 * disengaged mode) are usually done slowly by the firmware as the
Uwe Kleine-König9ddc5b62010-01-20 17:02:24 +01007176 * maximum amount of fan duty cycle change per second seems to be
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007177 * limited.
7178 *
7179 * Reading is not available if GFAN exists.
7180 * Writing is not available if SFAN exists.
7181 *
7182 * Bits
7183 * 7 automatic mode engaged;
7184 * (default operation mode of the ThinkPad)
7185 * fan level is ignored in this mode.
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007186 * 6 full speed mode (takes precedence over bit 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007187 * not available on all thinkpads. May disable
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007188 * the tachometer while the fan controller ramps up
7189 * the speed (which can take up to a few *minutes*).
7190 * Speeds up fan to 100% duty-cycle, which is far above
7191 * the standard RPM levels. It is not impossible that
7192 * it could cause hardware damage.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007193 * 5-3 unused in some models. Extra bits for fan level
7194 * in others, but still useless as all values above
7195 * 7 map to the same speed as level 7 in these models.
7196 * 2-0 fan level (0..7 usually)
7197 * 0x00 = stop
7198 * 0x07 = max (set when temperatures critical)
7199 * Some ThinkPads may have other levels, see
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007200 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007201 *
7202 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
7203 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
7204 * does so, its initial value is meaningless (0x07).
7205 *
7206 * For firmware bugs, refer to:
7207 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7208 *
7209 * ----
7210 *
7211 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
7212 * Main fan tachometer reading (in RPM)
7213 *
7214 * This register is present on all ThinkPads with a new-style EC, and
7215 * it is known not to be present on the A21m/e, and T22, as there is
7216 * something else in offset 0x84 according to the ACPI DSDT. Other
7217 * ThinkPads from this same time period (and earlier) probably lack the
7218 * tachometer as well.
7219 *
Nick Andrew877d0312009-01-26 11:06:57 +01007220 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007221 * was never fixed by IBM to report the EC firmware version string
7222 * probably support the tachometer (like the early X models), so
7223 * detecting it is quite hard. We need more data to know for sure.
7224 *
7225 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
7226 * might result.
7227 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007228 * FIRMWARE BUG: may go stale while the EC is switching to full speed
7229 * mode.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007230 *
7231 * For firmware bugs, refer to:
7232 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7233 *
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007234 * ----
7235 *
7236 * ThinkPad EC register 0x31 bit 0 (only on select models)
7237 *
7238 * When bit 0 of EC register 0x31 is zero, the tachometer registers
7239 * show the speed of the main fan. When bit 0 of EC register 0x31
7240 * is one, the tachometer registers show the speed of the auxiliary
7241 * fan.
7242 *
7243 * Fan control seems to affect both fans, regardless of the state
7244 * of this bit.
7245 *
7246 * So far, only the firmware for the X60/X61 non-tablet versions
7247 * seem to support this (firmware TP-7M).
7248 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007249 * TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007250 * ThinkPad X31, X40, X41. Not available in the X60.
7251 *
7252 * FANS ACPI handle: takes three arguments: low speed, medium speed,
7253 * high speed. ACPI DSDT seems to map these three speeds to levels
7254 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
7255 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
7256 *
7257 * The speeds are stored on handles
7258 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
7259 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007260 * There are three default speed sets, accessible as handles:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007261 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
7262 *
7263 * ACPI DSDT switches which set is in use depending on various
7264 * factors.
7265 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007266 * TPACPI_FAN_WR_TPEC is also available and should be used to
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007267 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
7268 * but the ACPI tables just mention level 7.
7269 */
7270
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007271enum { /* Fan control constants */
7272 fan_status_offset = 0x2f, /* EC register 0x2f */
7273 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
7274 * 0x84 must be read before 0x85 */
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007275 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
7276 bit 0 selects which fan is active */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007277
7278 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
7279 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
7280
7281 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
7282};
7283
7284enum fan_status_access_mode {
7285 TPACPI_FAN_NONE = 0, /* No fan status or control */
7286 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
7287 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
7288};
7289
7290enum fan_control_access_mode {
7291 TPACPI_FAN_WR_NONE = 0, /* No fan control */
7292 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
7293 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
7294 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
7295};
7296
7297enum fan_control_commands {
7298 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
7299 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
7300 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
7301 * and also watchdog cmd */
7302};
7303
7304static int fan_control_allowed;
7305
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007306static enum fan_status_access_mode fan_status_access_mode;
7307static enum fan_control_access_mode fan_control_access_mode;
7308static enum fan_control_commands fan_control_commands;
7309
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02007310static u8 fan_control_initial_status;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007311static u8 fan_control_desired_level;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007312static u8 fan_control_resume_level;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007313static int fan_watchdog_maxinterval;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007314
7315static struct mutex fan_mutex;
7316
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007317static void fan_watchdog_fire(struct work_struct *ignored);
Len Brown25c68a32006-12-08 04:43:41 -05007318static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007319
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007320TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
7321TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007322 "\\FSPD", /* 600e/x, 770e, 770x */
7323 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007324TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007325 "JFNS", /* 770x-JL */
7326 ); /* all others */
7327
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007328/*
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007329 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
7330 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
7331 * be in auto mode (0x80).
7332 *
7333 * This is corrected by any write to HFSP either by the driver, or
7334 * by the firmware.
7335 *
7336 * We assume 0x07 really means auto mode while this quirk is active,
7337 * as this is far more likely than the ThinkPad being in level 7,
7338 * which is only used by the firmware during thermal emergencies.
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007339 *
7340 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
7341 * TP-70 (T43, R52), which are known to be buggy.
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007342 */
7343
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007344static void fan_quirk1_setup(void)
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007345{
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007346 if (fan_control_initial_status == 0x07) {
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007347 printk(TPACPI_NOTICE
7348 "fan_init: initial fan status is unknown, "
7349 "assuming it is in auto mode\n");
7350 tp_features.fan_ctrl_status_undef = 1;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007351 }
7352}
7353
7354static void fan_quirk1_handle(u8 *fan_status)
7355{
7356 if (unlikely(tp_features.fan_ctrl_status_undef)) {
7357 if (*fan_status != fan_control_initial_status) {
7358 /* something changed the HFSP regisnter since
7359 * driver init time, so it is not undefined
7360 * anymore */
7361 tp_features.fan_ctrl_status_undef = 0;
7362 } else {
7363 /* Return most likely status. In fact, it
7364 * might be the only possible status */
7365 *fan_status = TP_EC_FAN_AUTO;
7366 }
7367 }
7368}
7369
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007370/* Select main fan on X60/X61, NOOP on others */
7371static bool fan_select_fan1(void)
7372{
7373 if (tp_features.second_fan) {
7374 u8 val;
7375
7376 if (ec_read(fan_select_offset, &val) < 0)
7377 return false;
7378 val &= 0xFEU;
7379 if (ec_write(fan_select_offset, val) < 0)
7380 return false;
7381 }
7382 return true;
7383}
7384
7385/* Select secondary fan on X60/X61 */
7386static bool fan_select_fan2(void)
7387{
7388 u8 val;
7389
7390 if (!tp_features.second_fan)
7391 return false;
7392
7393 if (ec_read(fan_select_offset, &val) < 0)
7394 return false;
7395 val |= 0x01U;
7396 if (ec_write(fan_select_offset, val) < 0)
7397 return false;
7398
7399 return true;
7400}
7401
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007402/*
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007403 * Call with fan_mutex held
7404 */
7405static void fan_update_desired_level(u8 status)
7406{
7407 if ((status &
7408 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7409 if (status > 7)
7410 fan_control_desired_level = 7;
7411 else
7412 fan_control_desired_level = status;
7413 }
7414}
7415
7416static int fan_get_status(u8 *status)
7417{
7418 u8 s;
7419
7420 /* TODO:
7421 * Add TPACPI_FAN_RD_ACPI_FANS ? */
7422
7423 switch (fan_status_access_mode) {
7424 case TPACPI_FAN_RD_ACPI_GFAN:
7425 /* 570, 600e/x, 770e, 770x */
7426
7427 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
7428 return -EIO;
7429
7430 if (likely(status))
7431 *status = s & 0x07;
7432
7433 break;
7434
7435 case TPACPI_FAN_RD_TPEC:
7436 /* all except 570, 600e/x, 770e, 770x */
7437 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
7438 return -EIO;
7439
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007440 if (likely(status)) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007441 *status = s;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007442 fan_quirk1_handle(status);
7443 }
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007444
7445 break;
7446
7447 default:
7448 return -ENXIO;
7449 }
7450
7451 return 0;
7452}
7453
7454static int fan_get_status_safe(u8 *status)
7455{
7456 int rc;
7457 u8 s;
7458
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007459 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007460 return -ERESTARTSYS;
7461 rc = fan_get_status(&s);
7462 if (!rc)
7463 fan_update_desired_level(s);
7464 mutex_unlock(&fan_mutex);
7465
7466 if (status)
7467 *status = s;
7468
7469 return rc;
7470}
7471
7472static int fan_get_speed(unsigned int *speed)
7473{
7474 u8 hi, lo;
7475
7476 switch (fan_status_access_mode) {
7477 case TPACPI_FAN_RD_TPEC:
7478 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007479 if (unlikely(!fan_select_fan1()))
7480 return -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007481 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
7482 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
7483 return -EIO;
7484
7485 if (likely(speed))
7486 *speed = (hi << 8) | lo;
7487
7488 break;
7489
7490 default:
7491 return -ENXIO;
7492 }
7493
7494 return 0;
7495}
7496
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007497static int fan2_get_speed(unsigned int *speed)
7498{
7499 u8 hi, lo;
7500 bool rc;
7501
7502 switch (fan_status_access_mode) {
7503 case TPACPI_FAN_RD_TPEC:
7504 /* all except 570, 600e/x, 770e, 770x */
7505 if (unlikely(!fan_select_fan2()))
7506 return -EIO;
7507 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
7508 !acpi_ec_read(fan_rpm_offset + 1, &hi);
7509 fan_select_fan1(); /* play it safe */
7510 if (rc)
7511 return -EIO;
7512
7513 if (likely(speed))
7514 *speed = (hi << 8) | lo;
7515
7516 break;
7517
7518 default:
7519 return -ENXIO;
7520 }
7521
7522 return 0;
7523}
7524
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007525static int fan_set_level(int level)
7526{
7527 if (!fan_control_allowed)
7528 return -EPERM;
7529
7530 switch (fan_control_access_mode) {
7531 case TPACPI_FAN_WR_ACPI_SFAN:
7532 if (level >= 0 && level <= 7) {
7533 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
7534 return -EIO;
7535 } else
7536 return -EINVAL;
7537 break;
7538
7539 case TPACPI_FAN_WR_ACPI_FANS:
7540 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007541 if (!(level & TP_EC_FAN_AUTO) &&
7542 !(level & TP_EC_FAN_FULLSPEED) &&
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007543 ((level < 0) || (level > 7)))
7544 return -EINVAL;
7545
7546 /* safety net should the EC not support AUTO
7547 * or FULLSPEED mode bits and just ignore them */
7548 if (level & TP_EC_FAN_FULLSPEED)
7549 level |= 7; /* safety min speed 7 */
Roel Kluin547266e2008-02-05 00:24:56 +01007550 else if (level & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007551 level |= 4; /* safety min speed 4 */
7552
7553 if (!acpi_ec_write(fan_status_offset, level))
7554 return -EIO;
7555 else
7556 tp_features.fan_ctrl_status_undef = 0;
7557 break;
7558
7559 default:
7560 return -ENXIO;
7561 }
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007562
7563 vdbg_printk(TPACPI_DBG_FAN,
7564 "fan control: set fan control register to 0x%02x\n", level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007565 return 0;
7566}
7567
7568static int fan_set_level_safe(int level)
7569{
7570 int rc;
7571
7572 if (!fan_control_allowed)
7573 return -EPERM;
7574
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007575 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007576 return -ERESTARTSYS;
7577
7578 if (level == TPACPI_FAN_LAST_LEVEL)
7579 level = fan_control_desired_level;
7580
7581 rc = fan_set_level(level);
7582 if (!rc)
7583 fan_update_desired_level(level);
7584
7585 mutex_unlock(&fan_mutex);
7586 return rc;
7587}
7588
7589static int fan_set_enable(void)
7590{
7591 u8 s;
7592 int rc;
7593
7594 if (!fan_control_allowed)
7595 return -EPERM;
7596
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007597 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007598 return -ERESTARTSYS;
7599
7600 switch (fan_control_access_mode) {
7601 case TPACPI_FAN_WR_ACPI_FANS:
7602 case TPACPI_FAN_WR_TPEC:
7603 rc = fan_get_status(&s);
7604 if (rc < 0)
7605 break;
7606
7607 /* Don't go out of emergency fan mode */
7608 if (s != 7) {
7609 s &= 0x07;
7610 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
7611 }
7612
7613 if (!acpi_ec_write(fan_status_offset, s))
7614 rc = -EIO;
7615 else {
7616 tp_features.fan_ctrl_status_undef = 0;
7617 rc = 0;
7618 }
7619 break;
7620
7621 case TPACPI_FAN_WR_ACPI_SFAN:
7622 rc = fan_get_status(&s);
7623 if (rc < 0)
7624 break;
7625
7626 s &= 0x07;
7627
7628 /* Set fan to at least level 4 */
7629 s |= 4;
7630
7631 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007632 rc = -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007633 else
7634 rc = 0;
7635 break;
7636
7637 default:
7638 rc = -ENXIO;
7639 }
7640
7641 mutex_unlock(&fan_mutex);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007642
7643 if (!rc)
7644 vdbg_printk(TPACPI_DBG_FAN,
7645 "fan control: set fan control register to 0x%02x\n",
7646 s);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007647 return rc;
7648}
7649
7650static int fan_set_disable(void)
7651{
7652 int rc;
7653
7654 if (!fan_control_allowed)
7655 return -EPERM;
7656
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007657 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007658 return -ERESTARTSYS;
7659
7660 rc = 0;
7661 switch (fan_control_access_mode) {
7662 case TPACPI_FAN_WR_ACPI_FANS:
7663 case TPACPI_FAN_WR_TPEC:
7664 if (!acpi_ec_write(fan_status_offset, 0x00))
7665 rc = -EIO;
7666 else {
7667 fan_control_desired_level = 0;
7668 tp_features.fan_ctrl_status_undef = 0;
7669 }
7670 break;
7671
7672 case TPACPI_FAN_WR_ACPI_SFAN:
7673 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
7674 rc = -EIO;
7675 else
7676 fan_control_desired_level = 0;
7677 break;
7678
7679 default:
7680 rc = -ENXIO;
7681 }
7682
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007683 if (!rc)
7684 vdbg_printk(TPACPI_DBG_FAN,
7685 "fan control: set fan control register to 0\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007686
7687 mutex_unlock(&fan_mutex);
7688 return rc;
7689}
7690
7691static int fan_set_speed(int speed)
7692{
7693 int rc;
7694
7695 if (!fan_control_allowed)
7696 return -EPERM;
7697
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007698 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007699 return -ERESTARTSYS;
7700
7701 rc = 0;
7702 switch (fan_control_access_mode) {
7703 case TPACPI_FAN_WR_ACPI_FANS:
7704 if (speed >= 0 && speed <= 65535) {
7705 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
7706 speed, speed, speed))
7707 rc = -EIO;
7708 } else
7709 rc = -EINVAL;
7710 break;
7711
7712 default:
7713 rc = -ENXIO;
7714 }
7715
7716 mutex_unlock(&fan_mutex);
7717 return rc;
7718}
7719
7720static void fan_watchdog_reset(void)
7721{
7722 static int fan_watchdog_active;
7723
7724 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
7725 return;
7726
7727 if (fan_watchdog_active)
7728 cancel_delayed_work(&fan_watchdog_task);
7729
7730 if (fan_watchdog_maxinterval > 0 &&
7731 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
7732 fan_watchdog_active = 1;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03007733 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007734 msecs_to_jiffies(fan_watchdog_maxinterval
7735 * 1000))) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007736 printk(TPACPI_ERR
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03007737 "failed to queue the fan watchdog, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007738 "watchdog will not trigger\n");
7739 }
7740 } else
7741 fan_watchdog_active = 0;
7742}
7743
7744static void fan_watchdog_fire(struct work_struct *ignored)
7745{
7746 int rc;
7747
7748 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
7749 return;
7750
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007751 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007752 rc = fan_set_enable();
7753 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007754 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007755 "will try again later...\n", -rc);
7756 /* reschedule for later */
7757 fan_watchdog_reset();
7758 }
7759}
7760
7761/*
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007762 * SYSFS fan layout: hwmon compatible (device)
7763 *
7764 * pwm*_enable:
7765 * 0: "disengaged" mode
7766 * 1: manual mode
7767 * 2: native EC "auto" mode (recommended, hardware default)
7768 *
7769 * pwm*: set speed in manual mode, ignored otherwise.
7770 * 0 is level 0; 255 is level 7. Intermediate points done with linear
7771 * interpolation.
7772 *
7773 * fan*_input: tachometer reading, RPM
7774 *
7775 *
7776 * SYSFS fan layout: extensions
7777 *
7778 * fan_watchdog (driver):
7779 * fan watchdog interval in seconds, 0 disables (default), max 120
7780 */
7781
7782/* sysfs fan pwm1_enable ----------------------------------------------- */
7783static ssize_t fan_pwm1_enable_show(struct device *dev,
7784 struct device_attribute *attr,
7785 char *buf)
7786{
7787 int res, mode;
7788 u8 status;
7789
7790 res = fan_get_status_safe(&status);
7791 if (res)
7792 return res;
7793
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007794 if (status & TP_EC_FAN_FULLSPEED) {
7795 mode = 0;
7796 } else if (status & TP_EC_FAN_AUTO) {
7797 mode = 2;
7798 } else
7799 mode = 1;
7800
7801 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
7802}
7803
7804static ssize_t fan_pwm1_enable_store(struct device *dev,
7805 struct device_attribute *attr,
7806 const char *buf, size_t count)
7807{
7808 unsigned long t;
7809 int res, level;
7810
7811 if (parse_strtoul(buf, 2, &t))
7812 return -EINVAL;
7813
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007814 tpacpi_disclose_usertask("hwmon pwm1_enable",
7815 "set fan mode to %lu\n", t);
7816
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007817 switch (t) {
7818 case 0:
7819 level = TP_EC_FAN_FULLSPEED;
7820 break;
7821 case 1:
7822 level = TPACPI_FAN_LAST_LEVEL;
7823 break;
7824 case 2:
7825 level = TP_EC_FAN_AUTO;
7826 break;
7827 case 3:
7828 /* reserved for software-controlled auto mode */
7829 return -ENOSYS;
7830 default:
7831 return -EINVAL;
7832 }
7833
7834 res = fan_set_level_safe(level);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03007835 if (res == -ENXIO)
7836 return -EINVAL;
7837 else if (res < 0)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007838 return res;
7839
7840 fan_watchdog_reset();
7841
7842 return count;
7843}
7844
7845static struct device_attribute dev_attr_fan_pwm1_enable =
7846 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
7847 fan_pwm1_enable_show, fan_pwm1_enable_store);
7848
7849/* sysfs fan pwm1 ------------------------------------------------------ */
7850static ssize_t fan_pwm1_show(struct device *dev,
7851 struct device_attribute *attr,
7852 char *buf)
7853{
7854 int res;
7855 u8 status;
7856
7857 res = fan_get_status_safe(&status);
7858 if (res)
7859 return res;
7860
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007861 if ((status &
7862 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
7863 status = fan_control_desired_level;
7864
7865 if (status > 7)
7866 status = 7;
7867
7868 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
7869}
7870
7871static ssize_t fan_pwm1_store(struct device *dev,
7872 struct device_attribute *attr,
7873 const char *buf, size_t count)
7874{
7875 unsigned long s;
7876 int rc;
7877 u8 status, newlevel;
7878
7879 if (parse_strtoul(buf, 255, &s))
7880 return -EINVAL;
7881
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007882 tpacpi_disclose_usertask("hwmon pwm1",
7883 "set fan speed to %lu\n", s);
7884
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007885 /* scale down from 0-255 to 0-7 */
7886 newlevel = (s >> 5) & 0x07;
7887
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007888 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02007889 return -ERESTARTSYS;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007890
7891 rc = fan_get_status(&status);
7892 if (!rc && (status &
7893 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7894 rc = fan_set_level(newlevel);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03007895 if (rc == -ENXIO)
7896 rc = -EINVAL;
7897 else if (!rc) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007898 fan_update_desired_level(newlevel);
Henrique de Moraes Holschuhca4ac2f2007-04-27 22:00:11 -03007899 fan_watchdog_reset();
7900 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007901 }
7902
7903 mutex_unlock(&fan_mutex);
7904 return (rc)? rc : count;
7905}
7906
7907static struct device_attribute dev_attr_fan_pwm1 =
7908 __ATTR(pwm1, S_IWUSR | S_IRUGO,
7909 fan_pwm1_show, fan_pwm1_store);
7910
7911/* sysfs fan fan1_input ------------------------------------------------ */
7912static ssize_t fan_fan1_input_show(struct device *dev,
7913 struct device_attribute *attr,
7914 char *buf)
7915{
7916 int res;
7917 unsigned int speed;
7918
7919 res = fan_get_speed(&speed);
7920 if (res < 0)
7921 return res;
7922
7923 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7924}
7925
7926static struct device_attribute dev_attr_fan_fan1_input =
7927 __ATTR(fan1_input, S_IRUGO,
7928 fan_fan1_input_show, NULL);
7929
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007930/* sysfs fan fan2_input ------------------------------------------------ */
7931static ssize_t fan_fan2_input_show(struct device *dev,
7932 struct device_attribute *attr,
7933 char *buf)
7934{
7935 int res;
7936 unsigned int speed;
7937
7938 res = fan2_get_speed(&speed);
7939 if (res < 0)
7940 return res;
7941
7942 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7943}
7944
7945static struct device_attribute dev_attr_fan_fan2_input =
7946 __ATTR(fan2_input, S_IRUGO,
7947 fan_fan2_input_show, NULL);
7948
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007949/* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007950static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
7951 char *buf)
7952{
7953 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
7954}
7955
7956static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
7957 const char *buf, size_t count)
7958{
7959 unsigned long t;
7960
7961 if (parse_strtoul(buf, 120, &t))
7962 return -EINVAL;
7963
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03007964 if (!fan_control_allowed)
7965 return -EPERM;
7966
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007967 fan_watchdog_maxinterval = t;
7968 fan_watchdog_reset();
7969
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007970 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
7971
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007972 return count;
7973}
7974
7975static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
7976 fan_fan_watchdog_show, fan_fan_watchdog_store);
7977
7978/* --------------------------------------------------------------------- */
7979static struct attribute *fan_attributes[] = {
7980 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
7981 &dev_attr_fan_fan1_input.attr,
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007982 NULL, /* for fan2_input */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007983 NULL
7984};
7985
7986static const struct attribute_group fan_attr_group = {
7987 .attrs = fan_attributes,
7988};
7989
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007990#define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
7991#define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007992
7993#define TPACPI_FAN_QI(__id1, __id2, __quirks) \
7994 { .vendor = PCI_VENDOR_ID_IBM, \
7995 .bios = TPACPI_MATCH_ANY, \
7996 .ec = TPID(__id1, __id2), \
7997 .quirks = __quirks }
7998
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007999#define TPACPI_FAN_QL(__id1, __id2, __quirks) \
8000 { .vendor = PCI_VENDOR_ID_LENOVO, \
8001 .bios = TPACPI_MATCH_ANY, \
8002 .ec = TPID(__id1, __id2), \
8003 .quirks = __quirks }
8004
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008005static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
8006 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
8007 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
8008 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
8009 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008010 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008011};
8012
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008013#undef TPACPI_FAN_QL
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008014#undef TPACPI_FAN_QI
8015
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008016static int __init fan_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008017{
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008018 int rc;
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008019 unsigned long quirks;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008020
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008021 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8022 "initializing fan subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008023
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03008024 mutex_init(&fan_mutex);
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008025 fan_status_access_mode = TPACPI_FAN_NONE;
8026 fan_control_access_mode = TPACPI_FAN_WR_NONE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008027 fan_control_commands = 0;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008028 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03008029 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008030 tp_features.second_fan = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008031 fan_control_desired_level = 7;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008032
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03008033 if (tpacpi_is_ibm()) {
8034 TPACPI_ACPIHANDLE_INIT(fans);
8035 TPACPI_ACPIHANDLE_INIT(gfan);
8036 TPACPI_ACPIHANDLE_INIT(sfan);
8037 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008038
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008039 quirks = tpacpi_check_quirks(fan_quirk_table,
8040 ARRAY_SIZE(fan_quirk_table));
8041
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008042 if (gfan_handle) {
8043 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008044 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008045 } else {
8046 /* all other ThinkPads: note that even old-style
8047 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02008048 if (likely(acpi_ec_read(fan_status_offset,
8049 &fan_control_initial_status))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008050 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008051 if (quirks & TPACPI_FAN_Q1)
8052 fan_quirk1_setup();
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008053 if (quirks & TPACPI_FAN_2FAN) {
8054 tp_features.second_fan = 1;
8055 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8056 "secondary fan support enabled\n");
8057 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008058 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008059 printk(TPACPI_ERR
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008060 "ThinkPad ACPI EC access misbehaving, "
8061 "fan status and control unavailable\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008062 return 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008063 }
8064 }
8065
8066 if (sfan_handle) {
8067 /* 570, 770x-JL */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008068 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02008069 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008070 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008071 } else {
8072 if (!gfan_handle) {
8073 /* gfan without sfan means no fan control */
8074 /* all other models implement TP EC 0x2f control */
8075
8076 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02008077 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008078 fan_control_access_mode =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008079 TPACPI_FAN_WR_ACPI_FANS;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008080 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008081 TPACPI_FAN_CMD_SPEED |
8082 TPACPI_FAN_CMD_LEVEL |
8083 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008084 } else {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008085 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02008086 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008087 TPACPI_FAN_CMD_LEVEL |
8088 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008089 }
8090 }
8091 }
8092
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008093 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8094 "fan is %s, modes %d, %d\n",
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008095 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
8096 fan_control_access_mode != TPACPI_FAN_WR_NONE),
8097 fan_status_access_mode, fan_control_access_mode);
8098
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008099 /* fan control master switch */
8100 if (!fan_control_allowed) {
8101 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8102 fan_control_commands = 0;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008103 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008104 "fan control features disabled by parameter\n");
8105 }
8106
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008107 /* update fan_control_desired_level */
8108 if (fan_status_access_mode != TPACPI_FAN_NONE)
8109 fan_get_status_safe(NULL);
8110
8111 if (fan_status_access_mode != TPACPI_FAN_NONE ||
8112 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008113 if (tp_features.second_fan) {
8114 /* attach second fan tachometer */
8115 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
8116 &dev_attr_fan_fan2_input.attr;
8117 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008118 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008119 &fan_attr_group);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008120 if (rc < 0)
8121 return rc;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03008122
8123 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
8124 &driver_attr_fan_watchdog);
8125 if (rc < 0) {
8126 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
8127 &fan_attr_group);
8128 return rc;
8129 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008130 return 0;
8131 } else
8132 return 1;
8133}
8134
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008135static void fan_exit(void)
8136{
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008137 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008138 "cancelling any pending fan watchdog tasks\n");
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008139
8140 /* FIXME: can we really do this unconditionally? */
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008141 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008142 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
8143 &driver_attr_fan_watchdog);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008144
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008145 cancel_delayed_work(&fan_watchdog_task);
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03008146 flush_workqueue(tpacpi_wq);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008147}
8148
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008149static void fan_suspend(pm_message_t state)
8150{
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008151 int rc;
8152
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008153 if (!fan_control_allowed)
8154 return;
8155
8156 /* Store fan status in cache */
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008157 fan_control_resume_level = 0;
8158 rc = fan_get_status_safe(&fan_control_resume_level);
8159 if (rc < 0)
8160 printk(TPACPI_NOTICE
8161 "failed to read fan level for later "
8162 "restore during resume: %d\n", rc);
8163
8164 /* if it is undefined, don't attempt to restore it.
8165 * KEEP THIS LAST */
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008166 if (tp_features.fan_ctrl_status_undef)
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008167 fan_control_resume_level = 0;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008168}
8169
8170static void fan_resume(void)
8171{
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008172 u8 current_level = 7;
8173 bool do_set = false;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008174 int rc;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008175
8176 /* DSDT *always* updates status on resume */
8177 tp_features.fan_ctrl_status_undef = 0;
8178
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008179 if (!fan_control_allowed ||
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008180 !fan_control_resume_level ||
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008181 (fan_get_status_safe(&current_level) < 0))
8182 return;
8183
8184 switch (fan_control_access_mode) {
8185 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008186 /* never decrease fan level */
8187 do_set = (fan_control_resume_level > current_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008188 break;
8189 case TPACPI_FAN_WR_ACPI_FANS:
8190 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008191 /* never decrease fan level, scale is:
8192 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
8193 *
8194 * We expect the firmware to set either 7 or AUTO, but we
8195 * handle FULLSPEED out of paranoia.
8196 *
8197 * So, we can safely only restore FULLSPEED or 7, anything
8198 * else could slow the fan. Restoring AUTO is useless, at
8199 * best that's exactly what the DSDT already set (it is the
8200 * slower it uses).
8201 *
8202 * Always keep in mind that the DSDT *will* have set the
8203 * fans to what the vendor supposes is the best level. We
8204 * muck with it only to speed the fan up.
8205 */
8206 if (fan_control_resume_level != 7 &&
8207 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
8208 return;
8209 else
8210 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
8211 (current_level != fan_control_resume_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008212 break;
8213 default:
8214 return;
8215 }
8216 if (do_set) {
8217 printk(TPACPI_NOTICE
8218 "restoring fan level to 0x%02x\n",
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008219 fan_control_resume_level);
8220 rc = fan_set_level_safe(fan_control_resume_level);
8221 if (rc < 0)
8222 printk(TPACPI_NOTICE
8223 "failed to restore fan level: %d\n", rc);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008224 }
8225}
8226
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008227static int fan_read(struct seq_file *m)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008228{
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008229 int rc;
8230 u8 status;
8231 unsigned int speed = 0;
8232
8233 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008234 case TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008235 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008236 rc = fan_get_status_safe(&status);
8237 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008238 return rc;
8239
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008240 seq_printf(m, "status:\t\t%s\n"
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008241 "level:\t\t%d\n",
8242 (status != 0) ? "enabled" : "disabled", status);
8243 break;
8244
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008245 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008246 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008247 rc = fan_get_status_safe(&status);
8248 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008249 return rc;
8250
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008251 seq_printf(m, "status:\t\t%s\n",
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008252 (status != 0) ? "enabled" : "disabled");
8253
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008254 rc = fan_get_speed(&speed);
8255 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008256 return rc;
8257
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008258 seq_printf(m, "speed:\t\t%d\n", speed);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008259
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008260 if (status & TP_EC_FAN_FULLSPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008261 /* Disengaged mode takes precedence */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008262 seq_printf(m, "level:\t\tdisengaged\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008263 else if (status & TP_EC_FAN_AUTO)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008264 seq_printf(m, "level:\t\tauto\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008265 else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008266 seq_printf(m, "level:\t\t%d\n", status);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008267 break;
8268
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008269 case TPACPI_FAN_NONE:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008270 default:
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008271 seq_printf(m, "status:\t\tnot supported\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008272 }
8273
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008274 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008275 seq_printf(m, "commands:\tlevel <level>");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008276
8277 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008278 case TPACPI_FAN_WR_ACPI_SFAN:
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008279 seq_printf(m, " (<level> is 0-7)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008280 break;
8281
8282 default:
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008283 seq_printf(m, " (<level> is 0-7, "
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008284 "auto, disengaged, full-speed)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008285 break;
8286 }
8287 }
8288
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008289 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008290 seq_printf(m, "commands:\tenable, disable\n"
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008291 "commands:\twatchdog <timeout> (<timeout> "
8292 "is 0 (off), 1-120 (seconds))\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008293
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008294 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008295 seq_printf(m, "commands:\tspeed <speed>"
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008296 " (<speed> is 0-65535)\n");
8297
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008298 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008299}
8300
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008301static int fan_write_cmd_level(const char *cmd, int *rc)
8302{
8303 int level;
8304
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02008305 if (strlencmp(cmd, "level auto") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008306 level = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008307 else if ((strlencmp(cmd, "level disengaged") == 0) |
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008308 (strlencmp(cmd, "level full-speed") == 0))
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008309 level = TP_EC_FAN_FULLSPEED;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02008310 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008311 return 0;
8312
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008313 *rc = fan_set_level_safe(level);
8314 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008315 printk(TPACPI_ERR "level command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008316 "access mode %d", fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008317 else if (!*rc)
8318 tpacpi_disclose_usertask("procfs fan",
8319 "set level to %d\n", level);
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008320
8321 return 1;
8322}
8323
8324static int fan_write_cmd_enable(const char *cmd, int *rc)
8325{
8326 if (strlencmp(cmd, "enable") != 0)
8327 return 0;
8328
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008329 *rc = fan_set_enable();
8330 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008331 printk(TPACPI_ERR "enable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008332 "access mode %d", fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008333 else if (!*rc)
8334 tpacpi_disclose_usertask("procfs fan", "enable\n");
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008335
8336 return 1;
8337}
8338
8339static int fan_write_cmd_disable(const char *cmd, int *rc)
8340{
8341 if (strlencmp(cmd, "disable") != 0)
8342 return 0;
8343
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008344 *rc = fan_set_disable();
8345 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008346 printk(TPACPI_ERR "disable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008347 "access mode %d", fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008348 else if (!*rc)
8349 tpacpi_disclose_usertask("procfs fan", "disable\n");
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008350
8351 return 1;
8352}
8353
8354static int fan_write_cmd_speed(const char *cmd, int *rc)
8355{
8356 int speed;
8357
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02008358 /* TODO:
8359 * Support speed <low> <medium> <high> ? */
8360
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008361 if (sscanf(cmd, "speed %d", &speed) != 1)
8362 return 0;
8363
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008364 *rc = fan_set_speed(speed);
8365 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008366 printk(TPACPI_ERR "speed command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008367 "access mode %d", fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008368 else if (!*rc)
8369 tpacpi_disclose_usertask("procfs fan",
8370 "set speed to %d\n", speed);
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008371
8372 return 1;
8373}
8374
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008375static int fan_write_cmd_watchdog(const char *cmd, int *rc)
8376{
8377 int interval;
8378
8379 if (sscanf(cmd, "watchdog %d", &interval) != 1)
8380 return 0;
8381
8382 if (interval < 0 || interval > 120)
8383 *rc = -EINVAL;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008384 else {
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008385 fan_watchdog_maxinterval = interval;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008386 tpacpi_disclose_usertask("procfs fan",
8387 "set watchdog timer to %d\n",
8388 interval);
8389 }
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008390
8391 return 1;
8392}
8393
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008394static int fan_write(char *buf)
8395{
8396 char *cmd;
8397 int rc = 0;
8398
8399 while (!rc && (cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008400 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008401 fan_write_cmd_level(cmd, &rc)) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008402 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008403 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008404 fan_write_cmd_disable(cmd, &rc) ||
8405 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008406 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008407 fan_write_cmd_speed(cmd, &rc))
8408 )
8409 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008410 else if (!rc)
8411 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008412 }
8413
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008414 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008415}
8416
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008417static struct ibm_struct fan_driver_data = {
8418 .name = "fan",
8419 .read = fan_read,
8420 .write = fan_write,
8421 .exit = fan_exit,
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008422 .suspend = fan_suspend,
8423 .resume = fan_resume,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008424};
8425
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008426/****************************************************************************
8427 ****************************************************************************
8428 *
8429 * Infrastructure
8430 *
8431 ****************************************************************************
8432 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07008433
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03008434/*
8435 * HKEY event callout for other subdrivers go here
8436 * (yes, it is ugly, but it is quick, safe, and gets the job done
8437 */
8438static void tpacpi_driver_event(const unsigned int hkey_event)
8439{
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00008440 if (ibm_backlight_device) {
8441 switch (hkey_event) {
8442 case TP_HKEY_EV_BRGHT_UP:
8443 case TP_HKEY_EV_BRGHT_DOWN:
8444 tpacpi_brightness_notify_change();
8445 }
8446 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02008447 if (alsa_card) {
8448 switch (hkey_event) {
8449 case TP_HKEY_EV_VOL_UP:
8450 case TP_HKEY_EV_VOL_DOWN:
8451 case TP_HKEY_EV_VOL_MUTE:
8452 volume_alsa_notify_change();
8453 }
8454 }
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03008455}
8456
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03008457static void hotkey_driver_event(const unsigned int scancode)
8458{
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03008459 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode);
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03008460}
8461
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008462/* sysfs name ---------------------------------------------------------- */
8463static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
8464 struct device_attribute *attr,
8465 char *buf)
8466{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008467 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008468}
8469
8470static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
8471 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
8472
8473/* --------------------------------------------------------------------- */
8474
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008475/* /proc support */
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03008476static struct proc_dir_entry *proc_dir;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008477
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008478/*
8479 * Module and infrastructure proble, init and exit handling
8480 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008481
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008482static int force_load;
8483
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008484#ifdef CONFIG_THINKPAD_ACPI_DEBUG
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008485static const char * __init str_supported(int is_supported)
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008486{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008487 static char text_unsupported[] __initdata = "not supported";
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008488
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008489 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008490}
8491#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
8492
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008493static void ibm_exit(struct ibm_struct *ibm)
8494{
8495 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
8496
8497 list_del_init(&ibm->all_drivers);
8498
8499 if (ibm->flags.acpi_notify_installed) {
8500 dbg_printk(TPACPI_DBG_EXIT,
8501 "%s: acpi_remove_notify_handler\n", ibm->name);
8502 BUG_ON(!ibm->acpi);
8503 acpi_remove_notify_handler(*ibm->acpi->handle,
8504 ibm->acpi->type,
8505 dispatch_acpi_notify);
8506 ibm->flags.acpi_notify_installed = 0;
8507 ibm->flags.acpi_notify_installed = 0;
8508 }
8509
8510 if (ibm->flags.proc_created) {
8511 dbg_printk(TPACPI_DBG_EXIT,
8512 "%s: remove_proc_entry\n", ibm->name);
8513 remove_proc_entry(ibm->name, proc_dir);
8514 ibm->flags.proc_created = 0;
8515 }
8516
8517 if (ibm->flags.acpi_driver_registered) {
8518 dbg_printk(TPACPI_DBG_EXIT,
8519 "%s: acpi_bus_unregister_driver\n", ibm->name);
8520 BUG_ON(!ibm->acpi);
8521 acpi_bus_unregister_driver(ibm->acpi->driver);
8522 kfree(ibm->acpi->driver);
8523 ibm->acpi->driver = NULL;
8524 ibm->flags.acpi_driver_registered = 0;
8525 }
8526
8527 if (ibm->flags.init_called && ibm->exit) {
8528 ibm->exit();
8529 ibm->flags.init_called = 0;
8530 }
8531
8532 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
8533}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02008534
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008535static int __init ibm_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008536{
8537 int ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008538 struct ibm_struct *ibm = iibm->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008539 struct proc_dir_entry *entry;
8540
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008541 BUG_ON(ibm == NULL);
8542
8543 INIT_LIST_HEAD(&ibm->all_drivers);
8544
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03008545 if (ibm->flags.experimental && !experimental)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008546 return 0;
8547
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008548 dbg_printk(TPACPI_DBG_INIT,
8549 "probing for %s\n", ibm->name);
8550
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008551 if (iibm->init) {
8552 ret = iibm->init(iibm);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008553 if (ret > 0)
8554 return 0; /* probe failed */
8555 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008556 return ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008557
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03008558 ibm->flags.init_called = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008559 }
8560
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03008561 if (ibm->acpi) {
8562 if (ibm->acpi->hid) {
8563 ret = register_tpacpi_subdriver(ibm);
8564 if (ret)
8565 goto err_out;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008566 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03008567
8568 if (ibm->acpi->notify) {
8569 ret = setup_acpi_notify(ibm);
8570 if (ret == -ENODEV) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008571 printk(TPACPI_NOTICE "disabling subdriver %s\n",
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03008572 ibm->name);
8573 ret = 0;
8574 goto err_out;
8575 }
8576 if (ret < 0)
8577 goto err_out;
8578 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008579 }
8580
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008581 dbg_printk(TPACPI_DBG_INIT,
8582 "%s installed\n", ibm->name);
8583
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008584 if (ibm->read) {
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03008585 mode_t mode = iibm->base_procfs_mode;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008586
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03008587 if (!mode)
8588 mode = S_IRUGO;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008589 if (ibm->write)
8590 mode |= S_IWUSR;
8591 entry = proc_create_data(ibm->name, mode, proc_dir,
8592 &dispatch_proc_fops, ibm);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008593 if (!entry) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008594 printk(TPACPI_ERR "unable to create proc entry %s\n",
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008595 ibm->name);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008596 ret = -ENODEV;
8597 goto err_out;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008598 }
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03008599 ibm->flags.proc_created = 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008600 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008601
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008602 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
8603
Linus Torvalds1da177e2005-04-16 15:20:36 -07008604 return 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008605
8606err_out:
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008607 dbg_printk(TPACPI_DBG_INIT,
8608 "%s: at error exit path with result %d\n",
8609 ibm->name, ret);
8610
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008611 ibm_exit(ibm);
8612 return (ret < 0)? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008613}
8614
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008615/* Probing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008616
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008617static bool __pure __init tpacpi_is_fw_digit(const char c)
8618{
8619 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
8620}
8621
8622/* Most models: xxyTkkWW (#.##c); Ancient 570/600 and -SL lacks (#.##c) */
8623static bool __pure __init tpacpi_is_valid_fw_id(const char* const s,
8624 const char t)
8625{
8626 return s && strlen(s) >= 8 &&
8627 tpacpi_is_fw_digit(s[0]) &&
8628 tpacpi_is_fw_digit(s[1]) &&
8629 s[2] == t && s[3] == 'T' &&
8630 tpacpi_is_fw_digit(s[4]) &&
8631 tpacpi_is_fw_digit(s[5]) &&
8632 s[6] == 'W' && s[7] == 'W';
8633}
8634
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008635/* returns 0 - probe ok, or < 0 - probe error.
8636 * Probe ok doesn't mean thinkpad found.
8637 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
8638static int __must_check __init get_thinkpad_model_data(
8639 struct thinkpad_id_data *tp)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02008640{
Jeff Garzik18552562007-10-03 15:15:40 -04008641 const struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02008642 char ec_fw_string[18];
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008643 char const *s;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02008644
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008645 if (!tp)
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008646 return -EINVAL;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008647
8648 memset(tp, 0, sizeof(*tp));
8649
8650 if (dmi_name_in_vendors("IBM"))
8651 tp->vendor = PCI_VENDOR_ID_IBM;
8652 else if (dmi_name_in_vendors("LENOVO"))
8653 tp->vendor = PCI_VENDOR_ID_LENOVO;
8654 else
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008655 return 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008656
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008657 s = dmi_get_system_info(DMI_BIOS_VERSION);
8658 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
8659 if (s && !tp->bios_version_str)
8660 return -ENOMEM;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008661
8662 /* Really ancient ThinkPad 240X will fail this, which is fine */
8663 if (!tpacpi_is_valid_fw_id(tp->bios_version_str, 'E'))
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008664 return 0;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008665
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008666 tp->bios_model = tp->bios_version_str[0]
8667 | (tp->bios_version_str[1] << 8);
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008668 tp->bios_release = (tp->bios_version_str[4] << 8)
8669 | tp->bios_version_str[5];
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008670
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02008671 /*
8672 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
8673 * X32 or newer, all Z series; Some models must have an
8674 * up-to-date BIOS or they will not be detected.
8675 *
8676 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
8677 */
8678 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02008679 if (sscanf(dev->name,
8680 "IBM ThinkPad Embedded Controller -[%17c",
8681 ec_fw_string) == 1) {
8682 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
8683 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008684
8685 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008686 if (!tp->ec_version_str)
8687 return -ENOMEM;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008688
8689 if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) {
8690 tp->ec_model = ec_fw_string[0]
8691 | (ec_fw_string[1] << 8);
8692 tp->ec_release = (ec_fw_string[4] << 8)
8693 | ec_fw_string[5];
8694 } else {
8695 printk(TPACPI_NOTICE
8696 "ThinkPad firmware release %s "
8697 "doesn't match the known patterns\n",
8698 ec_fw_string);
8699 printk(TPACPI_NOTICE
8700 "please report this to %s\n",
8701 TPACPI_MAIL);
8702 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008703 break;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02008704 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02008705 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008706
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008707 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
8708 if (s && !strnicmp(s, "ThinkPad", 8)) {
8709 tp->model_str = kstrdup(s, GFP_KERNEL);
8710 if (!tp->model_str)
8711 return -ENOMEM;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008712 }
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03008713
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008714 s = dmi_get_system_info(DMI_PRODUCT_NAME);
8715 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
8716 if (s && !tp->nummodel_str)
8717 return -ENOMEM;
8718
8719 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008720}
8721
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008722static int __init probe_for_thinkpad(void)
8723{
8724 int is_thinkpad;
8725
8726 if (acpi_disabled)
8727 return -ENODEV;
8728
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03008729 /* It would be dangerous to run the driver in this case */
8730 if (!tpacpi_is_ibm() && !tpacpi_is_lenovo())
8731 return -ENODEV;
8732
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008733 /*
8734 * Non-ancient models have better DMI tagging, but very old models
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03008735 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008736 */
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03008737 is_thinkpad = (thinkpad_id.model_str != NULL) ||
8738 (thinkpad_id.ec_model != 0) ||
8739 tpacpi_is_fw_known();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008740
8741 /* ec is required because many other handles are relative to it */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008742 TPACPI_ACPIHANDLE_INIT(ec);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008743 if (!ec_handle) {
8744 if (is_thinkpad)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008745 printk(TPACPI_ERR
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008746 "Not yet supported ThinkPad detected!\n");
8747 return -ENODEV;
8748 }
8749
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03008750 if (!is_thinkpad && !force_load)
8751 return -ENODEV;
8752
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008753 return 0;
8754}
8755
8756
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008757/* Module init, exit, parameters */
8758
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008759static struct ibm_init_struct ibms_init[] __initdata = {
8760 {
8761 .init = thinkpad_acpi_driver_init,
8762 .data = &thinkpad_acpi_driver_data,
8763 },
8764 {
8765 .init = hotkey_init,
8766 .data = &hotkey_driver_data,
8767 },
8768 {
8769 .init = bluetooth_init,
8770 .data = &bluetooth_driver_data,
8771 },
8772 {
8773 .init = wan_init,
8774 .data = &wan_driver_data,
8775 },
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02008776 {
8777 .init = uwb_init,
8778 .data = &uwb_driver_data,
8779 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02008780#ifdef CONFIG_THINKPAD_ACPI_VIDEO
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008781 {
8782 .init = video_init,
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03008783 .base_procfs_mode = S_IRUSR,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008784 .data = &video_driver_data,
8785 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02008786#endif
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008787 {
8788 .init = light_init,
8789 .data = &light_driver_data,
8790 },
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008791 {
8792 .init = cmos_init,
8793 .data = &cmos_driver_data,
8794 },
8795 {
8796 .init = led_init,
8797 .data = &led_driver_data,
8798 },
8799 {
8800 .init = beep_init,
8801 .data = &beep_driver_data,
8802 },
8803 {
8804 .init = thermal_init,
8805 .data = &thermal_driver_data,
8806 },
8807 {
8808 .data = &ecdump_driver_data,
8809 },
8810 {
8811 .init = brightness_init,
8812 .data = &brightness_driver_data,
8813 },
8814 {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02008815 .init = volume_init,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008816 .data = &volume_driver_data,
8817 },
8818 {
8819 .init = fan_init,
8820 .data = &fan_driver_data,
8821 },
8822};
8823
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008824static int __init set_ibm_param(const char *val, struct kernel_param *kp)
8825{
8826 unsigned int i;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008827 struct ibm_struct *ibm;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008828
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02008829 if (!kp || !kp->name || !val)
8830 return -EINVAL;
8831
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008832 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
8833 ibm = ibms_init[i].data;
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02008834 WARN_ON(ibm == NULL);
8835
8836 if (!ibm || !ibm->name)
8837 continue;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008838
8839 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
8840 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008841 return -ENOSPC;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008842 strcpy(ibms_init[i].param, val);
8843 strcat(ibms_init[i].param, ",");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008844 return 0;
8845 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008846 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008847
8848 return -EINVAL;
8849}
8850
Henrique de Moraes Holschuhb09c72252009-12-09 01:36:27 +00008851module_param(experimental, int, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008852MODULE_PARM_DESC(experimental,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008853 "Enables experimental features when non-zero");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008854
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03008855module_param_named(debug, dbg_level, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008856MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03008857
Henrique de Moraes Holschuhb09c72252009-12-09 01:36:27 +00008858module_param(force_load, bool, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008859MODULE_PARM_DESC(force_load,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008860 "Attempts to load the driver even on a "
8861 "mis-identified ThinkPad when true");
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03008862
Henrique de Moraes Holschuhb09c72252009-12-09 01:36:27 +00008863module_param_named(fan_control, fan_control_allowed, bool, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008864MODULE_PARM_DESC(fan_control,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008865 "Enables setting fan parameters features when true");
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008866
Henrique de Moraes Holschuhb09c72252009-12-09 01:36:27 +00008867module_param_named(brightness_mode, brightness_mode, uint, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008868MODULE_PARM_DESC(brightness_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008869 "Selects brightness control strategy: "
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00008870 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03008871
Henrique de Moraes Holschuhb09c72252009-12-09 01:36:27 +00008872module_param(brightness_enable, uint, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008873MODULE_PARM_DESC(brightness_enable,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008874 "Enables backlight control when 1, disables when 0");
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02008875
Henrique de Moraes Holschuhb09c72252009-12-09 01:36:27 +00008876module_param(hotkey_report_mode, uint, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008877MODULE_PARM_DESC(hotkey_report_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008878 "used for backwards compatibility with userspace, "
8879 "see documentation");
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03008880
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02008881#ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02008882module_param_named(volume_mode, volume_mode, uint, 0444);
8883MODULE_PARM_DESC(volume_mode,
8884 "Selects volume control strategy: "
8885 "0=auto, 1=EC, 2=N/A, 3=EC+NVRAM");
8886
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02008887module_param_named(volume_capabilities, volume_capabilities, uint, 0444);
8888MODULE_PARM_DESC(volume_capabilities,
8889 "Selects the mixer capabilites: "
8890 "0=auto, 1=volume and mute, 2=mute only");
8891
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02008892module_param_named(volume_control, volume_control_allowed, bool, 0444);
8893MODULE_PARM_DESC(volume_control,
8894 "Enables software override for the console audio "
8895 "control when true");
8896
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02008897/* ALSA module API parameters */
8898module_param_named(index, alsa_index, int, 0444);
8899MODULE_PARM_DESC(index, "ALSA index for the ACPI EC Mixer");
8900module_param_named(id, alsa_id, charp, 0444);
8901MODULE_PARM_DESC(id, "ALSA id for the ACPI EC Mixer");
8902module_param_named(enable, alsa_enable, bool, 0444);
8903MODULE_PARM_DESC(enable, "Enable the ALSA interface for the ACPI EC Mixer");
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02008904#endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02008905
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008906#define TPACPI_PARAM(feature) \
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008907 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
Henrique de Moraes Holschuhcbb14842008-02-16 02:17:50 -02008908 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008909 "at module load, see documentation")
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008910
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008911TPACPI_PARAM(hotkey);
8912TPACPI_PARAM(bluetooth);
8913TPACPI_PARAM(video);
8914TPACPI_PARAM(light);
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008915TPACPI_PARAM(cmos);
8916TPACPI_PARAM(led);
8917TPACPI_PARAM(beep);
8918TPACPI_PARAM(ecdump);
8919TPACPI_PARAM(brightness);
8920TPACPI_PARAM(volume);
8921TPACPI_PARAM(fan);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008922
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02008923#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
Henrique de Moraes Holschuhb09c72252009-12-09 01:36:27 +00008924module_param(dbg_wlswemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02008925MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
8926module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
8927MODULE_PARM_DESC(wlsw_state,
8928 "Initial state of the emulated WLSW switch");
8929
Henrique de Moraes Holschuhb09c72252009-12-09 01:36:27 +00008930module_param(dbg_bluetoothemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02008931MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
8932module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
8933MODULE_PARM_DESC(bluetooth_state,
8934 "Initial state of the emulated bluetooth switch");
8935
Henrique de Moraes Holschuhb09c72252009-12-09 01:36:27 +00008936module_param(dbg_wwanemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02008937MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
8938module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
8939MODULE_PARM_DESC(wwan_state,
8940 "Initial state of the emulated WWAN switch");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02008941
Henrique de Moraes Holschuhb09c72252009-12-09 01:36:27 +00008942module_param(dbg_uwbemul, uint, 0444);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02008943MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
8944module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
8945MODULE_PARM_DESC(uwb_state,
8946 "Initial state of the emulated UWB switch");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02008947#endif
8948
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008949static void thinkpad_acpi_module_exit(void)
8950{
8951 struct ibm_struct *ibm, *itmp;
8952
8953 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
8954
8955 list_for_each_entry_safe_reverse(ibm, itmp,
8956 &tpacpi_all_drivers,
8957 all_drivers) {
8958 ibm_exit(ibm);
8959 }
8960
8961 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
8962
8963 if (tpacpi_inputdev) {
8964 if (tp_features.input_device_registered)
8965 input_unregister_device(tpacpi_inputdev);
8966 else
8967 input_free_device(tpacpi_inputdev);
8968 }
8969
8970 if (tpacpi_hwmon)
8971 hwmon_device_unregister(tpacpi_hwmon);
8972
8973 if (tp_features.sensors_pdev_attrs_registered)
8974 device_remove_file(&tpacpi_sensors_pdev->dev,
8975 &dev_attr_thinkpad_acpi_pdev_name);
8976 if (tpacpi_sensors_pdev)
8977 platform_device_unregister(tpacpi_sensors_pdev);
8978 if (tpacpi_pdev)
8979 platform_device_unregister(tpacpi_pdev);
8980
8981 if (tp_features.sensors_pdrv_attrs_registered)
8982 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
8983 if (tp_features.platform_drv_attrs_registered)
8984 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
8985
8986 if (tp_features.sensors_pdrv_registered)
8987 platform_driver_unregister(&tpacpi_hwmon_pdriver);
8988
8989 if (tp_features.platform_drv_registered)
8990 platform_driver_unregister(&tpacpi_pdriver);
8991
8992 if (proc_dir)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008993 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008994
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03008995 if (tpacpi_wq)
8996 destroy_workqueue(tpacpi_wq);
8997
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008998 kfree(thinkpad_id.bios_version_str);
8999 kfree(thinkpad_id.ec_version_str);
9000 kfree(thinkpad_id.model_str);
9001}
9002
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02009003
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03009004static int __init thinkpad_acpi_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009005{
9006 int ret, i;
9007
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03009008 tpacpi_lifecycle = TPACPI_LIFE_INIT;
9009
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03009010 /* Parameter checking */
9011 if (hotkey_report_mode > 2)
9012 return -EINVAL;
9013
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009014 /* Driver-level probe */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009015
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009016 ret = get_thinkpad_model_data(&thinkpad_id);
9017 if (ret) {
9018 printk(TPACPI_ERR
9019 "unable to get DMI data: %d\n", ret);
9020 thinkpad_acpi_module_exit();
9021 return ret;
9022 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009023 ret = probe_for_thinkpad();
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009024 if (ret) {
9025 thinkpad_acpi_module_exit();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009026 return ret;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009027 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009028
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009029 /* Driver initialization */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009030
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009031 TPACPI_ACPIHANDLE_INIT(ecrd);
9032 TPACPI_ACPIHANDLE_INIT(ecwr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009033
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03009034 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
9035 if (!tpacpi_wq) {
9036 thinkpad_acpi_module_exit();
9037 return -ENOMEM;
9038 }
9039
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009040 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009041 if (!proc_dir) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009042 printk(TPACPI_ERR
9043 "unable to create proc dir " TPACPI_PROC_DIR);
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03009044 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07009045 return -ENODEV;
9046 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04009047
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009048 ret = platform_driver_register(&tpacpi_pdriver);
9049 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009050 printk(TPACPI_ERR
9051 "unable to register main platform driver\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009052 thinkpad_acpi_module_exit();
9053 return ret;
9054 }
Henrique de Moraes Holschuhac363932007-07-27 17:04:40 -03009055 tp_features.platform_drv_registered = 1;
9056
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009057 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
9058 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009059 printk(TPACPI_ERR
9060 "unable to register hwmon platform driver\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009061 thinkpad_acpi_module_exit();
9062 return ret;
9063 }
9064 tp_features.sensors_pdrv_registered = 1;
9065
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03009066 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03009067 if (!ret) {
9068 tp_features.platform_drv_attrs_registered = 1;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009069 ret = tpacpi_create_driver_attributes(
9070 &tpacpi_hwmon_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03009071 }
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03009072 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009073 printk(TPACPI_ERR
9074 "unable to create sysfs driver attributes\n");
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03009075 thinkpad_acpi_module_exit();
9076 return ret;
9077 }
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03009078 tp_features.sensors_pdrv_attrs_registered = 1;
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03009079
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009080
9081 /* Device initialization */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009082 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009083 NULL, 0);
9084 if (IS_ERR(tpacpi_pdev)) {
9085 ret = PTR_ERR(tpacpi_pdev);
9086 tpacpi_pdev = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009087 printk(TPACPI_ERR "unable to register platform device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009088 thinkpad_acpi_module_exit();
9089 return ret;
9090 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009091 tpacpi_sensors_pdev = platform_device_register_simple(
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009092 TPACPI_HWMON_DRVR_NAME,
9093 -1, NULL, 0);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009094 if (IS_ERR(tpacpi_sensors_pdev)) {
9095 ret = PTR_ERR(tpacpi_sensors_pdev);
9096 tpacpi_sensors_pdev = NULL;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009097 printk(TPACPI_ERR
9098 "unable to register hwmon platform device\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009099 thinkpad_acpi_module_exit();
9100 return ret;
9101 }
9102 ret = device_create_file(&tpacpi_sensors_pdev->dev,
9103 &dev_attr_thinkpad_acpi_pdev_name);
9104 if (ret) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009105 printk(TPACPI_ERR
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009106 "unable to create sysfs hwmon device attributes\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009107 thinkpad_acpi_module_exit();
9108 return ret;
9109 }
9110 tp_features.sensors_pdev_attrs_registered = 1;
9111 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009112 if (IS_ERR(tpacpi_hwmon)) {
9113 ret = PTR_ERR(tpacpi_hwmon);
9114 tpacpi_hwmon = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009115 printk(TPACPI_ERR "unable to register hwmon device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009116 thinkpad_acpi_module_exit();
9117 return ret;
9118 }
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -03009119 mutex_init(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009120 tpacpi_inputdev = input_allocate_device();
9121 if (!tpacpi_inputdev) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009122 printk(TPACPI_ERR "unable to allocate input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009123 thinkpad_acpi_module_exit();
9124 return -ENOMEM;
9125 } else {
9126 /* Prepare input device, but don't register */
9127 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009128 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009129 tpacpi_inputdev->id.bustype = BUS_HOST;
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03009130 tpacpi_inputdev->id.vendor = thinkpad_id.vendor;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009131 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
9132 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
Henrique de Moraes Holschuhd112ef92009-12-09 01:36:26 +00009133 tpacpi_inputdev->dev.parent = &tpacpi_pdev->dev;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009134 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009135 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
9136 ret = ibm_init(&ibms_init[i]);
9137 if (ret >= 0 && *ibms_init[i].param)
9138 ret = ibms_init[i].data->write(ibms_init[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009139 if (ret < 0) {
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03009140 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07009141 return ret;
9142 }
9143 }
Henrique de Moraes Holschuhb589ea42010-02-25 21:28:58 -03009144
9145 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
9146
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009147 ret = input_register_device(tpacpi_inputdev);
9148 if (ret < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009149 printk(TPACPI_ERR "unable to register input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009150 thinkpad_acpi_module_exit();
9151 return ret;
9152 } else {
9153 tp_features.input_device_registered = 1;
9154 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009155
9156 return 0;
9157}
9158
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03009159MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
9160
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009161/*
Henrique de Moraes Holschuh922fe092009-04-14 02:44:13 +00009162 * This will autoload the driver in almost every ThinkPad
9163 * in widespread use.
9164 *
9165 * Only _VERY_ old models, like the 240, 240x and 570 lack
9166 * the HKEY event interface.
9167 */
9168MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
9169
9170/*
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009171 * DMI matching for module autoloading
9172 *
9173 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
9174 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
9175 *
9176 * Only models listed in thinkwiki will be supported, so add yours
9177 * if it is not there yet.
9178 */
9179#define IBM_BIOS_MODULE_ALIAS(__type) \
Mathieu Chouquet-Stringerb36a50f2009-03-14 16:35:26 +01009180 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009181
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009182/* Ancient thinkpad BIOSes have to be identified by
9183 * BIOS type or model number, and there are far less
9184 * BIOS types than model numbers... */
Henrique de Moraes Holschuh922fe092009-04-14 02:44:13 +00009185IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009186
Henrique de Moraes Holschuhf68f53a2009-04-14 02:44:12 +00009187MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
9188MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009189MODULE_DESCRIPTION(TPACPI_DESC);
9190MODULE_VERSION(TPACPI_VERSION);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009191MODULE_LICENSE("GPL");
9192
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03009193module_init(thinkpad_acpi_module_init);
9194module_exit(thinkpad_acpi_module_exit);