blob: 4d909d5a0340bf682333e52cc4fc37fe22bb9809 [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 Holschuhb57f7e72009-04-14 02:44:14 +000024#define TPACPI_VERSION "0.23"
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -020025#define TPACPI_SYSFS_VERSION 0x020700
Borislav Deianov78f81cc2005-08-17 00:00:00 -040026
27/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 * Changelog:
Henrique de Moraes Holschuh4b45cc02008-01-08 13:02:46 -020029 * 2007-10-20 changelog trimmed down
30 *
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -030031 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
32 * drivers/misc.
Henrique de Moraes Holschuhf9ff43a2006-11-25 16:37:38 -020033 *
34 * 2006-11-22 0.13 new maintainer
35 * changelog now lives in git commit history, and will
36 * not be updated further in-file.
Henrique de Moraes Holschuh837ca6d2007-03-23 17:33:54 -030037 *
Borislav Deianov78f81cc2005-08-17 00:00:00 -040038 * 2005-03-17 0.11 support for 600e, 770x
39 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
Henrique de Moraes Holschuh4b45cc02008-01-08 13:02:46 -020040 *
41 * 2005-01-16 0.9 use MODULE_VERSION
Borislav Deianov78f81cc2005-08-17 00:00:00 -040042 * thanks to Henrik Brix Andersen <brix@gentoo.org>
43 * fix parameter passing on module loading
44 * thanks to Rusty Russell <rusty@rustcorp.com.au>
45 * thanks to Jim Radford <radford@blackbean.org>
46 * 2004-11-08 0.8 fix init error case, don't return from a macro
47 * thanks to Chris Wright <chrisw@osdl.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 */
49
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020050#include <linux/kernel.h>
51#include <linux/module.h>
52#include <linux/init.h>
53#include <linux/types.h>
54#include <linux/string.h>
55#include <linux/list.h>
56#include <linux/mutex.h>
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +000057#include <linux/sched.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020058#include <linux/kthread.h>
59#include <linux/freezer.h>
60#include <linux/delay.h>
61
62#include <linux/nvram.h>
63#include <linux/proc_fs.h>
64#include <linux/sysfs.h>
65#include <linux/backlight.h>
66#include <linux/fb.h>
67#include <linux/platform_device.h>
68#include <linux/hwmon.h>
69#include <linux/hwmon-sysfs.h>
70#include <linux/input.h>
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -030071#include <linux/leds.h>
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -030072#include <linux/rfkill.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020073#include <asm/uaccess.h>
74
75#include <linux/dmi.h>
76#include <linux/jiffies.h>
77#include <linux/workqueue.h>
78
79#include <acpi/acpi_drivers.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020080
81#include <linux/pci_ids.h>
82
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020083
84/* ThinkPad CMOS commands */
85#define TP_CMOS_VOLUME_DOWN 0
86#define TP_CMOS_VOLUME_UP 1
87#define TP_CMOS_VOLUME_MUTE 2
88#define TP_CMOS_BRIGHTNESS_UP 4
89#define TP_CMOS_BRIGHTNESS_DOWN 5
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -030090#define TP_CMOS_THINKLIGHT_ON 12
91#define TP_CMOS_THINKLIGHT_OFF 13
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020092
93/* NVRAM Addresses */
94enum tp_nvram_addr {
95 TP_NVRAM_ADDR_HK2 = 0x57,
96 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
97 TP_NVRAM_ADDR_VIDEO = 0x59,
98 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
99 TP_NVRAM_ADDR_MIXER = 0x60,
100};
101
102/* NVRAM bit masks */
103enum {
104 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
105 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
106 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
107 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
108 TP_NVRAM_MASK_THINKLIGHT = 0x10,
109 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
110 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
111 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
112 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
113 TP_NVRAM_MASK_MUTE = 0x40,
114 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
115 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
116 TP_NVRAM_POS_LEVEL_VOLUME = 0,
117};
118
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200119/* ACPI HIDs */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200120#define TPACPI_ACPI_HKEY_HID "IBM0068"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200121
122/* Input IDs */
123#define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
124#define TPACPI_HKEY_INPUT_VERSION 0x4101
125
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -0200126/* ACPI \WGSV commands */
127enum {
128 TP_ACPI_WGSV_GET_STATE = 0x01, /* Get state information */
129 TP_ACPI_WGSV_PWR_ON_ON_RESUME = 0x02, /* Resume WWAN powered on */
130 TP_ACPI_WGSV_PWR_OFF_ON_RESUME = 0x03, /* Resume WWAN powered off */
131 TP_ACPI_WGSV_SAVE_STATE = 0x04, /* Save state for S4/S5 */
132};
133
134/* TP_ACPI_WGSV_GET_STATE bits */
135enum {
136 TP_ACPI_WGSV_STATE_WWANEXIST = 0x0001, /* WWAN hw available */
137 TP_ACPI_WGSV_STATE_WWANPWR = 0x0002, /* WWAN radio enabled */
138 TP_ACPI_WGSV_STATE_WWANPWRRES = 0x0004, /* WWAN state at resume */
139 TP_ACPI_WGSV_STATE_WWANBIOSOFF = 0x0008, /* WWAN disabled in BIOS */
140 TP_ACPI_WGSV_STATE_BLTHEXIST = 0x0001, /* BLTH hw available */
141 TP_ACPI_WGSV_STATE_BLTHPWR = 0x0002, /* BLTH radio enabled */
142 TP_ACPI_WGSV_STATE_BLTHPWRRES = 0x0004, /* BLTH state at resume */
143 TP_ACPI_WGSV_STATE_BLTHBIOSOFF = 0x0008, /* BLTH disabled in BIOS */
144 TP_ACPI_WGSV_STATE_UWBEXIST = 0x0010, /* UWB hw available */
145 TP_ACPI_WGSV_STATE_UWBPWR = 0x0020, /* UWB radio enabled */
146};
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200147
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -0300148/* HKEY events */
149enum tpacpi_hkey_event_t {
150 /* Hotkey-related */
151 TP_HKEY_EV_HOTKEY_BASE = 0x1001, /* first hotkey (FN+F1) */
152 TP_HKEY_EV_BRGHT_UP = 0x1010, /* Brightness up */
153 TP_HKEY_EV_BRGHT_DOWN = 0x1011, /* Brightness down */
154 TP_HKEY_EV_VOL_UP = 0x1015, /* Volume up or unmute */
155 TP_HKEY_EV_VOL_DOWN = 0x1016, /* Volume down or unmute */
156 TP_HKEY_EV_VOL_MUTE = 0x1017, /* Mixer output mute */
157
158 /* Reasons for waking up from S3/S4 */
159 TP_HKEY_EV_WKUP_S3_UNDOCK = 0x2304, /* undock requested, S3 */
160 TP_HKEY_EV_WKUP_S4_UNDOCK = 0x2404, /* undock requested, S4 */
161 TP_HKEY_EV_WKUP_S3_BAYEJ = 0x2305, /* bay ejection req, S3 */
162 TP_HKEY_EV_WKUP_S4_BAYEJ = 0x2405, /* bay ejection req, S4 */
163 TP_HKEY_EV_WKUP_S3_BATLOW = 0x2313, /* battery empty, S3 */
164 TP_HKEY_EV_WKUP_S4_BATLOW = 0x2413, /* battery empty, S4 */
165
166 /* Auto-sleep after eject request */
167 TP_HKEY_EV_BAYEJ_ACK = 0x3003, /* bay ejection complete */
168 TP_HKEY_EV_UNDOCK_ACK = 0x4003, /* undock complete */
169
170 /* Misc bay events */
171 TP_HKEY_EV_OPTDRV_EJ = 0x3006, /* opt. drive tray ejected */
172
173 /* User-interface events */
174 TP_HKEY_EV_LID_CLOSE = 0x5001, /* laptop lid closed */
175 TP_HKEY_EV_LID_OPEN = 0x5002, /* laptop lid opened */
176 TP_HKEY_EV_TABLET_TABLET = 0x5009, /* tablet swivel up */
177 TP_HKEY_EV_TABLET_NOTEBOOK = 0x500a, /* tablet swivel down */
178 TP_HKEY_EV_PEN_INSERTED = 0x500b, /* tablet pen inserted */
179 TP_HKEY_EV_PEN_REMOVED = 0x500c, /* tablet pen removed */
180 TP_HKEY_EV_BRGHT_CHANGED = 0x5010, /* backlight control event */
181
182 /* Thermal events */
183 TP_HKEY_EV_ALARM_BAT_HOT = 0x6011, /* battery too hot */
184 TP_HKEY_EV_ALARM_BAT_XHOT = 0x6012, /* battery critically hot */
185 TP_HKEY_EV_ALARM_SENSOR_HOT = 0x6021, /* sensor too hot */
186 TP_HKEY_EV_ALARM_SENSOR_XHOT = 0x6022, /* sensor critically hot */
187 TP_HKEY_EV_THM_TABLE_CHANGED = 0x6030, /* thermal table changed */
188
189 /* Misc */
190 TP_HKEY_EV_RFKILL_CHANGED = 0x7000, /* rfkill switch changed */
191};
192
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200193/****************************************************************************
194 * Main driver
195 */
196
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200197#define TPACPI_NAME "thinkpad"
198#define TPACPI_DESC "ThinkPad ACPI Extras"
199#define TPACPI_FILE TPACPI_NAME "_acpi"
200#define TPACPI_URL "http://ibm-acpi.sf.net/"
201#define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200202
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200203#define TPACPI_PROC_DIR "ibm"
204#define TPACPI_ACPI_EVENT_PREFIX "ibm"
205#define TPACPI_DRVR_NAME TPACPI_FILE
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300206#define TPACPI_DRVR_SHORTNAME "tpacpi"
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200207#define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200208
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300209#define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -0300210#define TPACPI_WORKQUEUE_NAME "ktpacpid"
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300211
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200212#define TPACPI_MAX_ACPI_ARGS 3
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200213
Henrique de Moraes Holschuh7ff8d622009-04-04 04:25:46 +0000214/* printk headers */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200215#define TPACPI_LOG TPACPI_FILE ": "
Henrique de Moraes Holschuh7ff8d622009-04-04 04:25:46 +0000216#define TPACPI_EMERG KERN_EMERG TPACPI_LOG
217#define TPACPI_ALERT KERN_ALERT TPACPI_LOG
218#define TPACPI_CRIT KERN_CRIT TPACPI_LOG
219#define TPACPI_ERR KERN_ERR TPACPI_LOG
220#define TPACPI_WARN KERN_WARNING TPACPI_LOG
221#define TPACPI_NOTICE KERN_NOTICE TPACPI_LOG
222#define TPACPI_INFO KERN_INFO TPACPI_LOG
223#define TPACPI_DEBUG KERN_DEBUG TPACPI_LOG
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200224
Henrique de Moraes Holschuh7ff8d622009-04-04 04:25:46 +0000225/* Debugging printk groups */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200226#define TPACPI_DBG_ALL 0xffff
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +0000227#define TPACPI_DBG_DISCLOSETASK 0x8000
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200228#define TPACPI_DBG_INIT 0x0001
229#define TPACPI_DBG_EXIT 0x0002
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +0000230#define TPACPI_DBG_RFKILL 0x0004
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +0000231#define TPACPI_DBG_HKEY 0x0008
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +0000232#define TPACPI_DBG_FAN 0x0010
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +0000233#define TPACPI_DBG_BRGHT 0x0020
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -0200234#define TPACPI_DBG_MIXER 0x0040
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200235
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200236#define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
237#define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
238#define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200239
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200240
241/****************************************************************************
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200242 * Driver-wide structs and misc. variables
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200243 */
244
245struct ibm_struct;
246
247struct tp_acpi_drv_struct {
248 const struct acpi_device_id *hid;
249 struct acpi_driver *driver;
250
251 void (*notify) (struct ibm_struct *, u32);
252 acpi_handle *handle;
253 u32 type;
254 struct acpi_device *device;
255};
256
257struct ibm_struct {
258 char *name;
259
260 int (*read) (char *);
261 int (*write) (char *);
262 void (*exit) (void);
263 void (*resume) (void);
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200264 void (*suspend) (pm_message_t state);
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200265 void (*shutdown) (void);
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200266
267 struct list_head all_drivers;
268
269 struct tp_acpi_drv_struct *acpi;
270
271 struct {
272 u8 acpi_driver_registered:1;
273 u8 acpi_notify_installed:1;
274 u8 proc_created:1;
275 u8 init_called:1;
276 u8 experimental:1;
277 } flags;
278};
279
280struct ibm_init_struct {
281 char param[32];
282
283 int (*init) (struct ibm_init_struct *);
284 struct ibm_struct *data;
285};
286
287static struct {
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200288 u32 bluetooth:1;
289 u32 hotkey:1;
290 u32 hotkey_mask:1;
291 u32 hotkey_wlsw:1;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -0200292 u32 hotkey_tablet:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200293 u32 light:1;
294 u32 light_status:1;
295 u32 bright_16levels:1;
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -0300296 u32 bright_acpimode:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200297 u32 wan:1;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -0200298 u32 uwb:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200299 u32 fan_ctrl_status_undef:1;
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -0300300 u32 second_fan:1;
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -0300301 u32 beep_needs_two_args:1;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -0200302 u32 mixer_no_level_control:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200303 u32 input_device_registered:1;
304 u32 platform_drv_registered:1;
305 u32 platform_drv_attrs_registered:1;
306 u32 sensors_pdrv_registered:1;
307 u32 sensors_pdrv_attrs_registered:1;
308 u32 sensors_pdev_attrs_registered:1;
309 u32 hotkey_poll_active:1;
310} tp_features;
311
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -0300312static struct {
313 u16 hotkey_mask_ff:1;
314} tp_warned;
315
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200316struct thinkpad_id_data {
317 unsigned int vendor; /* ThinkPad vendor:
318 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
319
320 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
321 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
322
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -0300323 u16 bios_model; /* 1Y = 0x5931, 0 = unknown */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200324 u16 ec_model;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -0300325 u16 bios_release; /* 1ZETK1WW = 0x314b, 0 = unknown */
326 u16 ec_release;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200327
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -0300328 char *model_str; /* ThinkPad T43 */
329 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200330};
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200331static struct thinkpad_id_data thinkpad_id;
332
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300333static enum {
334 TPACPI_LIFE_INIT = 0,
335 TPACPI_LIFE_RUNNING,
336 TPACPI_LIFE_EXITING,
337} tpacpi_lifecycle;
338
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200339static int experimental;
340static u32 dbg_level;
341
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -0300342static struct workqueue_struct *tpacpi_wq;
343
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +0000344enum led_status_t {
345 TPACPI_LED_OFF = 0,
346 TPACPI_LED_ON,
347 TPACPI_LED_BLINK,
348};
349
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -0300350/* Special LED class that can defer work */
351struct tpacpi_led_classdev {
352 struct led_classdev led_classdev;
353 struct work_struct work;
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +0000354 enum led_status_t new_state;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -0300355 unsigned int led;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -0300356};
357
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200358#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
359static int dbg_wlswemul;
360static int tpacpi_wlsw_emulstate;
361static int dbg_bluetoothemul;
362static int tpacpi_bluetooth_emulstate;
363static int dbg_wwanemul;
364static int tpacpi_wwan_emulstate;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -0200365static int dbg_uwbemul;
366static int tpacpi_uwb_emulstate;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200367#endif
368
369
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000370/*************************************************************************
371 * Debugging helpers
372 */
373
374#define dbg_printk(a_dbg_level, format, arg...) \
375 do { if (dbg_level & (a_dbg_level)) \
376 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
377 } while (0)
378
379#ifdef CONFIG_THINKPAD_ACPI_DEBUG
380#define vdbg_printk dbg_printk
381static const char *str_supported(int is_supported);
382#else
383#define vdbg_printk(a_dbg_level, format, arg...) \
384 do { } while (0)
385#endif
386
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +0000387static void tpacpi_log_usertask(const char * const what)
388{
389 printk(TPACPI_DEBUG "%s: access by process with PID %d\n",
390 what, task_tgid_vnr(current));
391}
392
393#define tpacpi_disclose_usertask(what, format, arg...) \
394 do { \
395 if (unlikely( \
396 (dbg_level & TPACPI_DBG_DISCLOSETASK) && \
397 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
398 printk(TPACPI_DEBUG "%s: PID %d: " format, \
399 what, task_tgid_vnr(current), ## arg); \
400 } \
401 } while (0)
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000402
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -0300403/*
404 * Quirk handling helpers
405 *
406 * ThinkPad IDs and versions seen in the field so far
407 * are two-characters from the set [0-9A-Z], i.e. base 36.
408 *
409 * We use values well outside that range as specials.
410 */
411
412#define TPACPI_MATCH_ANY 0xffffU
413#define TPACPI_MATCH_UNKNOWN 0U
414
415/* TPID('1', 'Y') == 0x5931 */
416#define TPID(__c1, __c2) (((__c2) << 8) | (__c1))
417
418#define TPACPI_Q_IBM(__id1, __id2, __quirk) \
419 { .vendor = PCI_VENDOR_ID_IBM, \
420 .bios = TPID(__id1, __id2), \
421 .ec = TPACPI_MATCH_ANY, \
422 .quirks = (__quirk) }
423
424#define TPACPI_Q_LNV(__id1, __id2, __quirk) \
425 { .vendor = PCI_VENDOR_ID_LENOVO, \
426 .bios = TPID(__id1, __id2), \
427 .ec = TPACPI_MATCH_ANY, \
428 .quirks = (__quirk) }
429
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -0200430#define TPACPI_QEC_LNV(__id1, __id2, __quirk) \
431 { .vendor = PCI_VENDOR_ID_LENOVO, \
432 .bios = TPACPI_MATCH_ANY, \
433 .ec = TPID(__id1, __id2), \
434 .quirks = (__quirk) }
435
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -0300436struct tpacpi_quirk {
437 unsigned int vendor;
438 u16 bios;
439 u16 ec;
440 unsigned long quirks;
441};
442
443/**
444 * tpacpi_check_quirks() - search BIOS/EC version on a list
445 * @qlist: array of &struct tpacpi_quirk
446 * @qlist_size: number of elements in @qlist
447 *
448 * Iterates over a quirks list until one is found that matches the
449 * ThinkPad's vendor, BIOS and EC model.
450 *
451 * Returns 0 if nothing matches, otherwise returns the quirks field of
452 * the matching &struct tpacpi_quirk entry.
453 *
454 * The match criteria is: vendor, ec and bios much match.
455 */
456static unsigned long __init tpacpi_check_quirks(
457 const struct tpacpi_quirk *qlist,
458 unsigned int qlist_size)
459{
460 while (qlist_size) {
461 if ((qlist->vendor == thinkpad_id.vendor ||
462 qlist->vendor == TPACPI_MATCH_ANY) &&
463 (qlist->bios == thinkpad_id.bios_model ||
464 qlist->bios == TPACPI_MATCH_ANY) &&
465 (qlist->ec == thinkpad_id.ec_model ||
466 qlist->ec == TPACPI_MATCH_ANY))
467 return qlist->quirks;
468
469 qlist_size--;
470 qlist++;
471 }
472 return 0;
473}
474
475
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300476/****************************************************************************
477 ****************************************************************************
478 *
479 * ACPI Helpers and device model
480 *
481 ****************************************************************************
482 ****************************************************************************/
483
484/*************************************************************************
485 * ACPI basic handles
486 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -0300488static acpi_handle root_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200490#define TPACPI_HANDLE(object, parent, paths...) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 static acpi_handle object##_handle; \
492 static acpi_handle *object##_parent = &parent##_handle; \
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400493 static char *object##_path; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 static char *object##_paths[] = { paths }
495
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200496TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400497 "\\_SB.PCI.ISA.EC", /* 570 */
498 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
499 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
500 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
501 "\\_SB.PCI0.ICH3.EC0", /* R31 */
502 "\\_SB.PCI0.LPC.EC", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300503 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200505TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
506TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200508TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
509 /* T4x, X31, X40 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400510 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
511 "\\CMS", /* R40, R40e */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300512 ); /* all others */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400513
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200514TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400515 "^HKEY", /* R30, R31 */
516 "HKEY", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300517 ); /* 570 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400518
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -0200519TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
520 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
521 "\\_SB.PCI0.VID0", /* 770e */
522 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
523 "\\_SB.PCI0.AGP.VID", /* all others */
524 ); /* R30, R31 */
525
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400526
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300527/*************************************************************************
528 * ACPI helpers
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -0200529 */
530
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531static int acpi_evalf(acpi_handle handle,
532 void *res, char *method, char *fmt, ...)
533{
534 char *fmt0 = fmt;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400535 struct acpi_object_list params;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200536 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400537 struct acpi_buffer result, *resultp;
538 union acpi_object out_obj;
539 acpi_status status;
540 va_list ap;
541 char res_type;
542 int success;
543 int quiet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
545 if (!*fmt) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200546 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 return 0;
548 }
549
550 if (*fmt == 'q') {
551 quiet = 1;
552 fmt++;
553 } else
554 quiet = 0;
555
556 res_type = *(fmt++);
557
558 params.count = 0;
559 params.pointer = &in_objs[0];
560
561 va_start(ap, fmt);
562 while (*fmt) {
563 char c = *(fmt++);
564 switch (c) {
565 case 'd': /* int */
566 in_objs[params.count].integer.value = va_arg(ap, int);
567 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
568 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400569 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 default:
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200571 printk(TPACPI_ERR "acpi_evalf() called "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 "with invalid format character '%c'\n", c);
573 return 0;
574 }
575 }
576 va_end(ap);
577
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400578 if (res_type != 'v') {
579 result.length = sizeof(out_obj);
580 result.pointer = &out_obj;
581 resultp = &result;
582 } else
583 resultp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400585 status = acpi_evaluate_object(handle, method, &params, resultp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
587 switch (res_type) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400588 case 'd': /* int */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 if (res)
590 *(int *)res = out_obj.integer.value;
591 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
592 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400593 case 'v': /* void */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 success = status == AE_OK;
595 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400596 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 default:
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200598 printk(TPACPI_ERR "acpi_evalf() called "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 "with invalid format character '%c'\n", res_type);
600 return 0;
601 }
602
603 if (!success && !quiet)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200604 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 method, fmt0, status);
606
607 return success;
608}
609
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200610static int acpi_ec_read(int i, u8 *p)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300611{
612 int v;
613
614 if (ecrd_handle) {
615 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
616 return 0;
617 *p = v;
618 } else {
619 if (ec_read(i, p) < 0)
620 return 0;
621 }
622
623 return 1;
624}
625
626static int acpi_ec_write(int i, u8 v)
627{
628 if (ecwr_handle) {
629 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
630 return 0;
631 } else {
632 if (ec_write(i, v) < 0)
633 return 0;
634 }
635
636 return 1;
637}
638
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -0300639static int issue_thinkpad_cmos_command(int cmos_cmd)
640{
641 if (!cmos_handle)
642 return -ENXIO;
643
644 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
645 return -EIO;
646
647 return 0;
648}
649
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300650/*************************************************************************
651 * ACPI device model
652 */
653
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200654#define TPACPI_ACPIHANDLE_INIT(object) \
655 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200656 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
657
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300658static void drv_acpi_handle_init(char *name,
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300659 acpi_handle *handle, acpi_handle parent,
660 char **paths, int num_paths, char **path)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300661{
662 int i;
663 acpi_status status;
664
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300665 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
666 name);
667
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300668 for (i = 0; i < num_paths; i++) {
669 status = acpi_get_handle(parent, paths[i], handle);
670 if (ACPI_SUCCESS(status)) {
671 *path = paths[i];
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300672 dbg_printk(TPACPI_DBG_INIT,
673 "Found ACPI handle %s for %s\n",
674 *path, name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300675 return;
676 }
677 }
678
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300679 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
680 name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300681 *handle = NULL;
682}
683
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300684static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300685{
686 struct ibm_struct *ibm = data;
687
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300688 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
689 return;
690
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300691 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300692 return;
693
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300694 ibm->acpi->notify(ibm, event);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300695}
696
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300697static int __init setup_acpi_notify(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300698{
699 acpi_status status;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300700 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300701
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300702 BUG_ON(!ibm->acpi);
703
704 if (!*ibm->acpi->handle)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300705 return 0;
706
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300707 vdbg_printk(TPACPI_DBG_INIT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300708 "setting up ACPI notify for %s\n", ibm->name);
709
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300710 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
711 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200712 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300713 ibm->name, rc);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300714 return -ENODEV;
715 }
716
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700717 ibm->acpi->device->driver_data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300718 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200719 TPACPI_ACPI_EVENT_PREFIX,
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -0300720 ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300721
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300722 status = acpi_install_notify_handler(*ibm->acpi->handle,
723 ibm->acpi->type, dispatch_acpi_notify, ibm);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300724 if (ACPI_FAILURE(status)) {
725 if (status == AE_ALREADY_EXISTS) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200726 printk(TPACPI_NOTICE
727 "another device driver is already "
728 "handling %s events\n", ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300729 } else {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200730 printk(TPACPI_ERR
731 "acpi_install_notify_handler(%s) failed: %d\n",
732 ibm->name, status);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300733 }
734 return -ENODEV;
735 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300736 ibm->flags.acpi_notify_installed = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300737 return 0;
738}
739
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300740static int __init tpacpi_device_add(struct acpi_device *device)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300741{
742 return 0;
743}
744
Henrique de Moraes Holschuh67001212007-04-21 11:08:25 -0300745static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300746{
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300747 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300748
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300749 dbg_printk(TPACPI_DBG_INIT,
750 "registering %s as an ACPI driver\n", ibm->name);
751
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300752 BUG_ON(!ibm->acpi);
753
754 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
755 if (!ibm->acpi->driver) {
Mariusz Kozlowski4f778b92008-10-18 14:23:53 -0300756 printk(TPACPI_ERR
757 "failed to allocate memory for ibm->acpi->driver\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300758 return -ENOMEM;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300759 }
760
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200761 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300762 ibm->acpi->driver->ids = ibm->acpi->hid;
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200763
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300764 ibm->acpi->driver->ops.add = &tpacpi_device_add;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300765
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300766 rc = acpi_bus_register_driver(ibm->acpi->driver);
767 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200768 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200769 ibm->name, rc);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300770 kfree(ibm->acpi->driver);
771 ibm->acpi->driver = NULL;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300772 } else if (!rc)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300773 ibm->flags.acpi_driver_registered = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300774
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300775 return rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300776}
777
778
779/****************************************************************************
780 ****************************************************************************
781 *
782 * Procfs Helpers
783 *
784 ****************************************************************************
785 ****************************************************************************/
786
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300787static int dispatch_procfs_read(char *page, char **start, off_t off,
788 int count, int *eof, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300789{
790 struct ibm_struct *ibm = data;
791 int len;
792
793 if (!ibm || !ibm->read)
794 return -EINVAL;
795
796 len = ibm->read(page);
797 if (len < 0)
798 return len;
799
800 if (len <= off + count)
801 *eof = 1;
802 *start = page + off;
803 len -= off;
804 if (len > count)
805 len = count;
806 if (len < 0)
807 len = 0;
808
809 return len;
810}
811
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300812static int dispatch_procfs_write(struct file *file,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200813 const char __user *userbuf,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300814 unsigned long count, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300815{
816 struct ibm_struct *ibm = data;
817 char *kernbuf;
818 int ret;
819
820 if (!ibm || !ibm->write)
821 return -EINVAL;
Michael Buesch5b05d462009-08-01 12:04:19 -0300822 if (count > PAGE_SIZE - 2)
823 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300824
825 kernbuf = kmalloc(count + 2, GFP_KERNEL);
826 if (!kernbuf)
827 return -ENOMEM;
828
829 if (copy_from_user(kernbuf, userbuf, count)) {
830 kfree(kernbuf);
831 return -EFAULT;
832 }
833
834 kernbuf[count] = 0;
835 strcat(kernbuf, ",");
836 ret = ibm->write(kernbuf);
837 if (ret == 0)
838 ret = count;
839
840 kfree(kernbuf);
841
842 return ret;
843}
844
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845static char *next_cmd(char **cmds)
846{
847 char *start = *cmds;
848 char *end;
849
850 while ((end = strchr(start, ',')) && end == start)
851 start = end + 1;
852
853 if (!end)
854 return NULL;
855
856 *end = 0;
857 *cmds = end + 1;
858 return start;
859}
860
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300861
862/****************************************************************************
863 ****************************************************************************
864 *
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300865 * Device model: input, hwmon and platform
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300866 *
867 ****************************************************************************
868 ****************************************************************************/
869
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -0300870static struct platform_device *tpacpi_pdev;
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300871static struct platform_device *tpacpi_sensors_pdev;
Tony Jones1beeffe2007-08-20 13:46:20 -0700872static struct device *tpacpi_hwmon;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300873static struct input_dev *tpacpi_inputdev;
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -0300874static struct mutex tpacpi_inputdev_send_mutex;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200875static LIST_HEAD(tpacpi_all_drivers);
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300876
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200877static int tpacpi_suspend_handler(struct platform_device *pdev,
878 pm_message_t state)
879{
880 struct ibm_struct *ibm, *itmp;
881
882 list_for_each_entry_safe(ibm, itmp,
883 &tpacpi_all_drivers,
884 all_drivers) {
885 if (ibm->suspend)
886 (ibm->suspend)(state);
887 }
888
889 return 0;
890}
891
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300892static int tpacpi_resume_handler(struct platform_device *pdev)
893{
894 struct ibm_struct *ibm, *itmp;
895
896 list_for_each_entry_safe(ibm, itmp,
897 &tpacpi_all_drivers,
898 all_drivers) {
899 if (ibm->resume)
900 (ibm->resume)();
901 }
902
903 return 0;
904}
905
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200906static void tpacpi_shutdown_handler(struct platform_device *pdev)
907{
908 struct ibm_struct *ibm, *itmp;
909
910 list_for_each_entry_safe(ibm, itmp,
911 &tpacpi_all_drivers,
912 all_drivers) {
913 if (ibm->shutdown)
914 (ibm->shutdown)();
915 }
916}
917
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300918static struct platform_driver tpacpi_pdriver = {
919 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200920 .name = TPACPI_DRVR_NAME,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300921 .owner = THIS_MODULE,
922 },
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200923 .suspend = tpacpi_suspend_handler,
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300924 .resume = tpacpi_resume_handler,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200925 .shutdown = tpacpi_shutdown_handler,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300926};
927
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300928static struct platform_driver tpacpi_hwmon_pdriver = {
929 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200930 .name = TPACPI_HWMON_DRVR_NAME,
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300931 .owner = THIS_MODULE,
932 },
933};
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300934
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -0300935/*************************************************************************
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300936 * sysfs support helpers
937 */
938
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200939struct attribute_set {
940 unsigned int members, max_members;
941 struct attribute_group group;
942};
943
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300944struct attribute_set_obj {
945 struct attribute_set s;
946 struct attribute *a;
947} __attribute__((packed));
948
949static struct attribute_set *create_attr_set(unsigned int max_members,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200950 const char *name)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300951{
952 struct attribute_set_obj *sobj;
953
954 if (max_members == 0)
955 return NULL;
956
957 /* Allocates space for implicit NULL at the end too */
958 sobj = kzalloc(sizeof(struct attribute_set_obj) +
959 max_members * sizeof(struct attribute *),
960 GFP_KERNEL);
961 if (!sobj)
962 return NULL;
963 sobj->s.max_members = max_members;
964 sobj->s.group.attrs = &sobj->a;
965 sobj->s.group.name = name;
966
967 return &sobj->s;
968}
969
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200970#define destroy_attr_set(_set) \
971 kfree(_set);
972
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300973/* not multi-threaded safe, use it in a single thread per set */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200974static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300975{
976 if (!s || !attr)
977 return -EINVAL;
978
979 if (s->members >= s->max_members)
980 return -ENOMEM;
981
982 s->group.attrs[s->members] = attr;
983 s->members++;
984
985 return 0;
986}
987
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200988static int add_many_to_attr_set(struct attribute_set *s,
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300989 struct attribute **attr,
990 unsigned int count)
991{
992 int i, res;
993
994 for (i = 0; i < count; i++) {
995 res = add_to_attr_set(s, attr[i]);
996 if (res)
997 return res;
998 }
999
1000 return 0;
1001}
1002
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001003static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001004{
1005 sysfs_remove_group(kobj, &s->group);
1006 destroy_attr_set(s);
1007}
1008
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001009#define register_attr_set_with_sysfs(_attr_set, _kobj) \
1010 sysfs_create_group(_kobj, &_attr_set->group)
1011
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001012static int parse_strtoul(const char *buf,
1013 unsigned long max, unsigned long *value)
1014{
1015 char *endp;
1016
Henrique de Moraes Holschuh32afbf02007-10-08 10:12:56 -03001017 while (*buf && isspace(*buf))
1018 buf++;
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001019 *value = simple_strtoul(buf, &endp, 0);
1020 while (*endp && isspace(*endp))
1021 endp++;
1022 if (*endp || *value > max)
1023 return -EINVAL;
1024
1025 return 0;
1026}
1027
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03001028static void tpacpi_disable_brightness_delay(void)
1029{
1030 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
1031 printk(TPACPI_NOTICE
1032 "ACPI backlight control delay disabled\n");
1033}
1034
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03001035static int __init tpacpi_query_bcl_levels(acpi_handle handle)
1036{
1037 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1038 union acpi_object *obj;
1039 int rc;
1040
1041 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
1042 obj = (union acpi_object *)buffer.pointer;
1043 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
1044 printk(TPACPI_ERR "Unknown _BCL data, "
1045 "please report this to %s\n", TPACPI_MAIL);
1046 rc = 0;
1047 } else {
1048 rc = obj->package.count;
1049 }
1050 } else {
1051 return 0;
1052 }
1053
1054 kfree(buffer.pointer);
1055 return rc;
1056}
1057
1058static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
1059 u32 lvl, void *context, void **rv)
1060{
1061 char name[ACPI_PATH_SEGMENT_LENGTH];
1062 struct acpi_buffer buffer = { sizeof(name), &name };
1063
1064 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
1065 !strncmp("_BCL", name, sizeof(name) - 1)) {
1066 BUG_ON(!rv || !*rv);
1067 **(int **)rv = tpacpi_query_bcl_levels(handle);
1068 return AE_CTRL_TERMINATE;
1069 } else {
1070 return AE_OK;
1071 }
1072}
1073
1074/*
1075 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
1076 */
1077static int __init tpacpi_check_std_acpi_brightness_support(void)
1078{
1079 int status;
1080 int bcl_levels = 0;
1081 void *bcl_ptr = &bcl_levels;
1082
1083 if (!vid_handle) {
1084 TPACPI_ACPIHANDLE_INIT(vid);
1085 }
1086 if (!vid_handle)
1087 return 0;
1088
1089 /*
1090 * Search for a _BCL method, and execute it. This is safe on all
1091 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
1092 * BIOS in ACPI backlight control mode. We do NOT have to care
1093 * about calling the _BCL method in an enabled video device, any
1094 * will do for our purposes.
1095 */
1096
1097 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
1098 tpacpi_acpi_walk_find_bcl, NULL,
1099 &bcl_ptr);
1100
1101 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
1102 tp_features.bright_acpimode = 1;
1103 return (bcl_levels - 2);
1104 }
1105
1106 return 0;
1107}
1108
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00001109static void printk_deprecated_attribute(const char * const what,
1110 const char * const details)
1111{
1112 tpacpi_log_usertask("deprecated sysfs attribute");
1113 printk(TPACPI_WARN "WARNING: sysfs attribute %s is deprecated and "
1114 "will be removed. %s\n",
1115 what, details);
1116}
1117
Johannes Berg19d337d2009-06-02 13:01:37 +02001118/*************************************************************************
1119 * rfkill and radio control support helpers
1120 */
1121
1122/*
1123 * ThinkPad-ACPI firmware handling model:
1124 *
1125 * WLSW (master wireless switch) is event-driven, and is common to all
1126 * firmware-controlled radios. It cannot be controlled, just monitored,
1127 * as expected. It overrides all radio state in firmware
1128 *
1129 * The kernel, a masked-off hotkey, and WLSW can change the radio state
1130 * (TODO: verify how WLSW interacts with the returned radio state).
1131 *
1132 * The only time there are shadow radio state changes, is when
1133 * masked-off hotkeys are used.
1134 */
1135
1136/*
1137 * Internal driver API for radio state:
1138 *
1139 * int: < 0 = error, otherwise enum tpacpi_rfkill_state
1140 * bool: true means radio blocked (off)
1141 */
1142enum tpacpi_rfkill_state {
1143 TPACPI_RFK_RADIO_OFF = 0,
1144 TPACPI_RFK_RADIO_ON
1145};
1146
1147/* rfkill switches */
1148enum tpacpi_rfk_id {
1149 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
1150 TPACPI_RFK_WWAN_SW_ID,
1151 TPACPI_RFK_UWB_SW_ID,
1152 TPACPI_RFK_SW_MAX
1153};
1154
1155static const char *tpacpi_rfkill_names[] = {
1156 [TPACPI_RFK_BLUETOOTH_SW_ID] = "bluetooth",
1157 [TPACPI_RFK_WWAN_SW_ID] = "wwan",
1158 [TPACPI_RFK_UWB_SW_ID] = "uwb",
1159 [TPACPI_RFK_SW_MAX] = NULL
1160};
1161
1162/* ThinkPad-ACPI rfkill subdriver */
1163struct tpacpi_rfk {
1164 struct rfkill *rfkill;
1165 enum tpacpi_rfk_id id;
1166 const struct tpacpi_rfk_ops *ops;
1167};
1168
1169struct tpacpi_rfk_ops {
1170 /* firmware interface */
1171 int (*get_status)(void);
1172 int (*set_status)(const enum tpacpi_rfkill_state);
1173};
1174
1175static struct tpacpi_rfk *tpacpi_rfkill_switches[TPACPI_RFK_SW_MAX];
1176
1177/* Query FW and update rfkill sw state for a given rfkill switch */
1178static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk *tp_rfk)
1179{
1180 int status;
1181
1182 if (!tp_rfk)
1183 return -ENODEV;
1184
1185 status = (tp_rfk->ops->get_status)();
1186 if (status < 0)
1187 return status;
1188
1189 rfkill_set_sw_state(tp_rfk->rfkill,
1190 (status == TPACPI_RFK_RADIO_OFF));
1191
1192 return status;
1193}
1194
1195/* Query FW and update rfkill sw state for all rfkill switches */
1196static void tpacpi_rfk_update_swstate_all(void)
1197{
1198 unsigned int i;
1199
1200 for (i = 0; i < TPACPI_RFK_SW_MAX; i++)
1201 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[i]);
1202}
1203
1204/*
1205 * Sync the HW-blocking state of all rfkill switches,
1206 * do notice it causes the rfkill core to schedule uevents
1207 */
1208static void tpacpi_rfk_update_hwblock_state(bool blocked)
1209{
1210 unsigned int i;
1211 struct tpacpi_rfk *tp_rfk;
1212
1213 for (i = 0; i < TPACPI_RFK_SW_MAX; i++) {
1214 tp_rfk = tpacpi_rfkill_switches[i];
1215 if (tp_rfk) {
1216 if (rfkill_set_hw_state(tp_rfk->rfkill,
1217 blocked)) {
1218 /* ignore -- we track sw block */
1219 }
1220 }
1221 }
1222}
1223
1224/* Call to get the WLSW state from the firmware */
1225static int hotkey_get_wlsw(void);
1226
1227/* Call to query WLSW state and update all rfkill switches */
1228static bool tpacpi_rfk_check_hwblock_state(void)
1229{
1230 int res = hotkey_get_wlsw();
1231 int hw_blocked;
1232
1233 /* When unknown or unsupported, we have to assume it is unblocked */
1234 if (res < 0)
1235 return false;
1236
1237 hw_blocked = (res == TPACPI_RFK_RADIO_OFF);
1238 tpacpi_rfk_update_hwblock_state(hw_blocked);
1239
1240 return hw_blocked;
1241}
1242
1243static int tpacpi_rfk_hook_set_block(void *data, bool blocked)
1244{
1245 struct tpacpi_rfk *tp_rfk = data;
1246 int res;
1247
1248 dbg_printk(TPACPI_DBG_RFKILL,
1249 "request to change radio state to %s\n",
1250 blocked ? "blocked" : "unblocked");
1251
1252 /* try to set radio state */
1253 res = (tp_rfk->ops->set_status)(blocked ?
1254 TPACPI_RFK_RADIO_OFF : TPACPI_RFK_RADIO_ON);
1255
1256 /* and update the rfkill core with whatever the FW really did */
1257 tpacpi_rfk_update_swstate(tp_rfk);
1258
1259 return (res < 0) ? res : 0;
1260}
1261
1262static const struct rfkill_ops tpacpi_rfk_rfkill_ops = {
1263 .set_block = tpacpi_rfk_hook_set_block,
1264};
1265
1266static int __init tpacpi_new_rfkill(const enum tpacpi_rfk_id id,
1267 const struct tpacpi_rfk_ops *tp_rfkops,
1268 const enum rfkill_type rfktype,
1269 const char *name,
1270 const bool set_default)
1271{
1272 struct tpacpi_rfk *atp_rfk;
1273 int res;
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001274 bool sw_state = false;
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001275 bool hw_state;
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001276 int sw_status;
Johannes Berg19d337d2009-06-02 13:01:37 +02001277
1278 BUG_ON(id >= TPACPI_RFK_SW_MAX || tpacpi_rfkill_switches[id]);
1279
Johannes Berg19d337d2009-06-02 13:01:37 +02001280 atp_rfk = kzalloc(sizeof(struct tpacpi_rfk), GFP_KERNEL);
1281 if (atp_rfk)
1282 atp_rfk->rfkill = rfkill_alloc(name,
1283 &tpacpi_pdev->dev,
1284 rfktype,
1285 &tpacpi_rfk_rfkill_ops,
1286 atp_rfk);
1287 if (!atp_rfk || !atp_rfk->rfkill) {
1288 printk(TPACPI_ERR
1289 "failed to allocate memory for rfkill class\n");
1290 kfree(atp_rfk);
1291 return -ENOMEM;
1292 }
1293
1294 atp_rfk->id = id;
1295 atp_rfk->ops = tp_rfkops;
1296
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001297 sw_status = (tp_rfkops->get_status)();
1298 if (sw_status < 0) {
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001299 printk(TPACPI_ERR
1300 "failed to read initial state for %s, error %d\n",
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001301 name, sw_status);
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001302 } else {
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001303 sw_state = (sw_status == TPACPI_RFK_RADIO_OFF);
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001304 if (set_default) {
1305 /* try to keep the initial state, since we ask the
1306 * firmware to preserve it across S5 in NVRAM */
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001307 rfkill_init_sw_state(atp_rfk->rfkill, sw_state);
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001308 }
1309 }
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001310 hw_state = tpacpi_rfk_check_hwblock_state();
1311 rfkill_set_hw_state(atp_rfk->rfkill, hw_state);
Johannes Berg19d337d2009-06-02 13:01:37 +02001312
1313 res = rfkill_register(atp_rfk->rfkill);
1314 if (res < 0) {
1315 printk(TPACPI_ERR
1316 "failed to register %s rfkill switch: %d\n",
1317 name, res);
1318 rfkill_destroy(atp_rfk->rfkill);
1319 kfree(atp_rfk);
1320 return res;
1321 }
1322
1323 tpacpi_rfkill_switches[id] = atp_rfk;
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001324
1325 printk(TPACPI_INFO "rfkill switch %s: radio is %sblocked\n",
1326 name, (sw_state || hw_state) ? "" : "un");
Johannes Berg19d337d2009-06-02 13:01:37 +02001327 return 0;
1328}
1329
1330static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id)
1331{
1332 struct tpacpi_rfk *tp_rfk;
1333
1334 BUG_ON(id >= TPACPI_RFK_SW_MAX);
1335
1336 tp_rfk = tpacpi_rfkill_switches[id];
1337 if (tp_rfk) {
1338 rfkill_unregister(tp_rfk->rfkill);
Corentin Chary5f0dadb2009-09-14 12:43:52 +02001339 rfkill_destroy(tp_rfk->rfkill);
Johannes Berg19d337d2009-06-02 13:01:37 +02001340 tpacpi_rfkill_switches[id] = NULL;
1341 kfree(tp_rfk);
1342 }
1343}
1344
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00001345static void printk_deprecated_rfkill_attribute(const char * const what)
1346{
1347 printk_deprecated_attribute(what,
1348 "Please switch to generic rfkill before year 2010");
1349}
1350
Johannes Berg19d337d2009-06-02 13:01:37 +02001351/* sysfs <radio> enable ------------------------------------------------ */
1352static ssize_t tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id,
1353 struct device_attribute *attr,
1354 char *buf)
1355{
1356 int status;
1357
1358 printk_deprecated_rfkill_attribute(attr->attr.name);
1359
1360 /* This is in the ABI... */
1361 if (tpacpi_rfk_check_hwblock_state()) {
1362 status = TPACPI_RFK_RADIO_OFF;
1363 } else {
1364 status = tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1365 if (status < 0)
1366 return status;
1367 }
1368
1369 return snprintf(buf, PAGE_SIZE, "%d\n",
1370 (status == TPACPI_RFK_RADIO_ON) ? 1 : 0);
1371}
1372
1373static ssize_t tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id,
1374 struct device_attribute *attr,
1375 const char *buf, size_t count)
1376{
1377 unsigned long t;
1378 int res;
1379
1380 printk_deprecated_rfkill_attribute(attr->attr.name);
1381
1382 if (parse_strtoul(buf, 1, &t))
1383 return -EINVAL;
1384
1385 tpacpi_disclose_usertask(attr->attr.name, "set to %ld\n", t);
1386
1387 /* This is in the ABI... */
1388 if (tpacpi_rfk_check_hwblock_state() && !!t)
1389 return -EPERM;
1390
1391 res = tpacpi_rfkill_switches[id]->ops->set_status((!!t) ?
1392 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF);
1393 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1394
1395 return (res < 0) ? res : count;
1396}
1397
1398/* procfs -------------------------------------------------------------- */
1399static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id, char *p)
1400{
1401 int len = 0;
1402
1403 if (id >= TPACPI_RFK_SW_MAX)
1404 len += sprintf(p + len, "status:\t\tnot supported\n");
1405 else {
1406 int status;
1407
1408 /* This is in the ABI... */
1409 if (tpacpi_rfk_check_hwblock_state()) {
1410 status = TPACPI_RFK_RADIO_OFF;
1411 } else {
1412 status = tpacpi_rfk_update_swstate(
1413 tpacpi_rfkill_switches[id]);
1414 if (status < 0)
1415 return status;
1416 }
1417
1418 len += sprintf(p + len, "status:\t\t%s\n",
1419 (status == TPACPI_RFK_RADIO_ON) ?
1420 "enabled" : "disabled");
1421 len += sprintf(p + len, "commands:\tenable, disable\n");
1422 }
1423
1424 return len;
1425}
1426
1427static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id, char *buf)
1428{
1429 char *cmd;
1430 int status = -1;
1431 int res = 0;
1432
1433 if (id >= TPACPI_RFK_SW_MAX)
1434 return -ENODEV;
1435
1436 while ((cmd = next_cmd(&buf))) {
1437 if (strlencmp(cmd, "enable") == 0)
1438 status = TPACPI_RFK_RADIO_ON;
1439 else if (strlencmp(cmd, "disable") == 0)
1440 status = TPACPI_RFK_RADIO_OFF;
1441 else
1442 return -EINVAL;
1443 }
1444
1445 if (status != -1) {
1446 tpacpi_disclose_usertask("procfs", "attempt to %s %s\n",
1447 (status == TPACPI_RFK_RADIO_ON) ?
1448 "enable" : "disable",
1449 tpacpi_rfkill_names[id]);
1450 res = (tpacpi_rfkill_switches[id]->ops->set_status)(status);
1451 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1452 }
1453
1454 return res;
1455}
1456
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001457/*************************************************************************
1458 * thinkpad-acpi driver attributes
1459 */
1460
1461/* interface_version --------------------------------------------------- */
1462static ssize_t tpacpi_driver_interface_version_show(
1463 struct device_driver *drv,
1464 char *buf)
1465{
1466 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1467}
1468
1469static DRIVER_ATTR(interface_version, S_IRUGO,
1470 tpacpi_driver_interface_version_show, NULL);
1471
1472/* debug_level --------------------------------------------------------- */
1473static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1474 char *buf)
1475{
1476 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1477}
1478
1479static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1480 const char *buf, size_t count)
1481{
1482 unsigned long t;
1483
1484 if (parse_strtoul(buf, 0xffff, &t))
1485 return -EINVAL;
1486
1487 dbg_level = t;
1488
1489 return count;
1490}
1491
1492static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1493 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1494
1495/* version ------------------------------------------------------------- */
1496static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1497 char *buf)
1498{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001499 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1500 TPACPI_DESC, TPACPI_VERSION);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001501}
1502
1503static DRIVER_ATTR(version, S_IRUGO,
1504 tpacpi_driver_version_show, NULL);
1505
1506/* --------------------------------------------------------------------- */
1507
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001508#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1509
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001510/* wlsw_emulstate ------------------------------------------------------ */
1511static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1512 char *buf)
1513{
1514 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1515}
1516
1517static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1518 const char *buf, size_t count)
1519{
1520 unsigned long t;
1521
1522 if (parse_strtoul(buf, 1, &t))
1523 return -EINVAL;
1524
Johannes Berg19d337d2009-06-02 13:01:37 +02001525 if (tpacpi_wlsw_emulstate != !!t) {
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001526 tpacpi_wlsw_emulstate = !!t;
Johannes Berg19d337d2009-06-02 13:01:37 +02001527 tpacpi_rfk_update_hwblock_state(!t); /* negative logic */
1528 }
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001529
1530 return count;
1531}
1532
1533static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1534 tpacpi_driver_wlsw_emulstate_show,
1535 tpacpi_driver_wlsw_emulstate_store);
1536
1537/* bluetooth_emulstate ------------------------------------------------- */
1538static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1539 struct device_driver *drv,
1540 char *buf)
1541{
1542 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1543}
1544
1545static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1546 struct device_driver *drv,
1547 const char *buf, size_t count)
1548{
1549 unsigned long t;
1550
1551 if (parse_strtoul(buf, 1, &t))
1552 return -EINVAL;
1553
1554 tpacpi_bluetooth_emulstate = !!t;
1555
1556 return count;
1557}
1558
1559static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1560 tpacpi_driver_bluetooth_emulstate_show,
1561 tpacpi_driver_bluetooth_emulstate_store);
1562
1563/* wwan_emulstate ------------------------------------------------- */
1564static ssize_t tpacpi_driver_wwan_emulstate_show(
1565 struct device_driver *drv,
1566 char *buf)
1567{
1568 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1569}
1570
1571static ssize_t tpacpi_driver_wwan_emulstate_store(
1572 struct device_driver *drv,
1573 const char *buf, size_t count)
1574{
1575 unsigned long t;
1576
1577 if (parse_strtoul(buf, 1, &t))
1578 return -EINVAL;
1579
1580 tpacpi_wwan_emulstate = !!t;
1581
1582 return count;
1583}
1584
1585static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1586 tpacpi_driver_wwan_emulstate_show,
1587 tpacpi_driver_wwan_emulstate_store);
1588
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001589/* uwb_emulstate ------------------------------------------------- */
1590static ssize_t tpacpi_driver_uwb_emulstate_show(
1591 struct device_driver *drv,
1592 char *buf)
1593{
1594 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1595}
1596
1597static ssize_t tpacpi_driver_uwb_emulstate_store(
1598 struct device_driver *drv,
1599 const char *buf, size_t count)
1600{
1601 unsigned long t;
1602
1603 if (parse_strtoul(buf, 1, &t))
1604 return -EINVAL;
1605
1606 tpacpi_uwb_emulstate = !!t;
1607
1608 return count;
1609}
1610
1611static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
1612 tpacpi_driver_uwb_emulstate_show,
1613 tpacpi_driver_uwb_emulstate_store);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001614#endif
1615
1616/* --------------------------------------------------------------------- */
1617
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001618static struct driver_attribute *tpacpi_driver_attributes[] = {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001619 &driver_attr_debug_level, &driver_attr_version,
1620 &driver_attr_interface_version,
1621};
1622
1623static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1624{
1625 int i, res;
1626
1627 i = 0;
1628 res = 0;
1629 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1630 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1631 i++;
1632 }
1633
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001634#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1635 if (!res && dbg_wlswemul)
1636 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1637 if (!res && dbg_bluetoothemul)
1638 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1639 if (!res && dbg_wwanemul)
1640 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001641 if (!res && dbg_uwbemul)
1642 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001643#endif
1644
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001645 return res;
1646}
1647
1648static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1649{
1650 int i;
1651
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001652 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001653 driver_remove_file(drv, tpacpi_driver_attributes[i]);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001654
1655#ifdef THINKPAD_ACPI_DEBUGFACILITIES
1656 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1657 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1658 driver_remove_file(drv, &driver_attr_wwan_emulstate);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001659 driver_remove_file(drv, &driver_attr_uwb_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001660#endif
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001661}
1662
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001663/*************************************************************************
1664 * Firmware Data
1665 */
1666
1667/*
1668 * Table of recommended minimum BIOS versions
1669 *
1670 * Reasons for listing:
1671 * 1. Stable BIOS, listed because the unknown ammount of
1672 * bugs and bad ACPI behaviour on older versions
1673 *
1674 * 2. BIOS or EC fw with known bugs that trigger on Linux
1675 *
1676 * 3. BIOS with known reduced functionality in older versions
1677 *
1678 * We recommend the latest BIOS and EC version.
1679 * We only support the latest BIOS and EC fw version as a rule.
1680 *
1681 * Sources: IBM ThinkPad Public Web Documents (update changelogs),
1682 * Information from users in ThinkWiki
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03001683 *
1684 * WARNING: we use this table also to detect that the machine is
1685 * a ThinkPad in some cases, so don't remove entries lightly.
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001686 */
1687
1688#define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1689 { .vendor = (__v), \
1690 .bios = TPID(__id1, __id2), \
1691 .ec = TPACPI_MATCH_ANY, \
1692 .quirks = TPACPI_MATCH_ANY << 16 \
1693 | (__bv1) << 8 | (__bv2) }
1694
1695#define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001696 __eid, __ev1, __ev2) \
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001697 { .vendor = (__v), \
1698 .bios = TPID(__bid1, __bid2), \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001699 .ec = __eid, \
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001700 .quirks = (__ev1) << 24 | (__ev2) << 16 \
1701 | (__bv1) << 8 | (__bv2) }
1702
1703#define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1704 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1705
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001706/* Outdated IBM BIOSes often lack the EC id string */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001707#define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1708 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001709 __bv1, __bv2, TPID(__id1, __id2), \
1710 __ev1, __ev2), \
1711 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1712 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1713 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001714
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001715/* Outdated IBM BIOSes often lack the EC id string */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001716#define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1717 __eid1, __eid2, __ev1, __ev2) \
1718 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001719 __bv1, __bv2, TPID(__eid1, __eid2), \
1720 __ev1, __ev2), \
1721 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1722 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1723 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001724
1725#define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1726 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1727
1728#define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1729 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001730 __bv1, __bv2, TPID(__id1, __id2), \
1731 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001732
1733#define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1734 __eid1, __eid2, __ev1, __ev2) \
1735 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001736 __bv1, __bv2, TPID(__eid1, __eid2), \
1737 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001738
1739static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
1740 /* Numeric models ------------------ */
1741 /* FW MODEL BIOS VERS */
1742 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1743 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1744 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1745 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1746 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1747 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1748 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1749 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1750 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1751
1752 /* A-series ------------------------- */
1753 /* FW MODEL BIOS VERS EC VERS */
1754 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1755 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1756 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1757 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1758 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1759 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1760 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1761 TPV_QI0('1', '3', '2', '0'), /* A22m */
1762 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1763 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1764 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1765
1766 /* G-series ------------------------- */
1767 /* FW MODEL BIOS VERS */
1768 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1769 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1770
1771 /* R-series, T-series --------------- */
1772 /* FW MODEL BIOS VERS EC VERS */
1773 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1774 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1775 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1776 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1777 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1778 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1779 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1780 T40/p, T41/p, T42/p (1) */
1781 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1782 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1783 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1784 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1785
1786 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1787 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1788 TPV_QI0('1', '6', '3', '2'), /* T22 */
1789 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1790 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1791 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1792
1793 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1794 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
Henrique de Moraes Holschuh90765c62009-12-09 01:36:23 +00001795 TPV_QL1('7', 'E', 'D', '0', '1', '5'), /* R60e, R60i */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001796
1797 /* BIOS FW BIOS VERS EC FW EC VERS */
1798 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1799 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1800
1801 /* X-series ------------------------- */
1802 /* FW MODEL BIOS VERS EC VERS */
1803 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1804 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1805 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1806 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1807 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1808 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1809 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1810
Henrique de Moraes Holschuh90765c62009-12-09 01:36:23 +00001811 TPV_QL1('7', 'B', 'D', '7', '4', '0'), /* X60/s */
1812 TPV_QL1('7', 'J', '3', '0', '1', '3'), /* X60t */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001813
1814 /* (0) - older versions lack DMI EC fw string and functionality */
1815 /* (1) - older versions known to lack functionality */
1816};
1817
1818#undef TPV_QL1
1819#undef TPV_QL0
1820#undef TPV_QI2
1821#undef TPV_QI1
1822#undef TPV_QI0
1823#undef TPV_Q_X
1824#undef TPV_Q
1825
1826static void __init tpacpi_check_outdated_fw(void)
1827{
1828 unsigned long fwvers;
1829 u16 ec_version, bios_version;
1830
1831 fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
1832 ARRAY_SIZE(tpacpi_bios_version_qtable));
1833
1834 if (!fwvers)
1835 return;
1836
1837 bios_version = fwvers & 0xffffU;
1838 ec_version = (fwvers >> 16) & 0xffffU;
1839
1840 /* note that unknown versions are set to 0x0000 and we use that */
1841 if ((bios_version > thinkpad_id.bios_release) ||
1842 (ec_version > thinkpad_id.ec_release &&
1843 ec_version != TPACPI_MATCH_ANY)) {
1844 /*
1845 * The changelogs would let us track down the exact
1846 * reason, but it is just too much of a pain to track
1847 * it. We only list BIOSes that are either really
1848 * broken, or really stable to begin with, so it is
1849 * best if the user upgrades the firmware anyway.
1850 */
1851 printk(TPACPI_WARN
1852 "WARNING: Outdated ThinkPad BIOS/EC firmware\n");
1853 printk(TPACPI_WARN
1854 "WARNING: This firmware may be missing critical bug "
1855 "fixes and/or important features\n");
1856 }
1857}
1858
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03001859static bool __init tpacpi_is_fw_known(void)
1860{
1861 return tpacpi_check_quirks(tpacpi_bios_version_qtable,
1862 ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
1863}
1864
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03001865/****************************************************************************
1866 ****************************************************************************
1867 *
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001868 * Subdrivers
1869 *
1870 ****************************************************************************
1871 ****************************************************************************/
1872
1873/*************************************************************************
Henrique de Moraes Holschuh142cfc92007-04-21 11:08:26 -03001874 * thinkpad-acpi init subdriver
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001875 */
1876
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001877static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001879 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
1880 printk(TPACPI_INFO "%s\n", TPACPI_URL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001882 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03001883 (thinkpad_id.bios_version_str) ?
1884 thinkpad_id.bios_version_str : "unknown",
1885 (thinkpad_id.ec_version_str) ?
1886 thinkpad_id.ec_version_str : "unknown");
1887
1888 if (thinkpad_id.vendor && thinkpad_id.model_str)
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03001889 printk(TPACPI_INFO "%s %s, model %s\n",
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03001890 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
1891 "IBM" : ((thinkpad_id.vendor ==
1892 PCI_VENDOR_ID_LENOVO) ?
1893 "Lenovo" : "Unknown vendor"),
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03001894 thinkpad_id.model_str,
1895 (thinkpad_id.nummodel_str) ?
1896 thinkpad_id.nummodel_str : "unknown");
Henrique de Moraes Holschuh3945ac32007-02-06 19:13:44 -02001897
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001898 tpacpi_check_outdated_fw();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 return 0;
1900}
1901
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -03001902static int thinkpad_acpi_driver_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903{
1904 int len = 0;
1905
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001906 len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
1907 len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908
1909 return len;
1910}
1911
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001912static struct ibm_struct thinkpad_acpi_driver_data = {
1913 .name = "driver",
1914 .read = thinkpad_acpi_driver_read,
1915};
1916
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001917/*************************************************************************
1918 * Hotkey subdriver
1919 */
1920
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001921/*
1922 * ThinkPad firmware event model
1923 *
1924 * The ThinkPad firmware has two main event interfaces: normal ACPI
1925 * notifications (which follow the ACPI standard), and a private event
1926 * interface.
1927 *
1928 * The private event interface also issues events for the hotkeys. As
1929 * the driver gained features, the event handling code ended up being
1930 * built around the hotkey subdriver. This will need to be refactored
1931 * to a more formal event API eventually.
1932 *
1933 * Some "hotkeys" are actually supposed to be used as event reports,
1934 * such as "brightness has changed", "volume has changed", depending on
1935 * the ThinkPad model and how the firmware is operating.
1936 *
1937 * Unlike other classes, hotkey-class events have mask/unmask control on
1938 * non-ancient firmware. However, how it behaves changes a lot with the
1939 * firmware model and version.
1940 */
1941
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001942enum { /* hot key scan codes (derived from ACPI DSDT) */
1943 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1944 TP_ACPI_HOTKEYSCAN_FNF2,
1945 TP_ACPI_HOTKEYSCAN_FNF3,
1946 TP_ACPI_HOTKEYSCAN_FNF4,
1947 TP_ACPI_HOTKEYSCAN_FNF5,
1948 TP_ACPI_HOTKEYSCAN_FNF6,
1949 TP_ACPI_HOTKEYSCAN_FNF7,
1950 TP_ACPI_HOTKEYSCAN_FNF8,
1951 TP_ACPI_HOTKEYSCAN_FNF9,
1952 TP_ACPI_HOTKEYSCAN_FNF10,
1953 TP_ACPI_HOTKEYSCAN_FNF11,
1954 TP_ACPI_HOTKEYSCAN_FNF12,
1955 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1956 TP_ACPI_HOTKEYSCAN_FNINSERT,
1957 TP_ACPI_HOTKEYSCAN_FNDELETE,
1958 TP_ACPI_HOTKEYSCAN_FNHOME,
1959 TP_ACPI_HOTKEYSCAN_FNEND,
1960 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1961 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1962 TP_ACPI_HOTKEYSCAN_FNSPACE,
1963 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1964 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1965 TP_ACPI_HOTKEYSCAN_MUTE,
1966 TP_ACPI_HOTKEYSCAN_THINKPAD,
1967};
1968
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001969enum { /* Keys/events available through NVRAM polling */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001970 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1971 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1972};
1973
1974enum { /* Positions of some of the keys in hotkey masks */
1975 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1976 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1977 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1978 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1979 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1980 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1981 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1982 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1983 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1984 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1985 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1986};
1987
1988enum { /* NVRAM to ACPI HKEY group map */
1989 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1990 TP_ACPI_HKEY_ZOOM_MASK |
1991 TP_ACPI_HKEY_DISPSWTCH_MASK |
1992 TP_ACPI_HKEY_HIBERNATE_MASK,
1993 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1994 TP_ACPI_HKEY_BRGHTDWN_MASK,
1995 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1996 TP_ACPI_HKEY_VOLDWN_MASK |
1997 TP_ACPI_HKEY_MUTE_MASK,
1998};
1999
2000#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2001struct tp_nvram_state {
2002 u16 thinkpad_toggle:1;
2003 u16 zoom_toggle:1;
2004 u16 display_toggle:1;
2005 u16 thinklight_toggle:1;
2006 u16 hibernate_toggle:1;
2007 u16 displayexp_toggle:1;
2008 u16 display_state:1;
2009 u16 brightness_toggle:1;
2010 u16 volume_toggle:1;
2011 u16 mute:1;
2012
2013 u8 brightness_level;
2014 u8 volume_level;
2015};
2016
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002017/* kthread for the hotkey poller */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002018static struct task_struct *tpacpi_hotkey_task;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002019
2020/* Acquired while the poller kthread is running, use to sync start/stop */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002021static struct mutex hotkey_thread_mutex;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002022
2023/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002024 * Acquire mutex to write poller control variables as an
2025 * atomic block.
2026 *
2027 * Increment hotkey_config_change when changing them if you
2028 * want the kthread to forget old state.
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002029 *
2030 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
2031 */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002032static struct mutex hotkey_thread_data_mutex;
2033static unsigned int hotkey_config_change;
2034
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002035/*
2036 * hotkey poller control variables
2037 *
2038 * Must be atomic or readers will also need to acquire mutex
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002039 *
2040 * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
2041 * should be used only when the changes need to be taken as
2042 * a block, OR when one needs to force the kthread to forget
2043 * old state.
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002044 */
2045static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
2046static unsigned int hotkey_poll_freq = 10; /* Hz */
2047
2048#define HOTKEY_CONFIG_CRITICAL_START \
2049 do { \
2050 mutex_lock(&hotkey_thread_data_mutex); \
2051 hotkey_config_change++; \
2052 } while (0);
2053#define HOTKEY_CONFIG_CRITICAL_END \
2054 mutex_unlock(&hotkey_thread_data_mutex);
2055
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002056#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2057
2058#define hotkey_source_mask 0U
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002059#define HOTKEY_CONFIG_CRITICAL_START
2060#define HOTKEY_CONFIG_CRITICAL_END
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002061
2062#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2063
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002064static struct mutex hotkey_mutex;
2065
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002066static enum { /* Reasons for waking up */
2067 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
2068 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
2069 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
2070} hotkey_wakeup_reason;
2071
2072static int hotkey_autosleep_ack;
2073
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002074static u32 hotkey_orig_mask; /* events the BIOS had enabled */
2075static u32 hotkey_all_mask; /* all events supported in fw */
2076static u32 hotkey_reserved_mask; /* events better left disabled */
2077static u32 hotkey_driver_mask; /* events needed by the driver */
2078static u32 hotkey_user_mask; /* events visible to userspace */
2079static u32 hotkey_acpi_mask; /* events enabled in firmware */
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002080
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02002081static unsigned int hotkey_report_mode;
2082
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002083static u16 *hotkey_keycode_map;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002084
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03002085static struct attribute_set *hotkey_dev_attributes;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002086
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002087static void tpacpi_driver_event(const unsigned int hkey_event);
2088static void hotkey_driver_event(const unsigned int scancode);
2089
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002090/* HKEY.MHKG() return bits */
2091#define TP_HOTKEY_TABLET_MASK (1 << 3)
2092
Johannes Berg19d337d2009-06-02 13:01:37 +02002093static int hotkey_get_wlsw(void)
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002094{
Johannes Berg19d337d2009-06-02 13:01:37 +02002095 int status;
2096
2097 if (!tp_features.hotkey_wlsw)
2098 return -ENODEV;
2099
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02002100#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
Johannes Berg19d337d2009-06-02 13:01:37 +02002101 if (dbg_wlswemul)
2102 return (tpacpi_wlsw_emulstate) ?
2103 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02002104#endif
Johannes Berg19d337d2009-06-02 13:01:37 +02002105
2106 if (!acpi_evalf(hkey_handle, &status, "WLSW", "d"))
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002107 return -EIO;
Johannes Berg19d337d2009-06-02 13:01:37 +02002108
2109 return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002110}
2111
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002112static int hotkey_get_tablet_mode(int *status)
2113{
2114 int s;
2115
2116 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
2117 return -EIO;
2118
Henrique de Moraes Holschuhcee47f52008-03-04 14:29:21 -08002119 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
2120 return 0;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002121}
2122
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002123/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002124 * Reads current event mask from firmware, and updates
2125 * hotkey_acpi_mask accordingly. Also resets any bits
2126 * from hotkey_user_mask that are unavailable to be
2127 * delivered (shadow requirement of the userspace ABI).
2128 *
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002129 * Call with hotkey_mutex held
2130 */
2131static int hotkey_mask_get(void)
2132{
2133 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002134 u32 m = 0;
2135
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002136 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002137 return -EIO;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002138
2139 hotkey_acpi_mask = m;
2140 } else {
2141 /* no mask support doesn't mean no event support... */
2142 hotkey_acpi_mask = hotkey_all_mask;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002143 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002144
2145 /* sync userspace-visible mask */
2146 hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002147
2148 return 0;
2149}
2150
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002151void static hotkey_mask_warn_incomplete_mask(void)
2152{
2153 /* log only what the user can fix... */
2154 const u32 wantedmask = hotkey_driver_mask &
2155 ~(hotkey_acpi_mask | hotkey_source_mask) &
2156 (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
2157
2158 if (wantedmask)
2159 printk(TPACPI_NOTICE
2160 "required events 0x%08x not enabled!\n",
2161 wantedmask);
2162}
2163
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002164/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002165 * Set the firmware mask when supported
2166 *
2167 * Also calls hotkey_mask_get to update hotkey_acpi_mask.
2168 *
2169 * NOTE: does not set bits in hotkey_user_mask, but may reset them.
2170 *
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002171 * Call with hotkey_mutex held
2172 */
2173static int hotkey_mask_set(u32 mask)
2174{
2175 int i;
2176 int rc = 0;
2177
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002178 const u32 fwmask = mask & ~hotkey_source_mask;
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -03002179
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002180 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002181 for (i = 0; i < 32; i++) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002182 if (!acpi_evalf(hkey_handle,
2183 NULL, "MHKM", "vdd", i + 1,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002184 !!(mask & (1 << i)))) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002185 rc = -EIO;
2186 break;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002187 }
2188 }
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002189 }
2190
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002191 /*
2192 * We *must* make an inconditional call to hotkey_mask_get to
2193 * refresh hotkey_acpi_mask and update hotkey_user_mask
2194 *
2195 * Take the opportunity to also log when we cannot _enable_
2196 * a given event.
2197 */
2198 if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
2199 printk(TPACPI_NOTICE
2200 "asked for hotkey mask 0x%08x, but "
2201 "firmware forced it to 0x%08x\n",
2202 fwmask, hotkey_acpi_mask);
2203 }
2204
Henrique de Moraes Holschuh6b30eb72009-12-09 01:36:25 +00002205 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING)
2206 hotkey_mask_warn_incomplete_mask();
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002207
2208 return rc;
2209}
2210
2211/*
2212 * Sets hotkey_user_mask and tries to set the firmware mask
2213 *
2214 * Call with hotkey_mutex held
2215 */
2216static int hotkey_user_mask_set(const u32 mask)
2217{
2218 int rc;
2219
2220 /* Give people a chance to notice they are doing something that
2221 * is bound to go boom on their users sooner or later */
2222 if (!tp_warned.hotkey_mask_ff &&
2223 (mask == 0xffff || mask == 0xffffff ||
2224 mask == 0xffffffff)) {
2225 tp_warned.hotkey_mask_ff = 1;
2226 printk(TPACPI_NOTICE
2227 "setting the hotkey mask to 0x%08x is likely "
2228 "not the best way to go about it\n", mask);
2229 printk(TPACPI_NOTICE
2230 "please consider using the driver defaults, "
2231 "and refer to up-to-date thinkpad-acpi "
2232 "documentation\n");
2233 }
2234
2235 /* Try to enable what the user asked for, plus whatever we need.
2236 * this syncs everything but won't enable bits in hotkey_user_mask */
2237 rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
2238
2239 /* Enable the available bits in hotkey_user_mask */
2240 hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
2241
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002242 return rc;
2243}
2244
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002245/*
2246 * Sets the driver hotkey mask.
2247 *
2248 * Can be called even if the hotkey subdriver is inactive
2249 */
2250static int tpacpi_hotkey_driver_mask_set(const u32 mask)
2251{
2252 int rc;
2253
2254 /* Do the right thing if hotkey_init has not been called yet */
2255 if (!tp_features.hotkey) {
2256 hotkey_driver_mask = mask;
2257 return 0;
2258 }
2259
2260 mutex_lock(&hotkey_mutex);
2261
2262 HOTKEY_CONFIG_CRITICAL_START
2263 hotkey_driver_mask = mask;
Henrique de Moraes Holschuhb684a362009-09-26 21:42:49 -03002264#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002265 hotkey_source_mask |= (mask & ~hotkey_all_mask);
Henrique de Moraes Holschuhb684a362009-09-26 21:42:49 -03002266#endif
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002267 HOTKEY_CONFIG_CRITICAL_END
2268
2269 rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) &
2270 ~hotkey_source_mask);
2271 mutex_unlock(&hotkey_mutex);
2272
2273 return rc;
2274}
2275
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002276static int hotkey_status_get(int *status)
2277{
2278 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
2279 return -EIO;
2280
2281 return 0;
2282}
2283
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002284static int hotkey_status_set(bool enable)
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002285{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002286 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002287 return -EIO;
2288
2289 return 0;
2290}
2291
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002292static void tpacpi_input_send_tabletsw(void)
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002293{
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002294 int state;
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002295
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002296 if (tp_features.hotkey_tablet &&
2297 !hotkey_get_tablet_mode(&state)) {
2298 mutex_lock(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002299
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002300 input_report_switch(tpacpi_inputdev,
2301 SW_TABLET_MODE, !!state);
2302 input_sync(tpacpi_inputdev);
2303
2304 mutex_unlock(&tpacpi_inputdev_send_mutex);
2305 }
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002306}
2307
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002308/* Do NOT call without validating scancode first */
2309static void tpacpi_input_send_key(const unsigned int scancode)
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002310{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002311 const unsigned int keycode = hotkey_keycode_map[scancode];
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002312
2313 if (keycode != KEY_RESERVED) {
2314 mutex_lock(&tpacpi_inputdev_send_mutex);
2315
2316 input_report_key(tpacpi_inputdev, keycode, 1);
2317 if (keycode == KEY_UNKNOWN)
2318 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
2319 scancode);
2320 input_sync(tpacpi_inputdev);
2321
2322 input_report_key(tpacpi_inputdev, keycode, 0);
2323 if (keycode == KEY_UNKNOWN)
2324 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
2325 scancode);
2326 input_sync(tpacpi_inputdev);
2327
2328 mutex_unlock(&tpacpi_inputdev_send_mutex);
2329 }
2330}
2331
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002332/* Do NOT call without validating scancode first */
2333static void tpacpi_input_send_key_masked(const unsigned int scancode)
2334{
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002335 hotkey_driver_event(scancode);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002336 if (hotkey_user_mask & (1 << scancode))
2337 tpacpi_input_send_key(scancode);
2338}
2339
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002340#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2341static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
2342
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002343/* Do NOT call without validating scancode first */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002344static void tpacpi_hotkey_send_key(unsigned int scancode)
2345{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002346 tpacpi_input_send_key_masked(scancode);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002347 if (hotkey_report_mode < 2) {
2348 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03002349 0x80, TP_HKEY_EV_HOTKEY_BASE + scancode);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002350 }
2351}
2352
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002353static void hotkey_read_nvram(struct tp_nvram_state *n, const u32 m)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002354{
2355 u8 d;
2356
2357 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
2358 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
2359 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
2360 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
2361 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
2362 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
2363 }
2364 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
2365 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
2366 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
2367 }
2368 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
2369 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
2370 n->displayexp_toggle =
2371 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
2372 }
2373 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
2374 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
2375 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
2376 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
2377 n->brightness_toggle =
2378 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
2379 }
2380 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
2381 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
2382 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
2383 >> TP_NVRAM_POS_LEVEL_VOLUME;
2384 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2385 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2386 }
2387}
2388
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002389static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
2390 struct tp_nvram_state *newn,
2391 const u32 event_mask)
2392{
2393
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002394#define TPACPI_COMPARE_KEY(__scancode, __member) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002395 do { \
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002396 if ((event_mask & (1 << __scancode)) && \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002397 oldn->__member != newn->__member) \
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002398 tpacpi_hotkey_send_key(__scancode); \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002399 } while (0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002400
2401#define TPACPI_MAY_SEND_KEY(__scancode) \
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002402 do { \
2403 if (event_mask & (1 << __scancode)) \
2404 tpacpi_hotkey_send_key(__scancode); \
2405 } while (0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002406
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002407 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2408 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2409 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2410 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2411
2412 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2413
2414 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2415
2416 /* handle volume */
2417 if (oldn->volume_toggle != newn->volume_toggle) {
2418 if (oldn->mute != newn->mute) {
2419 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2420 }
2421 if (oldn->volume_level > newn->volume_level) {
2422 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2423 } else if (oldn->volume_level < newn->volume_level) {
2424 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2425 } else if (oldn->mute == newn->mute) {
2426 /* repeated key presses that didn't change state */
2427 if (newn->mute) {
2428 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2429 } else if (newn->volume_level != 0) {
2430 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2431 } else {
2432 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2433 }
2434 }
2435 }
2436
2437 /* handle brightness */
2438 if (oldn->brightness_toggle != newn->brightness_toggle) {
2439 if (oldn->brightness_level < newn->brightness_level) {
2440 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2441 } else if (oldn->brightness_level > newn->brightness_level) {
2442 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2443 } else {
2444 /* repeated key presses that didn't change state */
2445 if (newn->brightness_level != 0) {
2446 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2447 } else {
2448 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2449 }
2450 }
2451 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002452
2453#undef TPACPI_COMPARE_KEY
2454#undef TPACPI_MAY_SEND_KEY
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002455}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002456
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002457/*
2458 * Polling driver
2459 *
2460 * We track all events in hotkey_source_mask all the time, since
2461 * most of them are edge-based. We only issue those requested by
2462 * hotkey_user_mask or hotkey_driver_mask, though.
2463 */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002464static int hotkey_kthread(void *data)
2465{
2466 struct tp_nvram_state s[2];
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002467 u32 poll_mask, event_mask;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002468 unsigned int si, so;
2469 unsigned long t;
2470 unsigned int change_detector, must_reset;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002471 unsigned int poll_freq;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002472
2473 mutex_lock(&hotkey_thread_mutex);
2474
2475 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2476 goto exit;
2477
2478 set_freezable();
2479
2480 so = 0;
2481 si = 1;
2482 t = 0;
2483
2484 /* Initial state for compares */
2485 mutex_lock(&hotkey_thread_data_mutex);
2486 change_detector = hotkey_config_change;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002487 poll_mask = hotkey_source_mask;
2488 event_mask = hotkey_source_mask &
2489 (hotkey_driver_mask | hotkey_user_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002490 poll_freq = hotkey_poll_freq;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002491 mutex_unlock(&hotkey_thread_data_mutex);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002492 hotkey_read_nvram(&s[so], poll_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002493
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002494 while (!kthread_should_stop()) {
2495 if (t == 0) {
2496 if (likely(poll_freq))
2497 t = 1000/poll_freq;
2498 else
2499 t = 100; /* should never happen... */
2500 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002501 t = msleep_interruptible(t);
2502 if (unlikely(kthread_should_stop()))
2503 break;
2504 must_reset = try_to_freeze();
2505 if (t > 0 && !must_reset)
2506 continue;
2507
2508 mutex_lock(&hotkey_thread_data_mutex);
2509 if (must_reset || hotkey_config_change != change_detector) {
2510 /* forget old state on thaw or config change */
2511 si = so;
2512 t = 0;
2513 change_detector = hotkey_config_change;
2514 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002515 poll_mask = hotkey_source_mask;
2516 event_mask = hotkey_source_mask &
2517 (hotkey_driver_mask | hotkey_user_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002518 poll_freq = hotkey_poll_freq;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002519 mutex_unlock(&hotkey_thread_data_mutex);
2520
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002521 if (likely(poll_mask)) {
2522 hotkey_read_nvram(&s[si], poll_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002523 if (likely(si != so)) {
2524 hotkey_compare_and_issue_event(&s[so], &s[si],
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002525 event_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002526 }
2527 }
2528
2529 so = si;
2530 si ^= 1;
2531 }
2532
2533exit:
2534 mutex_unlock(&hotkey_thread_mutex);
2535 return 0;
2536}
2537
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002538/* call with hotkey_mutex held */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002539static void hotkey_poll_stop_sync(void)
2540{
2541 if (tpacpi_hotkey_task) {
2542 if (frozen(tpacpi_hotkey_task) ||
2543 freezing(tpacpi_hotkey_task))
2544 thaw_process(tpacpi_hotkey_task);
2545
2546 kthread_stop(tpacpi_hotkey_task);
2547 tpacpi_hotkey_task = NULL;
2548 mutex_lock(&hotkey_thread_mutex);
2549 /* at this point, the thread did exit */
2550 mutex_unlock(&hotkey_thread_mutex);
2551 }
2552}
2553
2554/* call with hotkey_mutex held */
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002555static void hotkey_poll_setup(bool may_warn)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002556{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002557 const u32 poll_driver_mask = hotkey_driver_mask & hotkey_source_mask;
2558 const u32 poll_user_mask = hotkey_user_mask & hotkey_source_mask;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002559
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002560 if (hotkey_poll_freq > 0 &&
2561 (poll_driver_mask ||
2562 (poll_user_mask && tpacpi_inputdev->users > 0))) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002563 if (!tpacpi_hotkey_task) {
2564 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03002565 NULL, TPACPI_NVRAM_KTHREAD_NAME);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002566 if (IS_ERR(tpacpi_hotkey_task)) {
2567 tpacpi_hotkey_task = NULL;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002568 printk(TPACPI_ERR
2569 "could not create kernel thread "
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002570 "for hotkey polling\n");
2571 }
2572 }
2573 } else {
2574 hotkey_poll_stop_sync();
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002575 if (may_warn && (poll_driver_mask || poll_user_mask) &&
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002576 hotkey_poll_freq == 0) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002577 printk(TPACPI_NOTICE
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002578 "hot keys 0x%08x and/or events 0x%08x "
2579 "require polling, which is currently "
2580 "disabled\n",
2581 poll_user_mask, poll_driver_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002582 }
2583 }
2584}
2585
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002586static void hotkey_poll_setup_safe(bool may_warn)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002587{
2588 mutex_lock(&hotkey_mutex);
2589 hotkey_poll_setup(may_warn);
2590 mutex_unlock(&hotkey_mutex);
2591}
2592
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002593/* call with hotkey_mutex held */
2594static void hotkey_poll_set_freq(unsigned int freq)
2595{
2596 if (!freq)
2597 hotkey_poll_stop_sync();
2598
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002599 hotkey_poll_freq = freq;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002600}
2601
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02002602#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2603
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002604static void hotkey_poll_setup_safe(bool __unused)
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02002605{
2606}
2607
2608#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2609
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002610static int hotkey_inputdev_open(struct input_dev *dev)
2611{
2612 switch (tpacpi_lifecycle) {
2613 case TPACPI_LIFE_INIT:
2614 /*
2615 * hotkey_init will call hotkey_poll_setup_safe
2616 * at the appropriate moment
2617 */
2618 return 0;
2619 case TPACPI_LIFE_EXITING:
2620 return -EBUSY;
2621 case TPACPI_LIFE_RUNNING:
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002622 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002623 return 0;
2624 }
2625
2626 /* Should only happen if tpacpi_lifecycle is corrupt */
2627 BUG();
2628 return -EBUSY;
2629}
2630
2631static void hotkey_inputdev_close(struct input_dev *dev)
2632{
2633 /* disable hotkey polling when possible */
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002634 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING &&
2635 !(hotkey_source_mask & hotkey_driver_mask))
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002636 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002637}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002638
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002639/* sysfs hotkey enable ------------------------------------------------- */
2640static ssize_t hotkey_enable_show(struct device *dev,
2641 struct device_attribute *attr,
2642 char *buf)
2643{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002644 int res, status;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002645
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002646 printk_deprecated_attribute("hotkey_enable",
2647 "Hotkey reporting is always enabled");
2648
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002649 res = hotkey_status_get(&status);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002650 if (res)
2651 return res;
2652
2653 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2654}
2655
2656static ssize_t hotkey_enable_store(struct device *dev,
2657 struct device_attribute *attr,
2658 const char *buf, size_t count)
2659{
2660 unsigned long t;
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002661
2662 printk_deprecated_attribute("hotkey_enable",
2663 "Hotkeys can be disabled through hotkey_mask");
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002664
2665 if (parse_strtoul(buf, 1, &t))
2666 return -EINVAL;
2667
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002668 if (t == 0)
2669 return -EPERM;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002670
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002671 return count;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002672}
2673
2674static struct device_attribute dev_attr_hotkey_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002675 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002676 hotkey_enable_show, hotkey_enable_store);
2677
2678/* sysfs hotkey mask --------------------------------------------------- */
2679static ssize_t hotkey_mask_show(struct device *dev,
2680 struct device_attribute *attr,
2681 char *buf)
2682{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002683 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_user_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002684}
2685
2686static ssize_t hotkey_mask_store(struct device *dev,
2687 struct device_attribute *attr,
2688 const char *buf, size_t count)
2689{
2690 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002691 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002692
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002693 if (parse_strtoul(buf, 0xffffffffUL, &t))
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002694 return -EINVAL;
2695
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002696 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002697 return -ERESTARTSYS;
2698
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002699 res = hotkey_user_mask_set(t);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002700
2701#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002702 hotkey_poll_setup(true);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002703#endif
2704
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002705 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002706
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002707 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2708
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002709 return (res) ? res : count;
2710}
2711
2712static struct device_attribute dev_attr_hotkey_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002713 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002714 hotkey_mask_show, hotkey_mask_store);
2715
2716/* sysfs hotkey bios_enabled ------------------------------------------- */
2717static ssize_t hotkey_bios_enabled_show(struct device *dev,
2718 struct device_attribute *attr,
2719 char *buf)
2720{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002721 return sprintf(buf, "0\n");
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002722}
2723
2724static struct device_attribute dev_attr_hotkey_bios_enabled =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002725 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002726
2727/* sysfs hotkey bios_mask ---------------------------------------------- */
2728static ssize_t hotkey_bios_mask_show(struct device *dev,
2729 struct device_attribute *attr,
2730 char *buf)
2731{
Henrique de Moraes Holschuh06777be2009-09-12 15:22:15 -03002732 printk_deprecated_attribute("hotkey_bios_mask",
2733 "This attribute is useless.");
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002734 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002735}
2736
2737static struct device_attribute dev_attr_hotkey_bios_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002738 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002739
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002740/* sysfs hotkey all_mask ----------------------------------------------- */
2741static ssize_t hotkey_all_mask_show(struct device *dev,
2742 struct device_attribute *attr,
2743 char *buf)
2744{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002745 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2746 hotkey_all_mask | hotkey_source_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002747}
2748
2749static struct device_attribute dev_attr_hotkey_all_mask =
2750 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
2751
2752/* sysfs hotkey recommended_mask --------------------------------------- */
2753static ssize_t hotkey_recommended_mask_show(struct device *dev,
2754 struct device_attribute *attr,
2755 char *buf)
2756{
2757 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002758 (hotkey_all_mask | hotkey_source_mask)
2759 & ~hotkey_reserved_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002760}
2761
2762static struct device_attribute dev_attr_hotkey_recommended_mask =
2763 __ATTR(hotkey_recommended_mask, S_IRUGO,
2764 hotkey_recommended_mask_show, NULL);
2765
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002766#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2767
2768/* sysfs hotkey hotkey_source_mask ------------------------------------- */
2769static ssize_t hotkey_source_mask_show(struct device *dev,
2770 struct device_attribute *attr,
2771 char *buf)
2772{
2773 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2774}
2775
2776static ssize_t hotkey_source_mask_store(struct device *dev,
2777 struct device_attribute *attr,
2778 const char *buf, size_t count)
2779{
2780 unsigned long t;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002781 u32 r_ev;
2782 int rc;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002783
2784 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2785 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2786 return -EINVAL;
2787
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002788 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002789 return -ERESTARTSYS;
2790
2791 HOTKEY_CONFIG_CRITICAL_START
2792 hotkey_source_mask = t;
2793 HOTKEY_CONFIG_CRITICAL_END
2794
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002795 rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
2796 ~hotkey_source_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002797 hotkey_poll_setup(true);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002798
2799 /* check if events needed by the driver got disabled */
2800 r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
2801 & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002802
2803 mutex_unlock(&hotkey_mutex);
2804
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002805 if (rc < 0)
2806 printk(TPACPI_ERR "hotkey_source_mask: failed to update the"
2807 "firmware event mask!\n");
2808
2809 if (r_ev)
2810 printk(TPACPI_NOTICE "hotkey_source_mask: "
2811 "some important events were disabled: "
2812 "0x%04x\n", r_ev);
2813
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002814 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2815
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002816 return (rc < 0) ? rc : count;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002817}
2818
2819static struct device_attribute dev_attr_hotkey_source_mask =
2820 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2821 hotkey_source_mask_show, hotkey_source_mask_store);
2822
2823/* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2824static ssize_t hotkey_poll_freq_show(struct device *dev,
2825 struct device_attribute *attr,
2826 char *buf)
2827{
2828 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2829}
2830
2831static ssize_t hotkey_poll_freq_store(struct device *dev,
2832 struct device_attribute *attr,
2833 const char *buf, size_t count)
2834{
2835 unsigned long t;
2836
2837 if (parse_strtoul(buf, 25, &t))
2838 return -EINVAL;
2839
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002840 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002841 return -ERESTARTSYS;
2842
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002843 hotkey_poll_set_freq(t);
2844 hotkey_poll_setup(true);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002845
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002846 mutex_unlock(&hotkey_mutex);
2847
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002848 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2849
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002850 return count;
2851}
2852
2853static struct device_attribute dev_attr_hotkey_poll_freq =
2854 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2855 hotkey_poll_freq_show, hotkey_poll_freq_store);
2856
2857#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2858
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002859/* sysfs hotkey radio_sw (pollable) ------------------------------------ */
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002860static ssize_t hotkey_radio_sw_show(struct device *dev,
2861 struct device_attribute *attr,
2862 char *buf)
2863{
Johannes Berg19d337d2009-06-02 13:01:37 +02002864 int res;
2865 res = hotkey_get_wlsw();
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002866 if (res < 0)
2867 return res;
2868
Johannes Berg19d337d2009-06-02 13:01:37 +02002869 /* Opportunistic update */
2870 tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
2871
2872 return snprintf(buf, PAGE_SIZE, "%d\n",
2873 (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002874}
2875
2876static struct device_attribute dev_attr_hotkey_radio_sw =
2877 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2878
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002879static void hotkey_radio_sw_notify_change(void)
2880{
2881 if (tp_features.hotkey_wlsw)
2882 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2883 "hotkey_radio_sw");
2884}
2885
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002886/* sysfs hotkey tablet mode (pollable) --------------------------------- */
2887static ssize_t hotkey_tablet_mode_show(struct device *dev,
2888 struct device_attribute *attr,
2889 char *buf)
2890{
2891 int res, s;
2892 res = hotkey_get_tablet_mode(&s);
2893 if (res < 0)
2894 return res;
2895
2896 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2897}
2898
2899static struct device_attribute dev_attr_hotkey_tablet_mode =
2900 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2901
2902static void hotkey_tablet_mode_notify_change(void)
2903{
2904 if (tp_features.hotkey_tablet)
2905 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2906 "hotkey_tablet_mode");
2907}
2908
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002909/* sysfs hotkey report_mode -------------------------------------------- */
2910static ssize_t hotkey_report_mode_show(struct device *dev,
2911 struct device_attribute *attr,
2912 char *buf)
2913{
2914 return snprintf(buf, PAGE_SIZE, "%d\n",
2915 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2916}
2917
2918static struct device_attribute dev_attr_hotkey_report_mode =
2919 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2920
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002921/* sysfs wakeup reason (pollable) -------------------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002922static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2923 struct device_attribute *attr,
2924 char *buf)
2925{
2926 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2927}
2928
2929static struct device_attribute dev_attr_hotkey_wakeup_reason =
2930 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2931
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02002932static void hotkey_wakeup_reason_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002933{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002934 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2935 "wakeup_reason");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002936}
2937
2938/* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002939static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2940 struct device_attribute *attr,
2941 char *buf)
2942{
2943 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2944}
2945
2946static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2947 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2948 hotkey_wakeup_hotunplug_complete_show, NULL);
2949
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02002950static void hotkey_wakeup_hotunplug_complete_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002951{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002952 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2953 "wakeup_hotunplug_complete");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002954}
2955
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002956/* --------------------------------------------------------------------- */
2957
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002958static struct attribute *hotkey_attributes[] __initdata = {
2959 &dev_attr_hotkey_enable.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002960 &dev_attr_hotkey_bios_enabled.attr,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002961 &dev_attr_hotkey_bios_mask.attr,
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002962 &dev_attr_hotkey_report_mode.attr,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002963 &dev_attr_hotkey_wakeup_reason.attr,
2964 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002965 &dev_attr_hotkey_mask.attr,
2966 &dev_attr_hotkey_all_mask.attr,
2967 &dev_attr_hotkey_recommended_mask.attr,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002968#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002969 &dev_attr_hotkey_source_mask.attr,
2970 &dev_attr_hotkey_poll_freq.attr,
2971#endif
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002972};
2973
Johannes Berg19d337d2009-06-02 13:01:37 +02002974/*
2975 * Sync both the hw and sw blocking state of all switches
2976 */
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002977static void tpacpi_send_radiosw_update(void)
2978{
2979 int wlsw;
2980
Johannes Berg19d337d2009-06-02 13:01:37 +02002981 /*
2982 * We must sync all rfkill controllers *before* issuing any
2983 * rfkill input events, or we will race the rfkill core input
2984 * handler.
2985 *
2986 * tpacpi_inputdev_send_mutex works as a syncronization point
2987 * for the above.
2988 *
2989 * We optimize to avoid numerous calls to hotkey_get_wlsw.
2990 */
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002991
Johannes Berg19d337d2009-06-02 13:01:37 +02002992 wlsw = hotkey_get_wlsw();
2993
2994 /* Sync hw blocking state first if it is hw-blocked */
2995 if (wlsw == TPACPI_RFK_RADIO_OFF)
2996 tpacpi_rfk_update_hwblock_state(true);
2997
2998 /* Sync sw blocking state */
2999 tpacpi_rfk_update_swstate_all();
3000
3001 /* Sync hw blocking state last if it is hw-unblocked */
3002 if (wlsw == TPACPI_RFK_RADIO_ON)
3003 tpacpi_rfk_update_hwblock_state(false);
3004
3005 /* Issue rfkill input event for WLSW switch */
3006 if (!(wlsw < 0)) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003007 mutex_lock(&tpacpi_inputdev_send_mutex);
3008
3009 input_report_switch(tpacpi_inputdev,
Johannes Berg19d337d2009-06-02 13:01:37 +02003010 SW_RFKILL_ALL, (wlsw > 0));
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003011 input_sync(tpacpi_inputdev);
3012
3013 mutex_unlock(&tpacpi_inputdev_send_mutex);
3014 }
Johannes Berg19d337d2009-06-02 13:01:37 +02003015
3016 /*
3017 * this can be unconditional, as we will poll state again
3018 * if userspace uses the notify to read data
3019 */
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003020 hotkey_radio_sw_notify_change();
3021}
3022
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003023static void hotkey_exit(void)
3024{
3025#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003026 mutex_lock(&hotkey_mutex);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003027 hotkey_poll_stop_sync();
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003028 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003029#endif
3030
3031 if (hotkey_dev_attributes)
3032 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3033
3034 kfree(hotkey_keycode_map);
3035
Henrique de Moraes Holschuh4be73002009-09-20 14:09:23 -03003036 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003037 "restoring original HKEY status and mask\n");
3038 /* yes, there is a bitwise or below, we want the
3039 * functions to be called even if one of them fail */
3040 if (((tp_features.hotkey_mask &&
3041 hotkey_mask_set(hotkey_orig_mask)) |
3042 hotkey_status_set(false)) != 0)
Henrique de Moraes Holschuh4be73002009-09-20 14:09:23 -03003043 printk(TPACPI_ERR
3044 "failed to restore hot key mask "
3045 "to BIOS defaults\n");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003046}
3047
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003048static void __init hotkey_unmap(const unsigned int scancode)
3049{
3050 if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
3051 clear_bit(hotkey_keycode_map[scancode],
3052 tpacpi_inputdev->keybit);
3053 hotkey_keycode_map[scancode] = KEY_RESERVED;
3054 }
3055}
3056
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003057/*
3058 * HKEY quirks:
3059 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
3060 */
3061
3062#define TPACPI_HK_Q_INIMASK 0x0001
3063
3064static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
3065 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
3066 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
3067 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
3068 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
3069 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
3070 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
3071 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
3072 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
3073 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
3074 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
3075 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
3076 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
3077 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
3078 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
3079 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
3080 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
3081 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
3082 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
3083 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
3084};
3085
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003086static int __init hotkey_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003087{
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003088 /* Requirements for changing the default keymaps:
3089 *
3090 * 1. Many of the keys are mapped to KEY_RESERVED for very
3091 * good reasons. Do not change them unless you have deep
3092 * knowledge on the IBM and Lenovo ThinkPad firmware for
3093 * the various ThinkPad models. The driver behaves
3094 * differently for KEY_RESERVED: such keys have their
3095 * hot key mask *unset* in mask_recommended, and also
3096 * in the initial hot key mask programmed into the
3097 * firmware at driver load time, which means the firm-
3098 * ware may react very differently if you change them to
3099 * something else;
3100 *
3101 * 2. You must be subscribed to the linux-thinkpad and
3102 * ibm-acpi-devel mailing lists, and you should read the
3103 * list archives since 2007 if you want to change the
3104 * keymaps. This requirement exists so that you will
3105 * know the past history of problems with the thinkpad-
3106 * acpi driver keymaps, and also that you will be
3107 * listening to any bug reports;
3108 *
3109 * 3. Do not send thinkpad-acpi specific patches directly to
3110 * for merging, *ever*. Send them to the linux-acpi
3111 * mailinglist for comments. Merging is to be done only
3112 * through acpi-test and the ACPI maintainer.
3113 *
3114 * If the above is too much to ask, don't change the keymap.
3115 * Ask the thinkpad-acpi maintainer to do it, instead.
3116 */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003117 static u16 ibm_keycode_map[] __initdata = {
3118 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3119 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
3120 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3121 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003122
3123 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003124 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3125 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3126 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003127
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003128 /* brightness: firmware always reacts to them */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003129 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003130 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003131
3132 /* Thinklight: firmware always react to it */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003133 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003134
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003135 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3136 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003137
3138 /* Volume: firmware always react to it and reprograms
3139 * the built-in *extra* mixer. Never map it to control
3140 * another mixer by default. */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003141 KEY_RESERVED, /* 0x14: VOLUME UP */
3142 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3143 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003144
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003145 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003146
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003147 /* (assignments unknown, please report if found) */
3148 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3149 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3150 };
3151 static u16 lenovo_keycode_map[] __initdata = {
3152 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3153 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
3154 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3155 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003156
3157 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003158 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3159 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3160 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003161
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003162 /* These should be enabled --only-- when ACPI video
3163 * is disabled (i.e. in "vendor" mode), and are handled
3164 * in a special way by the init code */
3165 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
3166 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003167
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003168 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003169
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003170 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3171 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003172
3173 /* Volume: z60/z61, T60 (BIOS version?): firmware always
3174 * react to it and reprograms the built-in *extra* mixer.
3175 * Never map it to control another mixer by default.
3176 *
3177 * T60?, T61, R60?, R61: firmware and EC tries to send
3178 * these over the regular keyboard, so these are no-ops,
3179 * but there are still weird bugs re. MUTE, so do not
3180 * change unless you get test reports from all Lenovo
3181 * models. May cause the BIOS to interfere with the
3182 * HDA mixer.
3183 */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003184 KEY_RESERVED, /* 0x14: VOLUME UP */
3185 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3186 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003187
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003188 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003189
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003190 /* (assignments unknown, please report if found) */
3191 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3192 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3193 };
3194
3195#define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
3196#define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
3197#define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
3198
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003199 int res, i;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003200 int status;
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03003201 int hkeyv;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003202 bool radiosw_state = false;
3203 bool tabletsw_state = false;
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03003204
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003205 unsigned long quirks;
3206
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003207 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3208 "initializing hotkey subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003209
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003210 BUG_ON(!tpacpi_inputdev);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003211 BUG_ON(tpacpi_inputdev->open != NULL ||
3212 tpacpi_inputdev->close != NULL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003213
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003214 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003215 mutex_init(&hotkey_mutex);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003216
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003217#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3218 mutex_init(&hotkey_thread_mutex);
3219 mutex_init(&hotkey_thread_data_mutex);
3220#endif
3221
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003222 /* hotkey not supported on 570 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003223 tp_features.hotkey = hkey_handle != NULL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003224
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003225 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3226 "hotkeys are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003227 str_supported(tp_features.hotkey));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003228
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003229 if (!tp_features.hotkey)
3230 return 1;
3231
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003232 quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
3233 ARRAY_SIZE(tpacpi_hotkey_qtable));
3234
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03003235 tpacpi_disable_brightness_delay();
3236
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003237 /* MUST have enough space for all attributes to be added to
3238 * hotkey_dev_attributes */
3239 hotkey_dev_attributes = create_attr_set(
3240 ARRAY_SIZE(hotkey_attributes) + 2,
3241 NULL);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003242 if (!hotkey_dev_attributes)
3243 return -ENOMEM;
3244 res = add_many_to_attr_set(hotkey_dev_attributes,
3245 hotkey_attributes,
3246 ARRAY_SIZE(hotkey_attributes));
3247 if (res)
3248 goto err_exit;
3249
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003250 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003251 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
3252 for HKEY interface version 0x100 */
3253 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
3254 if ((hkeyv >> 8) != 1) {
3255 printk(TPACPI_ERR "unknown version of the "
3256 "HKEY interface: 0x%x\n", hkeyv);
3257 printk(TPACPI_ERR "please report this to %s\n",
3258 TPACPI_MAIL);
3259 } else {
3260 /*
3261 * MHKV 0x100 in A31, R40, R40e,
3262 * T4x, X31, and later
3263 */
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003264 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3265 "firmware HKEY interface version: 0x%x\n",
3266 hkeyv);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003267
3268 /* Paranoia check AND init hotkey_all_mask */
3269 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3270 "MHKA", "qd")) {
3271 printk(TPACPI_ERR
3272 "missing MHKA handler, "
3273 "please report this to %s\n",
3274 TPACPI_MAIL);
3275 /* Fallback: pre-init for FN+F3,F4,F12 */
3276 hotkey_all_mask = 0x080cU;
3277 } else {
3278 tp_features.hotkey_mask = 1;
3279 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003280 }
3281 }
3282
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003283 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3284 "hotkey masks are %s\n",
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003285 str_supported(tp_features.hotkey_mask));
3286
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003287 /* Init hotkey_all_mask if not initialized yet */
3288 if (!tp_features.hotkey_mask && !hotkey_all_mask &&
3289 (quirks & TPACPI_HK_Q_INIMASK))
3290 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003291
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003292 /* Init hotkey_acpi_mask and hotkey_orig_mask */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003293 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003294 /* hotkey_source_mask *must* be zero for
3295 * the first hotkey_mask_get to return hotkey_orig_mask */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003296 res = hotkey_mask_get();
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003297 if (res)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003298 goto err_exit;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003299
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003300 hotkey_orig_mask = hotkey_acpi_mask;
3301 } else {
3302 hotkey_orig_mask = hotkey_all_mask;
3303 hotkey_acpi_mask = hotkey_all_mask;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003304 }
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003305
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003306#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3307 if (dbg_wlswemul) {
3308 tp_features.hotkey_wlsw = 1;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003309 radiosw_state = !!tpacpi_wlsw_emulstate;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003310 printk(TPACPI_INFO
3311 "radio switch emulation enabled\n");
3312 } else
3313#endif
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003314 /* Not all thinkpads have a hardware radio switch */
3315 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
3316 tp_features.hotkey_wlsw = 1;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003317 radiosw_state = !!status;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003318 printk(TPACPI_INFO
3319 "radio switch found; radios are %s\n",
3320 enabled(status, 0));
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003321 }
3322 if (tp_features.hotkey_wlsw)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003323 res = add_to_attr_set(hotkey_dev_attributes,
3324 &dev_attr_hotkey_radio_sw.attr);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003325
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003326 /* For X41t, X60t, X61t Tablets... */
3327 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
3328 tp_features.hotkey_tablet = 1;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003329 tabletsw_state = !!(status & TP_HOTKEY_TABLET_MASK);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003330 printk(TPACPI_INFO
3331 "possible tablet mode switch found; "
3332 "ThinkPad in %s mode\n",
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003333 (tabletsw_state) ? "tablet" : "laptop");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003334 res = add_to_attr_set(hotkey_dev_attributes,
3335 &dev_attr_hotkey_tablet_mode.attr);
3336 }
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02003337
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003338 if (!res)
3339 res = register_attr_set_with_sysfs(
3340 hotkey_dev_attributes,
3341 &tpacpi_pdev->dev.kobj);
3342 if (res)
3343 goto err_exit;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003344
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003345 /* Set up key map */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003346
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003347 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
3348 GFP_KERNEL);
3349 if (!hotkey_keycode_map) {
3350 printk(TPACPI_ERR
3351 "failed to allocate memory for key map\n");
3352 res = -ENOMEM;
3353 goto err_exit;
3354 }
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003355
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003356 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003357 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003358 "using Lenovo default hot key map\n");
3359 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
3360 TPACPI_HOTKEY_MAP_SIZE);
3361 } else {
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003362 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003363 "using IBM default hot key map\n");
3364 memcpy(hotkey_keycode_map, &ibm_keycode_map,
3365 TPACPI_HOTKEY_MAP_SIZE);
3366 }
3367
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003368 input_set_capability(tpacpi_inputdev, EV_MSC, MSC_SCAN);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003369 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
3370 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3371 tpacpi_inputdev->keycode = hotkey_keycode_map;
3372 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3373 if (hotkey_keycode_map[i] != KEY_RESERVED) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003374 input_set_capability(tpacpi_inputdev, EV_KEY,
3375 hotkey_keycode_map[i]);
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003376 } else {
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003377 if (i < sizeof(hotkey_reserved_mask)*8)
3378 hotkey_reserved_mask |= 1 << i;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003379 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003380 }
3381
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003382 if (tp_features.hotkey_wlsw) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003383 input_set_capability(tpacpi_inputdev, EV_SW, SW_RFKILL_ALL);
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003384 input_report_switch(tpacpi_inputdev,
3385 SW_RFKILL_ALL, radiosw_state);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003386 }
3387 if (tp_features.hotkey_tablet) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003388 input_set_capability(tpacpi_inputdev, EV_SW, SW_TABLET_MODE);
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003389 input_report_switch(tpacpi_inputdev,
3390 SW_TABLET_MODE, tabletsw_state);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003391 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003392
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003393 /* Do not issue duplicate brightness change events to
3394 * userspace */
3395 if (!tp_features.bright_acpimode)
3396 /* update bright_acpimode... */
3397 tpacpi_check_std_acpi_brightness_support();
3398
Henrique de Moraes Holschuh8bf3d4c2009-05-30 13:25:09 -03003399 if (tp_features.bright_acpimode && acpi_video_backlight_support()) {
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003400 printk(TPACPI_INFO
3401 "This ThinkPad has standard ACPI backlight "
3402 "brightness control, supported by the ACPI "
3403 "video driver\n");
3404 printk(TPACPI_NOTICE
3405 "Disabling thinkpad-acpi brightness events "
3406 "by default...\n");
3407
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003408 /* Disable brightness up/down on Lenovo thinkpads when
3409 * ACPI is handling them, otherwise it is plain impossible
3410 * for userspace to do something even remotely sane */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003411 hotkey_reserved_mask |=
3412 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3413 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003414 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
3415 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003416 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003417
Henrique de Moraes Holschuh230d8cf252009-09-12 15:22:17 -03003418#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003419 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3420 & ~hotkey_all_mask
3421 & ~hotkey_reserved_mask;
Henrique de Moraes Holschuh230d8cf252009-09-12 15:22:17 -03003422
3423 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3424 "hotkey source mask 0x%08x, polling freq %u\n",
3425 hotkey_source_mask, hotkey_poll_freq);
3426#endif
3427
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003428 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3429 "enabling firmware HKEY event interface...\n");
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003430 res = hotkey_status_set(true);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003431 if (res) {
3432 hotkey_exit();
3433 return res;
3434 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003435 res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
3436 | hotkey_driver_mask)
3437 & ~hotkey_source_mask);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003438 if (res < 0 && res != -ENXIO) {
3439 hotkey_exit();
3440 return res;
3441 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003442 hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
3443 & ~hotkey_reserved_mask;
3444 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3445 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3446 hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003447
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003448 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3449 "legacy ibm/hotkey event reporting over procfs %s\n",
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003450 (hotkey_report_mode < 2) ?
3451 "enabled" : "disabled");
3452
3453 tpacpi_inputdev->open = &hotkey_inputdev_open;
3454 tpacpi_inputdev->close = &hotkey_inputdev_close;
3455
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003456 hotkey_poll_setup_safe(true);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003457
3458 return 0;
3459
3460err_exit:
3461 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3462 hotkey_dev_attributes = NULL;
3463
3464 return (res < 0)? res : 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003465}
3466
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003467static bool hotkey_notify_hotkey(const u32 hkey,
3468 bool *send_acpi_ev,
3469 bool *ignore_acpi_ev)
3470{
3471 /* 0x1000-0x1FFF: key presses */
3472 unsigned int scancode = hkey & 0xfff;
3473 *send_acpi_ev = true;
3474 *ignore_acpi_ev = false;
3475
3476 if (scancode > 0 && scancode < 0x21) {
3477 scancode--;
3478 if (!(hotkey_source_mask & (1 << scancode))) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003479 tpacpi_input_send_key_masked(scancode);
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003480 *send_acpi_ev = false;
3481 } else {
3482 *ignore_acpi_ev = true;
3483 }
3484 return true;
3485 }
3486 return false;
3487}
3488
3489static bool hotkey_notify_wakeup(const u32 hkey,
3490 bool *send_acpi_ev,
3491 bool *ignore_acpi_ev)
3492{
3493 /* 0x2000-0x2FFF: Wakeup reason */
3494 *send_acpi_ev = true;
3495 *ignore_acpi_ev = false;
3496
3497 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003498 case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */
3499 case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003500 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3501 *ignore_acpi_ev = true;
3502 break;
3503
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003504 case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */
3505 case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003506 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3507 *ignore_acpi_ev = true;
3508 break;
3509
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003510 case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */
3511 case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003512 printk(TPACPI_ALERT
3513 "EMERGENCY WAKEUP: battery almost empty\n");
3514 /* how to auto-heal: */
3515 /* 2313: woke up from S3, go to S4/S5 */
3516 /* 2413: woke up from S4, go to S5 */
3517 break;
3518
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003519 default:
3520 return false;
3521 }
3522
3523 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
3524 printk(TPACPI_INFO
3525 "woke up due to a hot-unplug "
3526 "request...\n");
3527 hotkey_wakeup_reason_notify_change();
3528 }
3529 return true;
3530}
3531
3532static bool hotkey_notify_usrevent(const u32 hkey,
3533 bool *send_acpi_ev,
3534 bool *ignore_acpi_ev)
3535{
3536 /* 0x5000-0x5FFF: human interface helpers */
3537 *send_acpi_ev = true;
3538 *ignore_acpi_ev = false;
3539
3540 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003541 case TP_HKEY_EV_PEN_INSERTED: /* X61t: tablet pen inserted into bay */
3542 case TP_HKEY_EV_PEN_REMOVED: /* X61t: tablet pen removed from bay */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003543 return true;
3544
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003545 case TP_HKEY_EV_TABLET_TABLET: /* X41t-X61t: tablet mode */
3546 case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003547 tpacpi_input_send_tabletsw();
3548 hotkey_tablet_mode_notify_change();
3549 *send_acpi_ev = false;
3550 return true;
3551
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003552 case TP_HKEY_EV_LID_CLOSE: /* Lid closed */
3553 case TP_HKEY_EV_LID_OPEN: /* Lid opened */
3554 case TP_HKEY_EV_BRGHT_CHANGED: /* brightness changed */
Henrique de Moraes Holschuh176dd982009-09-20 14:09:24 -03003555 /* do not propagate these events */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003556 *ignore_acpi_ev = true;
3557 return true;
3558
3559 default:
3560 return false;
3561 }
3562}
3563
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003564static void thermal_dump_all_sensors(void);
3565
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003566static bool hotkey_notify_thermal(const u32 hkey,
3567 bool *send_acpi_ev,
3568 bool *ignore_acpi_ev)
3569{
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003570 bool known = true;
3571
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003572 /* 0x6000-0x6FFF: thermal alarms */
3573 *send_acpi_ev = true;
3574 *ignore_acpi_ev = false;
3575
3576 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003577 case TP_HKEY_EV_THM_TABLE_CHANGED:
Henrique de Moraes Holschuh54926ce2009-01-11 03:01:09 -02003578 printk(TPACPI_INFO
3579 "EC reports that Thermal Table has changed\n");
3580 /* recommended action: do nothing, we don't have
3581 * Lenovo ATM information */
3582 return true;
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003583 case TP_HKEY_EV_ALARM_BAT_HOT:
3584 printk(TPACPI_CRIT
3585 "THERMAL ALARM: battery is too hot!\n");
3586 /* recommended action: warn user through gui */
3587 break;
3588 case TP_HKEY_EV_ALARM_BAT_XHOT:
3589 printk(TPACPI_ALERT
3590 "THERMAL EMERGENCY: battery is extremely hot!\n");
3591 /* recommended action: immediate sleep/hibernate */
3592 break;
3593 case TP_HKEY_EV_ALARM_SENSOR_HOT:
3594 printk(TPACPI_CRIT
3595 "THERMAL ALARM: "
3596 "a sensor reports something is too hot!\n");
3597 /* recommended action: warn user through gui, that */
3598 /* some internal component is too hot */
3599 break;
3600 case TP_HKEY_EV_ALARM_SENSOR_XHOT:
3601 printk(TPACPI_ALERT
3602 "THERMAL EMERGENCY: "
3603 "a sensor reports something is extremely hot!\n");
3604 /* recommended action: immediate sleep/hibernate */
3605 break;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003606 default:
3607 printk(TPACPI_ALERT
3608 "THERMAL ALERT: unknown thermal alarm received\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003609 known = false;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003610 }
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003611
3612 thermal_dump_all_sensors();
3613
3614 return known;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003615}
3616
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003617static void hotkey_notify(struct ibm_struct *ibm, u32 event)
3618{
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003619 u32 hkey;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003620 bool send_acpi_ev;
3621 bool ignore_acpi_ev;
3622 bool known_ev;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003623
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003624 if (event != 0x80) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003625 printk(TPACPI_ERR
3626 "unknown HKEY notification event %d\n", event);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003627 /* forward it to userspace, maybe it knows how to handle it */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003628 acpi_bus_generate_netlink_event(
3629 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02003630 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003631 event, 0);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003632 return;
3633 }
3634
3635 while (1) {
3636 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003637 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003638 return;
3639 }
3640
3641 if (hkey == 0) {
3642 /* queue empty */
3643 return;
3644 }
3645
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003646 send_acpi_ev = true;
3647 ignore_acpi_ev = false;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003648
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003649 switch (hkey >> 12) {
3650 case 1:
3651 /* 0x1000-0x1FFF: key presses */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003652 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
3653 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003654 break;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003655 case 2:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003656 /* 0x2000-0x2FFF: Wakeup reason */
3657 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
3658 &ignore_acpi_ev);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003659 break;
3660 case 3:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003661 /* 0x3000-0x3FFF: bay-related wakeups */
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003662 if (hkey == TP_HKEY_EV_BAYEJ_ACK) {
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003663 hotkey_autosleep_ack = 1;
3664 printk(TPACPI_INFO
3665 "bay ejected\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003666 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003667 known_ev = true;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003668 } else {
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003669 known_ev = false;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003670 }
3671 break;
3672 case 4:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003673 /* 0x4000-0x4FFF: dock-related wakeups */
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003674 if (hkey == TP_HKEY_EV_UNDOCK_ACK) {
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003675 hotkey_autosleep_ack = 1;
3676 printk(TPACPI_INFO
3677 "undocked\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003678 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003679 known_ev = true;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003680 } else {
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003681 known_ev = false;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003682 }
3683 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003684 case 5:
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02003685 /* 0x5000-0x5FFF: human interface helpers */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003686 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
3687 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003688 break;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003689 case 6:
3690 /* 0x6000-0x6FFF: thermal alarms */
3691 known_ev = hotkey_notify_thermal(hkey, &send_acpi_ev,
3692 &ignore_acpi_ev);
3693 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003694 case 7:
3695 /* 0x7000-0x7FFF: misc */
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003696 if (tp_features.hotkey_wlsw &&
3697 hkey == TP_HKEY_EV_RFKILL_CHANGED) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003698 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02003699 send_acpi_ev = 0;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003700 known_ev = true;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003701 break;
3702 }
3703 /* fallthrough to default */
3704 default:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003705 known_ev = false;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02003706 }
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003707 if (!known_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003708 printk(TPACPI_NOTICE
3709 "unhandled HKEY event 0x%04x\n", hkey);
Henrique de Moraes Holschuhcb429352009-01-11 03:01:07 -02003710 printk(TPACPI_NOTICE
3711 "please report the conditions when this "
3712 "event happened to %s\n", TPACPI_MAIL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003713 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003714
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003715 /* Legacy events */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003716 if (!ignore_acpi_ev &&
3717 (send_acpi_ev || hotkey_report_mode < 2)) {
3718 acpi_bus_generate_proc_event(ibm->acpi->device,
3719 event, hkey);
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03003720 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003721
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003722 /* netlink events */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03003723 if (!ignore_acpi_ev && send_acpi_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003724 acpi_bus_generate_netlink_event(
3725 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02003726 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003727 event, hkey);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003728 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003729 }
3730}
3731
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003732static void hotkey_suspend(pm_message_t state)
3733{
3734 /* Do these on suspend, we get the events on early resume! */
3735 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
3736 hotkey_autosleep_ack = 0;
3737}
3738
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03003739static void hotkey_resume(void)
3740{
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03003741 tpacpi_disable_brightness_delay();
3742
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003743 if (hotkey_status_set(true) < 0 ||
3744 hotkey_mask_set(hotkey_acpi_mask) < 0)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003745 printk(TPACPI_ERR
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003746 "error while attempting to reset the event "
3747 "firmware interface\n");
3748
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003749 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02003750 hotkey_tablet_mode_notify_change();
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003751 hotkey_wakeup_reason_notify_change();
3752 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003753 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03003754}
3755
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003756/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003757static int hotkey_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03003759 int res, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760 int len = 0;
3761
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003762 if (!tp_features.hotkey) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003763 len += sprintf(p + len, "status:\t\tnot supported\n");
3764 return len;
3765 }
3766
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02003767 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02003768 return -ERESTARTSYS;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02003769 res = hotkey_status_get(&status);
3770 if (!res)
3771 res = hotkey_mask_get();
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003772 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03003773 if (res)
3774 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775
3776 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003777 if (hotkey_all_mask) {
3778 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_user_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779 len += sprintf(p + len,
3780 "commands:\tenable, disable, reset, <mask>\n");
3781 } else {
3782 len += sprintf(p + len, "mask:\t\tnot supported\n");
3783 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
3784 }
3785
3786 return len;
3787}
3788
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003789static void hotkey_enabledisable_warn(bool enable)
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003790{
3791 tpacpi_log_usertask("procfs hotkey enable/disable");
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003792 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
3793 TPACPI_WARN
3794 "hotkey enable/disable functionality has been "
3795 "removed from the driver. Hotkeys are always "
3796 "enabled\n"))
3797 printk(TPACPI_ERR
3798 "Please remove the hotkey=enable module "
3799 "parameter, it is deprecated. Hotkeys are always "
3800 "enabled\n");
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003801}
3802
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003803static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003805 int res;
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03003806 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003809 if (!tp_features.hotkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810 return -ENODEV;
3811
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02003812 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02003813 return -ERESTARTSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003814
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003815 mask = hotkey_user_mask;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003816
3817 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818 while ((cmd = next_cmd(&buf))) {
3819 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003820 hotkey_enabledisable_warn(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003822 hotkey_enabledisable_warn(0);
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003823 res = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824 } else if (strlencmp(cmd, "reset") == 0) {
Henrique de Moraes Holschuh20c9aa42009-09-12 15:22:16 -03003825 mask = (hotkey_all_mask | hotkey_source_mask)
3826 & ~hotkey_reserved_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
3828 /* mask set */
3829 } else if (sscanf(cmd, "%x", &mask) == 1) {
3830 /* mask set */
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003831 } else {
3832 res = -EINVAL;
3833 goto errexit;
3834 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835 }
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003836
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003837 if (!res) {
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003838 tpacpi_disclose_usertask("procfs hotkey",
3839 "set mask to 0x%08x\n", mask);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003840 res = hotkey_user_mask_set(mask);
3841 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003842
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003843errexit:
3844 mutex_unlock(&hotkey_mutex);
3845 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003846}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003848static const struct acpi_device_id ibm_htk_device_ids[] = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003849 {TPACPI_ACPI_HKEY_HID, 0},
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003850 {"", 0},
3851};
3852
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003853static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003854 .hid = ibm_htk_device_ids,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003855 .notify = hotkey_notify,
3856 .handle = &hkey_handle,
3857 .type = ACPI_DEVICE_NOTIFY,
3858};
3859
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003860static struct ibm_struct hotkey_driver_data = {
3861 .name = "hotkey",
3862 .read = hotkey_read,
3863 .write = hotkey_write,
3864 .exit = hotkey_exit,
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03003865 .resume = hotkey_resume,
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003866 .suspend = hotkey_suspend,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003867 .acpi = &ibm_hotkey_acpidriver,
3868};
3869
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003870/*************************************************************************
3871 * Bluetooth subdriver
3872 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003874enum {
3875 /* ACPI GBDC/SBDC bits */
3876 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
3877 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003878 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
3879 off / last state */
3880};
3881
3882enum {
3883 /* ACPI \BLTH commands */
3884 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
3885 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
3886 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
3887 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
3888 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003889};
3890
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003891#define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
3892
Johannes Berg19d337d2009-06-02 13:01:37 +02003893static int bluetooth_get_status(void)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003894{
3895 int status;
3896
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003897#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3898 if (dbg_bluetoothemul)
3899 return (tpacpi_bluetooth_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02003900 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003901#endif
3902
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003903 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
3904 return -EIO;
3905
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003906 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02003907 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003908}
3909
Johannes Berg19d337d2009-06-02 13:01:37 +02003910static int bluetooth_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003911{
3912 int status;
3913
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003914 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02003915 "will attempt to %s bluetooth\n",
3916 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003917
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003918#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3919 if (dbg_bluetoothemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02003920 tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003921 return 0;
3922 }
3923#endif
3924
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003925 /* We make sure to keep TP_ACPI_BLUETOOTH_RESUMECTRL off */
Henrique de Moraes Holschuh208b9962009-12-09 01:36:22 +00003926 status = TP_ACPI_BLUETOOTH_RESUMECTRL;
Johannes Berg19d337d2009-06-02 13:01:37 +02003927 if (state == TPACPI_RFK_RADIO_ON)
Henrique de Moraes Holschuh208b9962009-12-09 01:36:22 +00003928 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
Johannes Berg19d337d2009-06-02 13:01:37 +02003929
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003930 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
3931 return -EIO;
3932
3933 return 0;
3934}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003935
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003936/* sysfs bluetooth enable ---------------------------------------------- */
3937static ssize_t bluetooth_enable_show(struct device *dev,
3938 struct device_attribute *attr,
3939 char *buf)
3940{
Johannes Berg19d337d2009-06-02 13:01:37 +02003941 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID,
3942 attr, buf);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003943}
3944
3945static ssize_t bluetooth_enable_store(struct device *dev,
3946 struct device_attribute *attr,
3947 const char *buf, size_t count)
3948{
Johannes Berg19d337d2009-06-02 13:01:37 +02003949 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID,
3950 attr, buf, count);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003951}
3952
3953static struct device_attribute dev_attr_bluetooth_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03003954 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003955 bluetooth_enable_show, bluetooth_enable_store);
3956
3957/* --------------------------------------------------------------------- */
3958
3959static struct attribute *bluetooth_attributes[] = {
3960 &dev_attr_bluetooth_enable.attr,
3961 NULL
3962};
3963
3964static const struct attribute_group bluetooth_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003965 .attrs = bluetooth_attributes,
3966};
3967
Johannes Berg19d337d2009-06-02 13:01:37 +02003968static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = {
3969 .get_status = bluetooth_get_status,
3970 .set_status = bluetooth_set_status,
3971};
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003972
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003973static void bluetooth_shutdown(void)
3974{
3975 /* Order firmware to save current state to NVRAM */
3976 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3977 TP_ACPI_BLTH_SAVE_STATE))
3978 printk(TPACPI_NOTICE
3979 "failed to save bluetooth state to NVRAM\n");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003980 else
3981 vdbg_printk(TPACPI_DBG_RFKILL,
3982 "bluestooth state saved to NVRAM\n");
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003983}
3984
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003985static void bluetooth_exit(void)
3986{
3987 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3988 &bluetooth_attr_group);
Johannes Berg19d337d2009-06-02 13:01:37 +02003989
3990 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
3991
3992 bluetooth_shutdown();
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003993}
3994
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003995static int __init bluetooth_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003997 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003998 int status = 0;
3999
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004000 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4001 "initializing bluetooth subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004002
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004003 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004004
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004005 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4006 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004007 tp_features.bluetooth = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004008 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004010 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4011 "bluetooth is %s, status 0x%02x\n",
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004012 str_supported(tp_features.bluetooth),
4013 status);
4014
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004015#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4016 if (dbg_bluetoothemul) {
4017 tp_features.bluetooth = 1;
4018 printk(TPACPI_INFO
4019 "bluetooth switch emulation enabled\n");
4020 } else
4021#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004022 if (tp_features.bluetooth &&
4023 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
4024 /* no bluetooth hardware present in system */
4025 tp_features.bluetooth = 0;
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004026 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004027 "bluetooth hardware not installed\n");
4028 }
4029
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004030 if (!tp_features.bluetooth)
4031 return 1;
4032
Johannes Berg19d337d2009-06-02 13:01:37 +02004033 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
4034 &bluetooth_tprfk_ops,
4035 RFKILL_TYPE_BLUETOOTH,
4036 TPACPI_RFK_BLUETOOTH_SW_NAME,
4037 true);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004038 if (res)
4039 return res;
4040
Johannes Berg19d337d2009-06-02 13:01:37 +02004041 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4042 &bluetooth_attr_group);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004043 if (res) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004044 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004045 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004046 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004047
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004048 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049}
4050
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004051/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004052static int bluetooth_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053{
Johannes Berg19d337d2009-06-02 13:01:37 +02004054 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055}
4056
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004057static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058{
Johannes Berg19d337d2009-06-02 13:01:37 +02004059 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060}
4061
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004062static struct ibm_struct bluetooth_driver_data = {
4063 .name = "bluetooth",
4064 .read = bluetooth_read,
4065 .write = bluetooth_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004066 .exit = bluetooth_exit,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004067 .shutdown = bluetooth_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004068};
4069
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004070/*************************************************************************
4071 * Wan subdriver
4072 */
4073
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004074enum {
4075 /* ACPI GWAN/SWAN bits */
4076 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
4077 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02004078 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
4079 off / last state */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004080};
4081
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004082#define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
4083
Johannes Berg19d337d2009-06-02 13:01:37 +02004084static int wan_get_status(void)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004085{
4086 int status;
4087
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004088#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4089 if (dbg_wwanemul)
4090 return (tpacpi_wwan_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004091 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004092#endif
4093
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004094 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
4095 return -EIO;
4096
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004097 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004098 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004099}
4100
Johannes Berg19d337d2009-06-02 13:01:37 +02004101static int wan_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004102{
4103 int status;
4104
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004105 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02004106 "will attempt to %s wwan\n",
4107 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004108
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004109#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4110 if (dbg_wwanemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004111 tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004112 return 0;
4113 }
4114#endif
4115
Henrique de Moraes Holschuh208b9962009-12-09 01:36:22 +00004116 /* We make sure to set TP_ACPI_WANCARD_RESUMECTRL */
4117 status = TP_ACPI_WANCARD_RESUMECTRL;
Johannes Berg19d337d2009-06-02 13:01:37 +02004118 if (state == TPACPI_RFK_RADIO_ON)
Henrique de Moraes Holschuh208b9962009-12-09 01:36:22 +00004119 status |= TP_ACPI_WANCARD_RADIOSSW;
Johannes Berg19d337d2009-06-02 13:01:37 +02004120
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004121 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
4122 return -EIO;
4123
4124 return 0;
4125}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004126
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004127/* sysfs wan enable ---------------------------------------------------- */
4128static ssize_t wan_enable_show(struct device *dev,
4129 struct device_attribute *attr,
4130 char *buf)
4131{
Johannes Berg19d337d2009-06-02 13:01:37 +02004132 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
4133 attr, buf);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004134}
4135
4136static ssize_t wan_enable_store(struct device *dev,
4137 struct device_attribute *attr,
4138 const char *buf, size_t count)
4139{
Johannes Berg19d337d2009-06-02 13:01:37 +02004140 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
4141 attr, buf, count);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004142}
4143
4144static struct device_attribute dev_attr_wan_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03004145 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004146 wan_enable_show, wan_enable_store);
4147
4148/* --------------------------------------------------------------------- */
4149
4150static struct attribute *wan_attributes[] = {
4151 &dev_attr_wan_enable.attr,
4152 NULL
4153};
4154
4155static const struct attribute_group wan_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004156 .attrs = wan_attributes,
4157};
4158
Johannes Berg19d337d2009-06-02 13:01:37 +02004159static const struct tpacpi_rfk_ops wan_tprfk_ops = {
4160 .get_status = wan_get_status,
4161 .set_status = wan_set_status,
4162};
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004163
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004164static void wan_shutdown(void)
4165{
4166 /* Order firmware to save current state to NVRAM */
4167 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
4168 TP_ACPI_WGSV_SAVE_STATE))
4169 printk(TPACPI_NOTICE
4170 "failed to save WWAN state to NVRAM\n");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004171 else
4172 vdbg_printk(TPACPI_DBG_RFKILL,
4173 "WWAN state saved to NVRAM\n");
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004174}
4175
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004176static void wan_exit(void)
4177{
4178 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4179 &wan_attr_group);
Johannes Berg19d337d2009-06-02 13:01:37 +02004180
4181 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4182
4183 wan_shutdown();
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004184}
4185
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004186static int __init wan_init(struct ibm_init_struct *iibm)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004187{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004188 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004189 int status = 0;
4190
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004191 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4192 "initializing wan subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004193
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004194 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004195
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004196 tp_features.wan = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004197 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004198
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004199 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4200 "wan is %s, status 0x%02x\n",
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004201 str_supported(tp_features.wan),
4202 status);
4203
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004204#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4205 if (dbg_wwanemul) {
4206 tp_features.wan = 1;
4207 printk(TPACPI_INFO
4208 "wwan switch emulation enabled\n");
4209 } else
4210#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004211 if (tp_features.wan &&
4212 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
4213 /* no wan hardware present in system */
4214 tp_features.wan = 0;
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004215 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004216 "wan hardware not installed\n");
4217 }
4218
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004219 if (!tp_features.wan)
4220 return 1;
4221
Johannes Berg19d337d2009-06-02 13:01:37 +02004222 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
4223 &wan_tprfk_ops,
4224 RFKILL_TYPE_WWAN,
4225 TPACPI_RFK_WWAN_SW_NAME,
4226 true);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004227 if (res)
4228 return res;
4229
Johannes Berg19d337d2009-06-02 13:01:37 +02004230 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4231 &wan_attr_group);
4232
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004233 if (res) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004234 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004235 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004236 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004237
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004238 return 0;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004239}
4240
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004241/* procfs -------------------------------------------------------------- */
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004242static int wan_read(char *p)
4243{
Johannes Berg19d337d2009-06-02 13:01:37 +02004244 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, p);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004245}
4246
4247static int wan_write(char *buf)
4248{
Johannes Berg19d337d2009-06-02 13:01:37 +02004249 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004250}
4251
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004252static struct ibm_struct wan_driver_data = {
4253 .name = "wan",
4254 .read = wan_read,
4255 .write = wan_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004256 .exit = wan_exit,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004257 .shutdown = wan_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004258};
4259
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004260/*************************************************************************
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004261 * UWB subdriver
4262 */
4263
4264enum {
4265 /* ACPI GUWB/SUWB bits */
4266 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4267 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4268};
4269
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004270#define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4271
Johannes Berg19d337d2009-06-02 13:01:37 +02004272static int uwb_get_status(void)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004273{
4274 int status;
4275
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004276#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4277 if (dbg_uwbemul)
4278 return (tpacpi_uwb_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004279 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004280#endif
4281
4282 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4283 return -EIO;
4284
4285 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004286 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004287}
4288
Johannes Berg19d337d2009-06-02 13:01:37 +02004289static int uwb_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004290{
4291 int status;
4292
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004293 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02004294 "will attempt to %s UWB\n",
4295 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004296
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004297#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4298 if (dbg_uwbemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004299 tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004300 return 0;
4301 }
4302#endif
4303
Johannes Berg19d337d2009-06-02 13:01:37 +02004304 if (state == TPACPI_RFK_RADIO_ON)
4305 status = TP_ACPI_UWB_RADIOSSW;
4306 else
4307 status = 0;
4308
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004309 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4310 return -EIO;
4311
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004312 return 0;
4313}
4314
4315/* --------------------------------------------------------------------- */
4316
Johannes Berg19d337d2009-06-02 13:01:37 +02004317static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
4318 .get_status = uwb_get_status,
4319 .set_status = uwb_set_status,
4320};
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004321
4322static void uwb_exit(void)
4323{
Johannes Berg19d337d2009-06-02 13:01:37 +02004324 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004325}
4326
4327static int __init uwb_init(struct ibm_init_struct *iibm)
4328{
4329 int res;
4330 int status = 0;
4331
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004332 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4333 "initializing uwb subdriver\n");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004334
4335 TPACPI_ACPIHANDLE_INIT(hkey);
4336
4337 tp_features.uwb = hkey_handle &&
4338 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4339
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004340 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4341 "uwb is %s, status 0x%02x\n",
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004342 str_supported(tp_features.uwb),
4343 status);
4344
4345#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4346 if (dbg_uwbemul) {
4347 tp_features.uwb = 1;
4348 printk(TPACPI_INFO
4349 "uwb switch emulation enabled\n");
4350 } else
4351#endif
4352 if (tp_features.uwb &&
4353 !(status & TP_ACPI_UWB_HWPRESENT)) {
4354 /* no uwb hardware present in system */
4355 tp_features.uwb = 0;
4356 dbg_printk(TPACPI_DBG_INIT,
4357 "uwb hardware not installed\n");
4358 }
4359
4360 if (!tp_features.uwb)
4361 return 1;
4362
4363 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
Johannes Berg19d337d2009-06-02 13:01:37 +02004364 &uwb_tprfk_ops,
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004365 RFKILL_TYPE_UWB,
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004366 TPACPI_RFK_UWB_SW_NAME,
Johannes Berg19d337d2009-06-02 13:01:37 +02004367 false);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004368 return res;
4369}
4370
4371static struct ibm_struct uwb_driver_data = {
4372 .name = "uwb",
4373 .exit = uwb_exit,
4374 .flags.experimental = 1,
4375};
4376
4377/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004378 * Video subdriver
4379 */
4380
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02004381#ifdef CONFIG_THINKPAD_ACPI_VIDEO
4382
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004383enum video_access_mode {
4384 TPACPI_VIDEO_NONE = 0,
4385 TPACPI_VIDEO_570, /* 570 */
4386 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4387 TPACPI_VIDEO_NEW, /* all others */
4388};
4389
4390enum { /* video status flags, based on VIDEO_570 */
4391 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4392 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4393 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4394};
4395
4396enum { /* TPACPI_VIDEO_570 constants */
4397 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4398 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4399 * video_status_flags */
4400 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4401 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4402};
4403
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02004404static enum video_access_mode video_supported;
4405static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004406
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004407static int video_autosw_get(void);
4408static int video_autosw_set(int enable);
4409
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004410TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004411
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004412static int __init video_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004414 int ivga;
4415
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004416 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4417
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004418 TPACPI_ACPIHANDLE_INIT(vid);
4419 TPACPI_ACPIHANDLE_INIT(vid2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004420
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004421 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4422 /* G41, assume IVGA doesn't change */
4423 vid_handle = vid2_handle;
4424
4425 if (!vid_handle)
4426 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004427 video_supported = TPACPI_VIDEO_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004428 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
4429 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004430 video_supported = TPACPI_VIDEO_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004431 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
4432 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004433 video_supported = TPACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004434 else
4435 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004436 video_supported = TPACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004438 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
4439 str_supported(video_supported != TPACPI_VIDEO_NONE),
4440 video_supported);
4441
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004442 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443}
4444
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004445static void video_exit(void)
4446{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004447 dbg_printk(TPACPI_DBG_EXIT,
4448 "restoring original video autoswitch mode\n");
4449 if (video_autosw_set(video_orig_autosw))
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004450 printk(TPACPI_ERR "error while trying to restore original "
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004451 "video autoswitch mode\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004452}
4453
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004454static int video_outputsw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455{
4456 int status = 0;
4457 int i;
4458
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004459 switch (video_supported) {
4460 case TPACPI_VIDEO_570:
4461 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
4462 TP_ACPI_VIDEO_570_PHSCMD))
4463 return -EIO;
4464 status = i & TP_ACPI_VIDEO_570_PHSMASK;
4465 break;
4466 case TPACPI_VIDEO_770:
4467 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4468 return -EIO;
4469 if (i)
4470 status |= TP_ACPI_VIDEO_S_LCD;
4471 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4472 return -EIO;
4473 if (i)
4474 status |= TP_ACPI_VIDEO_S_CRT;
4475 break;
4476 case TPACPI_VIDEO_NEW:
4477 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4478 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4479 return -EIO;
4480 if (i)
4481 status |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004483 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4484 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4485 return -EIO;
4486 if (i)
4487 status |= TP_ACPI_VIDEO_S_LCD;
4488 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4489 return -EIO;
4490 if (i)
4491 status |= TP_ACPI_VIDEO_S_DVI;
4492 break;
4493 default:
4494 return -ENOSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004495 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496
4497 return status;
4498}
4499
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004500static int video_outputsw_set(int status)
4501{
4502 int autosw;
4503 int res = 0;
4504
4505 switch (video_supported) {
4506 case TPACPI_VIDEO_570:
4507 res = acpi_evalf(NULL, NULL,
4508 "\\_SB.PHS2", "vdd",
4509 TP_ACPI_VIDEO_570_PHS2CMD,
4510 status | TP_ACPI_VIDEO_570_PHS2SET);
4511 break;
4512 case TPACPI_VIDEO_770:
4513 autosw = video_autosw_get();
4514 if (autosw < 0)
4515 return autosw;
4516
4517 res = video_autosw_set(1);
4518 if (res)
4519 return res;
4520 res = acpi_evalf(vid_handle, NULL,
4521 "ASWT", "vdd", status * 0x100, 0);
4522 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004523 printk(TPACPI_ERR
4524 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004525 return -EIO;
4526 }
4527 break;
4528 case TPACPI_VIDEO_NEW:
4529 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004530 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004531 break;
4532 default:
4533 return -ENOSYS;
4534 }
4535
4536 return (res)? 0 : -EIO;
4537}
4538
4539static int video_autosw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004540{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004541 int autosw = 0;
4542
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004543 switch (video_supported) {
4544 case TPACPI_VIDEO_570:
4545 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
4546 return -EIO;
4547 break;
4548 case TPACPI_VIDEO_770:
4549 case TPACPI_VIDEO_NEW:
4550 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4551 return -EIO;
4552 break;
4553 default:
4554 return -ENOSYS;
4555 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004556
4557 return autosw & 1;
4558}
4559
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004560static int video_autosw_set(int enable)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004561{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004562 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004563 return -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004564 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004565}
4566
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004567static int video_outputsw_cycle(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004568{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004569 int autosw = video_autosw_get();
4570 int res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004571
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004572 if (autosw < 0)
4573 return autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004574
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004575 switch (video_supported) {
4576 case TPACPI_VIDEO_570:
4577 res = video_autosw_set(1);
4578 if (res)
4579 return res;
4580 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4581 break;
4582 case TPACPI_VIDEO_770:
4583 case TPACPI_VIDEO_NEW:
4584 res = video_autosw_set(1);
4585 if (res)
4586 return res;
4587 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4588 break;
4589 default:
4590 return -ENOSYS;
4591 }
4592 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004593 printk(TPACPI_ERR
4594 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004595 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004596 }
4597
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004598 return (res)? 0 : -EIO;
4599}
4600
4601static int video_expand_toggle(void)
4602{
4603 switch (video_supported) {
4604 case TPACPI_VIDEO_570:
4605 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
4606 0 : -EIO;
4607 case TPACPI_VIDEO_770:
4608 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
4609 0 : -EIO;
4610 case TPACPI_VIDEO_NEW:
4611 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
4612 0 : -EIO;
4613 default:
4614 return -ENOSYS;
4615 }
4616 /* not reached */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004617}
4618
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004619static int video_read(char *p)
4620{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004621 int status, autosw;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004622 int len = 0;
4623
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004624 if (video_supported == TPACPI_VIDEO_NONE) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004625 len += sprintf(p + len, "status:\t\tnot supported\n");
4626 return len;
4627 }
4628
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004629 status = video_outputsw_get();
4630 if (status < 0)
4631 return status;
4632
4633 autosw = video_autosw_get();
4634 if (autosw < 0)
4635 return autosw;
4636
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004637 len += sprintf(p + len, "status:\t\tsupported\n");
4638 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
4639 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004640 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004641 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
4642 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
4643 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
4644 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004645 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004646 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
4647 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
4648 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
4649
4650 return len;
4651}
4652
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004653static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654{
4655 char *cmd;
4656 int enable, disable, status;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004657 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004659 if (video_supported == TPACPI_VIDEO_NONE)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004660 return -ENODEV;
4661
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004662 enable = 0;
4663 disable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664
4665 while ((cmd = next_cmd(&buf))) {
4666 if (strlencmp(cmd, "lcd_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004667 enable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668 } else if (strlencmp(cmd, "lcd_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004669 disable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670 } else if (strlencmp(cmd, "crt_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004671 enable |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004672 } else if (strlencmp(cmd, "crt_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004673 disable |= TP_ACPI_VIDEO_S_CRT;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004674 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004675 strlencmp(cmd, "dvi_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004676 enable |= TP_ACPI_VIDEO_S_DVI;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004677 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004678 strlencmp(cmd, "dvi_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004679 disable |= TP_ACPI_VIDEO_S_DVI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680 } else if (strlencmp(cmd, "auto_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004681 res = video_autosw_set(1);
4682 if (res)
4683 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684 } else if (strlencmp(cmd, "auto_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004685 res = video_autosw_set(0);
4686 if (res)
4687 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688 } else if (strlencmp(cmd, "video_switch") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004689 res = video_outputsw_cycle();
4690 if (res)
4691 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004692 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004693 res = video_expand_toggle();
4694 if (res)
4695 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696 } else
4697 return -EINVAL;
4698 }
4699
4700 if (enable || disable) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004701 status = video_outputsw_get();
4702 if (status < 0)
4703 return status;
4704 res = video_outputsw_set((status & ~disable) | enable);
4705 if (res)
4706 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707 }
4708
4709 return 0;
4710}
4711
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004712static struct ibm_struct video_driver_data = {
4713 .name = "video",
4714 .read = video_read,
4715 .write = video_write,
4716 .exit = video_exit,
4717};
4718
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02004719#endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4720
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004721/*************************************************************************
4722 * Light (thinklight) subdriver
4723 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004724
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004725TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4726TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004727
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004728static int light_get_status(void)
4729{
4730 int status = 0;
4731
4732 if (tp_features.light_status) {
4733 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4734 return -EIO;
4735 return (!!status);
4736 }
4737
4738 return -ENXIO;
4739}
4740
4741static int light_set_status(int status)
4742{
4743 int rc;
4744
4745 if (tp_features.light) {
4746 if (cmos_handle) {
4747 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4748 (status)?
4749 TP_CMOS_THINKLIGHT_ON :
4750 TP_CMOS_THINKLIGHT_OFF);
4751 } else {
4752 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4753 (status)? 1 : 0);
4754 }
4755 return (rc)? 0 : -EIO;
4756 }
4757
4758 return -ENXIO;
4759}
4760
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004761static void light_set_status_worker(struct work_struct *work)
4762{
4763 struct tpacpi_led_classdev *data =
4764 container_of(work, struct tpacpi_led_classdev, work);
4765
4766 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00004767 light_set_status((data->new_state != TPACPI_LED_OFF));
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004768}
4769
4770static void light_sysfs_set(struct led_classdev *led_cdev,
4771 enum led_brightness brightness)
4772{
4773 struct tpacpi_led_classdev *data =
4774 container_of(led_cdev,
4775 struct tpacpi_led_classdev,
4776 led_classdev);
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00004777 data->new_state = (brightness != LED_OFF) ?
4778 TPACPI_LED_ON : TPACPI_LED_OFF;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004779 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004780}
4781
4782static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4783{
4784 return (light_get_status() == 1)? LED_FULL : LED_OFF;
4785}
4786
4787static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4788 .led_classdev = {
4789 .name = "tpacpi::thinklight",
4790 .brightness_set = &light_sysfs_set,
4791 .brightness_get = &light_sysfs_get,
4792 }
4793};
4794
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004795static int __init light_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796{
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004797 int rc;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004798
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004799 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4800
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004801 TPACPI_ACPIHANDLE_INIT(ledb);
4802 TPACPI_ACPIHANDLE_INIT(lght);
4803 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004804 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004805
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004806 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004807 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004808
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004809 if (tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004810 /* light status not supported on
4811 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004812 tp_features.light_status =
4813 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004814
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004815 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4816 str_supported(tp_features.light),
4817 str_supported(tp_features.light_status));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004818
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004819 if (!tp_features.light)
4820 return 1;
4821
4822 rc = led_classdev_register(&tpacpi_pdev->dev,
4823 &tpacpi_led_thinklight.led_classdev);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004824
4825 if (rc < 0) {
4826 tp_features.light = 0;
4827 tp_features.light_status = 0;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004828 } else {
4829 rc = 0;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004830 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004831
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004832 return rc;
4833}
4834
4835static void light_exit(void)
4836{
4837 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
4838 if (work_pending(&tpacpi_led_thinklight.work))
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004839 flush_workqueue(tpacpi_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840}
4841
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004842static int light_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843{
4844 int len = 0;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004845 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004847 if (!tp_features.light) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004848 len += sprintf(p + len, "status:\t\tnot supported\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004849 } else if (!tp_features.light_status) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004850 len += sprintf(p + len, "status:\t\tunknown\n");
4851 len += sprintf(p + len, "commands:\ton, off\n");
4852 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004853 status = light_get_status();
4854 if (status < 0)
4855 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004857 len += sprintf(p + len, "commands:\ton, off\n");
4858 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004859
4860 return len;
4861}
4862
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004863static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004866 int newstatus = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004867
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004868 if (!tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004869 return -ENODEV;
4870
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871 while ((cmd = next_cmd(&buf))) {
4872 if (strlencmp(cmd, "on") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004873 newstatus = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874 } else if (strlencmp(cmd, "off") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004875 newstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876 } else
4877 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004878 }
4879
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004880 return light_set_status(newstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004881}
4882
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004883static struct ibm_struct light_driver_data = {
4884 .name = "light",
4885 .read = light_read,
4886 .write = light_write,
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004887 .exit = light_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004888};
4889
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004890/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004891 * CMOS subdriver
4892 */
4893
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004894/* sysfs cmos_command -------------------------------------------------- */
4895static ssize_t cmos_command_store(struct device *dev,
4896 struct device_attribute *attr,
4897 const char *buf, size_t count)
4898{
4899 unsigned long cmos_cmd;
4900 int res;
4901
4902 if (parse_strtoul(buf, 21, &cmos_cmd))
4903 return -EINVAL;
4904
4905 res = issue_thinkpad_cmos_command(cmos_cmd);
4906 return (res)? res : count;
4907}
4908
4909static struct device_attribute dev_attr_cmos_command =
4910 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4911
4912/* --------------------------------------------------------------------- */
4913
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004914static int __init cmos_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004915{
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004916 int res;
4917
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004918 vdbg_printk(TPACPI_DBG_INIT,
4919 "initializing cmos commands subdriver\n");
4920
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004921 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004922
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004923 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4924 str_supported(cmos_handle != NULL));
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004925
4926 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4927 if (res)
4928 return res;
4929
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004930 return (cmos_handle)? 0 : 1;
4931}
4932
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004933static void cmos_exit(void)
4934{
4935 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4936}
4937
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004938static int cmos_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004939{
4940 int len = 0;
4941
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004942 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4943 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944 if (!cmos_handle)
4945 len += sprintf(p + len, "status:\t\tnot supported\n");
4946 else {
4947 len += sprintf(p + len, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004948 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949 }
4950
4951 return len;
4952}
4953
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004954static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955{
4956 char *cmd;
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004957 int cmos_cmd, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004958
4959 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004960 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
4961 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962 /* cmos_cmd set */
4963 } else
4964 return -EINVAL;
4965
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004966 res = issue_thinkpad_cmos_command(cmos_cmd);
4967 if (res)
4968 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969 }
4970
4971 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004972}
4973
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004974static struct ibm_struct cmos_driver_data = {
4975 .name = "cmos",
4976 .read = cmos_read,
4977 .write = cmos_write,
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004978 .exit = cmos_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004979};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004980
4981/*************************************************************************
4982 * LED subdriver
4983 */
4984
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004985enum led_access_mode {
4986 TPACPI_LED_NONE = 0,
4987 TPACPI_LED_570, /* 570 */
4988 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4989 TPACPI_LED_NEW, /* all others */
4990};
4991
4992enum { /* For TPACPI_LED_OLD */
4993 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
4994 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
4995 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
4996};
4997
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02004998static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004999
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005000TPACPI_HANDLE(led, ec, "SLED", /* 570 */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005001 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
5002 /* T20-22, X20-21 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005003 "LED", /* all others */
5004 ); /* R30, R31 */
5005
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005006#define TPACPI_LED_NUMLEDS 16
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005007static struct tpacpi_led_classdev *tpacpi_leds;
5008static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
Harvey Harrisone3aa51f2008-05-29 17:51:57 -07005009static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005010 /* there's a limit of 19 chars + NULL before 2.6.26 */
5011 "tpacpi::power",
5012 "tpacpi:orange:batt",
5013 "tpacpi:green:batt",
5014 "tpacpi::dock_active",
5015 "tpacpi::bay_active",
5016 "tpacpi::dock_batt",
5017 "tpacpi::unknown_led",
5018 "tpacpi::standby",
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005019 "tpacpi::dock_status1",
5020 "tpacpi::dock_status2",
5021 "tpacpi::unknown_led2",
5022 "tpacpi::unknown_led3",
5023 "tpacpi::thinkvantage",
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005024};
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005025#define TPACPI_SAFE_LEDS 0x1081U
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005026
5027static inline bool tpacpi_is_led_restricted(const unsigned int led)
5028{
5029#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5030 return false;
5031#else
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005032 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005033#endif
5034}
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005035
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005036static int led_get_status(const unsigned int led)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005037{
5038 int status;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005039 enum led_status_t led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005040
5041 switch (led_supported) {
5042 case TPACPI_LED_570:
5043 if (!acpi_evalf(ec_handle,
5044 &status, "GLED", "dd", 1 << led))
5045 return -EIO;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005046 led_s = (status == 0)?
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005047 TPACPI_LED_OFF :
5048 ((status == 1)?
5049 TPACPI_LED_ON :
5050 TPACPI_LED_BLINK);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005051 tpacpi_led_state_cache[led] = led_s;
5052 return led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005053 default:
5054 return -ENXIO;
5055 }
5056
5057 /* not reached */
5058}
5059
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005060static int led_set_status(const unsigned int led,
5061 const enum led_status_t ledstatus)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005062{
5063 /* off, on, blink. Index is led_status_t */
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005064 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
5065 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005066
5067 int rc = 0;
5068
5069 switch (led_supported) {
5070 case TPACPI_LED_570:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005071 /* 570 */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005072 if (unlikely(led > 7))
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005073 return -EINVAL;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005074 if (unlikely(tpacpi_is_led_restricted(led)))
5075 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005076 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5077 (1 << led), led_sled_arg1[ledstatus]))
5078 rc = -EIO;
5079 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005080 case TPACPI_LED_OLD:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005081 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005082 if (unlikely(led > 7))
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005083 return -EINVAL;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005084 if (unlikely(tpacpi_is_led_restricted(led)))
5085 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005086 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
5087 if (rc >= 0)
5088 rc = ec_write(TPACPI_LED_EC_HLBL,
5089 (ledstatus == TPACPI_LED_BLINK) << led);
5090 if (rc >= 0)
5091 rc = ec_write(TPACPI_LED_EC_HLCL,
5092 (ledstatus != TPACPI_LED_OFF) << led);
5093 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005094 case TPACPI_LED_NEW:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005095 /* all others */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005096 if (unlikely(led >= TPACPI_LED_NUMLEDS))
5097 return -EINVAL;
5098 if (unlikely(tpacpi_is_led_restricted(led)))
5099 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005100 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5101 led, led_led_arg1[ledstatus]))
5102 rc = -EIO;
5103 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005104 default:
5105 rc = -ENXIO;
5106 }
5107
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005108 if (!rc)
5109 tpacpi_led_state_cache[led] = ledstatus;
5110
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005111 return rc;
5112}
5113
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005114static void led_set_status_worker(struct work_struct *work)
5115{
5116 struct tpacpi_led_classdev *data =
5117 container_of(work, struct tpacpi_led_classdev, work);
5118
5119 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005120 led_set_status(data->led, data->new_state);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005121}
5122
5123static void led_sysfs_set(struct led_classdev *led_cdev,
5124 enum led_brightness brightness)
5125{
5126 struct tpacpi_led_classdev *data = container_of(led_cdev,
5127 struct tpacpi_led_classdev, led_classdev);
5128
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005129 if (brightness == LED_OFF)
5130 data->new_state = TPACPI_LED_OFF;
5131 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
5132 data->new_state = TPACPI_LED_ON;
5133 else
5134 data->new_state = TPACPI_LED_BLINK;
5135
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03005136 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005137}
5138
5139static int led_sysfs_blink_set(struct led_classdev *led_cdev,
5140 unsigned long *delay_on, unsigned long *delay_off)
5141{
5142 struct tpacpi_led_classdev *data = container_of(led_cdev,
5143 struct tpacpi_led_classdev, led_classdev);
5144
5145 /* Can we choose the flash rate? */
5146 if (*delay_on == 0 && *delay_off == 0) {
5147 /* yes. set them to the hardware blink rate (1 Hz) */
5148 *delay_on = 500; /* ms */
5149 *delay_off = 500; /* ms */
5150 } else if ((*delay_on != 500) || (*delay_off != 500))
5151 return -EINVAL;
5152
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005153 data->new_state = TPACPI_LED_BLINK;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03005154 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005155
5156 return 0;
5157}
5158
5159static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
5160{
5161 int rc;
5162
5163 struct tpacpi_led_classdev *data = container_of(led_cdev,
5164 struct tpacpi_led_classdev, led_classdev);
5165
5166 rc = led_get_status(data->led);
5167
5168 if (rc == TPACPI_LED_OFF || rc < 0)
5169 rc = LED_OFF; /* no error handling in led class :( */
5170 else
5171 rc = LED_FULL;
5172
5173 return rc;
5174}
5175
5176static void led_exit(void)
5177{
5178 unsigned int i;
5179
5180 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5181 if (tpacpi_leds[i].led_classdev.name)
5182 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
5183 }
5184
5185 kfree(tpacpi_leds);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005186}
5187
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005188static int __init tpacpi_init_led(unsigned int led)
5189{
5190 int rc;
5191
5192 tpacpi_leds[led].led = led;
5193
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005194 /* LEDs with no name don't get registered */
5195 if (!tpacpi_led_names[led])
5196 return 0;
5197
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005198 tpacpi_leds[led].led_classdev.brightness_set = &led_sysfs_set;
5199 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5200 if (led_supported == TPACPI_LED_570)
5201 tpacpi_leds[led].led_classdev.brightness_get =
5202 &led_sysfs_get;
5203
5204 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5205
5206 INIT_WORK(&tpacpi_leds[led].work, led_set_status_worker);
5207
5208 rc = led_classdev_register(&tpacpi_pdev->dev,
5209 &tpacpi_leds[led].led_classdev);
5210 if (rc < 0)
5211 tpacpi_leds[led].led_classdev.name = NULL;
5212
5213 return rc;
5214}
5215
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005216static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
5217 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5218 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5219 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5220
5221 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5222 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5223 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5224 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5225 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5226 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5227 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5228 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5229
5230 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5231 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5232 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5233 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5234 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5235
5236 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5237 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5238 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5239 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5240
5241 /* (1) - may have excess leds enabled on MSB */
5242
5243 /* Defaults (order matters, keep last, don't reorder!) */
5244 { /* Lenovo */
5245 .vendor = PCI_VENDOR_ID_LENOVO,
5246 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5247 .quirks = 0x1fffU,
5248 },
5249 { /* IBM ThinkPads with no EC version string */
5250 .vendor = PCI_VENDOR_ID_IBM,
5251 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
5252 .quirks = 0x00ffU,
5253 },
5254 { /* IBM ThinkPads with EC version string */
5255 .vendor = PCI_VENDOR_ID_IBM,
5256 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5257 .quirks = 0x00bfU,
5258 },
5259};
5260
5261#undef TPACPI_LEDQ_IBM
5262#undef TPACPI_LEDQ_LNV
5263
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005264static int __init led_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005265{
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005266 unsigned int i;
5267 int rc;
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005268 unsigned long useful_leds;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005269
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005270 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
5271
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005272 TPACPI_ACPIHANDLE_INIT(led);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005273
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005274 if (!led_handle)
5275 /* led not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005276 led_supported = TPACPI_LED_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005277 else if (strlencmp(led_path, "SLED") == 0)
5278 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005279 led_supported = TPACPI_LED_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005280 else if (strlencmp(led_path, "SYSL") == 0)
5281 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005282 led_supported = TPACPI_LED_OLD;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005283 else
5284 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005285 led_supported = TPACPI_LED_NEW;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005286
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005287 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
5288 str_supported(led_supported), led_supported);
5289
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005290 if (led_supported == TPACPI_LED_NONE)
5291 return 1;
5292
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005293 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
5294 GFP_KERNEL);
5295 if (!tpacpi_leds) {
5296 printk(TPACPI_ERR "Out of memory for LED data\n");
5297 return -ENOMEM;
5298 }
5299
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005300 useful_leds = tpacpi_check_quirks(led_useful_qtable,
5301 ARRAY_SIZE(led_useful_qtable));
5302
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005303 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005304 if (!tpacpi_is_led_restricted(i) &&
5305 test_bit(i, &useful_leds)) {
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005306 rc = tpacpi_init_led(i);
5307 if (rc < 0) {
5308 led_exit();
5309 return rc;
5310 }
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005311 }
5312 }
5313
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005314#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005315 printk(TPACPI_NOTICE
5316 "warning: userspace override of important "
5317 "firmware LEDs is enabled\n");
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005318#endif
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005319 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005320}
5321
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005322#define str_led_status(s) \
5323 ((s) == TPACPI_LED_OFF ? "off" : \
5324 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005325
5326static int led_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005327{
5328 int len = 0;
5329
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005330 if (!led_supported) {
5331 len += sprintf(p + len, "status:\t\tnot supported\n");
5332 return len;
5333 }
5334 len += sprintf(p + len, "status:\t\tsupported\n");
5335
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005336 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005337 /* 570 */
5338 int i, status;
5339 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005340 status = led_get_status(i);
5341 if (status < 0)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005342 return -EIO;
5343 len += sprintf(p + len, "%d:\t\t%s\n",
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005344 i, str_led_status(status));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005345 }
5346 }
5347
Linus Torvalds1da177e2005-04-16 15:20:36 -07005348 len += sprintf(p + len, "commands:\t"
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005349 "<led> on, <led> off, <led> blink (<led> is 0-15)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005350
5351 return len;
5352}
5353
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005354static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005355{
5356 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005357 int led, rc;
5358 enum led_status_t s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005359
5360 if (!led_supported)
5361 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005362
5363 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005364 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 15)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005365 return -EINVAL;
5366
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005367 if (strstr(cmd, "off")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005368 s = TPACPI_LED_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005369 } else if (strstr(cmd, "on")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005370 s = TPACPI_LED_ON;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005371 } else if (strstr(cmd, "blink")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005372 s = TPACPI_LED_BLINK;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005373 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005374 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005375 }
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005376
5377 rc = led_set_status(led, s);
5378 if (rc < 0)
5379 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005380 }
5381
5382 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005383}
5384
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005385static struct ibm_struct led_driver_data = {
5386 .name = "led",
5387 .read = led_read,
5388 .write = led_write,
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005389 .exit = led_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005390};
5391
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005392/*************************************************************************
5393 * Beep subdriver
5394 */
5395
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005396TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005397
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005398#define TPACPI_BEEP_Q1 0x0001
5399
5400static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
5401 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
5402 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
5403};
5404
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005405static int __init beep_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005406{
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005407 unsigned long quirks;
5408
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005409 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
5410
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005411 TPACPI_ACPIHANDLE_INIT(beep);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005412
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005413 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
5414 str_supported(beep_handle != NULL));
5415
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005416 quirks = tpacpi_check_quirks(beep_quirk_table,
5417 ARRAY_SIZE(beep_quirk_table));
5418
5419 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
5420
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005421 return (beep_handle)? 0 : 1;
5422}
5423
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005424static int beep_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005425{
5426 int len = 0;
5427
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005428 if (!beep_handle)
5429 len += sprintf(p + len, "status:\t\tnot supported\n");
5430 else {
5431 len += sprintf(p + len, "status:\t\tsupported\n");
5432 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
5433 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005434
5435 return len;
5436}
5437
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005438static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005439{
5440 char *cmd;
5441 int beep_cmd;
5442
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005443 if (!beep_handle)
5444 return -ENODEV;
5445
Linus Torvalds1da177e2005-04-16 15:20:36 -07005446 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005447 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
5448 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005449 /* beep_cmd set */
5450 } else
5451 return -EINVAL;
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005452 if (tp_features.beep_needs_two_args) {
5453 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
5454 beep_cmd, 0))
5455 return -EIO;
5456 } else {
5457 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
5458 beep_cmd))
5459 return -EIO;
5460 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005461 }
5462
5463 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005464}
5465
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005466static struct ibm_struct beep_driver_data = {
5467 .name = "beep",
5468 .read = beep_read,
5469 .write = beep_write,
5470};
5471
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005472/*************************************************************************
5473 * Thermal subdriver
5474 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005475
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005476enum thermal_access_mode {
5477 TPACPI_THERMAL_NONE = 0, /* No thermal support */
5478 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
5479 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
5480 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
5481 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
5482};
5483
5484enum { /* TPACPI_THERMAL_TPEC_* */
5485 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
5486 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
5487 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005488
5489 TPACPI_THERMAL_SENSOR_NA = -128000, /* Sensor not available */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005490};
5491
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005492
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005493#define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5494struct ibm_thermal_sensors_struct {
5495 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
5496};
5497
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005498static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005499
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005500/* idx is zero-based */
5501static int thermal_get_sensor(int idx, s32 *value)
5502{
5503 int t;
5504 s8 tmp;
5505 char tmpi[5];
5506
5507 t = TP_EC_THERMAL_TMP0;
5508
5509 switch (thermal_read_mode) {
5510#if TPACPI_MAX_THERMAL_SENSORS >= 16
5511 case TPACPI_THERMAL_TPEC_16:
5512 if (idx >= 8 && idx <= 15) {
5513 t = TP_EC_THERMAL_TMP8;
5514 idx -= 8;
5515 }
5516 /* fallthrough */
5517#endif
5518 case TPACPI_THERMAL_TPEC_8:
5519 if (idx <= 7) {
5520 if (!acpi_ec_read(t + idx, &tmp))
5521 return -EIO;
5522 *value = tmp * 1000;
5523 return 0;
5524 }
5525 break;
5526
5527 case TPACPI_THERMAL_ACPI_UPDT:
5528 if (idx <= 7) {
5529 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5530 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
5531 return -EIO;
5532 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5533 return -EIO;
5534 *value = (t - 2732) * 100;
5535 return 0;
5536 }
5537 break;
5538
5539 case TPACPI_THERMAL_ACPI_TMP07:
5540 if (idx <= 7) {
5541 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5542 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5543 return -EIO;
5544 if (t > 127 || t < -127)
5545 t = TP_EC_THERMAL_TMP_NA;
5546 *value = t * 1000;
5547 return 0;
5548 }
5549 break;
5550
5551 case TPACPI_THERMAL_NONE:
5552 default:
5553 return -ENOSYS;
5554 }
5555
5556 return -EINVAL;
5557}
5558
5559static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5560{
5561 int res, i;
5562 int n;
5563
5564 n = 8;
5565 i = 0;
5566
5567 if (!s)
5568 return -EINVAL;
5569
5570 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5571 n = 16;
5572
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005573 for (i = 0 ; i < n; i++) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005574 res = thermal_get_sensor(i, &s->temp[i]);
5575 if (res)
5576 return res;
5577 }
5578
5579 return n;
5580}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005581
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005582static void thermal_dump_all_sensors(void)
5583{
5584 int n, i;
5585 struct ibm_thermal_sensors_struct t;
5586
5587 n = thermal_get_sensors(&t);
5588 if (n <= 0)
5589 return;
5590
5591 printk(TPACPI_NOTICE
5592 "temperatures (Celsius):");
5593
5594 for (i = 0; i < n; i++) {
5595 if (t.temp[i] != TPACPI_THERMAL_SENSOR_NA)
5596 printk(KERN_CONT " %d", (int)(t.temp[i] / 1000));
5597 else
5598 printk(KERN_CONT " N/A");
5599 }
5600
5601 printk(KERN_CONT "\n");
5602}
5603
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005604/* sysfs temp##_input -------------------------------------------------- */
5605
5606static ssize_t thermal_temp_input_show(struct device *dev,
5607 struct device_attribute *attr,
5608 char *buf)
5609{
5610 struct sensor_device_attribute *sensor_attr =
5611 to_sensor_dev_attr(attr);
5612 int idx = sensor_attr->index;
5613 s32 value;
5614 int res;
5615
5616 res = thermal_get_sensor(idx, &value);
5617 if (res)
5618 return res;
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005619 if (value == TPACPI_THERMAL_SENSOR_NA)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005620 return -ENXIO;
5621
5622 return snprintf(buf, PAGE_SIZE, "%d\n", value);
5623}
5624
5625#define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005626 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5627 thermal_temp_input_show, NULL, _idxB)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005628
5629static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5630 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5631 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5632 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5633 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5634 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5635 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5636 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5637 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5638 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5639 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5640 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5641 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5642 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5643 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5644 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5645 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5646};
5647
5648#define THERMAL_ATTRS(X) \
5649 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5650
5651static struct attribute *thermal_temp_input_attr[] = {
5652 THERMAL_ATTRS(8),
5653 THERMAL_ATTRS(9),
5654 THERMAL_ATTRS(10),
5655 THERMAL_ATTRS(11),
5656 THERMAL_ATTRS(12),
5657 THERMAL_ATTRS(13),
5658 THERMAL_ATTRS(14),
5659 THERMAL_ATTRS(15),
5660 THERMAL_ATTRS(0),
5661 THERMAL_ATTRS(1),
5662 THERMAL_ATTRS(2),
5663 THERMAL_ATTRS(3),
5664 THERMAL_ATTRS(4),
5665 THERMAL_ATTRS(5),
5666 THERMAL_ATTRS(6),
5667 THERMAL_ATTRS(7),
5668 NULL
5669};
5670
5671static const struct attribute_group thermal_temp_input16_group = {
5672 .attrs = thermal_temp_input_attr
5673};
5674
5675static const struct attribute_group thermal_temp_input8_group = {
5676 .attrs = &thermal_temp_input_attr[8]
5677};
5678
5679#undef THERMAL_SENSOR_ATTR_TEMP
5680#undef THERMAL_ATTRS
5681
5682/* --------------------------------------------------------------------- */
5683
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005684static int __init thermal_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005685{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005686 u8 t, ta1, ta2;
5687 int i;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005688 int acpi_tmp7;
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005689 int res;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005690
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005691 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5692
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005693 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005694
Henrique de Moraes Holschuh3d6f99c2007-07-18 23:45:46 -03005695 if (thinkpad_id.ec_model) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005696 /*
5697 * Direct EC access mode: sensors at registers
5698 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5699 * non-implemented, thermal sensors return 0x80 when
5700 * not available
5701 */
5702
5703 ta1 = ta2 = 0;
5704 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03005705 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005706 ta1 |= t;
5707 } else {
5708 ta1 = 0;
5709 break;
5710 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03005711 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005712 ta2 |= t;
5713 } else {
5714 ta1 = 0;
5715 break;
5716 }
5717 }
5718 if (ta1 == 0) {
5719 /* This is sheer paranoia, but we handle it anyway */
5720 if (acpi_tmp7) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005721 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005722 "ThinkPad ACPI EC access misbehaving, "
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005723 "falling back to ACPI TMPx access "
5724 "mode\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005725 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005726 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005727 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005728 "ThinkPad ACPI EC access misbehaving, "
5729 "disabling thermal sensors access\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005730 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005731 }
5732 } else {
5733 thermal_read_mode =
5734 (ta2 != 0) ?
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005735 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005736 }
5737 } else if (acpi_tmp7) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005738 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
5739 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005740 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005741 } else {
5742 /* Standard ACPI TMPx access, max 8 sensors */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005743 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005744 }
5745 } else {
5746 /* temperatures not supported on 570, G4x, R30, R31, R32 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005747 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005748 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005749
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005750 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5751 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5752 thermal_read_mode);
5753
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005754 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005755 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005756 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005757 &thermal_temp_input16_group);
5758 if (res)
5759 return res;
5760 break;
5761 case TPACPI_THERMAL_TPEC_8:
5762 case TPACPI_THERMAL_ACPI_TMP07:
5763 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005764 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005765 &thermal_temp_input8_group);
5766 if (res)
5767 return res;
5768 break;
5769 case TPACPI_THERMAL_NONE:
5770 default:
5771 return 1;
5772 }
5773
5774 return 0;
5775}
5776
5777static void thermal_exit(void)
5778{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005779 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005780 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005781 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005782 &thermal_temp_input16_group);
5783 break;
5784 case TPACPI_THERMAL_TPEC_8:
5785 case TPACPI_THERMAL_ACPI_TMP07:
5786 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005787 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005788 &thermal_temp_input16_group);
5789 break;
5790 case TPACPI_THERMAL_NONE:
5791 default:
5792 break;
5793 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005794}
5795
5796static int thermal_read(char *p)
5797{
5798 int len = 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005799 int n, i;
5800 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005801
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005802 n = thermal_get_sensors(&t);
5803 if (unlikely(n < 0))
5804 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005805
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005806 len += sprintf(p + len, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005807
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005808 if (n > 0) {
5809 for (i = 0; i < (n - 1); i++)
5810 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
5811 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
5812 } else
5813 len += sprintf(p + len, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005814
5815 return len;
5816}
5817
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005818static struct ibm_struct thermal_driver_data = {
5819 .name = "thermal",
5820 .read = thermal_read,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005821 .exit = thermal_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005822};
5823
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005824/*************************************************************************
5825 * EC Dump subdriver
5826 */
5827
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005828static u8 ecdump_regs[256];
5829
5830static int ecdump_read(char *p)
5831{
5832 int len = 0;
5833 int i, j;
5834 u8 v;
5835
5836 len += sprintf(p + len, "EC "
5837 " +00 +01 +02 +03 +04 +05 +06 +07"
5838 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
5839 for (i = 0; i < 256; i += 16) {
5840 len += sprintf(p + len, "EC 0x%02x:", i);
5841 for (j = 0; j < 16; j++) {
5842 if (!acpi_ec_read(i + j, &v))
5843 break;
5844 if (v != ecdump_regs[i + j])
5845 len += sprintf(p + len, " *%02x", v);
5846 else
5847 len += sprintf(p + len, " %02x", v);
5848 ecdump_regs[i + j] = v;
5849 }
5850 len += sprintf(p + len, "\n");
5851 if (j != 16)
5852 break;
5853 }
5854
5855 /* These are way too dangerous to advertise openly... */
5856#if 0
5857 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
5858 " (<offset> is 00-ff, <value> is 00-ff)\n");
5859 len += sprintf(p + len, "commands:\t0x<offset> <value> "
5860 " (<offset> is 00-ff, <value> is 0-255)\n");
5861#endif
5862 return len;
5863}
5864
5865static int ecdump_write(char *buf)
5866{
5867 char *cmd;
5868 int i, v;
5869
5870 while ((cmd = next_cmd(&buf))) {
5871 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
5872 /* i and v set */
5873 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
5874 /* i and v set */
5875 } else
5876 return -EINVAL;
5877 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
5878 if (!acpi_ec_write(i, v))
5879 return -EIO;
5880 } else
5881 return -EINVAL;
5882 }
5883
5884 return 0;
5885}
5886
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005887static struct ibm_struct ecdump_driver_data = {
5888 .name = "ecdump",
5889 .read = ecdump_read,
5890 .write = ecdump_write,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03005891 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005892};
5893
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005894/*************************************************************************
5895 * Backlight/brightness subdriver
5896 */
Holger Macht8acb0252006-10-20 14:30:28 -07005897
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005898#define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5899
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005900/*
5901 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
5902 * CMOS NVRAM byte 0x5E, bits 0-3.
5903 *
5904 * EC HBRV (0x31) has the following layout
5905 * Bit 7: unknown function
5906 * Bit 6: unknown function
5907 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5908 * Bit 4: must be set to zero to avoid problems
5909 * Bit 3-0: backlight brightness level
5910 *
5911 * brightness_get_raw returns status data in the HBRV layout
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03005912 *
5913 * WARNING: The X61 has been verified to use HBRV for something else, so
5914 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
5915 * testing on the very early *60 Lenovo models...
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005916 */
5917
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005918enum {
5919 TP_EC_BACKLIGHT = 0x31,
5920
5921 /* TP_EC_BACKLIGHT bitmasks */
5922 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5923 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5924 TP_EC_BACKLIGHT_MAPSW = 0x20,
5925};
5926
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005927enum tpacpi_brightness_access_mode {
5928 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
5929 TPACPI_BRGHT_MODE_EC, /* EC control */
5930 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
5931 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
5932 TPACPI_BRGHT_MODE_MAX
5933};
5934
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03005935static struct backlight_device *ibm_backlight_device;
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005936
5937static enum tpacpi_brightness_access_mode brightness_mode =
5938 TPACPI_BRGHT_MODE_MAX;
5939
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005940static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5941
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005942static struct mutex brightness_mutex;
5943
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005944/* NVRAM brightness access,
5945 * call with brightness_mutex held! */
5946static unsigned int tpacpi_brightness_nvram_get(void)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005947{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005948 u8 lnvram;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005949
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005950 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5951 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5952 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
5953 lnvram &= (tp_features.bright_16levels) ? 0x0f : 0x07;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005954
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005955 return lnvram;
5956}
5957
5958static void tpacpi_brightness_checkpoint_nvram(void)
5959{
5960 u8 lec = 0;
5961 u8 b_nvram;
5962
5963 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
5964 return;
5965
5966 vdbg_printk(TPACPI_DBG_BRGHT,
5967 "trying to checkpoint backlight level to NVRAM...\n");
5968
5969 if (mutex_lock_killable(&brightness_mutex) < 0)
5970 return;
5971
5972 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5973 goto unlock;
5974 lec &= TP_EC_BACKLIGHT_LVLMSK;
5975 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
5976
5977 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5978 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
5979 /* NVRAM needs update */
5980 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
5981 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
5982 b_nvram |= lec;
5983 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
5984 dbg_printk(TPACPI_DBG_BRGHT,
5985 "updated NVRAM backlight level to %u (0x%02x)\n",
5986 (unsigned int) lec, (unsigned int) b_nvram);
5987 } else
5988 vdbg_printk(TPACPI_DBG_BRGHT,
5989 "NVRAM backlight level already is %u (0x%02x)\n",
5990 (unsigned int) lec, (unsigned int) b_nvram);
5991
5992unlock:
5993 mutex_unlock(&brightness_mutex);
5994}
5995
5996
5997/* call with brightness_mutex held! */
5998static int tpacpi_brightness_get_raw(int *status)
5999{
6000 u8 lec = 0;
6001
6002 switch (brightness_mode) {
6003 case TPACPI_BRGHT_MODE_UCMS_STEP:
6004 *status = tpacpi_brightness_nvram_get();
6005 return 0;
6006 case TPACPI_BRGHT_MODE_EC:
6007 case TPACPI_BRGHT_MODE_ECNVRAM:
6008 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
Henrique de Moraes Holschuh2d5e94d2008-04-26 01:02:20 -03006009 return -EIO;
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006010 *status = lec;
6011 return 0;
6012 default:
6013 return -ENXIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006014 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006015}
6016
6017/* call with brightness_mutex held! */
6018/* do NOT call with illegal backlight level value */
6019static int tpacpi_brightness_set_ec(unsigned int value)
6020{
6021 u8 lec = 0;
6022
6023 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6024 return -EIO;
6025
6026 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
6027 (lec & TP_EC_BACKLIGHT_CMDMSK) |
6028 (value & TP_EC_BACKLIGHT_LVLMSK))))
6029 return -EIO;
6030
6031 return 0;
6032}
6033
6034/* call with brightness_mutex held! */
6035static int tpacpi_brightness_set_ucmsstep(unsigned int value)
6036{
6037 int cmos_cmd, inc;
6038 unsigned int current_value, i;
6039
6040 current_value = tpacpi_brightness_nvram_get();
6041
6042 if (value == current_value)
6043 return 0;
6044
6045 cmos_cmd = (value > current_value) ?
6046 TP_CMOS_BRIGHTNESS_UP :
6047 TP_CMOS_BRIGHTNESS_DOWN;
6048 inc = (value > current_value) ? 1 : -1;
6049
6050 for (i = current_value; i != value; i += inc)
6051 if (issue_thinkpad_cmos_command(cmos_cmd))
6052 return -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006053
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006054 return 0;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006055}
6056
6057/* May return EINTR which can always be mapped to ERESTARTSYS */
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006058static int brightness_set(unsigned int value)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006059{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006060 int res;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006061
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006062 if (value > ((tp_features.bright_16levels)? 15 : 7) ||
6063 value < 0)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006064 return -EINVAL;
6065
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006066 vdbg_printk(TPACPI_DBG_BRGHT,
6067 "set backlight level to %d\n", value);
6068
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006069 res = mutex_lock_killable(&brightness_mutex);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006070 if (res < 0)
6071 return res;
6072
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006073 switch (brightness_mode) {
6074 case TPACPI_BRGHT_MODE_EC:
6075 case TPACPI_BRGHT_MODE_ECNVRAM:
6076 res = tpacpi_brightness_set_ec(value);
6077 break;
6078 case TPACPI_BRGHT_MODE_UCMS_STEP:
6079 res = tpacpi_brightness_set_ucmsstep(value);
6080 break;
6081 default:
6082 res = -ENXIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006083 }
6084
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006085 mutex_unlock(&brightness_mutex);
6086 return res;
6087}
6088
6089/* sysfs backlight class ----------------------------------------------- */
6090
6091static int brightness_update_status(struct backlight_device *bd)
6092{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006093 unsigned int level =
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006094 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
6095 bd->props.power == FB_BLANK_UNBLANK) ?
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006096 bd->props.brightness : 0;
6097
6098 dbg_printk(TPACPI_DBG_BRGHT,
6099 "backlight: attempt to set level to %d\n",
6100 level);
6101
6102 /* it is the backlight class's job (caller) to handle
6103 * EINTR and other errors properly */
6104 return brightness_set(level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006105}
Holger Macht8acb0252006-10-20 14:30:28 -07006106
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006107static int brightness_get(struct backlight_device *bd)
6108{
6109 int status, res;
6110
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006111 res = mutex_lock_killable(&brightness_mutex);
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006112 if (res < 0)
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006113 return 0;
6114
6115 res = tpacpi_brightness_get_raw(&status);
6116
6117 mutex_unlock(&brightness_mutex);
6118
6119 if (res < 0)
6120 return 0;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006121
6122 return status & TP_EC_BACKLIGHT_LVLMSK;
6123}
6124
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00006125static void tpacpi_brightness_notify_change(void)
6126{
6127 backlight_force_update(ibm_backlight_device,
6128 BACKLIGHT_UPDATE_HOTKEY);
6129}
6130
Richard Purdie599a52d2007-02-10 23:07:48 +00006131static struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006132 .get_brightness = brightness_get,
6133 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006134};
6135
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006136/* --------------------------------------------------------------------- */
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03006137
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006138/*
6139 * These are only useful for models that have only one possibility
6140 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6141 * these quirks.
6142 */
6143#define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6144#define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6145#define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6146
6147static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
6148 /* Models with ATI GPUs known to require ECNVRAM mode */
6149 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
6150
Henrique de Moraes Holschuh6da25bf2009-09-12 15:22:11 -03006151 /* Models with ATI GPUs that can use ECNVRAM */
6152 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC),
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006153 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6154 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6155 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6156
Henrique de Moraes Holschuh6da25bf2009-09-12 15:22:11 -03006157 /* Models with Intel Extreme Graphics 2 */
6158 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC),
Henrique de Moraes Holschuha9f8eac2009-12-09 01:36:21 +00006159 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6160 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006161
6162 /* Models with Intel GMA900 */
6163 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
6164 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
6165 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
6166};
6167
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006168static int __init brightness_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006169{
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006170 int b;
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006171 unsigned long quirks;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006172
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006173 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
6174
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03006175 mutex_init(&brightness_mutex);
6176
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006177 quirks = tpacpi_check_quirks(brightness_quirk_table,
6178 ARRAY_SIZE(brightness_quirk_table));
6179
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03006180 /*
6181 * We always attempt to detect acpi support, so as to switch
6182 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6183 * going to publish a backlight interface
6184 */
6185 b = tpacpi_check_std_acpi_brightness_support();
6186 if (b > 0) {
Thomas Renninger2dba1b52008-08-01 17:38:03 +02006187
6188 if (acpi_video_backlight_support()) {
6189 if (brightness_enable > 1) {
6190 printk(TPACPI_NOTICE
6191 "Standard ACPI backlight interface "
6192 "available, not loading native one.\n");
6193 return 1;
6194 } else if (brightness_enable == 1) {
6195 printk(TPACPI_NOTICE
6196 "Backlight control force enabled, even if standard "
6197 "ACPI backlight interface is available\n");
6198 }
6199 } else {
6200 if (brightness_enable > 1) {
6201 printk(TPACPI_NOTICE
6202 "Standard ACPI backlight interface not "
6203 "available, thinkpad_acpi native "
6204 "brightness control enabled\n");
6205 }
Henrique de Moraes Holschuhe11e2112007-10-30 17:46:22 -02006206 }
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02006207 }
6208
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03006209 if (!brightness_enable) {
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006210 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03006211 "brightness support disabled by "
6212 "module parameter\n");
6213 return 1;
6214 }
6215
6216 if (b > 16) {
6217 printk(TPACPI_ERR
6218 "Unsupported brightness interface, "
6219 "please contact %s\n", TPACPI_MAIL);
6220 return 1;
6221 }
6222 if (b == 16)
6223 tp_features.bright_16levels = 1;
6224
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006225 /*
6226 * Check for module parameter bogosity, note that we
6227 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
6228 * able to detect "unspecified"
6229 */
6230 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03006231 return -EINVAL;
6232
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006233 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
6234 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
6235 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006236 if (quirks & TPACPI_BRGHT_Q_EC)
6237 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
6238 else
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006239 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
6240
6241 dbg_printk(TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006242 "driver auto-selected brightness_mode=%d\n",
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006243 brightness_mode);
6244 }
6245
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03006246 /* Safety */
6247 if (thinkpad_id.vendor != PCI_VENDOR_ID_IBM &&
6248 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
6249 brightness_mode == TPACPI_BRGHT_MODE_EC))
6250 return -EINVAL;
6251
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006252 if (tpacpi_brightness_get_raw(&b) < 0)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03006253 return 1;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006254
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02006255 if (tp_features.bright_16levels)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006256 printk(TPACPI_INFO
6257 "detected a 16-level brightness capable ThinkPad\n");
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02006258
Henrique de Moraes Holschuh7d5a0152007-04-24 11:48:20 -03006259 ibm_backlight_device = backlight_device_register(
6260 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
6261 &ibm_backlight_data);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006262 if (IS_ERR(ibm_backlight_device)) {
Henrique de Moraes Holschuh435c47e2009-09-20 14:09:22 -03006263 int rc = PTR_ERR(ibm_backlight_device);
6264 ibm_backlight_device = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006265 printk(TPACPI_ERR "Could not register backlight device\n");
Henrique de Moraes Holschuh435c47e2009-09-20 14:09:22 -03006266 return rc;
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006267 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006268 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6269 "brightness is supported\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006270
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006271 if (quirks & TPACPI_BRGHT_Q_ASK) {
6272 printk(TPACPI_NOTICE
6273 "brightness: will use unverified default: "
6274 "brightness_mode=%d\n", brightness_mode);
6275 printk(TPACPI_NOTICE
6276 "brightness: please report to %s whether it works well "
6277 "or not on your ThinkPad\n", TPACPI_MAIL);
6278 }
6279
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02006280 ibm_backlight_device->props.max_brightness =
6281 (tp_features.bright_16levels)? 15 : 7;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006282 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006283 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00006284
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00006285 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6286 "brightness: registering brightness hotkeys "
6287 "as change notification\n");
6288 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6289 | TP_ACPI_HKEY_BRGHTUP_MASK
6290 | TP_ACPI_HKEY_BRGHTDWN_MASK);;
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006291 return 0;
6292}
6293
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006294static void brightness_suspend(pm_message_t state)
6295{
6296 tpacpi_brightness_checkpoint_nvram();
6297}
6298
6299static void brightness_shutdown(void)
6300{
6301 tpacpi_brightness_checkpoint_nvram();
6302}
6303
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006304static void brightness_exit(void)
6305{
6306 if (ibm_backlight_device) {
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006307 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006308 "calling backlight_device_unregister()\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006309 backlight_device_unregister(ibm_backlight_device);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006310 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006311
6312 tpacpi_brightness_checkpoint_nvram();
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006313}
6314
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006315static int brightness_read(char *p)
6316{
6317 int len = 0;
6318 int level;
6319
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006320 level = brightness_get(NULL);
6321 if (level < 0) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006322 len += sprintf(p + len, "level:\t\tunreadable\n");
6323 } else {
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02006324 len += sprintf(p + len, "level:\t\t%d\n", level);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006325 len += sprintf(p + len, "commands:\tup, down\n");
6326 len += sprintf(p + len, "commands:\tlevel <level>"
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02006327 " (<level> is 0-%d)\n",
6328 (tp_features.bright_16levels) ? 15 : 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006329 }
6330
6331 return len;
6332}
6333
6334static int brightness_write(char *buf)
6335{
6336 int level;
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006337 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006338 char *cmd;
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02006339 int max_level = (tp_features.bright_16levels) ? 15 : 7;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006340
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006341 level = brightness_get(NULL);
6342 if (level < 0)
6343 return level;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006344
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006345 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006346 if (strlencmp(cmd, "up") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006347 if (level < max_level)
6348 level++;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006349 } else if (strlencmp(cmd, "down") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006350 if (level > 0)
6351 level--;
6352 } else if (sscanf(cmd, "level %d", &level) == 1 &&
6353 level >= 0 && level <= max_level) {
6354 /* new level set */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006355 } else
6356 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006357 }
6358
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006359 tpacpi_disclose_usertask("procfs brightness",
6360 "set level to %d\n", level);
6361
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006362 /*
6363 * Now we know what the final level should be, so we try to set it.
6364 * Doing it this way makes the syscall restartable in case of EINTR
6365 */
6366 rc = brightness_set(level);
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00006367 if (!rc && ibm_backlight_device)
6368 backlight_force_update(ibm_backlight_device,
6369 BACKLIGHT_UPDATE_SYSFS);
Roel Kluin80a8d122009-11-20 19:48:23 +01006370 return (rc == -EINTR)? -ERESTARTSYS : rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006371}
6372
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006373static struct ibm_struct brightness_driver_data = {
6374 .name = "brightness",
6375 .read = brightness_read,
6376 .write = brightness_write,
6377 .exit = brightness_exit,
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006378 .suspend = brightness_suspend,
6379 .shutdown = brightness_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006380};
6381
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006382/*************************************************************************
6383 * Volume subdriver
6384 */
6385
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006386/*
6387 * IBM ThinkPads have a simple volume controller with MUTE gating.
6388 * Very early Lenovo ThinkPads follow the IBM ThinkPad spec.
6389 *
6390 * Since the *61 series (and probably also the later *60 series), Lenovo
6391 * ThinkPads only implement the MUTE gate.
6392 *
6393 * EC register 0x30
6394 * Bit 6: MUTE (1 mutes sound)
6395 * Bit 3-0: Volume
6396 * Other bits should be zero as far as we know.
6397 *
6398 * This is also stored in CMOS NVRAM, byte 0x60, bit 6 (MUTE), and
6399 * bits 3-0 (volume). Other bits in NVRAM may have other functions,
6400 * such as bit 7 which is used to detect repeated presses of MUTE,
6401 * and we leave them unchanged.
6402 */
6403
6404enum {
6405 TP_EC_AUDIO = 0x30,
6406
6407 /* TP_EC_AUDIO bits */
6408 TP_EC_AUDIO_MUTESW = 6,
6409
6410 /* TP_EC_AUDIO bitmasks */
6411 TP_EC_AUDIO_LVL_MSK = 0x0F,
6412 TP_EC_AUDIO_MUTESW_MSK = (1 << TP_EC_AUDIO_MUTESW),
6413
6414 /* Maximum volume */
6415 TP_EC_VOLUME_MAX = 14,
6416};
6417
6418enum tpacpi_volume_access_mode {
6419 TPACPI_VOL_MODE_AUTO = 0, /* Not implemented yet */
6420 TPACPI_VOL_MODE_EC, /* Pure EC control */
6421 TPACPI_VOL_MODE_UCMS_STEP, /* UCMS step-based control: N/A */
6422 TPACPI_VOL_MODE_ECNVRAM, /* EC control w/ NVRAM store */
6423 TPACPI_VOL_MODE_MAX
6424};
6425
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006426enum tpacpi_volume_capabilities {
6427 TPACPI_VOL_CAP_AUTO = 0, /* Use white/blacklist */
6428 TPACPI_VOL_CAP_VOLMUTE, /* Output vol and mute */
6429 TPACPI_VOL_CAP_MUTEONLY, /* Output mute only */
6430 TPACPI_VOL_CAP_MAX
6431};
6432
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006433static enum tpacpi_volume_access_mode volume_mode =
6434 TPACPI_VOL_MODE_MAX;
6435
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006436static enum tpacpi_volume_capabilities volume_capabilities;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006437
6438/*
6439 * Used to syncronize writers to TP_EC_AUDIO and
6440 * TP_NVRAM_ADDR_MIXER, as we need to do read-modify-write
6441 */
6442static struct mutex volume_mutex;
6443
6444static void tpacpi_volume_checkpoint_nvram(void)
6445{
6446 u8 lec = 0;
6447 u8 b_nvram;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006448 u8 ec_mask;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006449
6450 if (volume_mode != TPACPI_VOL_MODE_ECNVRAM)
6451 return;
6452
6453 vdbg_printk(TPACPI_DBG_MIXER,
6454 "trying to checkpoint mixer state to NVRAM...\n");
6455
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006456 if (tp_features.mixer_no_level_control)
6457 ec_mask = TP_EC_AUDIO_MUTESW_MSK;
6458 else
6459 ec_mask = TP_EC_AUDIO_MUTESW_MSK | TP_EC_AUDIO_LVL_MSK;
6460
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006461 if (mutex_lock_killable(&volume_mutex) < 0)
6462 return;
6463
6464 if (unlikely(!acpi_ec_read(TP_EC_AUDIO, &lec)))
6465 goto unlock;
6466 lec &= ec_mask;
6467 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
6468
6469 if (lec != (b_nvram & ec_mask)) {
6470 /* NVRAM needs update */
6471 b_nvram &= ~ec_mask;
6472 b_nvram |= lec;
6473 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_MIXER);
6474 dbg_printk(TPACPI_DBG_MIXER,
6475 "updated NVRAM mixer status to 0x%02x (0x%02x)\n",
6476 (unsigned int) lec, (unsigned int) b_nvram);
6477 } else {
6478 vdbg_printk(TPACPI_DBG_MIXER,
6479 "NVRAM mixer status already is 0x%02x (0x%02x)\n",
6480 (unsigned int) lec, (unsigned int) b_nvram);
6481 }
6482
6483unlock:
6484 mutex_unlock(&volume_mutex);
6485}
6486
6487static int volume_get_status_ec(u8 *status)
6488{
6489 u8 s;
6490
6491 if (!acpi_ec_read(TP_EC_AUDIO, &s))
6492 return -EIO;
6493
6494 *status = s;
6495
6496 dbg_printk(TPACPI_DBG_MIXER, "status 0x%02x\n", s);
6497
6498 return 0;
6499}
6500
6501static int volume_get_status(u8 *status)
6502{
6503 return volume_get_status_ec(status);
6504}
6505
6506static int volume_set_status_ec(const u8 status)
6507{
6508 if (!acpi_ec_write(TP_EC_AUDIO, status))
6509 return -EIO;
6510
6511 dbg_printk(TPACPI_DBG_MIXER, "set EC mixer to 0x%02x\n", status);
6512
6513 return 0;
6514}
6515
6516static int volume_set_status(const u8 status)
6517{
6518 return volume_set_status_ec(status);
6519}
6520
6521static int volume_set_mute_ec(const bool mute)
6522{
6523 int rc;
6524 u8 s, n;
6525
6526 if (mutex_lock_killable(&volume_mutex) < 0)
6527 return -EINTR;
6528
6529 rc = volume_get_status_ec(&s);
6530 if (rc)
6531 goto unlock;
6532
6533 n = (mute) ? s | TP_EC_AUDIO_MUTESW_MSK :
6534 s & ~TP_EC_AUDIO_MUTESW_MSK;
6535
6536 if (n != s)
6537 rc = volume_set_status_ec(n);
6538
6539unlock:
6540 mutex_unlock(&volume_mutex);
6541 return rc;
6542}
6543
6544static int volume_set_mute(const bool mute)
6545{
6546 dbg_printk(TPACPI_DBG_MIXER, "trying to %smute\n",
6547 (mute) ? "" : "un");
6548 return volume_set_mute_ec(mute);
6549}
6550
6551static int volume_set_volume_ec(const u8 vol)
6552{
6553 int rc;
6554 u8 s, n;
6555
6556 if (vol > TP_EC_VOLUME_MAX)
6557 return -EINVAL;
6558
6559 if (mutex_lock_killable(&volume_mutex) < 0)
6560 return -EINTR;
6561
6562 rc = volume_get_status_ec(&s);
6563 if (rc)
6564 goto unlock;
6565
6566 n = (s & ~TP_EC_AUDIO_LVL_MSK) | vol;
6567
6568 if (n != s)
6569 rc = volume_set_status_ec(n);
6570
6571unlock:
6572 mutex_unlock(&volume_mutex);
6573 return rc;
6574}
6575
6576static int volume_set_volume(const u8 vol)
6577{
6578 dbg_printk(TPACPI_DBG_MIXER,
6579 "trying to set volume level to %hu\n", vol);
6580 return volume_set_volume_ec(vol);
6581}
6582
6583static void volume_suspend(pm_message_t state)
6584{
6585 tpacpi_volume_checkpoint_nvram();
6586}
6587
6588static void volume_shutdown(void)
6589{
6590 tpacpi_volume_checkpoint_nvram();
6591}
6592
6593static void volume_exit(void)
6594{
6595 tpacpi_volume_checkpoint_nvram();
6596}
6597
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006598#define TPACPI_VOL_Q_MUTEONLY 0x0001 /* Mute-only control available */
6599#define TPACPI_VOL_Q_LEVEL 0x0002 /* Volume control available */
6600
6601static const struct tpacpi_quirk volume_quirk_table[] __initconst = {
6602 /* Whitelist volume level on all IBM by default */
6603 { .vendor = PCI_VENDOR_ID_IBM,
6604 .bios = TPACPI_MATCH_ANY,
6605 .ec = TPACPI_MATCH_ANY,
6606 .quirks = TPACPI_VOL_Q_LEVEL },
6607
6608 /* Lenovo models with volume control (needs confirmation) */
6609 TPACPI_QEC_LNV('7', 'C', TPACPI_VOL_Q_LEVEL), /* R60/i */
6610 TPACPI_QEC_LNV('7', 'E', TPACPI_VOL_Q_LEVEL), /* R60e/i */
6611 TPACPI_QEC_LNV('7', '9', TPACPI_VOL_Q_LEVEL), /* T60/p */
6612 TPACPI_QEC_LNV('7', 'B', TPACPI_VOL_Q_LEVEL), /* X60/s */
6613 TPACPI_QEC_LNV('7', 'J', TPACPI_VOL_Q_LEVEL), /* X60t */
6614 TPACPI_QEC_LNV('7', '7', TPACPI_VOL_Q_LEVEL), /* Z60 */
6615 TPACPI_QEC_LNV('7', 'F', TPACPI_VOL_Q_LEVEL), /* Z61 */
6616
6617 /* Whitelist mute-only on all Lenovo by default */
6618 { .vendor = PCI_VENDOR_ID_LENOVO,
6619 .bios = TPACPI_MATCH_ANY,
6620 .ec = TPACPI_MATCH_ANY,
6621 .quirks = TPACPI_VOL_Q_MUTEONLY }
6622};
6623
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006624static int __init volume_init(struct ibm_init_struct *iibm)
6625{
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006626 unsigned long quirks;
6627
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006628 vdbg_printk(TPACPI_DBG_INIT, "initializing volume subdriver\n");
6629
6630 mutex_init(&volume_mutex);
6631
6632 /*
6633 * Check for module parameter bogosity, note that we
6634 * init volume_mode to TPACPI_VOL_MODE_MAX in order to be
6635 * able to detect "unspecified"
6636 */
6637 if (volume_mode > TPACPI_VOL_MODE_MAX)
6638 return -EINVAL;
6639
6640 if (volume_mode == TPACPI_VOL_MODE_UCMS_STEP) {
6641 printk(TPACPI_ERR
6642 "UCMS step volume mode not implemented, "
6643 "please contact %s\n", TPACPI_MAIL);
6644 return 1;
6645 }
6646
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006647 if (volume_capabilities >= TPACPI_VOL_CAP_MAX)
6648 return -EINVAL;
6649
6650 quirks = tpacpi_check_quirks(volume_quirk_table,
6651 ARRAY_SIZE(volume_quirk_table));
6652
6653 switch (volume_capabilities) {
6654 case TPACPI_VOL_CAP_AUTO:
6655 if (quirks & TPACPI_VOL_Q_MUTEONLY)
6656 tp_features.mixer_no_level_control = 1;
6657 else if (quirks & TPACPI_VOL_Q_LEVEL)
6658 tp_features.mixer_no_level_control = 0;
6659 else
6660 return 1; /* no mixer */
6661 break;
6662 case TPACPI_VOL_CAP_VOLMUTE:
6663 tp_features.mixer_no_level_control = 0;
6664 break;
6665 case TPACPI_VOL_CAP_MUTEONLY:
6666 tp_features.mixer_no_level_control = 1;
6667 break;
6668 default:
6669 return 1;
6670 }
6671
6672 if (volume_capabilities != TPACPI_VOL_CAP_AUTO)
6673 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6674 "using user-supplied volume_capabilities=%d\n",
6675 volume_capabilities);
6676
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006677 if (volume_mode == TPACPI_VOL_MODE_AUTO ||
6678 volume_mode == TPACPI_VOL_MODE_MAX) {
6679 volume_mode = TPACPI_VOL_MODE_ECNVRAM;
6680
6681 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6682 "driver auto-selected volume_mode=%d\n",
6683 volume_mode);
6684 } else {
6685 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6686 "using user-supplied volume_mode=%d\n",
6687 volume_mode);
6688 }
6689
6690 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006691 "mute is supported, volume control is %s\n",
6692 str_supported(!tp_features.mixer_no_level_control));
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006693
6694 return 0;
6695}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006696
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006697static int volume_read(char *p)
6698{
6699 int len = 0;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006700 u8 status;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006701
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006702 if (volume_get_status(&status) < 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006703 len += sprintf(p + len, "level:\t\tunreadable\n");
6704 } else {
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006705 if (tp_features.mixer_no_level_control)
6706 len += sprintf(p + len, "level:\t\tunsupported\n");
6707 else
6708 len += sprintf(p + len, "level:\t\t%d\n",
6709 status & TP_EC_AUDIO_LVL_MSK);
6710
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006711 len += sprintf(p + len, "mute:\t\t%s\n",
6712 onoff(status, TP_EC_AUDIO_MUTESW));
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006713
6714 len += sprintf(p + len, "commands:\tunmute, mute\n");
6715 if (!tp_features.mixer_no_level_control) {
6716 len += sprintf(p + len, "commands:\tup, down\n");
6717 len += sprintf(p + len, "commands:\tlevel <level>"
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006718 " (<level> is 0-%d)\n", TP_EC_VOLUME_MAX);
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006719 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006720 }
6721
6722 return len;
6723}
6724
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006725static int volume_write(char *buf)
6726{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006727 u8 s;
6728 u8 new_level, new_mute;
6729 int l;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006730 char *cmd;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006731 int rc;
6732
6733 rc = volume_get_status(&s);
6734 if (rc < 0)
6735 return rc;
6736
6737 new_level = s & TP_EC_AUDIO_LVL_MSK;
6738 new_mute = s & TP_EC_AUDIO_MUTESW_MSK;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006739
6740 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006741 if (!tp_features.mixer_no_level_control) {
6742 if (strlencmp(cmd, "up") == 0) {
6743 if (new_mute)
6744 new_mute = 0;
6745 else if (new_level < TP_EC_VOLUME_MAX)
6746 new_level++;
6747 continue;
6748 } else if (strlencmp(cmd, "down") == 0) {
6749 if (new_mute)
6750 new_mute = 0;
6751 else if (new_level > 0)
6752 new_level--;
6753 continue;
6754 } else if (sscanf(cmd, "level %u", &l) == 1 &&
6755 l >= 0 && l <= TP_EC_VOLUME_MAX) {
6756 new_level = l;
6757 continue;
6758 }
6759 }
6760 if (strlencmp(cmd, "mute") == 0)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006761 new_mute = TP_EC_AUDIO_MUTESW_MSK;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006762 else if (strlencmp(cmd, "unmute") == 0)
6763 new_mute = 0;
6764 else
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006765 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006766 }
6767
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006768 if (tp_features.mixer_no_level_control) {
6769 tpacpi_disclose_usertask("procfs volume", "%smute\n",
6770 new_mute ? "" : "un");
6771 rc = volume_set_mute(!!new_mute);
6772 } else {
6773 tpacpi_disclose_usertask("procfs volume",
6774 "%smute and set level to %d\n",
6775 new_mute ? "" : "un", new_level);
6776 rc = volume_set_status(new_mute | new_level);
6777 }
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006778
6779 return (rc == -EINTR) ? -ERESTARTSYS : rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006780}
6781
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006782static struct ibm_struct volume_driver_data = {
6783 .name = "volume",
6784 .read = volume_read,
6785 .write = volume_write,
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006786 .exit = volume_exit,
6787 .suspend = volume_suspend,
6788 .shutdown = volume_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006789};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006790
6791/*************************************************************************
6792 * Fan subdriver
6793 */
6794
6795/*
6796 * FAN ACCESS MODES
6797 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006798 * TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006799 * ACPI GFAN method: returns fan level
6800 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006801 * see TPACPI_FAN_WR_ACPI_SFAN
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03006802 * EC 0x2f (HFSP) not available if GFAN exists
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006803 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006804 * TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006805 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
6806 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03006807 * EC 0x2f (HFSP) might be available *for reading*, but do not use
6808 * it for writing.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006809 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006810 * TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03006811 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
6812 * Supported on almost all ThinkPads
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006813 *
6814 * Fan speed changes of any sort (including those caused by the
6815 * disengaged mode) are usually done slowly by the firmware as the
6816 * maximum ammount of fan duty cycle change per second seems to be
6817 * limited.
6818 *
6819 * Reading is not available if GFAN exists.
6820 * Writing is not available if SFAN exists.
6821 *
6822 * Bits
6823 * 7 automatic mode engaged;
6824 * (default operation mode of the ThinkPad)
6825 * fan level is ignored in this mode.
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03006826 * 6 full speed mode (takes precedence over bit 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006827 * not available on all thinkpads. May disable
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03006828 * the tachometer while the fan controller ramps up
6829 * the speed (which can take up to a few *minutes*).
6830 * Speeds up fan to 100% duty-cycle, which is far above
6831 * the standard RPM levels. It is not impossible that
6832 * it could cause hardware damage.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006833 * 5-3 unused in some models. Extra bits for fan level
6834 * in others, but still useless as all values above
6835 * 7 map to the same speed as level 7 in these models.
6836 * 2-0 fan level (0..7 usually)
6837 * 0x00 = stop
6838 * 0x07 = max (set when temperatures critical)
6839 * Some ThinkPads may have other levels, see
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006840 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006841 *
6842 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
6843 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
6844 * does so, its initial value is meaningless (0x07).
6845 *
6846 * For firmware bugs, refer to:
6847 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
6848 *
6849 * ----
6850 *
6851 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
6852 * Main fan tachometer reading (in RPM)
6853 *
6854 * This register is present on all ThinkPads with a new-style EC, and
6855 * it is known not to be present on the A21m/e, and T22, as there is
6856 * something else in offset 0x84 according to the ACPI DSDT. Other
6857 * ThinkPads from this same time period (and earlier) probably lack the
6858 * tachometer as well.
6859 *
Nick Andrew877d0312009-01-26 11:06:57 +01006860 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006861 * was never fixed by IBM to report the EC firmware version string
6862 * probably support the tachometer (like the early X models), so
6863 * detecting it is quite hard. We need more data to know for sure.
6864 *
6865 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
6866 * might result.
6867 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03006868 * FIRMWARE BUG: may go stale while the EC is switching to full speed
6869 * mode.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006870 *
6871 * For firmware bugs, refer to:
6872 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
6873 *
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03006874 * ----
6875 *
6876 * ThinkPad EC register 0x31 bit 0 (only on select models)
6877 *
6878 * When bit 0 of EC register 0x31 is zero, the tachometer registers
6879 * show the speed of the main fan. When bit 0 of EC register 0x31
6880 * is one, the tachometer registers show the speed of the auxiliary
6881 * fan.
6882 *
6883 * Fan control seems to affect both fans, regardless of the state
6884 * of this bit.
6885 *
6886 * So far, only the firmware for the X60/X61 non-tablet versions
6887 * seem to support this (firmware TP-7M).
6888 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006889 * TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006890 * ThinkPad X31, X40, X41. Not available in the X60.
6891 *
6892 * FANS ACPI handle: takes three arguments: low speed, medium speed,
6893 * high speed. ACPI DSDT seems to map these three speeds to levels
6894 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
6895 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
6896 *
6897 * The speeds are stored on handles
6898 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
6899 *
6900 * There are three default speed sets, acessible as handles:
6901 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
6902 *
6903 * ACPI DSDT switches which set is in use depending on various
6904 * factors.
6905 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006906 * TPACPI_FAN_WR_TPEC is also available and should be used to
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006907 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
6908 * but the ACPI tables just mention level 7.
6909 */
6910
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006911enum { /* Fan control constants */
6912 fan_status_offset = 0x2f, /* EC register 0x2f */
6913 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
6914 * 0x84 must be read before 0x85 */
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03006915 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
6916 bit 0 selects which fan is active */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006917
6918 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
6919 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
6920
6921 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
6922};
6923
6924enum fan_status_access_mode {
6925 TPACPI_FAN_NONE = 0, /* No fan status or control */
6926 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
6927 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
6928};
6929
6930enum fan_control_access_mode {
6931 TPACPI_FAN_WR_NONE = 0, /* No fan control */
6932 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
6933 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
6934 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
6935};
6936
6937enum fan_control_commands {
6938 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
6939 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
6940 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
6941 * and also watchdog cmd */
6942};
6943
6944static int fan_control_allowed;
6945
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006946static enum fan_status_access_mode fan_status_access_mode;
6947static enum fan_control_access_mode fan_control_access_mode;
6948static enum fan_control_commands fan_control_commands;
6949
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02006950static u8 fan_control_initial_status;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006951static u8 fan_control_desired_level;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006952static u8 fan_control_resume_level;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02006953static int fan_watchdog_maxinterval;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006954
6955static struct mutex fan_mutex;
6956
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006957static void fan_watchdog_fire(struct work_struct *ignored);
Len Brown25c68a32006-12-08 04:43:41 -05006958static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02006959
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006960TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
6961TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006962 "\\FSPD", /* 600e/x, 770e, 770x */
6963 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006964TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006965 "JFNS", /* 770x-JL */
6966 ); /* all others */
6967
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006968/*
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006969 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
6970 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
6971 * be in auto mode (0x80).
6972 *
6973 * This is corrected by any write to HFSP either by the driver, or
6974 * by the firmware.
6975 *
6976 * We assume 0x07 really means auto mode while this quirk is active,
6977 * as this is far more likely than the ThinkPad being in level 7,
6978 * which is only used by the firmware during thermal emergencies.
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03006979 *
6980 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
6981 * TP-70 (T43, R52), which are known to be buggy.
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006982 */
6983
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03006984static void fan_quirk1_setup(void)
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006985{
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006986 if (fan_control_initial_status == 0x07) {
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03006987 printk(TPACPI_NOTICE
6988 "fan_init: initial fan status is unknown, "
6989 "assuming it is in auto mode\n");
6990 tp_features.fan_ctrl_status_undef = 1;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006991 }
6992}
6993
6994static void fan_quirk1_handle(u8 *fan_status)
6995{
6996 if (unlikely(tp_features.fan_ctrl_status_undef)) {
6997 if (*fan_status != fan_control_initial_status) {
6998 /* something changed the HFSP regisnter since
6999 * driver init time, so it is not undefined
7000 * anymore */
7001 tp_features.fan_ctrl_status_undef = 0;
7002 } else {
7003 /* Return most likely status. In fact, it
7004 * might be the only possible status */
7005 *fan_status = TP_EC_FAN_AUTO;
7006 }
7007 }
7008}
7009
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007010/* Select main fan on X60/X61, NOOP on others */
7011static bool fan_select_fan1(void)
7012{
7013 if (tp_features.second_fan) {
7014 u8 val;
7015
7016 if (ec_read(fan_select_offset, &val) < 0)
7017 return false;
7018 val &= 0xFEU;
7019 if (ec_write(fan_select_offset, val) < 0)
7020 return false;
7021 }
7022 return true;
7023}
7024
7025/* Select secondary fan on X60/X61 */
7026static bool fan_select_fan2(void)
7027{
7028 u8 val;
7029
7030 if (!tp_features.second_fan)
7031 return false;
7032
7033 if (ec_read(fan_select_offset, &val) < 0)
7034 return false;
7035 val |= 0x01U;
7036 if (ec_write(fan_select_offset, val) < 0)
7037 return false;
7038
7039 return true;
7040}
7041
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007042/*
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007043 * Call with fan_mutex held
7044 */
7045static void fan_update_desired_level(u8 status)
7046{
7047 if ((status &
7048 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7049 if (status > 7)
7050 fan_control_desired_level = 7;
7051 else
7052 fan_control_desired_level = status;
7053 }
7054}
7055
7056static int fan_get_status(u8 *status)
7057{
7058 u8 s;
7059
7060 /* TODO:
7061 * Add TPACPI_FAN_RD_ACPI_FANS ? */
7062
7063 switch (fan_status_access_mode) {
7064 case TPACPI_FAN_RD_ACPI_GFAN:
7065 /* 570, 600e/x, 770e, 770x */
7066
7067 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
7068 return -EIO;
7069
7070 if (likely(status))
7071 *status = s & 0x07;
7072
7073 break;
7074
7075 case TPACPI_FAN_RD_TPEC:
7076 /* all except 570, 600e/x, 770e, 770x */
7077 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
7078 return -EIO;
7079
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007080 if (likely(status)) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007081 *status = s;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007082 fan_quirk1_handle(status);
7083 }
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007084
7085 break;
7086
7087 default:
7088 return -ENXIO;
7089 }
7090
7091 return 0;
7092}
7093
7094static int fan_get_status_safe(u8 *status)
7095{
7096 int rc;
7097 u8 s;
7098
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007099 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007100 return -ERESTARTSYS;
7101 rc = fan_get_status(&s);
7102 if (!rc)
7103 fan_update_desired_level(s);
7104 mutex_unlock(&fan_mutex);
7105
7106 if (status)
7107 *status = s;
7108
7109 return rc;
7110}
7111
7112static int fan_get_speed(unsigned int *speed)
7113{
7114 u8 hi, lo;
7115
7116 switch (fan_status_access_mode) {
7117 case TPACPI_FAN_RD_TPEC:
7118 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007119 if (unlikely(!fan_select_fan1()))
7120 return -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007121 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
7122 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
7123 return -EIO;
7124
7125 if (likely(speed))
7126 *speed = (hi << 8) | lo;
7127
7128 break;
7129
7130 default:
7131 return -ENXIO;
7132 }
7133
7134 return 0;
7135}
7136
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007137static int fan2_get_speed(unsigned int *speed)
7138{
7139 u8 hi, lo;
7140 bool rc;
7141
7142 switch (fan_status_access_mode) {
7143 case TPACPI_FAN_RD_TPEC:
7144 /* all except 570, 600e/x, 770e, 770x */
7145 if (unlikely(!fan_select_fan2()))
7146 return -EIO;
7147 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
7148 !acpi_ec_read(fan_rpm_offset + 1, &hi);
7149 fan_select_fan1(); /* play it safe */
7150 if (rc)
7151 return -EIO;
7152
7153 if (likely(speed))
7154 *speed = (hi << 8) | lo;
7155
7156 break;
7157
7158 default:
7159 return -ENXIO;
7160 }
7161
7162 return 0;
7163}
7164
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007165static int fan_set_level(int level)
7166{
7167 if (!fan_control_allowed)
7168 return -EPERM;
7169
7170 switch (fan_control_access_mode) {
7171 case TPACPI_FAN_WR_ACPI_SFAN:
7172 if (level >= 0 && level <= 7) {
7173 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
7174 return -EIO;
7175 } else
7176 return -EINVAL;
7177 break;
7178
7179 case TPACPI_FAN_WR_ACPI_FANS:
7180 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007181 if (!(level & TP_EC_FAN_AUTO) &&
7182 !(level & TP_EC_FAN_FULLSPEED) &&
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007183 ((level < 0) || (level > 7)))
7184 return -EINVAL;
7185
7186 /* safety net should the EC not support AUTO
7187 * or FULLSPEED mode bits and just ignore them */
7188 if (level & TP_EC_FAN_FULLSPEED)
7189 level |= 7; /* safety min speed 7 */
Roel Kluin547266e2008-02-05 00:24:56 +01007190 else if (level & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007191 level |= 4; /* safety min speed 4 */
7192
7193 if (!acpi_ec_write(fan_status_offset, level))
7194 return -EIO;
7195 else
7196 tp_features.fan_ctrl_status_undef = 0;
7197 break;
7198
7199 default:
7200 return -ENXIO;
7201 }
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007202
7203 vdbg_printk(TPACPI_DBG_FAN,
7204 "fan control: set fan control register to 0x%02x\n", level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007205 return 0;
7206}
7207
7208static int fan_set_level_safe(int level)
7209{
7210 int rc;
7211
7212 if (!fan_control_allowed)
7213 return -EPERM;
7214
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007215 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007216 return -ERESTARTSYS;
7217
7218 if (level == TPACPI_FAN_LAST_LEVEL)
7219 level = fan_control_desired_level;
7220
7221 rc = fan_set_level(level);
7222 if (!rc)
7223 fan_update_desired_level(level);
7224
7225 mutex_unlock(&fan_mutex);
7226 return rc;
7227}
7228
7229static int fan_set_enable(void)
7230{
7231 u8 s;
7232 int rc;
7233
7234 if (!fan_control_allowed)
7235 return -EPERM;
7236
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007237 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007238 return -ERESTARTSYS;
7239
7240 switch (fan_control_access_mode) {
7241 case TPACPI_FAN_WR_ACPI_FANS:
7242 case TPACPI_FAN_WR_TPEC:
7243 rc = fan_get_status(&s);
7244 if (rc < 0)
7245 break;
7246
7247 /* Don't go out of emergency fan mode */
7248 if (s != 7) {
7249 s &= 0x07;
7250 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
7251 }
7252
7253 if (!acpi_ec_write(fan_status_offset, s))
7254 rc = -EIO;
7255 else {
7256 tp_features.fan_ctrl_status_undef = 0;
7257 rc = 0;
7258 }
7259 break;
7260
7261 case TPACPI_FAN_WR_ACPI_SFAN:
7262 rc = fan_get_status(&s);
7263 if (rc < 0)
7264 break;
7265
7266 s &= 0x07;
7267
7268 /* Set fan to at least level 4 */
7269 s |= 4;
7270
7271 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007272 rc = -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007273 else
7274 rc = 0;
7275 break;
7276
7277 default:
7278 rc = -ENXIO;
7279 }
7280
7281 mutex_unlock(&fan_mutex);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007282
7283 if (!rc)
7284 vdbg_printk(TPACPI_DBG_FAN,
7285 "fan control: set fan control register to 0x%02x\n",
7286 s);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007287 return rc;
7288}
7289
7290static int fan_set_disable(void)
7291{
7292 int rc;
7293
7294 if (!fan_control_allowed)
7295 return -EPERM;
7296
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007297 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007298 return -ERESTARTSYS;
7299
7300 rc = 0;
7301 switch (fan_control_access_mode) {
7302 case TPACPI_FAN_WR_ACPI_FANS:
7303 case TPACPI_FAN_WR_TPEC:
7304 if (!acpi_ec_write(fan_status_offset, 0x00))
7305 rc = -EIO;
7306 else {
7307 fan_control_desired_level = 0;
7308 tp_features.fan_ctrl_status_undef = 0;
7309 }
7310 break;
7311
7312 case TPACPI_FAN_WR_ACPI_SFAN:
7313 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
7314 rc = -EIO;
7315 else
7316 fan_control_desired_level = 0;
7317 break;
7318
7319 default:
7320 rc = -ENXIO;
7321 }
7322
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007323 if (!rc)
7324 vdbg_printk(TPACPI_DBG_FAN,
7325 "fan control: set fan control register to 0\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007326
7327 mutex_unlock(&fan_mutex);
7328 return rc;
7329}
7330
7331static int fan_set_speed(int speed)
7332{
7333 int rc;
7334
7335 if (!fan_control_allowed)
7336 return -EPERM;
7337
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007338 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007339 return -ERESTARTSYS;
7340
7341 rc = 0;
7342 switch (fan_control_access_mode) {
7343 case TPACPI_FAN_WR_ACPI_FANS:
7344 if (speed >= 0 && speed <= 65535) {
7345 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
7346 speed, speed, speed))
7347 rc = -EIO;
7348 } else
7349 rc = -EINVAL;
7350 break;
7351
7352 default:
7353 rc = -ENXIO;
7354 }
7355
7356 mutex_unlock(&fan_mutex);
7357 return rc;
7358}
7359
7360static void fan_watchdog_reset(void)
7361{
7362 static int fan_watchdog_active;
7363
7364 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
7365 return;
7366
7367 if (fan_watchdog_active)
7368 cancel_delayed_work(&fan_watchdog_task);
7369
7370 if (fan_watchdog_maxinterval > 0 &&
7371 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
7372 fan_watchdog_active = 1;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03007373 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007374 msecs_to_jiffies(fan_watchdog_maxinterval
7375 * 1000))) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007376 printk(TPACPI_ERR
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03007377 "failed to queue the fan watchdog, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007378 "watchdog will not trigger\n");
7379 }
7380 } else
7381 fan_watchdog_active = 0;
7382}
7383
7384static void fan_watchdog_fire(struct work_struct *ignored)
7385{
7386 int rc;
7387
7388 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
7389 return;
7390
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007391 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007392 rc = fan_set_enable();
7393 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007394 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007395 "will try again later...\n", -rc);
7396 /* reschedule for later */
7397 fan_watchdog_reset();
7398 }
7399}
7400
7401/*
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007402 * SYSFS fan layout: hwmon compatible (device)
7403 *
7404 * pwm*_enable:
7405 * 0: "disengaged" mode
7406 * 1: manual mode
7407 * 2: native EC "auto" mode (recommended, hardware default)
7408 *
7409 * pwm*: set speed in manual mode, ignored otherwise.
7410 * 0 is level 0; 255 is level 7. Intermediate points done with linear
7411 * interpolation.
7412 *
7413 * fan*_input: tachometer reading, RPM
7414 *
7415 *
7416 * SYSFS fan layout: extensions
7417 *
7418 * fan_watchdog (driver):
7419 * fan watchdog interval in seconds, 0 disables (default), max 120
7420 */
7421
7422/* sysfs fan pwm1_enable ----------------------------------------------- */
7423static ssize_t fan_pwm1_enable_show(struct device *dev,
7424 struct device_attribute *attr,
7425 char *buf)
7426{
7427 int res, mode;
7428 u8 status;
7429
7430 res = fan_get_status_safe(&status);
7431 if (res)
7432 return res;
7433
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007434 if (status & TP_EC_FAN_FULLSPEED) {
7435 mode = 0;
7436 } else if (status & TP_EC_FAN_AUTO) {
7437 mode = 2;
7438 } else
7439 mode = 1;
7440
7441 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
7442}
7443
7444static ssize_t fan_pwm1_enable_store(struct device *dev,
7445 struct device_attribute *attr,
7446 const char *buf, size_t count)
7447{
7448 unsigned long t;
7449 int res, level;
7450
7451 if (parse_strtoul(buf, 2, &t))
7452 return -EINVAL;
7453
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007454 tpacpi_disclose_usertask("hwmon pwm1_enable",
7455 "set fan mode to %lu\n", t);
7456
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007457 switch (t) {
7458 case 0:
7459 level = TP_EC_FAN_FULLSPEED;
7460 break;
7461 case 1:
7462 level = TPACPI_FAN_LAST_LEVEL;
7463 break;
7464 case 2:
7465 level = TP_EC_FAN_AUTO;
7466 break;
7467 case 3:
7468 /* reserved for software-controlled auto mode */
7469 return -ENOSYS;
7470 default:
7471 return -EINVAL;
7472 }
7473
7474 res = fan_set_level_safe(level);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03007475 if (res == -ENXIO)
7476 return -EINVAL;
7477 else if (res < 0)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007478 return res;
7479
7480 fan_watchdog_reset();
7481
7482 return count;
7483}
7484
7485static struct device_attribute dev_attr_fan_pwm1_enable =
7486 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
7487 fan_pwm1_enable_show, fan_pwm1_enable_store);
7488
7489/* sysfs fan pwm1 ------------------------------------------------------ */
7490static ssize_t fan_pwm1_show(struct device *dev,
7491 struct device_attribute *attr,
7492 char *buf)
7493{
7494 int res;
7495 u8 status;
7496
7497 res = fan_get_status_safe(&status);
7498 if (res)
7499 return res;
7500
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007501 if ((status &
7502 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
7503 status = fan_control_desired_level;
7504
7505 if (status > 7)
7506 status = 7;
7507
7508 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
7509}
7510
7511static ssize_t fan_pwm1_store(struct device *dev,
7512 struct device_attribute *attr,
7513 const char *buf, size_t count)
7514{
7515 unsigned long s;
7516 int rc;
7517 u8 status, newlevel;
7518
7519 if (parse_strtoul(buf, 255, &s))
7520 return -EINVAL;
7521
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007522 tpacpi_disclose_usertask("hwmon pwm1",
7523 "set fan speed to %lu\n", s);
7524
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007525 /* scale down from 0-255 to 0-7 */
7526 newlevel = (s >> 5) & 0x07;
7527
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007528 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02007529 return -ERESTARTSYS;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007530
7531 rc = fan_get_status(&status);
7532 if (!rc && (status &
7533 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7534 rc = fan_set_level(newlevel);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03007535 if (rc == -ENXIO)
7536 rc = -EINVAL;
7537 else if (!rc) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007538 fan_update_desired_level(newlevel);
Henrique de Moraes Holschuhca4ac2f2007-04-27 22:00:11 -03007539 fan_watchdog_reset();
7540 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007541 }
7542
7543 mutex_unlock(&fan_mutex);
7544 return (rc)? rc : count;
7545}
7546
7547static struct device_attribute dev_attr_fan_pwm1 =
7548 __ATTR(pwm1, S_IWUSR | S_IRUGO,
7549 fan_pwm1_show, fan_pwm1_store);
7550
7551/* sysfs fan fan1_input ------------------------------------------------ */
7552static ssize_t fan_fan1_input_show(struct device *dev,
7553 struct device_attribute *attr,
7554 char *buf)
7555{
7556 int res;
7557 unsigned int speed;
7558
7559 res = fan_get_speed(&speed);
7560 if (res < 0)
7561 return res;
7562
7563 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7564}
7565
7566static struct device_attribute dev_attr_fan_fan1_input =
7567 __ATTR(fan1_input, S_IRUGO,
7568 fan_fan1_input_show, NULL);
7569
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007570/* sysfs fan fan2_input ------------------------------------------------ */
7571static ssize_t fan_fan2_input_show(struct device *dev,
7572 struct device_attribute *attr,
7573 char *buf)
7574{
7575 int res;
7576 unsigned int speed;
7577
7578 res = fan2_get_speed(&speed);
7579 if (res < 0)
7580 return res;
7581
7582 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7583}
7584
7585static struct device_attribute dev_attr_fan_fan2_input =
7586 __ATTR(fan2_input, S_IRUGO,
7587 fan_fan2_input_show, NULL);
7588
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007589/* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007590static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
7591 char *buf)
7592{
7593 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
7594}
7595
7596static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
7597 const char *buf, size_t count)
7598{
7599 unsigned long t;
7600
7601 if (parse_strtoul(buf, 120, &t))
7602 return -EINVAL;
7603
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03007604 if (!fan_control_allowed)
7605 return -EPERM;
7606
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007607 fan_watchdog_maxinterval = t;
7608 fan_watchdog_reset();
7609
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007610 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
7611
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007612 return count;
7613}
7614
7615static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
7616 fan_fan_watchdog_show, fan_fan_watchdog_store);
7617
7618/* --------------------------------------------------------------------- */
7619static struct attribute *fan_attributes[] = {
7620 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
7621 &dev_attr_fan_fan1_input.attr,
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007622 NULL, /* for fan2_input */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007623 NULL
7624};
7625
7626static const struct attribute_group fan_attr_group = {
7627 .attrs = fan_attributes,
7628};
7629
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007630#define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
7631#define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007632
7633#define TPACPI_FAN_QI(__id1, __id2, __quirks) \
7634 { .vendor = PCI_VENDOR_ID_IBM, \
7635 .bios = TPACPI_MATCH_ANY, \
7636 .ec = TPID(__id1, __id2), \
7637 .quirks = __quirks }
7638
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007639#define TPACPI_FAN_QL(__id1, __id2, __quirks) \
7640 { .vendor = PCI_VENDOR_ID_LENOVO, \
7641 .bios = TPACPI_MATCH_ANY, \
7642 .ec = TPID(__id1, __id2), \
7643 .quirks = __quirks }
7644
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007645static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
7646 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
7647 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
7648 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
7649 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007650 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007651};
7652
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007653#undef TPACPI_FAN_QL
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007654#undef TPACPI_FAN_QI
7655
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007656static int __init fan_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007657{
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007658 int rc;
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007659 unsigned long quirks;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007660
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007661 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7662 "initializing fan subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007663
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03007664 mutex_init(&fan_mutex);
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007665 fan_status_access_mode = TPACPI_FAN_NONE;
7666 fan_control_access_mode = TPACPI_FAN_WR_NONE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007667 fan_control_commands = 0;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007668 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03007669 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007670 tp_features.second_fan = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007671 fan_control_desired_level = 7;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007672
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007673 TPACPI_ACPIHANDLE_INIT(fans);
7674 TPACPI_ACPIHANDLE_INIT(gfan);
7675 TPACPI_ACPIHANDLE_INIT(sfan);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007676
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007677 quirks = tpacpi_check_quirks(fan_quirk_table,
7678 ARRAY_SIZE(fan_quirk_table));
7679
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007680 if (gfan_handle) {
7681 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007682 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007683 } else {
7684 /* all other ThinkPads: note that even old-style
7685 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02007686 if (likely(acpi_ec_read(fan_status_offset,
7687 &fan_control_initial_status))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007688 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007689 if (quirks & TPACPI_FAN_Q1)
7690 fan_quirk1_setup();
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007691 if (quirks & TPACPI_FAN_2FAN) {
7692 tp_features.second_fan = 1;
7693 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7694 "secondary fan support enabled\n");
7695 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007696 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007697 printk(TPACPI_ERR
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007698 "ThinkPad ACPI EC access misbehaving, "
7699 "fan status and control unavailable\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007700 return 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007701 }
7702 }
7703
7704 if (sfan_handle) {
7705 /* 570, 770x-JL */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007706 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02007707 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007708 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007709 } else {
7710 if (!gfan_handle) {
7711 /* gfan without sfan means no fan control */
7712 /* all other models implement TP EC 0x2f control */
7713
7714 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02007715 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007716 fan_control_access_mode =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007717 TPACPI_FAN_WR_ACPI_FANS;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007718 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007719 TPACPI_FAN_CMD_SPEED |
7720 TPACPI_FAN_CMD_LEVEL |
7721 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007722 } else {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007723 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02007724 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007725 TPACPI_FAN_CMD_LEVEL |
7726 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007727 }
7728 }
7729 }
7730
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007731 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7732 "fan is %s, modes %d, %d\n",
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007733 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
7734 fan_control_access_mode != TPACPI_FAN_WR_NONE),
7735 fan_status_access_mode, fan_control_access_mode);
7736
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03007737 /* fan control master switch */
7738 if (!fan_control_allowed) {
7739 fan_control_access_mode = TPACPI_FAN_WR_NONE;
7740 fan_control_commands = 0;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007741 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03007742 "fan control features disabled by parameter\n");
7743 }
7744
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007745 /* update fan_control_desired_level */
7746 if (fan_status_access_mode != TPACPI_FAN_NONE)
7747 fan_get_status_safe(NULL);
7748
7749 if (fan_status_access_mode != TPACPI_FAN_NONE ||
7750 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007751 if (tp_features.second_fan) {
7752 /* attach second fan tachometer */
7753 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
7754 &dev_attr_fan_fan2_input.attr;
7755 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007756 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007757 &fan_attr_group);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007758 if (rc < 0)
7759 return rc;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03007760
7761 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
7762 &driver_attr_fan_watchdog);
7763 if (rc < 0) {
7764 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
7765 &fan_attr_group);
7766 return rc;
7767 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007768 return 0;
7769 } else
7770 return 1;
7771}
7772
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007773static void fan_exit(void)
7774{
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007775 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007776 "cancelling any pending fan watchdog tasks\n");
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007777
7778 /* FIXME: can we really do this unconditionally? */
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007779 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007780 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
7781 &driver_attr_fan_watchdog);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007782
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007783 cancel_delayed_work(&fan_watchdog_task);
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03007784 flush_workqueue(tpacpi_wq);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007785}
7786
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007787static void fan_suspend(pm_message_t state)
7788{
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007789 int rc;
7790
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007791 if (!fan_control_allowed)
7792 return;
7793
7794 /* Store fan status in cache */
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007795 fan_control_resume_level = 0;
7796 rc = fan_get_status_safe(&fan_control_resume_level);
7797 if (rc < 0)
7798 printk(TPACPI_NOTICE
7799 "failed to read fan level for later "
7800 "restore during resume: %d\n", rc);
7801
7802 /* if it is undefined, don't attempt to restore it.
7803 * KEEP THIS LAST */
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007804 if (tp_features.fan_ctrl_status_undef)
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007805 fan_control_resume_level = 0;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007806}
7807
7808static void fan_resume(void)
7809{
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007810 u8 current_level = 7;
7811 bool do_set = false;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007812 int rc;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007813
7814 /* DSDT *always* updates status on resume */
7815 tp_features.fan_ctrl_status_undef = 0;
7816
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007817 if (!fan_control_allowed ||
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007818 !fan_control_resume_level ||
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007819 (fan_get_status_safe(&current_level) < 0))
7820 return;
7821
7822 switch (fan_control_access_mode) {
7823 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007824 /* never decrease fan level */
7825 do_set = (fan_control_resume_level > current_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007826 break;
7827 case TPACPI_FAN_WR_ACPI_FANS:
7828 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007829 /* never decrease fan level, scale is:
7830 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
7831 *
7832 * We expect the firmware to set either 7 or AUTO, but we
7833 * handle FULLSPEED out of paranoia.
7834 *
7835 * So, we can safely only restore FULLSPEED or 7, anything
7836 * else could slow the fan. Restoring AUTO is useless, at
7837 * best that's exactly what the DSDT already set (it is the
7838 * slower it uses).
7839 *
7840 * Always keep in mind that the DSDT *will* have set the
7841 * fans to what the vendor supposes is the best level. We
7842 * muck with it only to speed the fan up.
7843 */
7844 if (fan_control_resume_level != 7 &&
7845 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
7846 return;
7847 else
7848 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
7849 (current_level != fan_control_resume_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007850 break;
7851 default:
7852 return;
7853 }
7854 if (do_set) {
7855 printk(TPACPI_NOTICE
7856 "restoring fan level to 0x%02x\n",
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007857 fan_control_resume_level);
7858 rc = fan_set_level_safe(fan_control_resume_level);
7859 if (rc < 0)
7860 printk(TPACPI_NOTICE
7861 "failed to restore fan level: %d\n", rc);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007862 }
7863}
7864
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007865static int fan_read(char *p)
7866{
7867 int len = 0;
7868 int rc;
7869 u8 status;
7870 unsigned int speed = 0;
7871
7872 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007873 case TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007874 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007875 rc = fan_get_status_safe(&status);
7876 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007877 return rc;
7878
7879 len += sprintf(p + len, "status:\t\t%s\n"
7880 "level:\t\t%d\n",
7881 (status != 0) ? "enabled" : "disabled", status);
7882 break;
7883
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007884 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007885 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007886 rc = fan_get_status_safe(&status);
7887 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007888 return rc;
7889
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007890 len += sprintf(p + len, "status:\t\t%s\n",
7891 (status != 0) ? "enabled" : "disabled");
7892
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007893 rc = fan_get_speed(&speed);
7894 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007895 return rc;
7896
7897 len += sprintf(p + len, "speed:\t\t%d\n", speed);
7898
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007899 if (status & TP_EC_FAN_FULLSPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007900 /* Disengaged mode takes precedence */
7901 len += sprintf(p + len, "level:\t\tdisengaged\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007902 else if (status & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007903 len += sprintf(p + len, "level:\t\tauto\n");
7904 else
7905 len += sprintf(p + len, "level:\t\t%d\n", status);
7906 break;
7907
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007908 case TPACPI_FAN_NONE:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007909 default:
7910 len += sprintf(p + len, "status:\t\tnot supported\n");
7911 }
7912
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007913 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007914 len += sprintf(p + len, "commands:\tlevel <level>");
7915
7916 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007917 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007918 len += sprintf(p + len, " (<level> is 0-7)\n");
7919 break;
7920
7921 default:
7922 len += sprintf(p + len, " (<level> is 0-7, "
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007923 "auto, disengaged, full-speed)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007924 break;
7925 }
7926 }
7927
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007928 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007929 len += sprintf(p + len, "commands:\tenable, disable\n"
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007930 "commands:\twatchdog <timeout> (<timeout> "
7931 "is 0 (off), 1-120 (seconds))\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007932
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007933 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007934 len += sprintf(p + len, "commands:\tspeed <speed>"
7935 " (<speed> is 0-65535)\n");
7936
7937 return len;
7938}
7939
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007940static int fan_write_cmd_level(const char *cmd, int *rc)
7941{
7942 int level;
7943
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02007944 if (strlencmp(cmd, "level auto") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007945 level = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007946 else if ((strlencmp(cmd, "level disengaged") == 0) |
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007947 (strlencmp(cmd, "level full-speed") == 0))
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007948 level = TP_EC_FAN_FULLSPEED;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02007949 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007950 return 0;
7951
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007952 *rc = fan_set_level_safe(level);
7953 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007954 printk(TPACPI_ERR "level command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007955 "access mode %d", fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007956 else if (!*rc)
7957 tpacpi_disclose_usertask("procfs fan",
7958 "set level to %d\n", level);
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007959
7960 return 1;
7961}
7962
7963static int fan_write_cmd_enable(const char *cmd, int *rc)
7964{
7965 if (strlencmp(cmd, "enable") != 0)
7966 return 0;
7967
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007968 *rc = fan_set_enable();
7969 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007970 printk(TPACPI_ERR "enable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007971 "access mode %d", fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007972 else if (!*rc)
7973 tpacpi_disclose_usertask("procfs fan", "enable\n");
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007974
7975 return 1;
7976}
7977
7978static int fan_write_cmd_disable(const char *cmd, int *rc)
7979{
7980 if (strlencmp(cmd, "disable") != 0)
7981 return 0;
7982
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007983 *rc = fan_set_disable();
7984 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007985 printk(TPACPI_ERR "disable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007986 "access mode %d", fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007987 else if (!*rc)
7988 tpacpi_disclose_usertask("procfs fan", "disable\n");
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007989
7990 return 1;
7991}
7992
7993static int fan_write_cmd_speed(const char *cmd, int *rc)
7994{
7995 int speed;
7996
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02007997 /* TODO:
7998 * Support speed <low> <medium> <high> ? */
7999
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008000 if (sscanf(cmd, "speed %d", &speed) != 1)
8001 return 0;
8002
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008003 *rc = fan_set_speed(speed);
8004 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008005 printk(TPACPI_ERR "speed command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008006 "access mode %d", fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008007 else if (!*rc)
8008 tpacpi_disclose_usertask("procfs fan",
8009 "set speed to %d\n", speed);
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008010
8011 return 1;
8012}
8013
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008014static int fan_write_cmd_watchdog(const char *cmd, int *rc)
8015{
8016 int interval;
8017
8018 if (sscanf(cmd, "watchdog %d", &interval) != 1)
8019 return 0;
8020
8021 if (interval < 0 || interval > 120)
8022 *rc = -EINVAL;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008023 else {
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008024 fan_watchdog_maxinterval = interval;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008025 tpacpi_disclose_usertask("procfs fan",
8026 "set watchdog timer to %d\n",
8027 interval);
8028 }
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008029
8030 return 1;
8031}
8032
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008033static int fan_write(char *buf)
8034{
8035 char *cmd;
8036 int rc = 0;
8037
8038 while (!rc && (cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008039 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008040 fan_write_cmd_level(cmd, &rc)) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008041 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008042 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008043 fan_write_cmd_disable(cmd, &rc) ||
8044 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008045 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008046 fan_write_cmd_speed(cmd, &rc))
8047 )
8048 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008049 else if (!rc)
8050 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008051 }
8052
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008053 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008054}
8055
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008056static struct ibm_struct fan_driver_data = {
8057 .name = "fan",
8058 .read = fan_read,
8059 .write = fan_write,
8060 .exit = fan_exit,
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008061 .suspend = fan_suspend,
8062 .resume = fan_resume,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008063};
8064
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008065/****************************************************************************
8066 ****************************************************************************
8067 *
8068 * Infrastructure
8069 *
8070 ****************************************************************************
8071 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07008072
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03008073/*
8074 * HKEY event callout for other subdrivers go here
8075 * (yes, it is ugly, but it is quick, safe, and gets the job done
8076 */
8077static void tpacpi_driver_event(const unsigned int hkey_event)
8078{
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00008079 if (ibm_backlight_device) {
8080 switch (hkey_event) {
8081 case TP_HKEY_EV_BRGHT_UP:
8082 case TP_HKEY_EV_BRGHT_DOWN:
8083 tpacpi_brightness_notify_change();
8084 }
8085 }
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03008086}
8087
8088
8089
8090static void hotkey_driver_event(const unsigned int scancode)
8091{
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03008092 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode);
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03008093}
8094
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008095/* sysfs name ---------------------------------------------------------- */
8096static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
8097 struct device_attribute *attr,
8098 char *buf)
8099{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008100 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008101}
8102
8103static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
8104 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
8105
8106/* --------------------------------------------------------------------- */
8107
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008108/* /proc support */
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03008109static struct proc_dir_entry *proc_dir;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008110
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008111/*
8112 * Module and infrastructure proble, init and exit handling
8113 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008114
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008115static int force_load;
8116
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008117#ifdef CONFIG_THINKPAD_ACPI_DEBUG
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008118static const char * __init str_supported(int is_supported)
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008119{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008120 static char text_unsupported[] __initdata = "not supported";
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008121
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008122 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008123}
8124#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
8125
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008126static void ibm_exit(struct ibm_struct *ibm)
8127{
8128 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
8129
8130 list_del_init(&ibm->all_drivers);
8131
8132 if (ibm->flags.acpi_notify_installed) {
8133 dbg_printk(TPACPI_DBG_EXIT,
8134 "%s: acpi_remove_notify_handler\n", ibm->name);
8135 BUG_ON(!ibm->acpi);
8136 acpi_remove_notify_handler(*ibm->acpi->handle,
8137 ibm->acpi->type,
8138 dispatch_acpi_notify);
8139 ibm->flags.acpi_notify_installed = 0;
8140 ibm->flags.acpi_notify_installed = 0;
8141 }
8142
8143 if (ibm->flags.proc_created) {
8144 dbg_printk(TPACPI_DBG_EXIT,
8145 "%s: remove_proc_entry\n", ibm->name);
8146 remove_proc_entry(ibm->name, proc_dir);
8147 ibm->flags.proc_created = 0;
8148 }
8149
8150 if (ibm->flags.acpi_driver_registered) {
8151 dbg_printk(TPACPI_DBG_EXIT,
8152 "%s: acpi_bus_unregister_driver\n", ibm->name);
8153 BUG_ON(!ibm->acpi);
8154 acpi_bus_unregister_driver(ibm->acpi->driver);
8155 kfree(ibm->acpi->driver);
8156 ibm->acpi->driver = NULL;
8157 ibm->flags.acpi_driver_registered = 0;
8158 }
8159
8160 if (ibm->flags.init_called && ibm->exit) {
8161 ibm->exit();
8162 ibm->flags.init_called = 0;
8163 }
8164
8165 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
8166}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02008167
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008168static int __init ibm_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008169{
8170 int ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008171 struct ibm_struct *ibm = iibm->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008172 struct proc_dir_entry *entry;
8173
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008174 BUG_ON(ibm == NULL);
8175
8176 INIT_LIST_HEAD(&ibm->all_drivers);
8177
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03008178 if (ibm->flags.experimental && !experimental)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008179 return 0;
8180
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008181 dbg_printk(TPACPI_DBG_INIT,
8182 "probing for %s\n", ibm->name);
8183
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008184 if (iibm->init) {
8185 ret = iibm->init(iibm);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008186 if (ret > 0)
8187 return 0; /* probe failed */
8188 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008189 return ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008190
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03008191 ibm->flags.init_called = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008192 }
8193
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03008194 if (ibm->acpi) {
8195 if (ibm->acpi->hid) {
8196 ret = register_tpacpi_subdriver(ibm);
8197 if (ret)
8198 goto err_out;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008199 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03008200
8201 if (ibm->acpi->notify) {
8202 ret = setup_acpi_notify(ibm);
8203 if (ret == -ENODEV) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008204 printk(TPACPI_NOTICE "disabling subdriver %s\n",
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03008205 ibm->name);
8206 ret = 0;
8207 goto err_out;
8208 }
8209 if (ret < 0)
8210 goto err_out;
8211 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008212 }
8213
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008214 dbg_printk(TPACPI_DBG_INIT,
8215 "%s installed\n", ibm->name);
8216
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008217 if (ibm->read) {
8218 entry = create_proc_entry(ibm->name,
8219 S_IFREG | S_IRUGO | S_IWUSR,
8220 proc_dir);
8221 if (!entry) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008222 printk(TPACPI_ERR "unable to create proc entry %s\n",
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008223 ibm->name);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008224 ret = -ENODEV;
8225 goto err_out;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008226 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008227 entry->data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03008228 entry->read_proc = &dispatch_procfs_read;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008229 if (ibm->write)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03008230 entry->write_proc = &dispatch_procfs_write;
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03008231 ibm->flags.proc_created = 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008232 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008233
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008234 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
8235
Linus Torvalds1da177e2005-04-16 15:20:36 -07008236 return 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008237
8238err_out:
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008239 dbg_printk(TPACPI_DBG_INIT,
8240 "%s: at error exit path with result %d\n",
8241 ibm->name, ret);
8242
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008243 ibm_exit(ibm);
8244 return (ret < 0)? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008245}
8246
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008247/* Probing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008248
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008249static bool __pure __init tpacpi_is_fw_digit(const char c)
8250{
8251 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
8252}
8253
8254/* Most models: xxyTkkWW (#.##c); Ancient 570/600 and -SL lacks (#.##c) */
8255static bool __pure __init tpacpi_is_valid_fw_id(const char* const s,
8256 const char t)
8257{
8258 return s && strlen(s) >= 8 &&
8259 tpacpi_is_fw_digit(s[0]) &&
8260 tpacpi_is_fw_digit(s[1]) &&
8261 s[2] == t && s[3] == 'T' &&
8262 tpacpi_is_fw_digit(s[4]) &&
8263 tpacpi_is_fw_digit(s[5]) &&
8264 s[6] == 'W' && s[7] == 'W';
8265}
8266
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008267/* returns 0 - probe ok, or < 0 - probe error.
8268 * Probe ok doesn't mean thinkpad found.
8269 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
8270static int __must_check __init get_thinkpad_model_data(
8271 struct thinkpad_id_data *tp)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02008272{
Jeff Garzik18552562007-10-03 15:15:40 -04008273 const struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02008274 char ec_fw_string[18];
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008275 char const *s;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02008276
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008277 if (!tp)
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008278 return -EINVAL;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008279
8280 memset(tp, 0, sizeof(*tp));
8281
8282 if (dmi_name_in_vendors("IBM"))
8283 tp->vendor = PCI_VENDOR_ID_IBM;
8284 else if (dmi_name_in_vendors("LENOVO"))
8285 tp->vendor = PCI_VENDOR_ID_LENOVO;
8286 else
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008287 return 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008288
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008289 s = dmi_get_system_info(DMI_BIOS_VERSION);
8290 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
8291 if (s && !tp->bios_version_str)
8292 return -ENOMEM;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008293
8294 /* Really ancient ThinkPad 240X will fail this, which is fine */
8295 if (!tpacpi_is_valid_fw_id(tp->bios_version_str, 'E'))
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008296 return 0;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008297
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008298 tp->bios_model = tp->bios_version_str[0]
8299 | (tp->bios_version_str[1] << 8);
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008300 tp->bios_release = (tp->bios_version_str[4] << 8)
8301 | tp->bios_version_str[5];
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008302
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02008303 /*
8304 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
8305 * X32 or newer, all Z series; Some models must have an
8306 * up-to-date BIOS or they will not be detected.
8307 *
8308 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
8309 */
8310 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02008311 if (sscanf(dev->name,
8312 "IBM ThinkPad Embedded Controller -[%17c",
8313 ec_fw_string) == 1) {
8314 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
8315 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008316
8317 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008318 if (!tp->ec_version_str)
8319 return -ENOMEM;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008320
8321 if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) {
8322 tp->ec_model = ec_fw_string[0]
8323 | (ec_fw_string[1] << 8);
8324 tp->ec_release = (ec_fw_string[4] << 8)
8325 | ec_fw_string[5];
8326 } else {
8327 printk(TPACPI_NOTICE
8328 "ThinkPad firmware release %s "
8329 "doesn't match the known patterns\n",
8330 ec_fw_string);
8331 printk(TPACPI_NOTICE
8332 "please report this to %s\n",
8333 TPACPI_MAIL);
8334 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008335 break;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02008336 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02008337 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008338
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008339 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
8340 if (s && !strnicmp(s, "ThinkPad", 8)) {
8341 tp->model_str = kstrdup(s, GFP_KERNEL);
8342 if (!tp->model_str)
8343 return -ENOMEM;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008344 }
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03008345
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008346 s = dmi_get_system_info(DMI_PRODUCT_NAME);
8347 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
8348 if (s && !tp->nummodel_str)
8349 return -ENOMEM;
8350
8351 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008352}
8353
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008354static int __init probe_for_thinkpad(void)
8355{
8356 int is_thinkpad;
8357
8358 if (acpi_disabled)
8359 return -ENODEV;
8360
8361 /*
8362 * Non-ancient models have better DMI tagging, but very old models
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03008363 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008364 */
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03008365 is_thinkpad = (thinkpad_id.model_str != NULL) ||
8366 (thinkpad_id.ec_model != 0) ||
8367 tpacpi_is_fw_known();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008368
8369 /* ec is required because many other handles are relative to it */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008370 TPACPI_ACPIHANDLE_INIT(ec);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008371 if (!ec_handle) {
8372 if (is_thinkpad)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008373 printk(TPACPI_ERR
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008374 "Not yet supported ThinkPad detected!\n");
8375 return -ENODEV;
8376 }
8377
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03008378 if (!is_thinkpad && !force_load)
8379 return -ENODEV;
8380
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008381 return 0;
8382}
8383
8384
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008385/* Module init, exit, parameters */
8386
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008387static struct ibm_init_struct ibms_init[] __initdata = {
8388 {
8389 .init = thinkpad_acpi_driver_init,
8390 .data = &thinkpad_acpi_driver_data,
8391 },
8392 {
8393 .init = hotkey_init,
8394 .data = &hotkey_driver_data,
8395 },
8396 {
8397 .init = bluetooth_init,
8398 .data = &bluetooth_driver_data,
8399 },
8400 {
8401 .init = wan_init,
8402 .data = &wan_driver_data,
8403 },
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02008404 {
8405 .init = uwb_init,
8406 .data = &uwb_driver_data,
8407 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02008408#ifdef CONFIG_THINKPAD_ACPI_VIDEO
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008409 {
8410 .init = video_init,
8411 .data = &video_driver_data,
8412 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02008413#endif
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008414 {
8415 .init = light_init,
8416 .data = &light_driver_data,
8417 },
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008418 {
8419 .init = cmos_init,
8420 .data = &cmos_driver_data,
8421 },
8422 {
8423 .init = led_init,
8424 .data = &led_driver_data,
8425 },
8426 {
8427 .init = beep_init,
8428 .data = &beep_driver_data,
8429 },
8430 {
8431 .init = thermal_init,
8432 .data = &thermal_driver_data,
8433 },
8434 {
8435 .data = &ecdump_driver_data,
8436 },
8437 {
8438 .init = brightness_init,
8439 .data = &brightness_driver_data,
8440 },
8441 {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02008442 .init = volume_init,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008443 .data = &volume_driver_data,
8444 },
8445 {
8446 .init = fan_init,
8447 .data = &fan_driver_data,
8448 },
8449};
8450
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008451static int __init set_ibm_param(const char *val, struct kernel_param *kp)
8452{
8453 unsigned int i;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008454 struct ibm_struct *ibm;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008455
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02008456 if (!kp || !kp->name || !val)
8457 return -EINVAL;
8458
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008459 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
8460 ibm = ibms_init[i].data;
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02008461 WARN_ON(ibm == NULL);
8462
8463 if (!ibm || !ibm->name)
8464 continue;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008465
8466 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
8467 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008468 return -ENOSPC;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008469 strcpy(ibms_init[i].param, val);
8470 strcat(ibms_init[i].param, ",");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008471 return 0;
8472 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008473 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008474
8475 return -EINVAL;
8476}
8477
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008478module_param(experimental, int, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008479MODULE_PARM_DESC(experimental,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008480 "Enables experimental features when non-zero");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008481
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03008482module_param_named(debug, dbg_level, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008483MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03008484
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008485module_param(force_load, bool, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008486MODULE_PARM_DESC(force_load,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008487 "Attempts to load the driver even on a "
8488 "mis-identified ThinkPad when true");
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03008489
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008490module_param_named(fan_control, fan_control_allowed, bool, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008491MODULE_PARM_DESC(fan_control,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008492 "Enables setting fan parameters features when true");
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008493
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008494module_param_named(brightness_mode, brightness_mode, uint, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008495MODULE_PARM_DESC(brightness_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008496 "Selects brightness control strategy: "
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00008497 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03008498
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008499module_param(brightness_enable, uint, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008500MODULE_PARM_DESC(brightness_enable,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008501 "Enables backlight control when 1, disables when 0");
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02008502
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008503module_param(hotkey_report_mode, uint, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008504MODULE_PARM_DESC(hotkey_report_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008505 "used for backwards compatibility with userspace, "
8506 "see documentation");
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03008507
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02008508module_param_named(volume_mode, volume_mode, uint, 0444);
8509MODULE_PARM_DESC(volume_mode,
8510 "Selects volume control strategy: "
8511 "0=auto, 1=EC, 2=N/A, 3=EC+NVRAM");
8512
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02008513module_param_named(volume_capabilities, volume_capabilities, uint, 0444);
8514MODULE_PARM_DESC(volume_capabilities,
8515 "Selects the mixer capabilites: "
8516 "0=auto, 1=volume and mute, 2=mute only");
8517
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008518#define TPACPI_PARAM(feature) \
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008519 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
Henrique de Moraes Holschuhcbb14842008-02-16 02:17:50 -02008520 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008521 "at module load, see documentation")
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008522
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008523TPACPI_PARAM(hotkey);
8524TPACPI_PARAM(bluetooth);
8525TPACPI_PARAM(video);
8526TPACPI_PARAM(light);
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008527TPACPI_PARAM(cmos);
8528TPACPI_PARAM(led);
8529TPACPI_PARAM(beep);
8530TPACPI_PARAM(ecdump);
8531TPACPI_PARAM(brightness);
8532TPACPI_PARAM(volume);
8533TPACPI_PARAM(fan);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008534
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02008535#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008536module_param(dbg_wlswemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02008537MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
8538module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
8539MODULE_PARM_DESC(wlsw_state,
8540 "Initial state of the emulated WLSW switch");
8541
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008542module_param(dbg_bluetoothemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02008543MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
8544module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
8545MODULE_PARM_DESC(bluetooth_state,
8546 "Initial state of the emulated bluetooth switch");
8547
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008548module_param(dbg_wwanemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02008549MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
8550module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
8551MODULE_PARM_DESC(wwan_state,
8552 "Initial state of the emulated WWAN switch");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02008553
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008554module_param(dbg_uwbemul, uint, 0444);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02008555MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
8556module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
8557MODULE_PARM_DESC(uwb_state,
8558 "Initial state of the emulated UWB switch");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02008559#endif
8560
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008561static void thinkpad_acpi_module_exit(void)
8562{
8563 struct ibm_struct *ibm, *itmp;
8564
8565 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
8566
8567 list_for_each_entry_safe_reverse(ibm, itmp,
8568 &tpacpi_all_drivers,
8569 all_drivers) {
8570 ibm_exit(ibm);
8571 }
8572
8573 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
8574
8575 if (tpacpi_inputdev) {
8576 if (tp_features.input_device_registered)
8577 input_unregister_device(tpacpi_inputdev);
8578 else
8579 input_free_device(tpacpi_inputdev);
8580 }
8581
8582 if (tpacpi_hwmon)
8583 hwmon_device_unregister(tpacpi_hwmon);
8584
8585 if (tp_features.sensors_pdev_attrs_registered)
8586 device_remove_file(&tpacpi_sensors_pdev->dev,
8587 &dev_attr_thinkpad_acpi_pdev_name);
8588 if (tpacpi_sensors_pdev)
8589 platform_device_unregister(tpacpi_sensors_pdev);
8590 if (tpacpi_pdev)
8591 platform_device_unregister(tpacpi_pdev);
8592
8593 if (tp_features.sensors_pdrv_attrs_registered)
8594 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
8595 if (tp_features.platform_drv_attrs_registered)
8596 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
8597
8598 if (tp_features.sensors_pdrv_registered)
8599 platform_driver_unregister(&tpacpi_hwmon_pdriver);
8600
8601 if (tp_features.platform_drv_registered)
8602 platform_driver_unregister(&tpacpi_pdriver);
8603
8604 if (proc_dir)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008605 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008606
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03008607 if (tpacpi_wq)
8608 destroy_workqueue(tpacpi_wq);
8609
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008610 kfree(thinkpad_id.bios_version_str);
8611 kfree(thinkpad_id.ec_version_str);
8612 kfree(thinkpad_id.model_str);
8613}
8614
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02008615
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03008616static int __init thinkpad_acpi_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008617{
8618 int ret, i;
8619
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03008620 tpacpi_lifecycle = TPACPI_LIFE_INIT;
8621
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03008622 /* Parameter checking */
8623 if (hotkey_report_mode > 2)
8624 return -EINVAL;
8625
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008626 /* Driver-level probe */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008627
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008628 ret = get_thinkpad_model_data(&thinkpad_id);
8629 if (ret) {
8630 printk(TPACPI_ERR
8631 "unable to get DMI data: %d\n", ret);
8632 thinkpad_acpi_module_exit();
8633 return ret;
8634 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008635 ret = probe_for_thinkpad();
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008636 if (ret) {
8637 thinkpad_acpi_module_exit();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008638 return ret;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008639 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008640
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008641 /* Driver initialization */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008642
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008643 TPACPI_ACPIHANDLE_INIT(ecrd);
8644 TPACPI_ACPIHANDLE_INIT(ecwr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008645
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03008646 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
8647 if (!tpacpi_wq) {
8648 thinkpad_acpi_module_exit();
8649 return -ENOMEM;
8650 }
8651
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008652 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008653 if (!proc_dir) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008654 printk(TPACPI_ERR
8655 "unable to create proc dir " TPACPI_PROC_DIR);
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03008656 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008657 return -ENODEV;
8658 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008659
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008660 ret = platform_driver_register(&tpacpi_pdriver);
8661 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008662 printk(TPACPI_ERR
8663 "unable to register main platform driver\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008664 thinkpad_acpi_module_exit();
8665 return ret;
8666 }
Henrique de Moraes Holschuhac363932007-07-27 17:04:40 -03008667 tp_features.platform_drv_registered = 1;
8668
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008669 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
8670 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008671 printk(TPACPI_ERR
8672 "unable to register hwmon platform driver\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008673 thinkpad_acpi_module_exit();
8674 return ret;
8675 }
8676 tp_features.sensors_pdrv_registered = 1;
8677
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03008678 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03008679 if (!ret) {
8680 tp_features.platform_drv_attrs_registered = 1;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008681 ret = tpacpi_create_driver_attributes(
8682 &tpacpi_hwmon_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03008683 }
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03008684 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008685 printk(TPACPI_ERR
8686 "unable to create sysfs driver attributes\n");
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03008687 thinkpad_acpi_module_exit();
8688 return ret;
8689 }
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03008690 tp_features.sensors_pdrv_attrs_registered = 1;
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03008691
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008692
8693 /* Device initialization */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008694 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008695 NULL, 0);
8696 if (IS_ERR(tpacpi_pdev)) {
8697 ret = PTR_ERR(tpacpi_pdev);
8698 tpacpi_pdev = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008699 printk(TPACPI_ERR "unable to register platform device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008700 thinkpad_acpi_module_exit();
8701 return ret;
8702 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008703 tpacpi_sensors_pdev = platform_device_register_simple(
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008704 TPACPI_HWMON_DRVR_NAME,
8705 -1, NULL, 0);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008706 if (IS_ERR(tpacpi_sensors_pdev)) {
8707 ret = PTR_ERR(tpacpi_sensors_pdev);
8708 tpacpi_sensors_pdev = NULL;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008709 printk(TPACPI_ERR
8710 "unable to register hwmon platform device\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008711 thinkpad_acpi_module_exit();
8712 return ret;
8713 }
8714 ret = device_create_file(&tpacpi_sensors_pdev->dev,
8715 &dev_attr_thinkpad_acpi_pdev_name);
8716 if (ret) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008717 printk(TPACPI_ERR
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008718 "unable to create sysfs hwmon device attributes\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008719 thinkpad_acpi_module_exit();
8720 return ret;
8721 }
8722 tp_features.sensors_pdev_attrs_registered = 1;
8723 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008724 if (IS_ERR(tpacpi_hwmon)) {
8725 ret = PTR_ERR(tpacpi_hwmon);
8726 tpacpi_hwmon = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008727 printk(TPACPI_ERR "unable to register hwmon device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008728 thinkpad_acpi_module_exit();
8729 return ret;
8730 }
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -03008731 mutex_init(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03008732 tpacpi_inputdev = input_allocate_device();
8733 if (!tpacpi_inputdev) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008734 printk(TPACPI_ERR "unable to allocate input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03008735 thinkpad_acpi_module_exit();
8736 return -ENOMEM;
8737 } else {
8738 /* Prepare input device, but don't register */
8739 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008740 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03008741 tpacpi_inputdev->id.bustype = BUS_HOST;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03008742 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
8743 thinkpad_id.vendor :
8744 PCI_VENDOR_ID_IBM;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03008745 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
8746 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
Henrique de Moraes Holschuhd112ef92009-12-09 01:36:26 +00008747 tpacpi_inputdev->dev.parent = &tpacpi_pdev->dev;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03008748 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008749 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
8750 ret = ibm_init(&ibms_init[i]);
8751 if (ret >= 0 && *ibms_init[i].param)
8752 ret = ibms_init[i].data->write(ibms_init[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008753 if (ret < 0) {
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03008754 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008755 return ret;
8756 }
8757 }
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03008758 ret = input_register_device(tpacpi_inputdev);
8759 if (ret < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008760 printk(TPACPI_ERR "unable to register input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03008761 thinkpad_acpi_module_exit();
8762 return ret;
8763 } else {
8764 tp_features.input_device_registered = 1;
8765 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008766
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03008767 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008768 return 0;
8769}
8770
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03008771MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
8772
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008773/*
Henrique de Moraes Holschuh922fe092009-04-14 02:44:13 +00008774 * This will autoload the driver in almost every ThinkPad
8775 * in widespread use.
8776 *
8777 * Only _VERY_ old models, like the 240, 240x and 570 lack
8778 * the HKEY event interface.
8779 */
8780MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
8781
8782/*
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008783 * DMI matching for module autoloading
8784 *
8785 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
8786 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
8787 *
8788 * Only models listed in thinkwiki will be supported, so add yours
8789 * if it is not there yet.
8790 */
8791#define IBM_BIOS_MODULE_ALIAS(__type) \
Mathieu Chouquet-Stringerb36a50f2009-03-14 16:35:26 +01008792 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008793
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008794/* Ancient thinkpad BIOSes have to be identified by
8795 * BIOS type or model number, and there are far less
8796 * BIOS types than model numbers... */
Henrique de Moraes Holschuh922fe092009-04-14 02:44:13 +00008797IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008798
Henrique de Moraes Holschuhf68f53a2009-04-14 02:44:12 +00008799MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
8800MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008801MODULE_DESCRIPTION(TPACPI_DESC);
8802MODULE_VERSION(TPACPI_VERSION);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008803MODULE_LICENSE("GPL");
8804
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03008805module_init(thinkpad_acpi_module_init);
8806module_exit(thinkpad_acpi_module_exit);