blob: 3daf349ddc98b5ac77b5f60eb3b24ae881de2b3c [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 Holschuh347a2682009-12-09 01:36:24 +000025#define TPACPI_SYSFS_VERSION 0x020600
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 Holschuh0c780392008-01-08 13:02:43 -0200234
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200235#define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
236#define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
237#define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200238
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200239
240/****************************************************************************
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200241 * Driver-wide structs and misc. variables
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200242 */
243
244struct ibm_struct;
245
246struct tp_acpi_drv_struct {
247 const struct acpi_device_id *hid;
248 struct acpi_driver *driver;
249
250 void (*notify) (struct ibm_struct *, u32);
251 acpi_handle *handle;
252 u32 type;
253 struct acpi_device *device;
254};
255
256struct ibm_struct {
257 char *name;
258
259 int (*read) (char *);
260 int (*write) (char *);
261 void (*exit) (void);
262 void (*resume) (void);
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200263 void (*suspend) (pm_message_t state);
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200264 void (*shutdown) (void);
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200265
266 struct list_head all_drivers;
267
268 struct tp_acpi_drv_struct *acpi;
269
270 struct {
271 u8 acpi_driver_registered:1;
272 u8 acpi_notify_installed:1;
273 u8 proc_created:1;
274 u8 init_called:1;
275 u8 experimental:1;
276 } flags;
277};
278
279struct ibm_init_struct {
280 char param[32];
281
282 int (*init) (struct ibm_init_struct *);
283 struct ibm_struct *data;
284};
285
286static struct {
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200287 u32 bluetooth:1;
288 u32 hotkey:1;
289 u32 hotkey_mask:1;
290 u32 hotkey_wlsw:1;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -0200291 u32 hotkey_tablet:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200292 u32 light:1;
293 u32 light_status:1;
294 u32 bright_16levels:1;
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -0300295 u32 bright_acpimode:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200296 u32 wan:1;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -0200297 u32 uwb:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200298 u32 fan_ctrl_status_undef:1;
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -0300299 u32 second_fan:1;
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -0300300 u32 beep_needs_two_args:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200301 u32 input_device_registered:1;
302 u32 platform_drv_registered:1;
303 u32 platform_drv_attrs_registered:1;
304 u32 sensors_pdrv_registered:1;
305 u32 sensors_pdrv_attrs_registered:1;
306 u32 sensors_pdev_attrs_registered:1;
307 u32 hotkey_poll_active:1;
308} tp_features;
309
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -0300310static struct {
311 u16 hotkey_mask_ff:1;
312} tp_warned;
313
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200314struct thinkpad_id_data {
315 unsigned int vendor; /* ThinkPad vendor:
316 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
317
318 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
319 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
320
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -0300321 u16 bios_model; /* 1Y = 0x5931, 0 = unknown */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200322 u16 ec_model;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -0300323 u16 bios_release; /* 1ZETK1WW = 0x314b, 0 = unknown */
324 u16 ec_release;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200325
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -0300326 char *model_str; /* ThinkPad T43 */
327 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200328};
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200329static struct thinkpad_id_data thinkpad_id;
330
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300331static enum {
332 TPACPI_LIFE_INIT = 0,
333 TPACPI_LIFE_RUNNING,
334 TPACPI_LIFE_EXITING,
335} tpacpi_lifecycle;
336
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200337static int experimental;
338static u32 dbg_level;
339
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -0300340static struct workqueue_struct *tpacpi_wq;
341
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +0000342enum led_status_t {
343 TPACPI_LED_OFF = 0,
344 TPACPI_LED_ON,
345 TPACPI_LED_BLINK,
346};
347
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -0300348/* Special LED class that can defer work */
349struct tpacpi_led_classdev {
350 struct led_classdev led_classdev;
351 struct work_struct work;
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +0000352 enum led_status_t new_state;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -0300353 unsigned int led;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -0300354};
355
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200356#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
357static int dbg_wlswemul;
358static int tpacpi_wlsw_emulstate;
359static int dbg_bluetoothemul;
360static int tpacpi_bluetooth_emulstate;
361static int dbg_wwanemul;
362static int tpacpi_wwan_emulstate;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -0200363static int dbg_uwbemul;
364static int tpacpi_uwb_emulstate;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200365#endif
366
367
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000368/*************************************************************************
369 * Debugging helpers
370 */
371
372#define dbg_printk(a_dbg_level, format, arg...) \
373 do { if (dbg_level & (a_dbg_level)) \
374 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
375 } while (0)
376
377#ifdef CONFIG_THINKPAD_ACPI_DEBUG
378#define vdbg_printk dbg_printk
379static const char *str_supported(int is_supported);
380#else
381#define vdbg_printk(a_dbg_level, format, arg...) \
382 do { } while (0)
383#endif
384
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +0000385static void tpacpi_log_usertask(const char * const what)
386{
387 printk(TPACPI_DEBUG "%s: access by process with PID %d\n",
388 what, task_tgid_vnr(current));
389}
390
391#define tpacpi_disclose_usertask(what, format, arg...) \
392 do { \
393 if (unlikely( \
394 (dbg_level & TPACPI_DBG_DISCLOSETASK) && \
395 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
396 printk(TPACPI_DEBUG "%s: PID %d: " format, \
397 what, task_tgid_vnr(current), ## arg); \
398 } \
399 } while (0)
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000400
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -0300401/*
402 * Quirk handling helpers
403 *
404 * ThinkPad IDs and versions seen in the field so far
405 * are two-characters from the set [0-9A-Z], i.e. base 36.
406 *
407 * We use values well outside that range as specials.
408 */
409
410#define TPACPI_MATCH_ANY 0xffffU
411#define TPACPI_MATCH_UNKNOWN 0U
412
413/* TPID('1', 'Y') == 0x5931 */
414#define TPID(__c1, __c2) (((__c2) << 8) | (__c1))
415
416#define TPACPI_Q_IBM(__id1, __id2, __quirk) \
417 { .vendor = PCI_VENDOR_ID_IBM, \
418 .bios = TPID(__id1, __id2), \
419 .ec = TPACPI_MATCH_ANY, \
420 .quirks = (__quirk) }
421
422#define TPACPI_Q_LNV(__id1, __id2, __quirk) \
423 { .vendor = PCI_VENDOR_ID_LENOVO, \
424 .bios = TPID(__id1, __id2), \
425 .ec = TPACPI_MATCH_ANY, \
426 .quirks = (__quirk) }
427
428struct tpacpi_quirk {
429 unsigned int vendor;
430 u16 bios;
431 u16 ec;
432 unsigned long quirks;
433};
434
435/**
436 * tpacpi_check_quirks() - search BIOS/EC version on a list
437 * @qlist: array of &struct tpacpi_quirk
438 * @qlist_size: number of elements in @qlist
439 *
440 * Iterates over a quirks list until one is found that matches the
441 * ThinkPad's vendor, BIOS and EC model.
442 *
443 * Returns 0 if nothing matches, otherwise returns the quirks field of
444 * the matching &struct tpacpi_quirk entry.
445 *
446 * The match criteria is: vendor, ec and bios much match.
447 */
448static unsigned long __init tpacpi_check_quirks(
449 const struct tpacpi_quirk *qlist,
450 unsigned int qlist_size)
451{
452 while (qlist_size) {
453 if ((qlist->vendor == thinkpad_id.vendor ||
454 qlist->vendor == TPACPI_MATCH_ANY) &&
455 (qlist->bios == thinkpad_id.bios_model ||
456 qlist->bios == TPACPI_MATCH_ANY) &&
457 (qlist->ec == thinkpad_id.ec_model ||
458 qlist->ec == TPACPI_MATCH_ANY))
459 return qlist->quirks;
460
461 qlist_size--;
462 qlist++;
463 }
464 return 0;
465}
466
467
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300468/****************************************************************************
469 ****************************************************************************
470 *
471 * ACPI Helpers and device model
472 *
473 ****************************************************************************
474 ****************************************************************************/
475
476/*************************************************************************
477 * ACPI basic handles
478 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -0300480static acpi_handle root_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200482#define TPACPI_HANDLE(object, parent, paths...) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 static acpi_handle object##_handle; \
484 static acpi_handle *object##_parent = &parent##_handle; \
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400485 static char *object##_path; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 static char *object##_paths[] = { paths }
487
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200488TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400489 "\\_SB.PCI.ISA.EC", /* 570 */
490 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
491 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
492 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
493 "\\_SB.PCI0.ICH3.EC0", /* R31 */
494 "\\_SB.PCI0.LPC.EC", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300495 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200497TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
498TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200500TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
501 /* T4x, X31, X40 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400502 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
503 "\\CMS", /* R40, R40e */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300504 ); /* all others */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400505
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200506TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400507 "^HKEY", /* R30, R31 */
508 "HKEY", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300509 ); /* 570 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400510
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -0200511TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
512 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
513 "\\_SB.PCI0.VID0", /* 770e */
514 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
515 "\\_SB.PCI0.AGP.VID", /* all others */
516 ); /* R30, R31 */
517
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400518
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300519/*************************************************************************
520 * ACPI helpers
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -0200521 */
522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523static int acpi_evalf(acpi_handle handle,
524 void *res, char *method, char *fmt, ...)
525{
526 char *fmt0 = fmt;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400527 struct acpi_object_list params;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200528 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400529 struct acpi_buffer result, *resultp;
530 union acpi_object out_obj;
531 acpi_status status;
532 va_list ap;
533 char res_type;
534 int success;
535 int quiet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
537 if (!*fmt) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200538 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 return 0;
540 }
541
542 if (*fmt == 'q') {
543 quiet = 1;
544 fmt++;
545 } else
546 quiet = 0;
547
548 res_type = *(fmt++);
549
550 params.count = 0;
551 params.pointer = &in_objs[0];
552
553 va_start(ap, fmt);
554 while (*fmt) {
555 char c = *(fmt++);
556 switch (c) {
557 case 'd': /* int */
558 in_objs[params.count].integer.value = va_arg(ap, int);
559 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
560 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400561 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 default:
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200563 printk(TPACPI_ERR "acpi_evalf() called "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 "with invalid format character '%c'\n", c);
565 return 0;
566 }
567 }
568 va_end(ap);
569
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400570 if (res_type != 'v') {
571 result.length = sizeof(out_obj);
572 result.pointer = &out_obj;
573 resultp = &result;
574 } else
575 resultp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400577 status = acpi_evaluate_object(handle, method, &params, resultp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
579 switch (res_type) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400580 case 'd': /* int */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 if (res)
582 *(int *)res = out_obj.integer.value;
583 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
584 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400585 case 'v': /* void */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 success = status == AE_OK;
587 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400588 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 default:
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200590 printk(TPACPI_ERR "acpi_evalf() called "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 "with invalid format character '%c'\n", res_type);
592 return 0;
593 }
594
595 if (!success && !quiet)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200596 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 method, fmt0, status);
598
599 return success;
600}
601
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200602static int acpi_ec_read(int i, u8 *p)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300603{
604 int v;
605
606 if (ecrd_handle) {
607 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
608 return 0;
609 *p = v;
610 } else {
611 if (ec_read(i, p) < 0)
612 return 0;
613 }
614
615 return 1;
616}
617
618static int acpi_ec_write(int i, u8 v)
619{
620 if (ecwr_handle) {
621 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
622 return 0;
623 } else {
624 if (ec_write(i, v) < 0)
625 return 0;
626 }
627
628 return 1;
629}
630
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -0300631static int issue_thinkpad_cmos_command(int cmos_cmd)
632{
633 if (!cmos_handle)
634 return -ENXIO;
635
636 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
637 return -EIO;
638
639 return 0;
640}
641
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300642/*************************************************************************
643 * ACPI device model
644 */
645
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200646#define TPACPI_ACPIHANDLE_INIT(object) \
647 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200648 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
649
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300650static void drv_acpi_handle_init(char *name,
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300651 acpi_handle *handle, acpi_handle parent,
652 char **paths, int num_paths, char **path)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300653{
654 int i;
655 acpi_status status;
656
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300657 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
658 name);
659
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300660 for (i = 0; i < num_paths; i++) {
661 status = acpi_get_handle(parent, paths[i], handle);
662 if (ACPI_SUCCESS(status)) {
663 *path = paths[i];
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300664 dbg_printk(TPACPI_DBG_INIT,
665 "Found ACPI handle %s for %s\n",
666 *path, name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300667 return;
668 }
669 }
670
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300671 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
672 name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300673 *handle = NULL;
674}
675
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300676static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300677{
678 struct ibm_struct *ibm = data;
679
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300680 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
681 return;
682
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300683 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300684 return;
685
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300686 ibm->acpi->notify(ibm, event);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300687}
688
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300689static int __init setup_acpi_notify(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300690{
691 acpi_status status;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300692 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300693
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300694 BUG_ON(!ibm->acpi);
695
696 if (!*ibm->acpi->handle)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300697 return 0;
698
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300699 vdbg_printk(TPACPI_DBG_INIT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300700 "setting up ACPI notify for %s\n", ibm->name);
701
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300702 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
703 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200704 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300705 ibm->name, rc);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300706 return -ENODEV;
707 }
708
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700709 ibm->acpi->device->driver_data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300710 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200711 TPACPI_ACPI_EVENT_PREFIX,
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -0300712 ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300713
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300714 status = acpi_install_notify_handler(*ibm->acpi->handle,
715 ibm->acpi->type, dispatch_acpi_notify, ibm);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300716 if (ACPI_FAILURE(status)) {
717 if (status == AE_ALREADY_EXISTS) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200718 printk(TPACPI_NOTICE
719 "another device driver is already "
720 "handling %s events\n", ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300721 } else {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200722 printk(TPACPI_ERR
723 "acpi_install_notify_handler(%s) failed: %d\n",
724 ibm->name, status);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300725 }
726 return -ENODEV;
727 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300728 ibm->flags.acpi_notify_installed = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300729 return 0;
730}
731
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300732static int __init tpacpi_device_add(struct acpi_device *device)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300733{
734 return 0;
735}
736
Henrique de Moraes Holschuh67001212007-04-21 11:08:25 -0300737static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300738{
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300739 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300740
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300741 dbg_printk(TPACPI_DBG_INIT,
742 "registering %s as an ACPI driver\n", ibm->name);
743
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300744 BUG_ON(!ibm->acpi);
745
746 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
747 if (!ibm->acpi->driver) {
Mariusz Kozlowski4f778b92008-10-18 14:23:53 -0300748 printk(TPACPI_ERR
749 "failed to allocate memory for ibm->acpi->driver\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300750 return -ENOMEM;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300751 }
752
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200753 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300754 ibm->acpi->driver->ids = ibm->acpi->hid;
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200755
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300756 ibm->acpi->driver->ops.add = &tpacpi_device_add;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300757
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300758 rc = acpi_bus_register_driver(ibm->acpi->driver);
759 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200760 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200761 ibm->name, rc);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300762 kfree(ibm->acpi->driver);
763 ibm->acpi->driver = NULL;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300764 } else if (!rc)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300765 ibm->flags.acpi_driver_registered = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300766
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300767 return rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300768}
769
770
771/****************************************************************************
772 ****************************************************************************
773 *
774 * Procfs Helpers
775 *
776 ****************************************************************************
777 ****************************************************************************/
778
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300779static int dispatch_procfs_read(char *page, char **start, off_t off,
780 int count, int *eof, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300781{
782 struct ibm_struct *ibm = data;
783 int len;
784
785 if (!ibm || !ibm->read)
786 return -EINVAL;
787
788 len = ibm->read(page);
789 if (len < 0)
790 return len;
791
792 if (len <= off + count)
793 *eof = 1;
794 *start = page + off;
795 len -= off;
796 if (len > count)
797 len = count;
798 if (len < 0)
799 len = 0;
800
801 return len;
802}
803
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300804static int dispatch_procfs_write(struct file *file,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200805 const char __user *userbuf,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300806 unsigned long count, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300807{
808 struct ibm_struct *ibm = data;
809 char *kernbuf;
810 int ret;
811
812 if (!ibm || !ibm->write)
813 return -EINVAL;
Michael Buesch5b05d462009-08-01 12:04:19 -0300814 if (count > PAGE_SIZE - 2)
815 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300816
817 kernbuf = kmalloc(count + 2, GFP_KERNEL);
818 if (!kernbuf)
819 return -ENOMEM;
820
821 if (copy_from_user(kernbuf, userbuf, count)) {
822 kfree(kernbuf);
823 return -EFAULT;
824 }
825
826 kernbuf[count] = 0;
827 strcat(kernbuf, ",");
828 ret = ibm->write(kernbuf);
829 if (ret == 0)
830 ret = count;
831
832 kfree(kernbuf);
833
834 return ret;
835}
836
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837static char *next_cmd(char **cmds)
838{
839 char *start = *cmds;
840 char *end;
841
842 while ((end = strchr(start, ',')) && end == start)
843 start = end + 1;
844
845 if (!end)
846 return NULL;
847
848 *end = 0;
849 *cmds = end + 1;
850 return start;
851}
852
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300853
854/****************************************************************************
855 ****************************************************************************
856 *
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300857 * Device model: input, hwmon and platform
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300858 *
859 ****************************************************************************
860 ****************************************************************************/
861
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -0300862static struct platform_device *tpacpi_pdev;
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300863static struct platform_device *tpacpi_sensors_pdev;
Tony Jones1beeffe2007-08-20 13:46:20 -0700864static struct device *tpacpi_hwmon;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300865static struct input_dev *tpacpi_inputdev;
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -0300866static struct mutex tpacpi_inputdev_send_mutex;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200867static LIST_HEAD(tpacpi_all_drivers);
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300868
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200869static int tpacpi_suspend_handler(struct platform_device *pdev,
870 pm_message_t state)
871{
872 struct ibm_struct *ibm, *itmp;
873
874 list_for_each_entry_safe(ibm, itmp,
875 &tpacpi_all_drivers,
876 all_drivers) {
877 if (ibm->suspend)
878 (ibm->suspend)(state);
879 }
880
881 return 0;
882}
883
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300884static int tpacpi_resume_handler(struct platform_device *pdev)
885{
886 struct ibm_struct *ibm, *itmp;
887
888 list_for_each_entry_safe(ibm, itmp,
889 &tpacpi_all_drivers,
890 all_drivers) {
891 if (ibm->resume)
892 (ibm->resume)();
893 }
894
895 return 0;
896}
897
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200898static void tpacpi_shutdown_handler(struct platform_device *pdev)
899{
900 struct ibm_struct *ibm, *itmp;
901
902 list_for_each_entry_safe(ibm, itmp,
903 &tpacpi_all_drivers,
904 all_drivers) {
905 if (ibm->shutdown)
906 (ibm->shutdown)();
907 }
908}
909
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300910static struct platform_driver tpacpi_pdriver = {
911 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200912 .name = TPACPI_DRVR_NAME,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300913 .owner = THIS_MODULE,
914 },
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200915 .suspend = tpacpi_suspend_handler,
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300916 .resume = tpacpi_resume_handler,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200917 .shutdown = tpacpi_shutdown_handler,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300918};
919
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300920static struct platform_driver tpacpi_hwmon_pdriver = {
921 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200922 .name = TPACPI_HWMON_DRVR_NAME,
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300923 .owner = THIS_MODULE,
924 },
925};
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300926
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -0300927/*************************************************************************
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300928 * sysfs support helpers
929 */
930
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200931struct attribute_set {
932 unsigned int members, max_members;
933 struct attribute_group group;
934};
935
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300936struct attribute_set_obj {
937 struct attribute_set s;
938 struct attribute *a;
939} __attribute__((packed));
940
941static struct attribute_set *create_attr_set(unsigned int max_members,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200942 const char *name)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300943{
944 struct attribute_set_obj *sobj;
945
946 if (max_members == 0)
947 return NULL;
948
949 /* Allocates space for implicit NULL at the end too */
950 sobj = kzalloc(sizeof(struct attribute_set_obj) +
951 max_members * sizeof(struct attribute *),
952 GFP_KERNEL);
953 if (!sobj)
954 return NULL;
955 sobj->s.max_members = max_members;
956 sobj->s.group.attrs = &sobj->a;
957 sobj->s.group.name = name;
958
959 return &sobj->s;
960}
961
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200962#define destroy_attr_set(_set) \
963 kfree(_set);
964
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300965/* not multi-threaded safe, use it in a single thread per set */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200966static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300967{
968 if (!s || !attr)
969 return -EINVAL;
970
971 if (s->members >= s->max_members)
972 return -ENOMEM;
973
974 s->group.attrs[s->members] = attr;
975 s->members++;
976
977 return 0;
978}
979
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200980static int add_many_to_attr_set(struct attribute_set *s,
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300981 struct attribute **attr,
982 unsigned int count)
983{
984 int i, res;
985
986 for (i = 0; i < count; i++) {
987 res = add_to_attr_set(s, attr[i]);
988 if (res)
989 return res;
990 }
991
992 return 0;
993}
994
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200995static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300996{
997 sysfs_remove_group(kobj, &s->group);
998 destroy_attr_set(s);
999}
1000
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001001#define register_attr_set_with_sysfs(_attr_set, _kobj) \
1002 sysfs_create_group(_kobj, &_attr_set->group)
1003
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001004static int parse_strtoul(const char *buf,
1005 unsigned long max, unsigned long *value)
1006{
1007 char *endp;
1008
Henrique de Moraes Holschuh32afbf02007-10-08 10:12:56 -03001009 while (*buf && isspace(*buf))
1010 buf++;
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001011 *value = simple_strtoul(buf, &endp, 0);
1012 while (*endp && isspace(*endp))
1013 endp++;
1014 if (*endp || *value > max)
1015 return -EINVAL;
1016
1017 return 0;
1018}
1019
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03001020static void tpacpi_disable_brightness_delay(void)
1021{
1022 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
1023 printk(TPACPI_NOTICE
1024 "ACPI backlight control delay disabled\n");
1025}
1026
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03001027static int __init tpacpi_query_bcl_levels(acpi_handle handle)
1028{
1029 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1030 union acpi_object *obj;
1031 int rc;
1032
1033 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
1034 obj = (union acpi_object *)buffer.pointer;
1035 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
1036 printk(TPACPI_ERR "Unknown _BCL data, "
1037 "please report this to %s\n", TPACPI_MAIL);
1038 rc = 0;
1039 } else {
1040 rc = obj->package.count;
1041 }
1042 } else {
1043 return 0;
1044 }
1045
1046 kfree(buffer.pointer);
1047 return rc;
1048}
1049
1050static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
1051 u32 lvl, void *context, void **rv)
1052{
1053 char name[ACPI_PATH_SEGMENT_LENGTH];
1054 struct acpi_buffer buffer = { sizeof(name), &name };
1055
1056 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
1057 !strncmp("_BCL", name, sizeof(name) - 1)) {
1058 BUG_ON(!rv || !*rv);
1059 **(int **)rv = tpacpi_query_bcl_levels(handle);
1060 return AE_CTRL_TERMINATE;
1061 } else {
1062 return AE_OK;
1063 }
1064}
1065
1066/*
1067 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
1068 */
1069static int __init tpacpi_check_std_acpi_brightness_support(void)
1070{
1071 int status;
1072 int bcl_levels = 0;
1073 void *bcl_ptr = &bcl_levels;
1074
1075 if (!vid_handle) {
1076 TPACPI_ACPIHANDLE_INIT(vid);
1077 }
1078 if (!vid_handle)
1079 return 0;
1080
1081 /*
1082 * Search for a _BCL method, and execute it. This is safe on all
1083 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
1084 * BIOS in ACPI backlight control mode. We do NOT have to care
1085 * about calling the _BCL method in an enabled video device, any
1086 * will do for our purposes.
1087 */
1088
1089 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
1090 tpacpi_acpi_walk_find_bcl, NULL,
1091 &bcl_ptr);
1092
1093 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
1094 tp_features.bright_acpimode = 1;
1095 return (bcl_levels - 2);
1096 }
1097
1098 return 0;
1099}
1100
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00001101static void printk_deprecated_attribute(const char * const what,
1102 const char * const details)
1103{
1104 tpacpi_log_usertask("deprecated sysfs attribute");
1105 printk(TPACPI_WARN "WARNING: sysfs attribute %s is deprecated and "
1106 "will be removed. %s\n",
1107 what, details);
1108}
1109
Johannes Berg19d337d2009-06-02 13:01:37 +02001110/*************************************************************************
1111 * rfkill and radio control support helpers
1112 */
1113
1114/*
1115 * ThinkPad-ACPI firmware handling model:
1116 *
1117 * WLSW (master wireless switch) is event-driven, and is common to all
1118 * firmware-controlled radios. It cannot be controlled, just monitored,
1119 * as expected. It overrides all radio state in firmware
1120 *
1121 * The kernel, a masked-off hotkey, and WLSW can change the radio state
1122 * (TODO: verify how WLSW interacts with the returned radio state).
1123 *
1124 * The only time there are shadow radio state changes, is when
1125 * masked-off hotkeys are used.
1126 */
1127
1128/*
1129 * Internal driver API for radio state:
1130 *
1131 * int: < 0 = error, otherwise enum tpacpi_rfkill_state
1132 * bool: true means radio blocked (off)
1133 */
1134enum tpacpi_rfkill_state {
1135 TPACPI_RFK_RADIO_OFF = 0,
1136 TPACPI_RFK_RADIO_ON
1137};
1138
1139/* rfkill switches */
1140enum tpacpi_rfk_id {
1141 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
1142 TPACPI_RFK_WWAN_SW_ID,
1143 TPACPI_RFK_UWB_SW_ID,
1144 TPACPI_RFK_SW_MAX
1145};
1146
1147static const char *tpacpi_rfkill_names[] = {
1148 [TPACPI_RFK_BLUETOOTH_SW_ID] = "bluetooth",
1149 [TPACPI_RFK_WWAN_SW_ID] = "wwan",
1150 [TPACPI_RFK_UWB_SW_ID] = "uwb",
1151 [TPACPI_RFK_SW_MAX] = NULL
1152};
1153
1154/* ThinkPad-ACPI rfkill subdriver */
1155struct tpacpi_rfk {
1156 struct rfkill *rfkill;
1157 enum tpacpi_rfk_id id;
1158 const struct tpacpi_rfk_ops *ops;
1159};
1160
1161struct tpacpi_rfk_ops {
1162 /* firmware interface */
1163 int (*get_status)(void);
1164 int (*set_status)(const enum tpacpi_rfkill_state);
1165};
1166
1167static struct tpacpi_rfk *tpacpi_rfkill_switches[TPACPI_RFK_SW_MAX];
1168
1169/* Query FW and update rfkill sw state for a given rfkill switch */
1170static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk *tp_rfk)
1171{
1172 int status;
1173
1174 if (!tp_rfk)
1175 return -ENODEV;
1176
1177 status = (tp_rfk->ops->get_status)();
1178 if (status < 0)
1179 return status;
1180
1181 rfkill_set_sw_state(tp_rfk->rfkill,
1182 (status == TPACPI_RFK_RADIO_OFF));
1183
1184 return status;
1185}
1186
1187/* Query FW and update rfkill sw state for all rfkill switches */
1188static void tpacpi_rfk_update_swstate_all(void)
1189{
1190 unsigned int i;
1191
1192 for (i = 0; i < TPACPI_RFK_SW_MAX; i++)
1193 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[i]);
1194}
1195
1196/*
1197 * Sync the HW-blocking state of all rfkill switches,
1198 * do notice it causes the rfkill core to schedule uevents
1199 */
1200static void tpacpi_rfk_update_hwblock_state(bool blocked)
1201{
1202 unsigned int i;
1203 struct tpacpi_rfk *tp_rfk;
1204
1205 for (i = 0; i < TPACPI_RFK_SW_MAX; i++) {
1206 tp_rfk = tpacpi_rfkill_switches[i];
1207 if (tp_rfk) {
1208 if (rfkill_set_hw_state(tp_rfk->rfkill,
1209 blocked)) {
1210 /* ignore -- we track sw block */
1211 }
1212 }
1213 }
1214}
1215
1216/* Call to get the WLSW state from the firmware */
1217static int hotkey_get_wlsw(void);
1218
1219/* Call to query WLSW state and update all rfkill switches */
1220static bool tpacpi_rfk_check_hwblock_state(void)
1221{
1222 int res = hotkey_get_wlsw();
1223 int hw_blocked;
1224
1225 /* When unknown or unsupported, we have to assume it is unblocked */
1226 if (res < 0)
1227 return false;
1228
1229 hw_blocked = (res == TPACPI_RFK_RADIO_OFF);
1230 tpacpi_rfk_update_hwblock_state(hw_blocked);
1231
1232 return hw_blocked;
1233}
1234
1235static int tpacpi_rfk_hook_set_block(void *data, bool blocked)
1236{
1237 struct tpacpi_rfk *tp_rfk = data;
1238 int res;
1239
1240 dbg_printk(TPACPI_DBG_RFKILL,
1241 "request to change radio state to %s\n",
1242 blocked ? "blocked" : "unblocked");
1243
1244 /* try to set radio state */
1245 res = (tp_rfk->ops->set_status)(blocked ?
1246 TPACPI_RFK_RADIO_OFF : TPACPI_RFK_RADIO_ON);
1247
1248 /* and update the rfkill core with whatever the FW really did */
1249 tpacpi_rfk_update_swstate(tp_rfk);
1250
1251 return (res < 0) ? res : 0;
1252}
1253
1254static const struct rfkill_ops tpacpi_rfk_rfkill_ops = {
1255 .set_block = tpacpi_rfk_hook_set_block,
1256};
1257
1258static int __init tpacpi_new_rfkill(const enum tpacpi_rfk_id id,
1259 const struct tpacpi_rfk_ops *tp_rfkops,
1260 const enum rfkill_type rfktype,
1261 const char *name,
1262 const bool set_default)
1263{
1264 struct tpacpi_rfk *atp_rfk;
1265 int res;
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001266 bool sw_state = false;
1267 int sw_status;
Johannes Berg19d337d2009-06-02 13:01:37 +02001268
1269 BUG_ON(id >= TPACPI_RFK_SW_MAX || tpacpi_rfkill_switches[id]);
1270
Johannes Berg19d337d2009-06-02 13:01:37 +02001271 atp_rfk = kzalloc(sizeof(struct tpacpi_rfk), GFP_KERNEL);
1272 if (atp_rfk)
1273 atp_rfk->rfkill = rfkill_alloc(name,
1274 &tpacpi_pdev->dev,
1275 rfktype,
1276 &tpacpi_rfk_rfkill_ops,
1277 atp_rfk);
1278 if (!atp_rfk || !atp_rfk->rfkill) {
1279 printk(TPACPI_ERR
1280 "failed to allocate memory for rfkill class\n");
1281 kfree(atp_rfk);
1282 return -ENOMEM;
1283 }
1284
1285 atp_rfk->id = id;
1286 atp_rfk->ops = tp_rfkops;
1287
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001288 sw_status = (tp_rfkops->get_status)();
1289 if (sw_status < 0) {
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001290 printk(TPACPI_ERR
1291 "failed to read initial state for %s, error %d\n",
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001292 name, sw_status);
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001293 } else {
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001294 sw_state = (sw_status == TPACPI_RFK_RADIO_OFF);
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001295 if (set_default) {
1296 /* try to keep the initial state, since we ask the
1297 * firmware to preserve it across S5 in NVRAM */
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001298 rfkill_init_sw_state(atp_rfk->rfkill, sw_state);
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001299 }
1300 }
1301 rfkill_set_hw_state(atp_rfk->rfkill, tpacpi_rfk_check_hwblock_state());
Johannes Berg19d337d2009-06-02 13:01:37 +02001302
1303 res = rfkill_register(atp_rfk->rfkill);
1304 if (res < 0) {
1305 printk(TPACPI_ERR
1306 "failed to register %s rfkill switch: %d\n",
1307 name, res);
1308 rfkill_destroy(atp_rfk->rfkill);
1309 kfree(atp_rfk);
1310 return res;
1311 }
1312
1313 tpacpi_rfkill_switches[id] = atp_rfk;
1314 return 0;
1315}
1316
1317static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id)
1318{
1319 struct tpacpi_rfk *tp_rfk;
1320
1321 BUG_ON(id >= TPACPI_RFK_SW_MAX);
1322
1323 tp_rfk = tpacpi_rfkill_switches[id];
1324 if (tp_rfk) {
1325 rfkill_unregister(tp_rfk->rfkill);
Corentin Chary5f0dadb2009-09-14 12:43:52 +02001326 rfkill_destroy(tp_rfk->rfkill);
Johannes Berg19d337d2009-06-02 13:01:37 +02001327 tpacpi_rfkill_switches[id] = NULL;
1328 kfree(tp_rfk);
1329 }
1330}
1331
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00001332static void printk_deprecated_rfkill_attribute(const char * const what)
1333{
1334 printk_deprecated_attribute(what,
1335 "Please switch to generic rfkill before year 2010");
1336}
1337
Johannes Berg19d337d2009-06-02 13:01:37 +02001338/* sysfs <radio> enable ------------------------------------------------ */
1339static ssize_t tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id,
1340 struct device_attribute *attr,
1341 char *buf)
1342{
1343 int status;
1344
1345 printk_deprecated_rfkill_attribute(attr->attr.name);
1346
1347 /* This is in the ABI... */
1348 if (tpacpi_rfk_check_hwblock_state()) {
1349 status = TPACPI_RFK_RADIO_OFF;
1350 } else {
1351 status = tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1352 if (status < 0)
1353 return status;
1354 }
1355
1356 return snprintf(buf, PAGE_SIZE, "%d\n",
1357 (status == TPACPI_RFK_RADIO_ON) ? 1 : 0);
1358}
1359
1360static ssize_t tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id,
1361 struct device_attribute *attr,
1362 const char *buf, size_t count)
1363{
1364 unsigned long t;
1365 int res;
1366
1367 printk_deprecated_rfkill_attribute(attr->attr.name);
1368
1369 if (parse_strtoul(buf, 1, &t))
1370 return -EINVAL;
1371
1372 tpacpi_disclose_usertask(attr->attr.name, "set to %ld\n", t);
1373
1374 /* This is in the ABI... */
1375 if (tpacpi_rfk_check_hwblock_state() && !!t)
1376 return -EPERM;
1377
1378 res = tpacpi_rfkill_switches[id]->ops->set_status((!!t) ?
1379 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF);
1380 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1381
1382 return (res < 0) ? res : count;
1383}
1384
1385/* procfs -------------------------------------------------------------- */
1386static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id, char *p)
1387{
1388 int len = 0;
1389
1390 if (id >= TPACPI_RFK_SW_MAX)
1391 len += sprintf(p + len, "status:\t\tnot supported\n");
1392 else {
1393 int status;
1394
1395 /* This is in the ABI... */
1396 if (tpacpi_rfk_check_hwblock_state()) {
1397 status = TPACPI_RFK_RADIO_OFF;
1398 } else {
1399 status = tpacpi_rfk_update_swstate(
1400 tpacpi_rfkill_switches[id]);
1401 if (status < 0)
1402 return status;
1403 }
1404
1405 len += sprintf(p + len, "status:\t\t%s\n",
1406 (status == TPACPI_RFK_RADIO_ON) ?
1407 "enabled" : "disabled");
1408 len += sprintf(p + len, "commands:\tenable, disable\n");
1409 }
1410
1411 return len;
1412}
1413
1414static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id, char *buf)
1415{
1416 char *cmd;
1417 int status = -1;
1418 int res = 0;
1419
1420 if (id >= TPACPI_RFK_SW_MAX)
1421 return -ENODEV;
1422
1423 while ((cmd = next_cmd(&buf))) {
1424 if (strlencmp(cmd, "enable") == 0)
1425 status = TPACPI_RFK_RADIO_ON;
1426 else if (strlencmp(cmd, "disable") == 0)
1427 status = TPACPI_RFK_RADIO_OFF;
1428 else
1429 return -EINVAL;
1430 }
1431
1432 if (status != -1) {
1433 tpacpi_disclose_usertask("procfs", "attempt to %s %s\n",
1434 (status == TPACPI_RFK_RADIO_ON) ?
1435 "enable" : "disable",
1436 tpacpi_rfkill_names[id]);
1437 res = (tpacpi_rfkill_switches[id]->ops->set_status)(status);
1438 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1439 }
1440
1441 return res;
1442}
1443
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001444/*************************************************************************
1445 * thinkpad-acpi driver attributes
1446 */
1447
1448/* interface_version --------------------------------------------------- */
1449static ssize_t tpacpi_driver_interface_version_show(
1450 struct device_driver *drv,
1451 char *buf)
1452{
1453 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1454}
1455
1456static DRIVER_ATTR(interface_version, S_IRUGO,
1457 tpacpi_driver_interface_version_show, NULL);
1458
1459/* debug_level --------------------------------------------------------- */
1460static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1461 char *buf)
1462{
1463 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1464}
1465
1466static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1467 const char *buf, size_t count)
1468{
1469 unsigned long t;
1470
1471 if (parse_strtoul(buf, 0xffff, &t))
1472 return -EINVAL;
1473
1474 dbg_level = t;
1475
1476 return count;
1477}
1478
1479static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1480 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1481
1482/* version ------------------------------------------------------------- */
1483static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1484 char *buf)
1485{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001486 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1487 TPACPI_DESC, TPACPI_VERSION);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001488}
1489
1490static DRIVER_ATTR(version, S_IRUGO,
1491 tpacpi_driver_version_show, NULL);
1492
1493/* --------------------------------------------------------------------- */
1494
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001495#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1496
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001497/* wlsw_emulstate ------------------------------------------------------ */
1498static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1499 char *buf)
1500{
1501 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1502}
1503
1504static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1505 const char *buf, size_t count)
1506{
1507 unsigned long t;
1508
1509 if (parse_strtoul(buf, 1, &t))
1510 return -EINVAL;
1511
Johannes Berg19d337d2009-06-02 13:01:37 +02001512 if (tpacpi_wlsw_emulstate != !!t) {
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001513 tpacpi_wlsw_emulstate = !!t;
Johannes Berg19d337d2009-06-02 13:01:37 +02001514 tpacpi_rfk_update_hwblock_state(!t); /* negative logic */
1515 }
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001516
1517 return count;
1518}
1519
1520static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1521 tpacpi_driver_wlsw_emulstate_show,
1522 tpacpi_driver_wlsw_emulstate_store);
1523
1524/* bluetooth_emulstate ------------------------------------------------- */
1525static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1526 struct device_driver *drv,
1527 char *buf)
1528{
1529 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1530}
1531
1532static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1533 struct device_driver *drv,
1534 const char *buf, size_t count)
1535{
1536 unsigned long t;
1537
1538 if (parse_strtoul(buf, 1, &t))
1539 return -EINVAL;
1540
1541 tpacpi_bluetooth_emulstate = !!t;
1542
1543 return count;
1544}
1545
1546static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1547 tpacpi_driver_bluetooth_emulstate_show,
1548 tpacpi_driver_bluetooth_emulstate_store);
1549
1550/* wwan_emulstate ------------------------------------------------- */
1551static ssize_t tpacpi_driver_wwan_emulstate_show(
1552 struct device_driver *drv,
1553 char *buf)
1554{
1555 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1556}
1557
1558static ssize_t tpacpi_driver_wwan_emulstate_store(
1559 struct device_driver *drv,
1560 const char *buf, size_t count)
1561{
1562 unsigned long t;
1563
1564 if (parse_strtoul(buf, 1, &t))
1565 return -EINVAL;
1566
1567 tpacpi_wwan_emulstate = !!t;
1568
1569 return count;
1570}
1571
1572static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1573 tpacpi_driver_wwan_emulstate_show,
1574 tpacpi_driver_wwan_emulstate_store);
1575
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001576/* uwb_emulstate ------------------------------------------------- */
1577static ssize_t tpacpi_driver_uwb_emulstate_show(
1578 struct device_driver *drv,
1579 char *buf)
1580{
1581 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1582}
1583
1584static ssize_t tpacpi_driver_uwb_emulstate_store(
1585 struct device_driver *drv,
1586 const char *buf, size_t count)
1587{
1588 unsigned long t;
1589
1590 if (parse_strtoul(buf, 1, &t))
1591 return -EINVAL;
1592
1593 tpacpi_uwb_emulstate = !!t;
1594
1595 return count;
1596}
1597
1598static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
1599 tpacpi_driver_uwb_emulstate_show,
1600 tpacpi_driver_uwb_emulstate_store);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001601#endif
1602
1603/* --------------------------------------------------------------------- */
1604
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001605static struct driver_attribute *tpacpi_driver_attributes[] = {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001606 &driver_attr_debug_level, &driver_attr_version,
1607 &driver_attr_interface_version,
1608};
1609
1610static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1611{
1612 int i, res;
1613
1614 i = 0;
1615 res = 0;
1616 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1617 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1618 i++;
1619 }
1620
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001621#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1622 if (!res && dbg_wlswemul)
1623 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1624 if (!res && dbg_bluetoothemul)
1625 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1626 if (!res && dbg_wwanemul)
1627 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001628 if (!res && dbg_uwbemul)
1629 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001630#endif
1631
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001632 return res;
1633}
1634
1635static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1636{
1637 int i;
1638
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001639 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001640 driver_remove_file(drv, tpacpi_driver_attributes[i]);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001641
1642#ifdef THINKPAD_ACPI_DEBUGFACILITIES
1643 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1644 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1645 driver_remove_file(drv, &driver_attr_wwan_emulstate);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001646 driver_remove_file(drv, &driver_attr_uwb_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001647#endif
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001648}
1649
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001650/*************************************************************************
1651 * Firmware Data
1652 */
1653
1654/*
1655 * Table of recommended minimum BIOS versions
1656 *
1657 * Reasons for listing:
1658 * 1. Stable BIOS, listed because the unknown ammount of
1659 * bugs and bad ACPI behaviour on older versions
1660 *
1661 * 2. BIOS or EC fw with known bugs that trigger on Linux
1662 *
1663 * 3. BIOS with known reduced functionality in older versions
1664 *
1665 * We recommend the latest BIOS and EC version.
1666 * We only support the latest BIOS and EC fw version as a rule.
1667 *
1668 * Sources: IBM ThinkPad Public Web Documents (update changelogs),
1669 * Information from users in ThinkWiki
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03001670 *
1671 * WARNING: we use this table also to detect that the machine is
1672 * a ThinkPad in some cases, so don't remove entries lightly.
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001673 */
1674
1675#define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1676 { .vendor = (__v), \
1677 .bios = TPID(__id1, __id2), \
1678 .ec = TPACPI_MATCH_ANY, \
1679 .quirks = TPACPI_MATCH_ANY << 16 \
1680 | (__bv1) << 8 | (__bv2) }
1681
1682#define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001683 __eid, __ev1, __ev2) \
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001684 { .vendor = (__v), \
1685 .bios = TPID(__bid1, __bid2), \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001686 .ec = __eid, \
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001687 .quirks = (__ev1) << 24 | (__ev2) << 16 \
1688 | (__bv1) << 8 | (__bv2) }
1689
1690#define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1691 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1692
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001693/* Outdated IBM BIOSes often lack the EC id string */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001694#define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1695 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001696 __bv1, __bv2, TPID(__id1, __id2), \
1697 __ev1, __ev2), \
1698 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1699 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1700 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001701
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001702/* Outdated IBM BIOSes often lack the EC id string */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001703#define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1704 __eid1, __eid2, __ev1, __ev2) \
1705 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001706 __bv1, __bv2, TPID(__eid1, __eid2), \
1707 __ev1, __ev2), \
1708 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1709 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1710 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001711
1712#define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1713 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1714
1715#define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1716 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001717 __bv1, __bv2, TPID(__id1, __id2), \
1718 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001719
1720#define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1721 __eid1, __eid2, __ev1, __ev2) \
1722 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001723 __bv1, __bv2, TPID(__eid1, __eid2), \
1724 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001725
1726static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
1727 /* Numeric models ------------------ */
1728 /* FW MODEL BIOS VERS */
1729 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1730 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1731 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1732 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1733 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1734 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1735 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1736 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1737 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1738
1739 /* A-series ------------------------- */
1740 /* FW MODEL BIOS VERS EC VERS */
1741 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1742 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1743 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1744 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1745 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1746 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1747 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1748 TPV_QI0('1', '3', '2', '0'), /* A22m */
1749 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1750 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1751 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1752
1753 /* G-series ------------------------- */
1754 /* FW MODEL BIOS VERS */
1755 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1756 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1757
1758 /* R-series, T-series --------------- */
1759 /* FW MODEL BIOS VERS EC VERS */
1760 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1761 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1762 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1763 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1764 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1765 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1766 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1767 T40/p, T41/p, T42/p (1) */
1768 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1769 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1770 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1771 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1772
1773 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1774 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1775 TPV_QI0('1', '6', '3', '2'), /* T22 */
1776 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1777 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1778 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1779
1780 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1781 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
Henrique de Moraes Holschuh90765c62009-12-09 01:36:23 +00001782 TPV_QL1('7', 'E', 'D', '0', '1', '5'), /* R60e, R60i */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001783
1784 /* BIOS FW BIOS VERS EC FW EC VERS */
1785 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1786 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1787
1788 /* X-series ------------------------- */
1789 /* FW MODEL BIOS VERS EC VERS */
1790 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1791 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1792 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1793 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1794 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1795 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1796 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1797
Henrique de Moraes Holschuh90765c62009-12-09 01:36:23 +00001798 TPV_QL1('7', 'B', 'D', '7', '4', '0'), /* X60/s */
1799 TPV_QL1('7', 'J', '3', '0', '1', '3'), /* X60t */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001800
1801 /* (0) - older versions lack DMI EC fw string and functionality */
1802 /* (1) - older versions known to lack functionality */
1803};
1804
1805#undef TPV_QL1
1806#undef TPV_QL0
1807#undef TPV_QI2
1808#undef TPV_QI1
1809#undef TPV_QI0
1810#undef TPV_Q_X
1811#undef TPV_Q
1812
1813static void __init tpacpi_check_outdated_fw(void)
1814{
1815 unsigned long fwvers;
1816 u16 ec_version, bios_version;
1817
1818 fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
1819 ARRAY_SIZE(tpacpi_bios_version_qtable));
1820
1821 if (!fwvers)
1822 return;
1823
1824 bios_version = fwvers & 0xffffU;
1825 ec_version = (fwvers >> 16) & 0xffffU;
1826
1827 /* note that unknown versions are set to 0x0000 and we use that */
1828 if ((bios_version > thinkpad_id.bios_release) ||
1829 (ec_version > thinkpad_id.ec_release &&
1830 ec_version != TPACPI_MATCH_ANY)) {
1831 /*
1832 * The changelogs would let us track down the exact
1833 * reason, but it is just too much of a pain to track
1834 * it. We only list BIOSes that are either really
1835 * broken, or really stable to begin with, so it is
1836 * best if the user upgrades the firmware anyway.
1837 */
1838 printk(TPACPI_WARN
1839 "WARNING: Outdated ThinkPad BIOS/EC firmware\n");
1840 printk(TPACPI_WARN
1841 "WARNING: This firmware may be missing critical bug "
1842 "fixes and/or important features\n");
1843 }
1844}
1845
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03001846static bool __init tpacpi_is_fw_known(void)
1847{
1848 return tpacpi_check_quirks(tpacpi_bios_version_qtable,
1849 ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
1850}
1851
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03001852/****************************************************************************
1853 ****************************************************************************
1854 *
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001855 * Subdrivers
1856 *
1857 ****************************************************************************
1858 ****************************************************************************/
1859
1860/*************************************************************************
Henrique de Moraes Holschuh142cfc92007-04-21 11:08:26 -03001861 * thinkpad-acpi init subdriver
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001862 */
1863
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001864static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001866 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
1867 printk(TPACPI_INFO "%s\n", TPACPI_URL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001869 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03001870 (thinkpad_id.bios_version_str) ?
1871 thinkpad_id.bios_version_str : "unknown",
1872 (thinkpad_id.ec_version_str) ?
1873 thinkpad_id.ec_version_str : "unknown");
1874
1875 if (thinkpad_id.vendor && thinkpad_id.model_str)
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03001876 printk(TPACPI_INFO "%s %s, model %s\n",
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03001877 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
1878 "IBM" : ((thinkpad_id.vendor ==
1879 PCI_VENDOR_ID_LENOVO) ?
1880 "Lenovo" : "Unknown vendor"),
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03001881 thinkpad_id.model_str,
1882 (thinkpad_id.nummodel_str) ?
1883 thinkpad_id.nummodel_str : "unknown");
Henrique de Moraes Holschuh3945ac32007-02-06 19:13:44 -02001884
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001885 tpacpi_check_outdated_fw();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 return 0;
1887}
1888
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -03001889static int thinkpad_acpi_driver_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890{
1891 int len = 0;
1892
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001893 len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
1894 len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895
1896 return len;
1897}
1898
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001899static struct ibm_struct thinkpad_acpi_driver_data = {
1900 .name = "driver",
1901 .read = thinkpad_acpi_driver_read,
1902};
1903
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001904/*************************************************************************
1905 * Hotkey subdriver
1906 */
1907
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001908/*
1909 * ThinkPad firmware event model
1910 *
1911 * The ThinkPad firmware has two main event interfaces: normal ACPI
1912 * notifications (which follow the ACPI standard), and a private event
1913 * interface.
1914 *
1915 * The private event interface also issues events for the hotkeys. As
1916 * the driver gained features, the event handling code ended up being
1917 * built around the hotkey subdriver. This will need to be refactored
1918 * to a more formal event API eventually.
1919 *
1920 * Some "hotkeys" are actually supposed to be used as event reports,
1921 * such as "brightness has changed", "volume has changed", depending on
1922 * the ThinkPad model and how the firmware is operating.
1923 *
1924 * Unlike other classes, hotkey-class events have mask/unmask control on
1925 * non-ancient firmware. However, how it behaves changes a lot with the
1926 * firmware model and version.
1927 */
1928
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001929enum { /* hot key scan codes (derived from ACPI DSDT) */
1930 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1931 TP_ACPI_HOTKEYSCAN_FNF2,
1932 TP_ACPI_HOTKEYSCAN_FNF3,
1933 TP_ACPI_HOTKEYSCAN_FNF4,
1934 TP_ACPI_HOTKEYSCAN_FNF5,
1935 TP_ACPI_HOTKEYSCAN_FNF6,
1936 TP_ACPI_HOTKEYSCAN_FNF7,
1937 TP_ACPI_HOTKEYSCAN_FNF8,
1938 TP_ACPI_HOTKEYSCAN_FNF9,
1939 TP_ACPI_HOTKEYSCAN_FNF10,
1940 TP_ACPI_HOTKEYSCAN_FNF11,
1941 TP_ACPI_HOTKEYSCAN_FNF12,
1942 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1943 TP_ACPI_HOTKEYSCAN_FNINSERT,
1944 TP_ACPI_HOTKEYSCAN_FNDELETE,
1945 TP_ACPI_HOTKEYSCAN_FNHOME,
1946 TP_ACPI_HOTKEYSCAN_FNEND,
1947 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1948 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1949 TP_ACPI_HOTKEYSCAN_FNSPACE,
1950 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1951 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1952 TP_ACPI_HOTKEYSCAN_MUTE,
1953 TP_ACPI_HOTKEYSCAN_THINKPAD,
1954};
1955
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001956enum { /* Keys/events available through NVRAM polling */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001957 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1958 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1959};
1960
1961enum { /* Positions of some of the keys in hotkey masks */
1962 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1963 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1964 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1965 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1966 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1967 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1968 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1969 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1970 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1971 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1972 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1973};
1974
1975enum { /* NVRAM to ACPI HKEY group map */
1976 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1977 TP_ACPI_HKEY_ZOOM_MASK |
1978 TP_ACPI_HKEY_DISPSWTCH_MASK |
1979 TP_ACPI_HKEY_HIBERNATE_MASK,
1980 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1981 TP_ACPI_HKEY_BRGHTDWN_MASK,
1982 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1983 TP_ACPI_HKEY_VOLDWN_MASK |
1984 TP_ACPI_HKEY_MUTE_MASK,
1985};
1986
1987#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1988struct tp_nvram_state {
1989 u16 thinkpad_toggle:1;
1990 u16 zoom_toggle:1;
1991 u16 display_toggle:1;
1992 u16 thinklight_toggle:1;
1993 u16 hibernate_toggle:1;
1994 u16 displayexp_toggle:1;
1995 u16 display_state:1;
1996 u16 brightness_toggle:1;
1997 u16 volume_toggle:1;
1998 u16 mute:1;
1999
2000 u8 brightness_level;
2001 u8 volume_level;
2002};
2003
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002004/* kthread for the hotkey poller */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002005static struct task_struct *tpacpi_hotkey_task;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002006
2007/* Acquired while the poller kthread is running, use to sync start/stop */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002008static struct mutex hotkey_thread_mutex;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002009
2010/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002011 * Acquire mutex to write poller control variables as an
2012 * atomic block.
2013 *
2014 * Increment hotkey_config_change when changing them if you
2015 * want the kthread to forget old state.
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002016 *
2017 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
2018 */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002019static struct mutex hotkey_thread_data_mutex;
2020static unsigned int hotkey_config_change;
2021
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002022/*
2023 * hotkey poller control variables
2024 *
2025 * Must be atomic or readers will also need to acquire mutex
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002026 *
2027 * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
2028 * should be used only when the changes need to be taken as
2029 * a block, OR when one needs to force the kthread to forget
2030 * old state.
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002031 */
2032static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
2033static unsigned int hotkey_poll_freq = 10; /* Hz */
2034
2035#define HOTKEY_CONFIG_CRITICAL_START \
2036 do { \
2037 mutex_lock(&hotkey_thread_data_mutex); \
2038 hotkey_config_change++; \
2039 } while (0);
2040#define HOTKEY_CONFIG_CRITICAL_END \
2041 mutex_unlock(&hotkey_thread_data_mutex);
2042
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002043#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2044
2045#define hotkey_source_mask 0U
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002046#define HOTKEY_CONFIG_CRITICAL_START
2047#define HOTKEY_CONFIG_CRITICAL_END
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002048
2049#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2050
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002051static struct mutex hotkey_mutex;
2052
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002053static enum { /* Reasons for waking up */
2054 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
2055 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
2056 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
2057} hotkey_wakeup_reason;
2058
2059static int hotkey_autosleep_ack;
2060
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002061static u32 hotkey_orig_mask; /* events the BIOS had enabled */
2062static u32 hotkey_all_mask; /* all events supported in fw */
2063static u32 hotkey_reserved_mask; /* events better left disabled */
2064static u32 hotkey_driver_mask; /* events needed by the driver */
2065static u32 hotkey_user_mask; /* events visible to userspace */
2066static u32 hotkey_acpi_mask; /* events enabled in firmware */
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002067
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02002068static unsigned int hotkey_report_mode;
2069
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002070static u16 *hotkey_keycode_map;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002071
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03002072static struct attribute_set *hotkey_dev_attributes;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002073
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002074static void tpacpi_driver_event(const unsigned int hkey_event);
2075static void hotkey_driver_event(const unsigned int scancode);
2076
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002077/* HKEY.MHKG() return bits */
2078#define TP_HOTKEY_TABLET_MASK (1 << 3)
2079
Johannes Berg19d337d2009-06-02 13:01:37 +02002080static int hotkey_get_wlsw(void)
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002081{
Johannes Berg19d337d2009-06-02 13:01:37 +02002082 int status;
2083
2084 if (!tp_features.hotkey_wlsw)
2085 return -ENODEV;
2086
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02002087#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
Johannes Berg19d337d2009-06-02 13:01:37 +02002088 if (dbg_wlswemul)
2089 return (tpacpi_wlsw_emulstate) ?
2090 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02002091#endif
Johannes Berg19d337d2009-06-02 13:01:37 +02002092
2093 if (!acpi_evalf(hkey_handle, &status, "WLSW", "d"))
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002094 return -EIO;
Johannes Berg19d337d2009-06-02 13:01:37 +02002095
2096 return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002097}
2098
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002099static int hotkey_get_tablet_mode(int *status)
2100{
2101 int s;
2102
2103 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
2104 return -EIO;
2105
Henrique de Moraes Holschuhcee47f52008-03-04 14:29:21 -08002106 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
2107 return 0;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002108}
2109
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002110/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002111 * Reads current event mask from firmware, and updates
2112 * hotkey_acpi_mask accordingly. Also resets any bits
2113 * from hotkey_user_mask that are unavailable to be
2114 * delivered (shadow requirement of the userspace ABI).
2115 *
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002116 * Call with hotkey_mutex held
2117 */
2118static int hotkey_mask_get(void)
2119{
2120 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002121 u32 m = 0;
2122
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002123 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002124 return -EIO;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002125
2126 hotkey_acpi_mask = m;
2127 } else {
2128 /* no mask support doesn't mean no event support... */
2129 hotkey_acpi_mask = hotkey_all_mask;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002130 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002131
2132 /* sync userspace-visible mask */
2133 hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002134
2135 return 0;
2136}
2137
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002138void static hotkey_mask_warn_incomplete_mask(void)
2139{
2140 /* log only what the user can fix... */
2141 const u32 wantedmask = hotkey_driver_mask &
2142 ~(hotkey_acpi_mask | hotkey_source_mask) &
2143 (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
2144
2145 if (wantedmask)
2146 printk(TPACPI_NOTICE
2147 "required events 0x%08x not enabled!\n",
2148 wantedmask);
2149}
2150
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002151/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002152 * Set the firmware mask when supported
2153 *
2154 * Also calls hotkey_mask_get to update hotkey_acpi_mask.
2155 *
2156 * NOTE: does not set bits in hotkey_user_mask, but may reset them.
2157 *
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002158 * Call with hotkey_mutex held
2159 */
2160static int hotkey_mask_set(u32 mask)
2161{
2162 int i;
2163 int rc = 0;
2164
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002165 const u32 fwmask = mask & ~hotkey_source_mask;
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -03002166
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002167 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002168 for (i = 0; i < 32; i++) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002169 if (!acpi_evalf(hkey_handle,
2170 NULL, "MHKM", "vdd", i + 1,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002171 !!(mask & (1 << i)))) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002172 rc = -EIO;
2173 break;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002174 }
2175 }
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002176 }
2177
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002178 /*
2179 * We *must* make an inconditional call to hotkey_mask_get to
2180 * refresh hotkey_acpi_mask and update hotkey_user_mask
2181 *
2182 * Take the opportunity to also log when we cannot _enable_
2183 * a given event.
2184 */
2185 if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
2186 printk(TPACPI_NOTICE
2187 "asked for hotkey mask 0x%08x, but "
2188 "firmware forced it to 0x%08x\n",
2189 fwmask, hotkey_acpi_mask);
2190 }
2191
Henrique de Moraes Holschuh6b30eb72009-12-09 01:36:25 +00002192 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING)
2193 hotkey_mask_warn_incomplete_mask();
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002194
2195 return rc;
2196}
2197
2198/*
2199 * Sets hotkey_user_mask and tries to set the firmware mask
2200 *
2201 * Call with hotkey_mutex held
2202 */
2203static int hotkey_user_mask_set(const u32 mask)
2204{
2205 int rc;
2206
2207 /* Give people a chance to notice they are doing something that
2208 * is bound to go boom on their users sooner or later */
2209 if (!tp_warned.hotkey_mask_ff &&
2210 (mask == 0xffff || mask == 0xffffff ||
2211 mask == 0xffffffff)) {
2212 tp_warned.hotkey_mask_ff = 1;
2213 printk(TPACPI_NOTICE
2214 "setting the hotkey mask to 0x%08x is likely "
2215 "not the best way to go about it\n", mask);
2216 printk(TPACPI_NOTICE
2217 "please consider using the driver defaults, "
2218 "and refer to up-to-date thinkpad-acpi "
2219 "documentation\n");
2220 }
2221
2222 /* Try to enable what the user asked for, plus whatever we need.
2223 * this syncs everything but won't enable bits in hotkey_user_mask */
2224 rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
2225
2226 /* Enable the available bits in hotkey_user_mask */
2227 hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
2228
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002229 return rc;
2230}
2231
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002232/*
2233 * Sets the driver hotkey mask.
2234 *
2235 * Can be called even if the hotkey subdriver is inactive
2236 */
2237static int tpacpi_hotkey_driver_mask_set(const u32 mask)
2238{
2239 int rc;
2240
2241 /* Do the right thing if hotkey_init has not been called yet */
2242 if (!tp_features.hotkey) {
2243 hotkey_driver_mask = mask;
2244 return 0;
2245 }
2246
2247 mutex_lock(&hotkey_mutex);
2248
2249 HOTKEY_CONFIG_CRITICAL_START
2250 hotkey_driver_mask = mask;
Henrique de Moraes Holschuhb684a362009-09-26 21:42:49 -03002251#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002252 hotkey_source_mask |= (mask & ~hotkey_all_mask);
Henrique de Moraes Holschuhb684a362009-09-26 21:42:49 -03002253#endif
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002254 HOTKEY_CONFIG_CRITICAL_END
2255
2256 rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) &
2257 ~hotkey_source_mask);
2258 mutex_unlock(&hotkey_mutex);
2259
2260 return rc;
2261}
2262
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002263static int hotkey_status_get(int *status)
2264{
2265 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
2266 return -EIO;
2267
2268 return 0;
2269}
2270
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002271static int hotkey_status_set(bool enable)
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002272{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002273 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002274 return -EIO;
2275
2276 return 0;
2277}
2278
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002279static void tpacpi_input_send_tabletsw(void)
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002280{
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002281 int state;
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002282
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002283 if (tp_features.hotkey_tablet &&
2284 !hotkey_get_tablet_mode(&state)) {
2285 mutex_lock(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002286
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002287 input_report_switch(tpacpi_inputdev,
2288 SW_TABLET_MODE, !!state);
2289 input_sync(tpacpi_inputdev);
2290
2291 mutex_unlock(&tpacpi_inputdev_send_mutex);
2292 }
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002293}
2294
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002295/* Do NOT call without validating scancode first */
2296static void tpacpi_input_send_key(const unsigned int scancode)
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002297{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002298 const unsigned int keycode = hotkey_keycode_map[scancode];
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002299
2300 if (keycode != KEY_RESERVED) {
2301 mutex_lock(&tpacpi_inputdev_send_mutex);
2302
2303 input_report_key(tpacpi_inputdev, keycode, 1);
2304 if (keycode == KEY_UNKNOWN)
2305 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
2306 scancode);
2307 input_sync(tpacpi_inputdev);
2308
2309 input_report_key(tpacpi_inputdev, keycode, 0);
2310 if (keycode == KEY_UNKNOWN)
2311 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
2312 scancode);
2313 input_sync(tpacpi_inputdev);
2314
2315 mutex_unlock(&tpacpi_inputdev_send_mutex);
2316 }
2317}
2318
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002319/* Do NOT call without validating scancode first */
2320static void tpacpi_input_send_key_masked(const unsigned int scancode)
2321{
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002322 hotkey_driver_event(scancode);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002323 if (hotkey_user_mask & (1 << scancode))
2324 tpacpi_input_send_key(scancode);
2325}
2326
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002327#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2328static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
2329
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002330/* Do NOT call without validating scancode first */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002331static void tpacpi_hotkey_send_key(unsigned int scancode)
2332{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002333 tpacpi_input_send_key_masked(scancode);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002334 if (hotkey_report_mode < 2) {
2335 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03002336 0x80, TP_HKEY_EV_HOTKEY_BASE + scancode);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002337 }
2338}
2339
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002340static void hotkey_read_nvram(struct tp_nvram_state *n, const u32 m)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002341{
2342 u8 d;
2343
2344 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
2345 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
2346 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
2347 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
2348 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
2349 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
2350 }
2351 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
2352 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
2353 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
2354 }
2355 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
2356 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
2357 n->displayexp_toggle =
2358 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
2359 }
2360 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
2361 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
2362 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
2363 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
2364 n->brightness_toggle =
2365 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
2366 }
2367 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
2368 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
2369 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
2370 >> TP_NVRAM_POS_LEVEL_VOLUME;
2371 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2372 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2373 }
2374}
2375
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002376static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
2377 struct tp_nvram_state *newn,
2378 const u32 event_mask)
2379{
2380
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002381#define TPACPI_COMPARE_KEY(__scancode, __member) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002382 do { \
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002383 if ((event_mask & (1 << __scancode)) && \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002384 oldn->__member != newn->__member) \
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002385 tpacpi_hotkey_send_key(__scancode); \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002386 } while (0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002387
2388#define TPACPI_MAY_SEND_KEY(__scancode) \
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002389 do { \
2390 if (event_mask & (1 << __scancode)) \
2391 tpacpi_hotkey_send_key(__scancode); \
2392 } while (0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002393
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002394 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2395 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2396 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2397 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2398
2399 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2400
2401 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2402
2403 /* handle volume */
2404 if (oldn->volume_toggle != newn->volume_toggle) {
2405 if (oldn->mute != newn->mute) {
2406 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2407 }
2408 if (oldn->volume_level > newn->volume_level) {
2409 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2410 } else if (oldn->volume_level < newn->volume_level) {
2411 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2412 } else if (oldn->mute == newn->mute) {
2413 /* repeated key presses that didn't change state */
2414 if (newn->mute) {
2415 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2416 } else if (newn->volume_level != 0) {
2417 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2418 } else {
2419 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2420 }
2421 }
2422 }
2423
2424 /* handle brightness */
2425 if (oldn->brightness_toggle != newn->brightness_toggle) {
2426 if (oldn->brightness_level < newn->brightness_level) {
2427 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2428 } else if (oldn->brightness_level > newn->brightness_level) {
2429 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2430 } else {
2431 /* repeated key presses that didn't change state */
2432 if (newn->brightness_level != 0) {
2433 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2434 } else {
2435 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2436 }
2437 }
2438 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002439
2440#undef TPACPI_COMPARE_KEY
2441#undef TPACPI_MAY_SEND_KEY
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002442}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002443
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002444/*
2445 * Polling driver
2446 *
2447 * We track all events in hotkey_source_mask all the time, since
2448 * most of them are edge-based. We only issue those requested by
2449 * hotkey_user_mask or hotkey_driver_mask, though.
2450 */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002451static int hotkey_kthread(void *data)
2452{
2453 struct tp_nvram_state s[2];
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002454 u32 poll_mask, event_mask;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002455 unsigned int si, so;
2456 unsigned long t;
2457 unsigned int change_detector, must_reset;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002458 unsigned int poll_freq;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002459
2460 mutex_lock(&hotkey_thread_mutex);
2461
2462 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2463 goto exit;
2464
2465 set_freezable();
2466
2467 so = 0;
2468 si = 1;
2469 t = 0;
2470
2471 /* Initial state for compares */
2472 mutex_lock(&hotkey_thread_data_mutex);
2473 change_detector = hotkey_config_change;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002474 poll_mask = hotkey_source_mask;
2475 event_mask = hotkey_source_mask &
2476 (hotkey_driver_mask | hotkey_user_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002477 poll_freq = hotkey_poll_freq;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002478 mutex_unlock(&hotkey_thread_data_mutex);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002479 hotkey_read_nvram(&s[so], poll_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002480
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002481 while (!kthread_should_stop()) {
2482 if (t == 0) {
2483 if (likely(poll_freq))
2484 t = 1000/poll_freq;
2485 else
2486 t = 100; /* should never happen... */
2487 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002488 t = msleep_interruptible(t);
2489 if (unlikely(kthread_should_stop()))
2490 break;
2491 must_reset = try_to_freeze();
2492 if (t > 0 && !must_reset)
2493 continue;
2494
2495 mutex_lock(&hotkey_thread_data_mutex);
2496 if (must_reset || hotkey_config_change != change_detector) {
2497 /* forget old state on thaw or config change */
2498 si = so;
2499 t = 0;
2500 change_detector = hotkey_config_change;
2501 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002502 poll_mask = hotkey_source_mask;
2503 event_mask = hotkey_source_mask &
2504 (hotkey_driver_mask | hotkey_user_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002505 poll_freq = hotkey_poll_freq;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002506 mutex_unlock(&hotkey_thread_data_mutex);
2507
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002508 if (likely(poll_mask)) {
2509 hotkey_read_nvram(&s[si], poll_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002510 if (likely(si != so)) {
2511 hotkey_compare_and_issue_event(&s[so], &s[si],
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002512 event_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002513 }
2514 }
2515
2516 so = si;
2517 si ^= 1;
2518 }
2519
2520exit:
2521 mutex_unlock(&hotkey_thread_mutex);
2522 return 0;
2523}
2524
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002525/* call with hotkey_mutex held */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002526static void hotkey_poll_stop_sync(void)
2527{
2528 if (tpacpi_hotkey_task) {
2529 if (frozen(tpacpi_hotkey_task) ||
2530 freezing(tpacpi_hotkey_task))
2531 thaw_process(tpacpi_hotkey_task);
2532
2533 kthread_stop(tpacpi_hotkey_task);
2534 tpacpi_hotkey_task = NULL;
2535 mutex_lock(&hotkey_thread_mutex);
2536 /* at this point, the thread did exit */
2537 mutex_unlock(&hotkey_thread_mutex);
2538 }
2539}
2540
2541/* call with hotkey_mutex held */
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002542static void hotkey_poll_setup(bool may_warn)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002543{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002544 const u32 poll_driver_mask = hotkey_driver_mask & hotkey_source_mask;
2545 const u32 poll_user_mask = hotkey_user_mask & hotkey_source_mask;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002546
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002547 if (hotkey_poll_freq > 0 &&
2548 (poll_driver_mask ||
2549 (poll_user_mask && tpacpi_inputdev->users > 0))) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002550 if (!tpacpi_hotkey_task) {
2551 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03002552 NULL, TPACPI_NVRAM_KTHREAD_NAME);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002553 if (IS_ERR(tpacpi_hotkey_task)) {
2554 tpacpi_hotkey_task = NULL;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002555 printk(TPACPI_ERR
2556 "could not create kernel thread "
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002557 "for hotkey polling\n");
2558 }
2559 }
2560 } else {
2561 hotkey_poll_stop_sync();
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002562 if (may_warn && (poll_driver_mask || poll_user_mask) &&
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002563 hotkey_poll_freq == 0) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002564 printk(TPACPI_NOTICE
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002565 "hot keys 0x%08x and/or events 0x%08x "
2566 "require polling, which is currently "
2567 "disabled\n",
2568 poll_user_mask, poll_driver_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002569 }
2570 }
2571}
2572
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002573static void hotkey_poll_setup_safe(bool may_warn)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002574{
2575 mutex_lock(&hotkey_mutex);
2576 hotkey_poll_setup(may_warn);
2577 mutex_unlock(&hotkey_mutex);
2578}
2579
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002580/* call with hotkey_mutex held */
2581static void hotkey_poll_set_freq(unsigned int freq)
2582{
2583 if (!freq)
2584 hotkey_poll_stop_sync();
2585
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002586 hotkey_poll_freq = freq;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002587}
2588
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02002589#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2590
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002591static void hotkey_poll_setup_safe(bool __unused)
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02002592{
2593}
2594
2595#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2596
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002597static int hotkey_inputdev_open(struct input_dev *dev)
2598{
2599 switch (tpacpi_lifecycle) {
2600 case TPACPI_LIFE_INIT:
2601 /*
2602 * hotkey_init will call hotkey_poll_setup_safe
2603 * at the appropriate moment
2604 */
2605 return 0;
2606 case TPACPI_LIFE_EXITING:
2607 return -EBUSY;
2608 case TPACPI_LIFE_RUNNING:
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002609 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002610 return 0;
2611 }
2612
2613 /* Should only happen if tpacpi_lifecycle is corrupt */
2614 BUG();
2615 return -EBUSY;
2616}
2617
2618static void hotkey_inputdev_close(struct input_dev *dev)
2619{
2620 /* disable hotkey polling when possible */
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002621 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING &&
2622 !(hotkey_source_mask & hotkey_driver_mask))
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002623 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002624}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002625
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002626/* sysfs hotkey enable ------------------------------------------------- */
2627static ssize_t hotkey_enable_show(struct device *dev,
2628 struct device_attribute *attr,
2629 char *buf)
2630{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002631 int res, status;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002632
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002633 printk_deprecated_attribute("hotkey_enable",
2634 "Hotkey reporting is always enabled");
2635
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002636 res = hotkey_status_get(&status);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002637 if (res)
2638 return res;
2639
2640 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2641}
2642
2643static ssize_t hotkey_enable_store(struct device *dev,
2644 struct device_attribute *attr,
2645 const char *buf, size_t count)
2646{
2647 unsigned long t;
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002648
2649 printk_deprecated_attribute("hotkey_enable",
2650 "Hotkeys can be disabled through hotkey_mask");
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002651
2652 if (parse_strtoul(buf, 1, &t))
2653 return -EINVAL;
2654
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002655 if (t == 0)
2656 return -EPERM;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002657
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002658 return count;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002659}
2660
2661static struct device_attribute dev_attr_hotkey_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002662 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002663 hotkey_enable_show, hotkey_enable_store);
2664
2665/* sysfs hotkey mask --------------------------------------------------- */
2666static ssize_t hotkey_mask_show(struct device *dev,
2667 struct device_attribute *attr,
2668 char *buf)
2669{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002670 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_user_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002671}
2672
2673static ssize_t hotkey_mask_store(struct device *dev,
2674 struct device_attribute *attr,
2675 const char *buf, size_t count)
2676{
2677 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002678 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002679
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002680 if (parse_strtoul(buf, 0xffffffffUL, &t))
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002681 return -EINVAL;
2682
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002683 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002684 return -ERESTARTSYS;
2685
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002686 res = hotkey_user_mask_set(t);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002687
2688#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002689 hotkey_poll_setup(true);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002690#endif
2691
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002692 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002693
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002694 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2695
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002696 return (res) ? res : count;
2697}
2698
2699static struct device_attribute dev_attr_hotkey_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002700 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002701 hotkey_mask_show, hotkey_mask_store);
2702
2703/* sysfs hotkey bios_enabled ------------------------------------------- */
2704static ssize_t hotkey_bios_enabled_show(struct device *dev,
2705 struct device_attribute *attr,
2706 char *buf)
2707{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002708 return sprintf(buf, "0\n");
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002709}
2710
2711static struct device_attribute dev_attr_hotkey_bios_enabled =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002712 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002713
2714/* sysfs hotkey bios_mask ---------------------------------------------- */
2715static ssize_t hotkey_bios_mask_show(struct device *dev,
2716 struct device_attribute *attr,
2717 char *buf)
2718{
Henrique de Moraes Holschuh06777be2009-09-12 15:22:15 -03002719 printk_deprecated_attribute("hotkey_bios_mask",
2720 "This attribute is useless.");
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002721 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002722}
2723
2724static struct device_attribute dev_attr_hotkey_bios_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002725 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002726
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002727/* sysfs hotkey all_mask ----------------------------------------------- */
2728static ssize_t hotkey_all_mask_show(struct device *dev,
2729 struct device_attribute *attr,
2730 char *buf)
2731{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002732 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2733 hotkey_all_mask | hotkey_source_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002734}
2735
2736static struct device_attribute dev_attr_hotkey_all_mask =
2737 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
2738
2739/* sysfs hotkey recommended_mask --------------------------------------- */
2740static ssize_t hotkey_recommended_mask_show(struct device *dev,
2741 struct device_attribute *attr,
2742 char *buf)
2743{
2744 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002745 (hotkey_all_mask | hotkey_source_mask)
2746 & ~hotkey_reserved_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002747}
2748
2749static struct device_attribute dev_attr_hotkey_recommended_mask =
2750 __ATTR(hotkey_recommended_mask, S_IRUGO,
2751 hotkey_recommended_mask_show, NULL);
2752
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002753#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2754
2755/* sysfs hotkey hotkey_source_mask ------------------------------------- */
2756static ssize_t hotkey_source_mask_show(struct device *dev,
2757 struct device_attribute *attr,
2758 char *buf)
2759{
2760 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2761}
2762
2763static ssize_t hotkey_source_mask_store(struct device *dev,
2764 struct device_attribute *attr,
2765 const char *buf, size_t count)
2766{
2767 unsigned long t;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002768 u32 r_ev;
2769 int rc;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002770
2771 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2772 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2773 return -EINVAL;
2774
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002775 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002776 return -ERESTARTSYS;
2777
2778 HOTKEY_CONFIG_CRITICAL_START
2779 hotkey_source_mask = t;
2780 HOTKEY_CONFIG_CRITICAL_END
2781
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002782 rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
2783 ~hotkey_source_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002784 hotkey_poll_setup(true);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002785
2786 /* check if events needed by the driver got disabled */
2787 r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
2788 & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002789
2790 mutex_unlock(&hotkey_mutex);
2791
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002792 if (rc < 0)
2793 printk(TPACPI_ERR "hotkey_source_mask: failed to update the"
2794 "firmware event mask!\n");
2795
2796 if (r_ev)
2797 printk(TPACPI_NOTICE "hotkey_source_mask: "
2798 "some important events were disabled: "
2799 "0x%04x\n", r_ev);
2800
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002801 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2802
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002803 return (rc < 0) ? rc : count;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002804}
2805
2806static struct device_attribute dev_attr_hotkey_source_mask =
2807 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2808 hotkey_source_mask_show, hotkey_source_mask_store);
2809
2810/* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2811static ssize_t hotkey_poll_freq_show(struct device *dev,
2812 struct device_attribute *attr,
2813 char *buf)
2814{
2815 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2816}
2817
2818static ssize_t hotkey_poll_freq_store(struct device *dev,
2819 struct device_attribute *attr,
2820 const char *buf, size_t count)
2821{
2822 unsigned long t;
2823
2824 if (parse_strtoul(buf, 25, &t))
2825 return -EINVAL;
2826
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002827 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002828 return -ERESTARTSYS;
2829
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002830 hotkey_poll_set_freq(t);
2831 hotkey_poll_setup(true);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002832
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002833 mutex_unlock(&hotkey_mutex);
2834
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002835 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2836
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002837 return count;
2838}
2839
2840static struct device_attribute dev_attr_hotkey_poll_freq =
2841 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2842 hotkey_poll_freq_show, hotkey_poll_freq_store);
2843
2844#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2845
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002846/* sysfs hotkey radio_sw (pollable) ------------------------------------ */
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002847static ssize_t hotkey_radio_sw_show(struct device *dev,
2848 struct device_attribute *attr,
2849 char *buf)
2850{
Johannes Berg19d337d2009-06-02 13:01:37 +02002851 int res;
2852 res = hotkey_get_wlsw();
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002853 if (res < 0)
2854 return res;
2855
Johannes Berg19d337d2009-06-02 13:01:37 +02002856 /* Opportunistic update */
2857 tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
2858
2859 return snprintf(buf, PAGE_SIZE, "%d\n",
2860 (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002861}
2862
2863static struct device_attribute dev_attr_hotkey_radio_sw =
2864 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2865
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002866static void hotkey_radio_sw_notify_change(void)
2867{
2868 if (tp_features.hotkey_wlsw)
2869 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2870 "hotkey_radio_sw");
2871}
2872
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002873/* sysfs hotkey tablet mode (pollable) --------------------------------- */
2874static ssize_t hotkey_tablet_mode_show(struct device *dev,
2875 struct device_attribute *attr,
2876 char *buf)
2877{
2878 int res, s;
2879 res = hotkey_get_tablet_mode(&s);
2880 if (res < 0)
2881 return res;
2882
2883 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2884}
2885
2886static struct device_attribute dev_attr_hotkey_tablet_mode =
2887 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2888
2889static void hotkey_tablet_mode_notify_change(void)
2890{
2891 if (tp_features.hotkey_tablet)
2892 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2893 "hotkey_tablet_mode");
2894}
2895
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002896/* sysfs hotkey report_mode -------------------------------------------- */
2897static ssize_t hotkey_report_mode_show(struct device *dev,
2898 struct device_attribute *attr,
2899 char *buf)
2900{
2901 return snprintf(buf, PAGE_SIZE, "%d\n",
2902 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2903}
2904
2905static struct device_attribute dev_attr_hotkey_report_mode =
2906 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2907
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002908/* sysfs wakeup reason (pollable) -------------------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002909static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2910 struct device_attribute *attr,
2911 char *buf)
2912{
2913 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2914}
2915
2916static struct device_attribute dev_attr_hotkey_wakeup_reason =
2917 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2918
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02002919static void hotkey_wakeup_reason_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002920{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002921 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2922 "wakeup_reason");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002923}
2924
2925/* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002926static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2927 struct device_attribute *attr,
2928 char *buf)
2929{
2930 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2931}
2932
2933static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2934 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2935 hotkey_wakeup_hotunplug_complete_show, NULL);
2936
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02002937static void hotkey_wakeup_hotunplug_complete_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002938{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002939 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2940 "wakeup_hotunplug_complete");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002941}
2942
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002943/* --------------------------------------------------------------------- */
2944
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002945static struct attribute *hotkey_attributes[] __initdata = {
2946 &dev_attr_hotkey_enable.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002947 &dev_attr_hotkey_bios_enabled.attr,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002948 &dev_attr_hotkey_bios_mask.attr,
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002949 &dev_attr_hotkey_report_mode.attr,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002950 &dev_attr_hotkey_wakeup_reason.attr,
2951 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002952 &dev_attr_hotkey_mask.attr,
2953 &dev_attr_hotkey_all_mask.attr,
2954 &dev_attr_hotkey_recommended_mask.attr,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002955#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002956 &dev_attr_hotkey_source_mask.attr,
2957 &dev_attr_hotkey_poll_freq.attr,
2958#endif
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002959};
2960
Johannes Berg19d337d2009-06-02 13:01:37 +02002961/*
2962 * Sync both the hw and sw blocking state of all switches
2963 */
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002964static void tpacpi_send_radiosw_update(void)
2965{
2966 int wlsw;
2967
Johannes Berg19d337d2009-06-02 13:01:37 +02002968 /*
2969 * We must sync all rfkill controllers *before* issuing any
2970 * rfkill input events, or we will race the rfkill core input
2971 * handler.
2972 *
2973 * tpacpi_inputdev_send_mutex works as a syncronization point
2974 * for the above.
2975 *
2976 * We optimize to avoid numerous calls to hotkey_get_wlsw.
2977 */
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002978
Johannes Berg19d337d2009-06-02 13:01:37 +02002979 wlsw = hotkey_get_wlsw();
2980
2981 /* Sync hw blocking state first if it is hw-blocked */
2982 if (wlsw == TPACPI_RFK_RADIO_OFF)
2983 tpacpi_rfk_update_hwblock_state(true);
2984
2985 /* Sync sw blocking state */
2986 tpacpi_rfk_update_swstate_all();
2987
2988 /* Sync hw blocking state last if it is hw-unblocked */
2989 if (wlsw == TPACPI_RFK_RADIO_ON)
2990 tpacpi_rfk_update_hwblock_state(false);
2991
2992 /* Issue rfkill input event for WLSW switch */
2993 if (!(wlsw < 0)) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002994 mutex_lock(&tpacpi_inputdev_send_mutex);
2995
2996 input_report_switch(tpacpi_inputdev,
Johannes Berg19d337d2009-06-02 13:01:37 +02002997 SW_RFKILL_ALL, (wlsw > 0));
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002998 input_sync(tpacpi_inputdev);
2999
3000 mutex_unlock(&tpacpi_inputdev_send_mutex);
3001 }
Johannes Berg19d337d2009-06-02 13:01:37 +02003002
3003 /*
3004 * this can be unconditional, as we will poll state again
3005 * if userspace uses the notify to read data
3006 */
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003007 hotkey_radio_sw_notify_change();
3008}
3009
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003010static void hotkey_exit(void)
3011{
3012#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003013 mutex_lock(&hotkey_mutex);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003014 hotkey_poll_stop_sync();
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003015 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003016#endif
3017
3018 if (hotkey_dev_attributes)
3019 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3020
3021 kfree(hotkey_keycode_map);
3022
Henrique de Moraes Holschuh4be73002009-09-20 14:09:23 -03003023 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003024 "restoring original HKEY status and mask\n");
3025 /* yes, there is a bitwise or below, we want the
3026 * functions to be called even if one of them fail */
3027 if (((tp_features.hotkey_mask &&
3028 hotkey_mask_set(hotkey_orig_mask)) |
3029 hotkey_status_set(false)) != 0)
Henrique de Moraes Holschuh4be73002009-09-20 14:09:23 -03003030 printk(TPACPI_ERR
3031 "failed to restore hot key mask "
3032 "to BIOS defaults\n");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003033}
3034
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003035static void __init hotkey_unmap(const unsigned int scancode)
3036{
3037 if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
3038 clear_bit(hotkey_keycode_map[scancode],
3039 tpacpi_inputdev->keybit);
3040 hotkey_keycode_map[scancode] = KEY_RESERVED;
3041 }
3042}
3043
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003044/*
3045 * HKEY quirks:
3046 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
3047 */
3048
3049#define TPACPI_HK_Q_INIMASK 0x0001
3050
3051static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
3052 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
3053 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
3054 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
3055 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
3056 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
3057 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
3058 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
3059 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
3060 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
3061 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
3062 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
3063 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
3064 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
3065 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
3066 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
3067 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
3068 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
3069 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
3070 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
3071};
3072
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003073static int __init hotkey_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003074{
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003075 /* Requirements for changing the default keymaps:
3076 *
3077 * 1. Many of the keys are mapped to KEY_RESERVED for very
3078 * good reasons. Do not change them unless you have deep
3079 * knowledge on the IBM and Lenovo ThinkPad firmware for
3080 * the various ThinkPad models. The driver behaves
3081 * differently for KEY_RESERVED: such keys have their
3082 * hot key mask *unset* in mask_recommended, and also
3083 * in the initial hot key mask programmed into the
3084 * firmware at driver load time, which means the firm-
3085 * ware may react very differently if you change them to
3086 * something else;
3087 *
3088 * 2. You must be subscribed to the linux-thinkpad and
3089 * ibm-acpi-devel mailing lists, and you should read the
3090 * list archives since 2007 if you want to change the
3091 * keymaps. This requirement exists so that you will
3092 * know the past history of problems with the thinkpad-
3093 * acpi driver keymaps, and also that you will be
3094 * listening to any bug reports;
3095 *
3096 * 3. Do not send thinkpad-acpi specific patches directly to
3097 * for merging, *ever*. Send them to the linux-acpi
3098 * mailinglist for comments. Merging is to be done only
3099 * through acpi-test and the ACPI maintainer.
3100 *
3101 * If the above is too much to ask, don't change the keymap.
3102 * Ask the thinkpad-acpi maintainer to do it, instead.
3103 */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003104 static u16 ibm_keycode_map[] __initdata = {
3105 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3106 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
3107 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3108 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003109
3110 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003111 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3112 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3113 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003114
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003115 /* brightness: firmware always reacts to them */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003116 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003117 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003118
3119 /* Thinklight: firmware always react to it */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003120 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003121
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003122 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3123 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003124
3125 /* Volume: firmware always react to it and reprograms
3126 * the built-in *extra* mixer. Never map it to control
3127 * another mixer by default. */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003128 KEY_RESERVED, /* 0x14: VOLUME UP */
3129 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3130 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003131
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003132 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003133
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003134 /* (assignments unknown, please report if found) */
3135 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3136 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3137 };
3138 static u16 lenovo_keycode_map[] __initdata = {
3139 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3140 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
3141 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3142 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003143
3144 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003145 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3146 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3147 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003148
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003149 /* These should be enabled --only-- when ACPI video
3150 * is disabled (i.e. in "vendor" mode), and are handled
3151 * in a special way by the init code */
3152 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
3153 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003154
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003155 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003156
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003157 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3158 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003159
3160 /* Volume: z60/z61, T60 (BIOS version?): firmware always
3161 * react to it and reprograms the built-in *extra* mixer.
3162 * Never map it to control another mixer by default.
3163 *
3164 * T60?, T61, R60?, R61: firmware and EC tries to send
3165 * these over the regular keyboard, so these are no-ops,
3166 * but there are still weird bugs re. MUTE, so do not
3167 * change unless you get test reports from all Lenovo
3168 * models. May cause the BIOS to interfere with the
3169 * HDA mixer.
3170 */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003171 KEY_RESERVED, /* 0x14: VOLUME UP */
3172 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3173 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003174
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003175 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003176
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003177 /* (assignments unknown, please report if found) */
3178 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3179 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3180 };
3181
3182#define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
3183#define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
3184#define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
3185
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003186 int res, i;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003187 int status;
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03003188 int hkeyv;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003189 bool radiosw_state = false;
3190 bool tabletsw_state = false;
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03003191
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003192 unsigned long quirks;
3193
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003194 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3195 "initializing hotkey subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003196
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003197 BUG_ON(!tpacpi_inputdev);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003198 BUG_ON(tpacpi_inputdev->open != NULL ||
3199 tpacpi_inputdev->close != NULL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003200
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003201 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003202 mutex_init(&hotkey_mutex);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003203
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003204#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3205 mutex_init(&hotkey_thread_mutex);
3206 mutex_init(&hotkey_thread_data_mutex);
3207#endif
3208
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003209 /* hotkey not supported on 570 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003210 tp_features.hotkey = hkey_handle != NULL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003211
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003212 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3213 "hotkeys are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003214 str_supported(tp_features.hotkey));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003215
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003216 if (!tp_features.hotkey)
3217 return 1;
3218
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003219 quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
3220 ARRAY_SIZE(tpacpi_hotkey_qtable));
3221
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03003222 tpacpi_disable_brightness_delay();
3223
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003224 /* MUST have enough space for all attributes to be added to
3225 * hotkey_dev_attributes */
3226 hotkey_dev_attributes = create_attr_set(
3227 ARRAY_SIZE(hotkey_attributes) + 2,
3228 NULL);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003229 if (!hotkey_dev_attributes)
3230 return -ENOMEM;
3231 res = add_many_to_attr_set(hotkey_dev_attributes,
3232 hotkey_attributes,
3233 ARRAY_SIZE(hotkey_attributes));
3234 if (res)
3235 goto err_exit;
3236
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003237 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003238 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
3239 for HKEY interface version 0x100 */
3240 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
3241 if ((hkeyv >> 8) != 1) {
3242 printk(TPACPI_ERR "unknown version of the "
3243 "HKEY interface: 0x%x\n", hkeyv);
3244 printk(TPACPI_ERR "please report this to %s\n",
3245 TPACPI_MAIL);
3246 } else {
3247 /*
3248 * MHKV 0x100 in A31, R40, R40e,
3249 * T4x, X31, and later
3250 */
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003251 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3252 "firmware HKEY interface version: 0x%x\n",
3253 hkeyv);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003254
3255 /* Paranoia check AND init hotkey_all_mask */
3256 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3257 "MHKA", "qd")) {
3258 printk(TPACPI_ERR
3259 "missing MHKA handler, "
3260 "please report this to %s\n",
3261 TPACPI_MAIL);
3262 /* Fallback: pre-init for FN+F3,F4,F12 */
3263 hotkey_all_mask = 0x080cU;
3264 } else {
3265 tp_features.hotkey_mask = 1;
3266 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003267 }
3268 }
3269
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003270 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3271 "hotkey masks are %s\n",
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003272 str_supported(tp_features.hotkey_mask));
3273
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003274 /* Init hotkey_all_mask if not initialized yet */
3275 if (!tp_features.hotkey_mask && !hotkey_all_mask &&
3276 (quirks & TPACPI_HK_Q_INIMASK))
3277 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003278
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003279 /* Init hotkey_acpi_mask and hotkey_orig_mask */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003280 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003281 /* hotkey_source_mask *must* be zero for
3282 * the first hotkey_mask_get to return hotkey_orig_mask */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003283 res = hotkey_mask_get();
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003284 if (res)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003285 goto err_exit;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003286
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003287 hotkey_orig_mask = hotkey_acpi_mask;
3288 } else {
3289 hotkey_orig_mask = hotkey_all_mask;
3290 hotkey_acpi_mask = hotkey_all_mask;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003291 }
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003292
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003293#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3294 if (dbg_wlswemul) {
3295 tp_features.hotkey_wlsw = 1;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003296 radiosw_state = !!tpacpi_wlsw_emulstate;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003297 printk(TPACPI_INFO
3298 "radio switch emulation enabled\n");
3299 } else
3300#endif
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003301 /* Not all thinkpads have a hardware radio switch */
3302 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
3303 tp_features.hotkey_wlsw = 1;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003304 radiosw_state = !!status;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003305 printk(TPACPI_INFO
3306 "radio switch found; radios are %s\n",
3307 enabled(status, 0));
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003308 }
3309 if (tp_features.hotkey_wlsw)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003310 res = add_to_attr_set(hotkey_dev_attributes,
3311 &dev_attr_hotkey_radio_sw.attr);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003312
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003313 /* For X41t, X60t, X61t Tablets... */
3314 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
3315 tp_features.hotkey_tablet = 1;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003316 tabletsw_state = !!(status & TP_HOTKEY_TABLET_MASK);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003317 printk(TPACPI_INFO
3318 "possible tablet mode switch found; "
3319 "ThinkPad in %s mode\n",
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003320 (tabletsw_state) ? "tablet" : "laptop");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003321 res = add_to_attr_set(hotkey_dev_attributes,
3322 &dev_attr_hotkey_tablet_mode.attr);
3323 }
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02003324
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003325 if (!res)
3326 res = register_attr_set_with_sysfs(
3327 hotkey_dev_attributes,
3328 &tpacpi_pdev->dev.kobj);
3329 if (res)
3330 goto err_exit;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003331
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003332 /* Set up key map */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003333
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003334 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
3335 GFP_KERNEL);
3336 if (!hotkey_keycode_map) {
3337 printk(TPACPI_ERR
3338 "failed to allocate memory for key map\n");
3339 res = -ENOMEM;
3340 goto err_exit;
3341 }
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003342
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003343 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003344 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003345 "using Lenovo default hot key map\n");
3346 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
3347 TPACPI_HOTKEY_MAP_SIZE);
3348 } else {
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003349 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003350 "using IBM default hot key map\n");
3351 memcpy(hotkey_keycode_map, &ibm_keycode_map,
3352 TPACPI_HOTKEY_MAP_SIZE);
3353 }
3354
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003355 input_set_capability(tpacpi_inputdev, EV_MSC, MSC_SCAN);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003356 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
3357 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3358 tpacpi_inputdev->keycode = hotkey_keycode_map;
3359 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3360 if (hotkey_keycode_map[i] != KEY_RESERVED) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003361 input_set_capability(tpacpi_inputdev, EV_KEY,
3362 hotkey_keycode_map[i]);
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003363 } else {
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003364 if (i < sizeof(hotkey_reserved_mask)*8)
3365 hotkey_reserved_mask |= 1 << i;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003366 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003367 }
3368
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003369 if (tp_features.hotkey_wlsw) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003370 input_set_capability(tpacpi_inputdev, EV_SW, SW_RFKILL_ALL);
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003371 input_report_switch(tpacpi_inputdev,
3372 SW_RFKILL_ALL, radiosw_state);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003373 }
3374 if (tp_features.hotkey_tablet) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003375 input_set_capability(tpacpi_inputdev, EV_SW, SW_TABLET_MODE);
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003376 input_report_switch(tpacpi_inputdev,
3377 SW_TABLET_MODE, tabletsw_state);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003378 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003379
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003380 /* Do not issue duplicate brightness change events to
3381 * userspace */
3382 if (!tp_features.bright_acpimode)
3383 /* update bright_acpimode... */
3384 tpacpi_check_std_acpi_brightness_support();
3385
Henrique de Moraes Holschuh8bf3d4c2009-05-30 13:25:09 -03003386 if (tp_features.bright_acpimode && acpi_video_backlight_support()) {
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003387 printk(TPACPI_INFO
3388 "This ThinkPad has standard ACPI backlight "
3389 "brightness control, supported by the ACPI "
3390 "video driver\n");
3391 printk(TPACPI_NOTICE
3392 "Disabling thinkpad-acpi brightness events "
3393 "by default...\n");
3394
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003395 /* Disable brightness up/down on Lenovo thinkpads when
3396 * ACPI is handling them, otherwise it is plain impossible
3397 * for userspace to do something even remotely sane */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003398 hotkey_reserved_mask |=
3399 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3400 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003401 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
3402 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003403 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003404
Henrique de Moraes Holschuh230d8cf252009-09-12 15:22:17 -03003405#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003406 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3407 & ~hotkey_all_mask
3408 & ~hotkey_reserved_mask;
Henrique de Moraes Holschuh230d8cf252009-09-12 15:22:17 -03003409
3410 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3411 "hotkey source mask 0x%08x, polling freq %u\n",
3412 hotkey_source_mask, hotkey_poll_freq);
3413#endif
3414
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003415 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3416 "enabling firmware HKEY event interface...\n");
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003417 res = hotkey_status_set(true);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003418 if (res) {
3419 hotkey_exit();
3420 return res;
3421 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003422 res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
3423 | hotkey_driver_mask)
3424 & ~hotkey_source_mask);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003425 if (res < 0 && res != -ENXIO) {
3426 hotkey_exit();
3427 return res;
3428 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003429 hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
3430 & ~hotkey_reserved_mask;
3431 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3432 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3433 hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003434
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003435 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3436 "legacy ibm/hotkey event reporting over procfs %s\n",
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003437 (hotkey_report_mode < 2) ?
3438 "enabled" : "disabled");
3439
3440 tpacpi_inputdev->open = &hotkey_inputdev_open;
3441 tpacpi_inputdev->close = &hotkey_inputdev_close;
3442
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003443 hotkey_poll_setup_safe(true);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003444
3445 return 0;
3446
3447err_exit:
3448 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3449 hotkey_dev_attributes = NULL;
3450
3451 return (res < 0)? res : 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003452}
3453
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003454static bool hotkey_notify_hotkey(const u32 hkey,
3455 bool *send_acpi_ev,
3456 bool *ignore_acpi_ev)
3457{
3458 /* 0x1000-0x1FFF: key presses */
3459 unsigned int scancode = hkey & 0xfff;
3460 *send_acpi_ev = true;
3461 *ignore_acpi_ev = false;
3462
3463 if (scancode > 0 && scancode < 0x21) {
3464 scancode--;
3465 if (!(hotkey_source_mask & (1 << scancode))) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003466 tpacpi_input_send_key_masked(scancode);
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003467 *send_acpi_ev = false;
3468 } else {
3469 *ignore_acpi_ev = true;
3470 }
3471 return true;
3472 }
3473 return false;
3474}
3475
3476static bool hotkey_notify_wakeup(const u32 hkey,
3477 bool *send_acpi_ev,
3478 bool *ignore_acpi_ev)
3479{
3480 /* 0x2000-0x2FFF: Wakeup reason */
3481 *send_acpi_ev = true;
3482 *ignore_acpi_ev = false;
3483
3484 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003485 case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */
3486 case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003487 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3488 *ignore_acpi_ev = true;
3489 break;
3490
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003491 case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */
3492 case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003493 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3494 *ignore_acpi_ev = true;
3495 break;
3496
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003497 case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */
3498 case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003499 printk(TPACPI_ALERT
3500 "EMERGENCY WAKEUP: battery almost empty\n");
3501 /* how to auto-heal: */
3502 /* 2313: woke up from S3, go to S4/S5 */
3503 /* 2413: woke up from S4, go to S5 */
3504 break;
3505
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003506 default:
3507 return false;
3508 }
3509
3510 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
3511 printk(TPACPI_INFO
3512 "woke up due to a hot-unplug "
3513 "request...\n");
3514 hotkey_wakeup_reason_notify_change();
3515 }
3516 return true;
3517}
3518
3519static bool hotkey_notify_usrevent(const u32 hkey,
3520 bool *send_acpi_ev,
3521 bool *ignore_acpi_ev)
3522{
3523 /* 0x5000-0x5FFF: human interface helpers */
3524 *send_acpi_ev = true;
3525 *ignore_acpi_ev = false;
3526
3527 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003528 case TP_HKEY_EV_PEN_INSERTED: /* X61t: tablet pen inserted into bay */
3529 case TP_HKEY_EV_PEN_REMOVED: /* X61t: tablet pen removed from bay */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003530 return true;
3531
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003532 case TP_HKEY_EV_TABLET_TABLET: /* X41t-X61t: tablet mode */
3533 case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003534 tpacpi_input_send_tabletsw();
3535 hotkey_tablet_mode_notify_change();
3536 *send_acpi_ev = false;
3537 return true;
3538
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003539 case TP_HKEY_EV_LID_CLOSE: /* Lid closed */
3540 case TP_HKEY_EV_LID_OPEN: /* Lid opened */
3541 case TP_HKEY_EV_BRGHT_CHANGED: /* brightness changed */
Henrique de Moraes Holschuh176dd982009-09-20 14:09:24 -03003542 /* do not propagate these events */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003543 *ignore_acpi_ev = true;
3544 return true;
3545
3546 default:
3547 return false;
3548 }
3549}
3550
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003551static void thermal_dump_all_sensors(void);
3552
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003553static bool hotkey_notify_thermal(const u32 hkey,
3554 bool *send_acpi_ev,
3555 bool *ignore_acpi_ev)
3556{
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003557 bool known = true;
3558
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003559 /* 0x6000-0x6FFF: thermal alarms */
3560 *send_acpi_ev = true;
3561 *ignore_acpi_ev = false;
3562
3563 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003564 case TP_HKEY_EV_THM_TABLE_CHANGED:
Henrique de Moraes Holschuh54926ce2009-01-11 03:01:09 -02003565 printk(TPACPI_INFO
3566 "EC reports that Thermal Table has changed\n");
3567 /* recommended action: do nothing, we don't have
3568 * Lenovo ATM information */
3569 return true;
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003570 case TP_HKEY_EV_ALARM_BAT_HOT:
3571 printk(TPACPI_CRIT
3572 "THERMAL ALARM: battery is too hot!\n");
3573 /* recommended action: warn user through gui */
3574 break;
3575 case TP_HKEY_EV_ALARM_BAT_XHOT:
3576 printk(TPACPI_ALERT
3577 "THERMAL EMERGENCY: battery is extremely hot!\n");
3578 /* recommended action: immediate sleep/hibernate */
3579 break;
3580 case TP_HKEY_EV_ALARM_SENSOR_HOT:
3581 printk(TPACPI_CRIT
3582 "THERMAL ALARM: "
3583 "a sensor reports something is too hot!\n");
3584 /* recommended action: warn user through gui, that */
3585 /* some internal component is too hot */
3586 break;
3587 case TP_HKEY_EV_ALARM_SENSOR_XHOT:
3588 printk(TPACPI_ALERT
3589 "THERMAL EMERGENCY: "
3590 "a sensor reports something is extremely hot!\n");
3591 /* recommended action: immediate sleep/hibernate */
3592 break;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003593 default:
3594 printk(TPACPI_ALERT
3595 "THERMAL ALERT: unknown thermal alarm received\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003596 known = false;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003597 }
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003598
3599 thermal_dump_all_sensors();
3600
3601 return known;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003602}
3603
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003604static void hotkey_notify(struct ibm_struct *ibm, u32 event)
3605{
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003606 u32 hkey;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003607 bool send_acpi_ev;
3608 bool ignore_acpi_ev;
3609 bool known_ev;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003610
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003611 if (event != 0x80) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003612 printk(TPACPI_ERR
3613 "unknown HKEY notification event %d\n", event);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003614 /* forward it to userspace, maybe it knows how to handle it */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003615 acpi_bus_generate_netlink_event(
3616 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02003617 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003618 event, 0);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003619 return;
3620 }
3621
3622 while (1) {
3623 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003624 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003625 return;
3626 }
3627
3628 if (hkey == 0) {
3629 /* queue empty */
3630 return;
3631 }
3632
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003633 send_acpi_ev = true;
3634 ignore_acpi_ev = false;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003635
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003636 switch (hkey >> 12) {
3637 case 1:
3638 /* 0x1000-0x1FFF: key presses */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003639 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
3640 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003641 break;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003642 case 2:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003643 /* 0x2000-0x2FFF: Wakeup reason */
3644 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
3645 &ignore_acpi_ev);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003646 break;
3647 case 3:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003648 /* 0x3000-0x3FFF: bay-related wakeups */
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003649 if (hkey == TP_HKEY_EV_BAYEJ_ACK) {
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003650 hotkey_autosleep_ack = 1;
3651 printk(TPACPI_INFO
3652 "bay ejected\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003653 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003654 known_ev = true;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003655 } else {
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003656 known_ev = false;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003657 }
3658 break;
3659 case 4:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003660 /* 0x4000-0x4FFF: dock-related wakeups */
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003661 if (hkey == TP_HKEY_EV_UNDOCK_ACK) {
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003662 hotkey_autosleep_ack = 1;
3663 printk(TPACPI_INFO
3664 "undocked\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003665 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003666 known_ev = true;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003667 } else {
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003668 known_ev = false;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003669 }
3670 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003671 case 5:
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02003672 /* 0x5000-0x5FFF: human interface helpers */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003673 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
3674 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003675 break;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003676 case 6:
3677 /* 0x6000-0x6FFF: thermal alarms */
3678 known_ev = hotkey_notify_thermal(hkey, &send_acpi_ev,
3679 &ignore_acpi_ev);
3680 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003681 case 7:
3682 /* 0x7000-0x7FFF: misc */
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003683 if (tp_features.hotkey_wlsw &&
3684 hkey == TP_HKEY_EV_RFKILL_CHANGED) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003685 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02003686 send_acpi_ev = 0;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003687 known_ev = true;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003688 break;
3689 }
3690 /* fallthrough to default */
3691 default:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003692 known_ev = false;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02003693 }
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003694 if (!known_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003695 printk(TPACPI_NOTICE
3696 "unhandled HKEY event 0x%04x\n", hkey);
Henrique de Moraes Holschuhcb429352009-01-11 03:01:07 -02003697 printk(TPACPI_NOTICE
3698 "please report the conditions when this "
3699 "event happened to %s\n", TPACPI_MAIL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003700 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003701
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003702 /* Legacy events */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003703 if (!ignore_acpi_ev &&
3704 (send_acpi_ev || hotkey_report_mode < 2)) {
3705 acpi_bus_generate_proc_event(ibm->acpi->device,
3706 event, hkey);
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03003707 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003708
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003709 /* netlink events */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03003710 if (!ignore_acpi_ev && send_acpi_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003711 acpi_bus_generate_netlink_event(
3712 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02003713 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003714 event, hkey);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003715 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003716 }
3717}
3718
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003719static void hotkey_suspend(pm_message_t state)
3720{
3721 /* Do these on suspend, we get the events on early resume! */
3722 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
3723 hotkey_autosleep_ack = 0;
3724}
3725
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03003726static void hotkey_resume(void)
3727{
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03003728 tpacpi_disable_brightness_delay();
3729
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003730 if (hotkey_status_set(true) < 0 ||
3731 hotkey_mask_set(hotkey_acpi_mask) < 0)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003732 printk(TPACPI_ERR
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003733 "error while attempting to reset the event "
3734 "firmware interface\n");
3735
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003736 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02003737 hotkey_tablet_mode_notify_change();
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003738 hotkey_wakeup_reason_notify_change();
3739 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003740 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03003741}
3742
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003743/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003744static int hotkey_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03003746 int res, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747 int len = 0;
3748
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003749 if (!tp_features.hotkey) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003750 len += sprintf(p + len, "status:\t\tnot supported\n");
3751 return len;
3752 }
3753
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02003754 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02003755 return -ERESTARTSYS;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02003756 res = hotkey_status_get(&status);
3757 if (!res)
3758 res = hotkey_mask_get();
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003759 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03003760 if (res)
3761 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762
3763 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003764 if (hotkey_all_mask) {
3765 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_user_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766 len += sprintf(p + len,
3767 "commands:\tenable, disable, reset, <mask>\n");
3768 } else {
3769 len += sprintf(p + len, "mask:\t\tnot supported\n");
3770 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
3771 }
3772
3773 return len;
3774}
3775
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003776static void hotkey_enabledisable_warn(bool enable)
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003777{
3778 tpacpi_log_usertask("procfs hotkey enable/disable");
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003779 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
3780 TPACPI_WARN
3781 "hotkey enable/disable functionality has been "
3782 "removed from the driver. Hotkeys are always "
3783 "enabled\n"))
3784 printk(TPACPI_ERR
3785 "Please remove the hotkey=enable module "
3786 "parameter, it is deprecated. Hotkeys are always "
3787 "enabled\n");
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003788}
3789
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003790static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003792 int res;
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03003793 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003796 if (!tp_features.hotkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797 return -ENODEV;
3798
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02003799 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02003800 return -ERESTARTSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003801
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003802 mask = hotkey_user_mask;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003803
3804 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805 while ((cmd = next_cmd(&buf))) {
3806 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003807 hotkey_enabledisable_warn(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003809 hotkey_enabledisable_warn(0);
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003810 res = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811 } else if (strlencmp(cmd, "reset") == 0) {
Henrique de Moraes Holschuh20c9aa42009-09-12 15:22:16 -03003812 mask = (hotkey_all_mask | hotkey_source_mask)
3813 & ~hotkey_reserved_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
3815 /* mask set */
3816 } else if (sscanf(cmd, "%x", &mask) == 1) {
3817 /* mask set */
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003818 } else {
3819 res = -EINVAL;
3820 goto errexit;
3821 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822 }
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003823
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003824 if (!res) {
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003825 tpacpi_disclose_usertask("procfs hotkey",
3826 "set mask to 0x%08x\n", mask);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003827 res = hotkey_user_mask_set(mask);
3828 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003830errexit:
3831 mutex_unlock(&hotkey_mutex);
3832 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003833}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003835static const struct acpi_device_id ibm_htk_device_ids[] = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003836 {TPACPI_ACPI_HKEY_HID, 0},
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003837 {"", 0},
3838};
3839
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003840static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003841 .hid = ibm_htk_device_ids,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003842 .notify = hotkey_notify,
3843 .handle = &hkey_handle,
3844 .type = ACPI_DEVICE_NOTIFY,
3845};
3846
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003847static struct ibm_struct hotkey_driver_data = {
3848 .name = "hotkey",
3849 .read = hotkey_read,
3850 .write = hotkey_write,
3851 .exit = hotkey_exit,
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03003852 .resume = hotkey_resume,
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003853 .suspend = hotkey_suspend,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003854 .acpi = &ibm_hotkey_acpidriver,
3855};
3856
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003857/*************************************************************************
3858 * Bluetooth subdriver
3859 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003861enum {
3862 /* ACPI GBDC/SBDC bits */
3863 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
3864 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003865 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
3866 off / last state */
3867};
3868
3869enum {
3870 /* ACPI \BLTH commands */
3871 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
3872 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
3873 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
3874 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
3875 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003876};
3877
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003878#define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
3879
Johannes Berg19d337d2009-06-02 13:01:37 +02003880static int bluetooth_get_status(void)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003881{
3882 int status;
3883
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003884#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3885 if (dbg_bluetoothemul)
3886 return (tpacpi_bluetooth_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02003887 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003888#endif
3889
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003890 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
3891 return -EIO;
3892
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003893 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02003894 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003895}
3896
Johannes Berg19d337d2009-06-02 13:01:37 +02003897static int bluetooth_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003898{
3899 int status;
3900
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003901 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02003902 "will attempt to %s bluetooth\n",
3903 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003904
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003905#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3906 if (dbg_bluetoothemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02003907 tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003908 return 0;
3909 }
3910#endif
3911
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003912 /* We make sure to keep TP_ACPI_BLUETOOTH_RESUMECTRL off */
Henrique de Moraes Holschuh208b9962009-12-09 01:36:22 +00003913 status = TP_ACPI_BLUETOOTH_RESUMECTRL;
Johannes Berg19d337d2009-06-02 13:01:37 +02003914 if (state == TPACPI_RFK_RADIO_ON)
Henrique de Moraes Holschuh208b9962009-12-09 01:36:22 +00003915 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
Johannes Berg19d337d2009-06-02 13:01:37 +02003916
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003917 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
3918 return -EIO;
3919
3920 return 0;
3921}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003922
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003923/* sysfs bluetooth enable ---------------------------------------------- */
3924static ssize_t bluetooth_enable_show(struct device *dev,
3925 struct device_attribute *attr,
3926 char *buf)
3927{
Johannes Berg19d337d2009-06-02 13:01:37 +02003928 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID,
3929 attr, buf);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003930}
3931
3932static ssize_t bluetooth_enable_store(struct device *dev,
3933 struct device_attribute *attr,
3934 const char *buf, size_t count)
3935{
Johannes Berg19d337d2009-06-02 13:01:37 +02003936 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID,
3937 attr, buf, count);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003938}
3939
3940static struct device_attribute dev_attr_bluetooth_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03003941 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003942 bluetooth_enable_show, bluetooth_enable_store);
3943
3944/* --------------------------------------------------------------------- */
3945
3946static struct attribute *bluetooth_attributes[] = {
3947 &dev_attr_bluetooth_enable.attr,
3948 NULL
3949};
3950
3951static const struct attribute_group bluetooth_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003952 .attrs = bluetooth_attributes,
3953};
3954
Johannes Berg19d337d2009-06-02 13:01:37 +02003955static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = {
3956 .get_status = bluetooth_get_status,
3957 .set_status = bluetooth_set_status,
3958};
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003959
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003960static void bluetooth_shutdown(void)
3961{
3962 /* Order firmware to save current state to NVRAM */
3963 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3964 TP_ACPI_BLTH_SAVE_STATE))
3965 printk(TPACPI_NOTICE
3966 "failed to save bluetooth state to NVRAM\n");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003967 else
3968 vdbg_printk(TPACPI_DBG_RFKILL,
3969 "bluestooth state saved to NVRAM\n");
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003970}
3971
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003972static void bluetooth_exit(void)
3973{
3974 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3975 &bluetooth_attr_group);
Johannes Berg19d337d2009-06-02 13:01:37 +02003976
3977 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
3978
3979 bluetooth_shutdown();
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003980}
3981
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003982static int __init bluetooth_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003984 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003985 int status = 0;
3986
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003987 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3988 "initializing bluetooth subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003989
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003990 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003991
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003992 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3993 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003994 tp_features.bluetooth = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003995 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003997 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3998 "bluetooth is %s, status 0x%02x\n",
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003999 str_supported(tp_features.bluetooth),
4000 status);
4001
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004002#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4003 if (dbg_bluetoothemul) {
4004 tp_features.bluetooth = 1;
4005 printk(TPACPI_INFO
4006 "bluetooth switch emulation enabled\n");
4007 } else
4008#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004009 if (tp_features.bluetooth &&
4010 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
4011 /* no bluetooth hardware present in system */
4012 tp_features.bluetooth = 0;
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004013 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004014 "bluetooth hardware not installed\n");
4015 }
4016
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004017 if (!tp_features.bluetooth)
4018 return 1;
4019
Johannes Berg19d337d2009-06-02 13:01:37 +02004020 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
4021 &bluetooth_tprfk_ops,
4022 RFKILL_TYPE_BLUETOOTH,
4023 TPACPI_RFK_BLUETOOTH_SW_NAME,
4024 true);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004025 if (res)
4026 return res;
4027
Johannes Berg19d337d2009-06-02 13:01:37 +02004028 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4029 &bluetooth_attr_group);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004030 if (res) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004031 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004032 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004033 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004034
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004035 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036}
4037
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004038/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004039static int bluetooth_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040{
Johannes Berg19d337d2009-06-02 13:01:37 +02004041 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042}
4043
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004044static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045{
Johannes Berg19d337d2009-06-02 13:01:37 +02004046 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047}
4048
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004049static struct ibm_struct bluetooth_driver_data = {
4050 .name = "bluetooth",
4051 .read = bluetooth_read,
4052 .write = bluetooth_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004053 .exit = bluetooth_exit,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004054 .shutdown = bluetooth_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004055};
4056
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004057/*************************************************************************
4058 * Wan subdriver
4059 */
4060
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004061enum {
4062 /* ACPI GWAN/SWAN bits */
4063 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
4064 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02004065 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
4066 off / last state */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004067};
4068
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004069#define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
4070
Johannes Berg19d337d2009-06-02 13:01:37 +02004071static int wan_get_status(void)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004072{
4073 int status;
4074
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004075#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4076 if (dbg_wwanemul)
4077 return (tpacpi_wwan_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004078 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004079#endif
4080
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004081 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
4082 return -EIO;
4083
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004084 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004085 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004086}
4087
Johannes Berg19d337d2009-06-02 13:01:37 +02004088static int wan_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004089{
4090 int status;
4091
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004092 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02004093 "will attempt to %s wwan\n",
4094 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004095
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004096#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4097 if (dbg_wwanemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004098 tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004099 return 0;
4100 }
4101#endif
4102
Henrique de Moraes Holschuh208b9962009-12-09 01:36:22 +00004103 /* We make sure to set TP_ACPI_WANCARD_RESUMECTRL */
4104 status = TP_ACPI_WANCARD_RESUMECTRL;
Johannes Berg19d337d2009-06-02 13:01:37 +02004105 if (state == TPACPI_RFK_RADIO_ON)
Henrique de Moraes Holschuh208b9962009-12-09 01:36:22 +00004106 status |= TP_ACPI_WANCARD_RADIOSSW;
Johannes Berg19d337d2009-06-02 13:01:37 +02004107
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004108 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
4109 return -EIO;
4110
4111 return 0;
4112}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004113
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004114/* sysfs wan enable ---------------------------------------------------- */
4115static ssize_t wan_enable_show(struct device *dev,
4116 struct device_attribute *attr,
4117 char *buf)
4118{
Johannes Berg19d337d2009-06-02 13:01:37 +02004119 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
4120 attr, buf);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004121}
4122
4123static ssize_t wan_enable_store(struct device *dev,
4124 struct device_attribute *attr,
4125 const char *buf, size_t count)
4126{
Johannes Berg19d337d2009-06-02 13:01:37 +02004127 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
4128 attr, buf, count);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004129}
4130
4131static struct device_attribute dev_attr_wan_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03004132 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004133 wan_enable_show, wan_enable_store);
4134
4135/* --------------------------------------------------------------------- */
4136
4137static struct attribute *wan_attributes[] = {
4138 &dev_attr_wan_enable.attr,
4139 NULL
4140};
4141
4142static const struct attribute_group wan_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004143 .attrs = wan_attributes,
4144};
4145
Johannes Berg19d337d2009-06-02 13:01:37 +02004146static const struct tpacpi_rfk_ops wan_tprfk_ops = {
4147 .get_status = wan_get_status,
4148 .set_status = wan_set_status,
4149};
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004150
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004151static void wan_shutdown(void)
4152{
4153 /* Order firmware to save current state to NVRAM */
4154 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
4155 TP_ACPI_WGSV_SAVE_STATE))
4156 printk(TPACPI_NOTICE
4157 "failed to save WWAN state to NVRAM\n");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004158 else
4159 vdbg_printk(TPACPI_DBG_RFKILL,
4160 "WWAN state saved to NVRAM\n");
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004161}
4162
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004163static void wan_exit(void)
4164{
4165 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4166 &wan_attr_group);
Johannes Berg19d337d2009-06-02 13:01:37 +02004167
4168 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4169
4170 wan_shutdown();
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004171}
4172
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004173static int __init wan_init(struct ibm_init_struct *iibm)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004174{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004175 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004176 int status = 0;
4177
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004178 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4179 "initializing wan subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004180
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004181 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004182
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004183 tp_features.wan = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004184 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004185
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004186 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4187 "wan is %s, status 0x%02x\n",
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004188 str_supported(tp_features.wan),
4189 status);
4190
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004191#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4192 if (dbg_wwanemul) {
4193 tp_features.wan = 1;
4194 printk(TPACPI_INFO
4195 "wwan switch emulation enabled\n");
4196 } else
4197#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004198 if (tp_features.wan &&
4199 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
4200 /* no wan hardware present in system */
4201 tp_features.wan = 0;
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004202 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004203 "wan hardware not installed\n");
4204 }
4205
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004206 if (!tp_features.wan)
4207 return 1;
4208
Johannes Berg19d337d2009-06-02 13:01:37 +02004209 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
4210 &wan_tprfk_ops,
4211 RFKILL_TYPE_WWAN,
4212 TPACPI_RFK_WWAN_SW_NAME,
4213 true);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004214 if (res)
4215 return res;
4216
Johannes Berg19d337d2009-06-02 13:01:37 +02004217 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4218 &wan_attr_group);
4219
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004220 if (res) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004221 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004222 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004223 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004224
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004225 return 0;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004226}
4227
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004228/* procfs -------------------------------------------------------------- */
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004229static int wan_read(char *p)
4230{
Johannes Berg19d337d2009-06-02 13:01:37 +02004231 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, p);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004232}
4233
4234static int wan_write(char *buf)
4235{
Johannes Berg19d337d2009-06-02 13:01:37 +02004236 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004237}
4238
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004239static struct ibm_struct wan_driver_data = {
4240 .name = "wan",
4241 .read = wan_read,
4242 .write = wan_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004243 .exit = wan_exit,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004244 .shutdown = wan_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004245};
4246
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004247/*************************************************************************
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004248 * UWB subdriver
4249 */
4250
4251enum {
4252 /* ACPI GUWB/SUWB bits */
4253 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4254 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4255};
4256
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004257#define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4258
Johannes Berg19d337d2009-06-02 13:01:37 +02004259static int uwb_get_status(void)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004260{
4261 int status;
4262
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004263#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4264 if (dbg_uwbemul)
4265 return (tpacpi_uwb_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004266 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004267#endif
4268
4269 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4270 return -EIO;
4271
4272 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004273 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004274}
4275
Johannes Berg19d337d2009-06-02 13:01:37 +02004276static int uwb_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004277{
4278 int status;
4279
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004280 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02004281 "will attempt to %s UWB\n",
4282 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004283
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004284#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4285 if (dbg_uwbemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004286 tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004287 return 0;
4288 }
4289#endif
4290
Johannes Berg19d337d2009-06-02 13:01:37 +02004291 if (state == TPACPI_RFK_RADIO_ON)
4292 status = TP_ACPI_UWB_RADIOSSW;
4293 else
4294 status = 0;
4295
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004296 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4297 return -EIO;
4298
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004299 return 0;
4300}
4301
4302/* --------------------------------------------------------------------- */
4303
Johannes Berg19d337d2009-06-02 13:01:37 +02004304static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
4305 .get_status = uwb_get_status,
4306 .set_status = uwb_set_status,
4307};
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004308
4309static void uwb_exit(void)
4310{
Johannes Berg19d337d2009-06-02 13:01:37 +02004311 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004312}
4313
4314static int __init uwb_init(struct ibm_init_struct *iibm)
4315{
4316 int res;
4317 int status = 0;
4318
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004319 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4320 "initializing uwb subdriver\n");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004321
4322 TPACPI_ACPIHANDLE_INIT(hkey);
4323
4324 tp_features.uwb = hkey_handle &&
4325 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4326
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004327 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4328 "uwb is %s, status 0x%02x\n",
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004329 str_supported(tp_features.uwb),
4330 status);
4331
4332#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4333 if (dbg_uwbemul) {
4334 tp_features.uwb = 1;
4335 printk(TPACPI_INFO
4336 "uwb switch emulation enabled\n");
4337 } else
4338#endif
4339 if (tp_features.uwb &&
4340 !(status & TP_ACPI_UWB_HWPRESENT)) {
4341 /* no uwb hardware present in system */
4342 tp_features.uwb = 0;
4343 dbg_printk(TPACPI_DBG_INIT,
4344 "uwb hardware not installed\n");
4345 }
4346
4347 if (!tp_features.uwb)
4348 return 1;
4349
4350 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
Johannes Berg19d337d2009-06-02 13:01:37 +02004351 &uwb_tprfk_ops,
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004352 RFKILL_TYPE_UWB,
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004353 TPACPI_RFK_UWB_SW_NAME,
Johannes Berg19d337d2009-06-02 13:01:37 +02004354 false);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004355 return res;
4356}
4357
4358static struct ibm_struct uwb_driver_data = {
4359 .name = "uwb",
4360 .exit = uwb_exit,
4361 .flags.experimental = 1,
4362};
4363
4364/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004365 * Video subdriver
4366 */
4367
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02004368#ifdef CONFIG_THINKPAD_ACPI_VIDEO
4369
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004370enum video_access_mode {
4371 TPACPI_VIDEO_NONE = 0,
4372 TPACPI_VIDEO_570, /* 570 */
4373 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4374 TPACPI_VIDEO_NEW, /* all others */
4375};
4376
4377enum { /* video status flags, based on VIDEO_570 */
4378 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4379 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4380 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4381};
4382
4383enum { /* TPACPI_VIDEO_570 constants */
4384 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4385 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4386 * video_status_flags */
4387 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4388 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4389};
4390
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02004391static enum video_access_mode video_supported;
4392static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004393
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004394static int video_autosw_get(void);
4395static int video_autosw_set(int enable);
4396
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004397TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004398
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004399static int __init video_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004401 int ivga;
4402
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004403 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4404
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004405 TPACPI_ACPIHANDLE_INIT(vid);
4406 TPACPI_ACPIHANDLE_INIT(vid2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004407
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004408 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4409 /* G41, assume IVGA doesn't change */
4410 vid_handle = vid2_handle;
4411
4412 if (!vid_handle)
4413 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004414 video_supported = TPACPI_VIDEO_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004415 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
4416 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004417 video_supported = TPACPI_VIDEO_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004418 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
4419 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004420 video_supported = TPACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004421 else
4422 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004423 video_supported = TPACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004425 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
4426 str_supported(video_supported != TPACPI_VIDEO_NONE),
4427 video_supported);
4428
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004429 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430}
4431
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004432static void video_exit(void)
4433{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004434 dbg_printk(TPACPI_DBG_EXIT,
4435 "restoring original video autoswitch mode\n");
4436 if (video_autosw_set(video_orig_autosw))
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004437 printk(TPACPI_ERR "error while trying to restore original "
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004438 "video autoswitch mode\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004439}
4440
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004441static int video_outputsw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442{
4443 int status = 0;
4444 int i;
4445
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004446 switch (video_supported) {
4447 case TPACPI_VIDEO_570:
4448 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
4449 TP_ACPI_VIDEO_570_PHSCMD))
4450 return -EIO;
4451 status = i & TP_ACPI_VIDEO_570_PHSMASK;
4452 break;
4453 case TPACPI_VIDEO_770:
4454 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4455 return -EIO;
4456 if (i)
4457 status |= TP_ACPI_VIDEO_S_LCD;
4458 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4459 return -EIO;
4460 if (i)
4461 status |= TP_ACPI_VIDEO_S_CRT;
4462 break;
4463 case TPACPI_VIDEO_NEW:
4464 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4465 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4466 return -EIO;
4467 if (i)
4468 status |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004470 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4471 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4472 return -EIO;
4473 if (i)
4474 status |= TP_ACPI_VIDEO_S_LCD;
4475 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4476 return -EIO;
4477 if (i)
4478 status |= TP_ACPI_VIDEO_S_DVI;
4479 break;
4480 default:
4481 return -ENOSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004482 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483
4484 return status;
4485}
4486
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004487static int video_outputsw_set(int status)
4488{
4489 int autosw;
4490 int res = 0;
4491
4492 switch (video_supported) {
4493 case TPACPI_VIDEO_570:
4494 res = acpi_evalf(NULL, NULL,
4495 "\\_SB.PHS2", "vdd",
4496 TP_ACPI_VIDEO_570_PHS2CMD,
4497 status | TP_ACPI_VIDEO_570_PHS2SET);
4498 break;
4499 case TPACPI_VIDEO_770:
4500 autosw = video_autosw_get();
4501 if (autosw < 0)
4502 return autosw;
4503
4504 res = video_autosw_set(1);
4505 if (res)
4506 return res;
4507 res = acpi_evalf(vid_handle, NULL,
4508 "ASWT", "vdd", status * 0x100, 0);
4509 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004510 printk(TPACPI_ERR
4511 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004512 return -EIO;
4513 }
4514 break;
4515 case TPACPI_VIDEO_NEW:
4516 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004517 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004518 break;
4519 default:
4520 return -ENOSYS;
4521 }
4522
4523 return (res)? 0 : -EIO;
4524}
4525
4526static int video_autosw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004528 int autosw = 0;
4529
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004530 switch (video_supported) {
4531 case TPACPI_VIDEO_570:
4532 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
4533 return -EIO;
4534 break;
4535 case TPACPI_VIDEO_770:
4536 case TPACPI_VIDEO_NEW:
4537 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4538 return -EIO;
4539 break;
4540 default:
4541 return -ENOSYS;
4542 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004543
4544 return autosw & 1;
4545}
4546
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004547static int video_autosw_set(int enable)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004548{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004549 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004550 return -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004551 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004552}
4553
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004554static int video_outputsw_cycle(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004555{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004556 int autosw = video_autosw_get();
4557 int res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004558
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004559 if (autosw < 0)
4560 return autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004561
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004562 switch (video_supported) {
4563 case TPACPI_VIDEO_570:
4564 res = video_autosw_set(1);
4565 if (res)
4566 return res;
4567 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4568 break;
4569 case TPACPI_VIDEO_770:
4570 case TPACPI_VIDEO_NEW:
4571 res = video_autosw_set(1);
4572 if (res)
4573 return res;
4574 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4575 break;
4576 default:
4577 return -ENOSYS;
4578 }
4579 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004580 printk(TPACPI_ERR
4581 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004582 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004583 }
4584
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004585 return (res)? 0 : -EIO;
4586}
4587
4588static int video_expand_toggle(void)
4589{
4590 switch (video_supported) {
4591 case TPACPI_VIDEO_570:
4592 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
4593 0 : -EIO;
4594 case TPACPI_VIDEO_770:
4595 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
4596 0 : -EIO;
4597 case TPACPI_VIDEO_NEW:
4598 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
4599 0 : -EIO;
4600 default:
4601 return -ENOSYS;
4602 }
4603 /* not reached */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004604}
4605
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004606static int video_read(char *p)
4607{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004608 int status, autosw;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004609 int len = 0;
4610
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004611 if (video_supported == TPACPI_VIDEO_NONE) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004612 len += sprintf(p + len, "status:\t\tnot supported\n");
4613 return len;
4614 }
4615
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004616 status = video_outputsw_get();
4617 if (status < 0)
4618 return status;
4619
4620 autosw = video_autosw_get();
4621 if (autosw < 0)
4622 return autosw;
4623
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004624 len += sprintf(p + len, "status:\t\tsupported\n");
4625 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
4626 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004627 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004628 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
4629 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
4630 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
4631 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004632 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004633 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
4634 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
4635 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
4636
4637 return len;
4638}
4639
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004640static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641{
4642 char *cmd;
4643 int enable, disable, status;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004644 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004645
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004646 if (video_supported == TPACPI_VIDEO_NONE)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004647 return -ENODEV;
4648
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004649 enable = 0;
4650 disable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651
4652 while ((cmd = next_cmd(&buf))) {
4653 if (strlencmp(cmd, "lcd_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004654 enable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655 } else if (strlencmp(cmd, "lcd_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004656 disable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657 } else if (strlencmp(cmd, "crt_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004658 enable |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004659 } else if (strlencmp(cmd, "crt_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004660 disable |= TP_ACPI_VIDEO_S_CRT;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004661 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004662 strlencmp(cmd, "dvi_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004663 enable |= TP_ACPI_VIDEO_S_DVI;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004664 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004665 strlencmp(cmd, "dvi_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004666 disable |= TP_ACPI_VIDEO_S_DVI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667 } else if (strlencmp(cmd, "auto_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004668 res = video_autosw_set(1);
4669 if (res)
4670 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004671 } else if (strlencmp(cmd, "auto_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004672 res = video_autosw_set(0);
4673 if (res)
4674 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004675 } else if (strlencmp(cmd, "video_switch") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004676 res = video_outputsw_cycle();
4677 if (res)
4678 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004680 res = video_expand_toggle();
4681 if (res)
4682 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683 } else
4684 return -EINVAL;
4685 }
4686
4687 if (enable || disable) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004688 status = video_outputsw_get();
4689 if (status < 0)
4690 return status;
4691 res = video_outputsw_set((status & ~disable) | enable);
4692 if (res)
4693 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004694 }
4695
4696 return 0;
4697}
4698
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004699static struct ibm_struct video_driver_data = {
4700 .name = "video",
4701 .read = video_read,
4702 .write = video_write,
4703 .exit = video_exit,
4704};
4705
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02004706#endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4707
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004708/*************************************************************************
4709 * Light (thinklight) subdriver
4710 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004711
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004712TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4713TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004714
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004715static int light_get_status(void)
4716{
4717 int status = 0;
4718
4719 if (tp_features.light_status) {
4720 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4721 return -EIO;
4722 return (!!status);
4723 }
4724
4725 return -ENXIO;
4726}
4727
4728static int light_set_status(int status)
4729{
4730 int rc;
4731
4732 if (tp_features.light) {
4733 if (cmos_handle) {
4734 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4735 (status)?
4736 TP_CMOS_THINKLIGHT_ON :
4737 TP_CMOS_THINKLIGHT_OFF);
4738 } else {
4739 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4740 (status)? 1 : 0);
4741 }
4742 return (rc)? 0 : -EIO;
4743 }
4744
4745 return -ENXIO;
4746}
4747
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004748static void light_set_status_worker(struct work_struct *work)
4749{
4750 struct tpacpi_led_classdev *data =
4751 container_of(work, struct tpacpi_led_classdev, work);
4752
4753 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00004754 light_set_status((data->new_state != TPACPI_LED_OFF));
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004755}
4756
4757static void light_sysfs_set(struct led_classdev *led_cdev,
4758 enum led_brightness brightness)
4759{
4760 struct tpacpi_led_classdev *data =
4761 container_of(led_cdev,
4762 struct tpacpi_led_classdev,
4763 led_classdev);
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00004764 data->new_state = (brightness != LED_OFF) ?
4765 TPACPI_LED_ON : TPACPI_LED_OFF;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004766 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004767}
4768
4769static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4770{
4771 return (light_get_status() == 1)? LED_FULL : LED_OFF;
4772}
4773
4774static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4775 .led_classdev = {
4776 .name = "tpacpi::thinklight",
4777 .brightness_set = &light_sysfs_set,
4778 .brightness_get = &light_sysfs_get,
4779 }
4780};
4781
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004782static int __init light_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783{
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004784 int rc;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004785
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004786 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4787
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004788 TPACPI_ACPIHANDLE_INIT(ledb);
4789 TPACPI_ACPIHANDLE_INIT(lght);
4790 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004791 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004792
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004793 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004794 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004795
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004796 if (tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004797 /* light status not supported on
4798 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004799 tp_features.light_status =
4800 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004802 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4803 str_supported(tp_features.light),
4804 str_supported(tp_features.light_status));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004805
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004806 if (!tp_features.light)
4807 return 1;
4808
4809 rc = led_classdev_register(&tpacpi_pdev->dev,
4810 &tpacpi_led_thinklight.led_classdev);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004811
4812 if (rc < 0) {
4813 tp_features.light = 0;
4814 tp_features.light_status = 0;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004815 } else {
4816 rc = 0;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004817 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004818
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004819 return rc;
4820}
4821
4822static void light_exit(void)
4823{
4824 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
4825 if (work_pending(&tpacpi_led_thinklight.work))
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004826 flush_workqueue(tpacpi_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004827}
4828
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004829static int light_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004830{
4831 int len = 0;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004832 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004833
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004834 if (!tp_features.light) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004835 len += sprintf(p + len, "status:\t\tnot supported\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004836 } else if (!tp_features.light_status) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004837 len += sprintf(p + len, "status:\t\tunknown\n");
4838 len += sprintf(p + len, "commands:\ton, off\n");
4839 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004840 status = light_get_status();
4841 if (status < 0)
4842 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004844 len += sprintf(p + len, "commands:\ton, off\n");
4845 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846
4847 return len;
4848}
4849
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004850static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004851{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004853 int newstatus = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004854
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004855 if (!tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004856 return -ENODEV;
4857
Linus Torvalds1da177e2005-04-16 15:20:36 -07004858 while ((cmd = next_cmd(&buf))) {
4859 if (strlencmp(cmd, "on") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004860 newstatus = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004861 } else if (strlencmp(cmd, "off") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004862 newstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863 } else
4864 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865 }
4866
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004867 return light_set_status(newstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004868}
4869
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004870static struct ibm_struct light_driver_data = {
4871 .name = "light",
4872 .read = light_read,
4873 .write = light_write,
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004874 .exit = light_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004875};
4876
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004877/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004878 * CMOS subdriver
4879 */
4880
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004881/* sysfs cmos_command -------------------------------------------------- */
4882static ssize_t cmos_command_store(struct device *dev,
4883 struct device_attribute *attr,
4884 const char *buf, size_t count)
4885{
4886 unsigned long cmos_cmd;
4887 int res;
4888
4889 if (parse_strtoul(buf, 21, &cmos_cmd))
4890 return -EINVAL;
4891
4892 res = issue_thinkpad_cmos_command(cmos_cmd);
4893 return (res)? res : count;
4894}
4895
4896static struct device_attribute dev_attr_cmos_command =
4897 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4898
4899/* --------------------------------------------------------------------- */
4900
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004901static int __init cmos_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004902{
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004903 int res;
4904
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004905 vdbg_printk(TPACPI_DBG_INIT,
4906 "initializing cmos commands subdriver\n");
4907
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004908 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004909
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004910 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4911 str_supported(cmos_handle != NULL));
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004912
4913 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4914 if (res)
4915 return res;
4916
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004917 return (cmos_handle)? 0 : 1;
4918}
4919
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004920static void cmos_exit(void)
4921{
4922 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4923}
4924
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004925static int cmos_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004926{
4927 int len = 0;
4928
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004929 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4930 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004931 if (!cmos_handle)
4932 len += sprintf(p + len, "status:\t\tnot supported\n");
4933 else {
4934 len += sprintf(p + len, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004935 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004936 }
4937
4938 return len;
4939}
4940
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004941static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942{
4943 char *cmd;
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004944 int cmos_cmd, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945
4946 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004947 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
4948 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949 /* cmos_cmd set */
4950 } else
4951 return -EINVAL;
4952
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004953 res = issue_thinkpad_cmos_command(cmos_cmd);
4954 if (res)
4955 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004956 }
4957
4958 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004959}
4960
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004961static struct ibm_struct cmos_driver_data = {
4962 .name = "cmos",
4963 .read = cmos_read,
4964 .write = cmos_write,
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004965 .exit = cmos_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004966};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004967
4968/*************************************************************************
4969 * LED subdriver
4970 */
4971
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004972enum led_access_mode {
4973 TPACPI_LED_NONE = 0,
4974 TPACPI_LED_570, /* 570 */
4975 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4976 TPACPI_LED_NEW, /* all others */
4977};
4978
4979enum { /* For TPACPI_LED_OLD */
4980 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
4981 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
4982 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
4983};
4984
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02004985static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004986
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004987TPACPI_HANDLE(led, ec, "SLED", /* 570 */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004988 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
4989 /* T20-22, X20-21 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004990 "LED", /* all others */
4991 ); /* R30, R31 */
4992
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03004993#define TPACPI_LED_NUMLEDS 16
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004994static struct tpacpi_led_classdev *tpacpi_leds;
4995static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
Harvey Harrisone3aa51f2008-05-29 17:51:57 -07004996static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004997 /* there's a limit of 19 chars + NULL before 2.6.26 */
4998 "tpacpi::power",
4999 "tpacpi:orange:batt",
5000 "tpacpi:green:batt",
5001 "tpacpi::dock_active",
5002 "tpacpi::bay_active",
5003 "tpacpi::dock_batt",
5004 "tpacpi::unknown_led",
5005 "tpacpi::standby",
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005006 "tpacpi::dock_status1",
5007 "tpacpi::dock_status2",
5008 "tpacpi::unknown_led2",
5009 "tpacpi::unknown_led3",
5010 "tpacpi::thinkvantage",
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005011};
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005012#define TPACPI_SAFE_LEDS 0x1081U
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005013
5014static inline bool tpacpi_is_led_restricted(const unsigned int led)
5015{
5016#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5017 return false;
5018#else
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005019 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005020#endif
5021}
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005022
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005023static int led_get_status(const unsigned int led)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005024{
5025 int status;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005026 enum led_status_t led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005027
5028 switch (led_supported) {
5029 case TPACPI_LED_570:
5030 if (!acpi_evalf(ec_handle,
5031 &status, "GLED", "dd", 1 << led))
5032 return -EIO;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005033 led_s = (status == 0)?
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005034 TPACPI_LED_OFF :
5035 ((status == 1)?
5036 TPACPI_LED_ON :
5037 TPACPI_LED_BLINK);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005038 tpacpi_led_state_cache[led] = led_s;
5039 return led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005040 default:
5041 return -ENXIO;
5042 }
5043
5044 /* not reached */
5045}
5046
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005047static int led_set_status(const unsigned int led,
5048 const enum led_status_t ledstatus)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005049{
5050 /* off, on, blink. Index is led_status_t */
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005051 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
5052 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005053
5054 int rc = 0;
5055
5056 switch (led_supported) {
5057 case TPACPI_LED_570:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005058 /* 570 */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005059 if (unlikely(led > 7))
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005060 return -EINVAL;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005061 if (unlikely(tpacpi_is_led_restricted(led)))
5062 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005063 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5064 (1 << led), led_sled_arg1[ledstatus]))
5065 rc = -EIO;
5066 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005067 case TPACPI_LED_OLD:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005068 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005069 if (unlikely(led > 7))
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005070 return -EINVAL;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005071 if (unlikely(tpacpi_is_led_restricted(led)))
5072 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005073 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
5074 if (rc >= 0)
5075 rc = ec_write(TPACPI_LED_EC_HLBL,
5076 (ledstatus == TPACPI_LED_BLINK) << led);
5077 if (rc >= 0)
5078 rc = ec_write(TPACPI_LED_EC_HLCL,
5079 (ledstatus != TPACPI_LED_OFF) << led);
5080 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005081 case TPACPI_LED_NEW:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005082 /* all others */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005083 if (unlikely(led >= TPACPI_LED_NUMLEDS))
5084 return -EINVAL;
5085 if (unlikely(tpacpi_is_led_restricted(led)))
5086 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005087 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5088 led, led_led_arg1[ledstatus]))
5089 rc = -EIO;
5090 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005091 default:
5092 rc = -ENXIO;
5093 }
5094
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005095 if (!rc)
5096 tpacpi_led_state_cache[led] = ledstatus;
5097
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005098 return rc;
5099}
5100
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005101static void led_set_status_worker(struct work_struct *work)
5102{
5103 struct tpacpi_led_classdev *data =
5104 container_of(work, struct tpacpi_led_classdev, work);
5105
5106 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005107 led_set_status(data->led, data->new_state);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005108}
5109
5110static void led_sysfs_set(struct led_classdev *led_cdev,
5111 enum led_brightness brightness)
5112{
5113 struct tpacpi_led_classdev *data = container_of(led_cdev,
5114 struct tpacpi_led_classdev, led_classdev);
5115
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005116 if (brightness == LED_OFF)
5117 data->new_state = TPACPI_LED_OFF;
5118 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
5119 data->new_state = TPACPI_LED_ON;
5120 else
5121 data->new_state = TPACPI_LED_BLINK;
5122
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03005123 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005124}
5125
5126static int led_sysfs_blink_set(struct led_classdev *led_cdev,
5127 unsigned long *delay_on, unsigned long *delay_off)
5128{
5129 struct tpacpi_led_classdev *data = container_of(led_cdev,
5130 struct tpacpi_led_classdev, led_classdev);
5131
5132 /* Can we choose the flash rate? */
5133 if (*delay_on == 0 && *delay_off == 0) {
5134 /* yes. set them to the hardware blink rate (1 Hz) */
5135 *delay_on = 500; /* ms */
5136 *delay_off = 500; /* ms */
5137 } else if ((*delay_on != 500) || (*delay_off != 500))
5138 return -EINVAL;
5139
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005140 data->new_state = TPACPI_LED_BLINK;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03005141 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005142
5143 return 0;
5144}
5145
5146static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
5147{
5148 int rc;
5149
5150 struct tpacpi_led_classdev *data = container_of(led_cdev,
5151 struct tpacpi_led_classdev, led_classdev);
5152
5153 rc = led_get_status(data->led);
5154
5155 if (rc == TPACPI_LED_OFF || rc < 0)
5156 rc = LED_OFF; /* no error handling in led class :( */
5157 else
5158 rc = LED_FULL;
5159
5160 return rc;
5161}
5162
5163static void led_exit(void)
5164{
5165 unsigned int i;
5166
5167 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5168 if (tpacpi_leds[i].led_classdev.name)
5169 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
5170 }
5171
5172 kfree(tpacpi_leds);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005173}
5174
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005175static int __init tpacpi_init_led(unsigned int led)
5176{
5177 int rc;
5178
5179 tpacpi_leds[led].led = led;
5180
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005181 /* LEDs with no name don't get registered */
5182 if (!tpacpi_led_names[led])
5183 return 0;
5184
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005185 tpacpi_leds[led].led_classdev.brightness_set = &led_sysfs_set;
5186 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5187 if (led_supported == TPACPI_LED_570)
5188 tpacpi_leds[led].led_classdev.brightness_get =
5189 &led_sysfs_get;
5190
5191 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5192
5193 INIT_WORK(&tpacpi_leds[led].work, led_set_status_worker);
5194
5195 rc = led_classdev_register(&tpacpi_pdev->dev,
5196 &tpacpi_leds[led].led_classdev);
5197 if (rc < 0)
5198 tpacpi_leds[led].led_classdev.name = NULL;
5199
5200 return rc;
5201}
5202
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005203static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
5204 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5205 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5206 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5207
5208 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5209 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5210 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5211 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5212 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5213 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5214 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5215 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5216
5217 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5218 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5219 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5220 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5221 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5222
5223 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5224 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5225 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5226 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5227
5228 /* (1) - may have excess leds enabled on MSB */
5229
5230 /* Defaults (order matters, keep last, don't reorder!) */
5231 { /* Lenovo */
5232 .vendor = PCI_VENDOR_ID_LENOVO,
5233 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5234 .quirks = 0x1fffU,
5235 },
5236 { /* IBM ThinkPads with no EC version string */
5237 .vendor = PCI_VENDOR_ID_IBM,
5238 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
5239 .quirks = 0x00ffU,
5240 },
5241 { /* IBM ThinkPads with EC version string */
5242 .vendor = PCI_VENDOR_ID_IBM,
5243 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5244 .quirks = 0x00bfU,
5245 },
5246};
5247
5248#undef TPACPI_LEDQ_IBM
5249#undef TPACPI_LEDQ_LNV
5250
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005251static int __init led_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005252{
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005253 unsigned int i;
5254 int rc;
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005255 unsigned long useful_leds;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005256
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005257 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
5258
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005259 TPACPI_ACPIHANDLE_INIT(led);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005260
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005261 if (!led_handle)
5262 /* led not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005263 led_supported = TPACPI_LED_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005264 else if (strlencmp(led_path, "SLED") == 0)
5265 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005266 led_supported = TPACPI_LED_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005267 else if (strlencmp(led_path, "SYSL") == 0)
5268 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005269 led_supported = TPACPI_LED_OLD;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005270 else
5271 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005272 led_supported = TPACPI_LED_NEW;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005273
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005274 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
5275 str_supported(led_supported), led_supported);
5276
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005277 if (led_supported == TPACPI_LED_NONE)
5278 return 1;
5279
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005280 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
5281 GFP_KERNEL);
5282 if (!tpacpi_leds) {
5283 printk(TPACPI_ERR "Out of memory for LED data\n");
5284 return -ENOMEM;
5285 }
5286
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005287 useful_leds = tpacpi_check_quirks(led_useful_qtable,
5288 ARRAY_SIZE(led_useful_qtable));
5289
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005290 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005291 if (!tpacpi_is_led_restricted(i) &&
5292 test_bit(i, &useful_leds)) {
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005293 rc = tpacpi_init_led(i);
5294 if (rc < 0) {
5295 led_exit();
5296 return rc;
5297 }
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005298 }
5299 }
5300
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005301#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005302 printk(TPACPI_NOTICE
5303 "warning: userspace override of important "
5304 "firmware LEDs is enabled\n");
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005305#endif
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005306 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005307}
5308
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005309#define str_led_status(s) \
5310 ((s) == TPACPI_LED_OFF ? "off" : \
5311 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005312
5313static int led_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005314{
5315 int len = 0;
5316
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005317 if (!led_supported) {
5318 len += sprintf(p + len, "status:\t\tnot supported\n");
5319 return len;
5320 }
5321 len += sprintf(p + len, "status:\t\tsupported\n");
5322
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005323 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005324 /* 570 */
5325 int i, status;
5326 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005327 status = led_get_status(i);
5328 if (status < 0)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005329 return -EIO;
5330 len += sprintf(p + len, "%d:\t\t%s\n",
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005331 i, str_led_status(status));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005332 }
5333 }
5334
Linus Torvalds1da177e2005-04-16 15:20:36 -07005335 len += sprintf(p + len, "commands:\t"
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005336 "<led> on, <led> off, <led> blink (<led> is 0-15)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005337
5338 return len;
5339}
5340
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005341static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005342{
5343 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005344 int led, rc;
5345 enum led_status_t s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005346
5347 if (!led_supported)
5348 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005349
5350 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005351 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 15)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005352 return -EINVAL;
5353
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005354 if (strstr(cmd, "off")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005355 s = TPACPI_LED_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005356 } else if (strstr(cmd, "on")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005357 s = TPACPI_LED_ON;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005358 } else if (strstr(cmd, "blink")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005359 s = TPACPI_LED_BLINK;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005360 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005361 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005362 }
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005363
5364 rc = led_set_status(led, s);
5365 if (rc < 0)
5366 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005367 }
5368
5369 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005370}
5371
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005372static struct ibm_struct led_driver_data = {
5373 .name = "led",
5374 .read = led_read,
5375 .write = led_write,
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005376 .exit = led_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005377};
5378
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005379/*************************************************************************
5380 * Beep subdriver
5381 */
5382
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005383TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005384
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005385#define TPACPI_BEEP_Q1 0x0001
5386
5387static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
5388 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
5389 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
5390};
5391
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005392static int __init beep_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005393{
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005394 unsigned long quirks;
5395
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005396 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
5397
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005398 TPACPI_ACPIHANDLE_INIT(beep);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005399
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005400 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
5401 str_supported(beep_handle != NULL));
5402
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005403 quirks = tpacpi_check_quirks(beep_quirk_table,
5404 ARRAY_SIZE(beep_quirk_table));
5405
5406 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
5407
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005408 return (beep_handle)? 0 : 1;
5409}
5410
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005411static int beep_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005412{
5413 int len = 0;
5414
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005415 if (!beep_handle)
5416 len += sprintf(p + len, "status:\t\tnot supported\n");
5417 else {
5418 len += sprintf(p + len, "status:\t\tsupported\n");
5419 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
5420 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005421
5422 return len;
5423}
5424
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005425static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005426{
5427 char *cmd;
5428 int beep_cmd;
5429
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005430 if (!beep_handle)
5431 return -ENODEV;
5432
Linus Torvalds1da177e2005-04-16 15:20:36 -07005433 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005434 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
5435 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436 /* beep_cmd set */
5437 } else
5438 return -EINVAL;
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005439 if (tp_features.beep_needs_two_args) {
5440 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
5441 beep_cmd, 0))
5442 return -EIO;
5443 } else {
5444 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
5445 beep_cmd))
5446 return -EIO;
5447 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005448 }
5449
5450 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005451}
5452
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005453static struct ibm_struct beep_driver_data = {
5454 .name = "beep",
5455 .read = beep_read,
5456 .write = beep_write,
5457};
5458
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005459/*************************************************************************
5460 * Thermal subdriver
5461 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005462
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005463enum thermal_access_mode {
5464 TPACPI_THERMAL_NONE = 0, /* No thermal support */
5465 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
5466 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
5467 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
5468 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
5469};
5470
5471enum { /* TPACPI_THERMAL_TPEC_* */
5472 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
5473 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
5474 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005475
5476 TPACPI_THERMAL_SENSOR_NA = -128000, /* Sensor not available */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005477};
5478
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005479
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005480#define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5481struct ibm_thermal_sensors_struct {
5482 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
5483};
5484
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005485static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005486
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005487/* idx is zero-based */
5488static int thermal_get_sensor(int idx, s32 *value)
5489{
5490 int t;
5491 s8 tmp;
5492 char tmpi[5];
5493
5494 t = TP_EC_THERMAL_TMP0;
5495
5496 switch (thermal_read_mode) {
5497#if TPACPI_MAX_THERMAL_SENSORS >= 16
5498 case TPACPI_THERMAL_TPEC_16:
5499 if (idx >= 8 && idx <= 15) {
5500 t = TP_EC_THERMAL_TMP8;
5501 idx -= 8;
5502 }
5503 /* fallthrough */
5504#endif
5505 case TPACPI_THERMAL_TPEC_8:
5506 if (idx <= 7) {
5507 if (!acpi_ec_read(t + idx, &tmp))
5508 return -EIO;
5509 *value = tmp * 1000;
5510 return 0;
5511 }
5512 break;
5513
5514 case TPACPI_THERMAL_ACPI_UPDT:
5515 if (idx <= 7) {
5516 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5517 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
5518 return -EIO;
5519 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5520 return -EIO;
5521 *value = (t - 2732) * 100;
5522 return 0;
5523 }
5524 break;
5525
5526 case TPACPI_THERMAL_ACPI_TMP07:
5527 if (idx <= 7) {
5528 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5529 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5530 return -EIO;
5531 if (t > 127 || t < -127)
5532 t = TP_EC_THERMAL_TMP_NA;
5533 *value = t * 1000;
5534 return 0;
5535 }
5536 break;
5537
5538 case TPACPI_THERMAL_NONE:
5539 default:
5540 return -ENOSYS;
5541 }
5542
5543 return -EINVAL;
5544}
5545
5546static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5547{
5548 int res, i;
5549 int n;
5550
5551 n = 8;
5552 i = 0;
5553
5554 if (!s)
5555 return -EINVAL;
5556
5557 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5558 n = 16;
5559
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005560 for (i = 0 ; i < n; i++) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005561 res = thermal_get_sensor(i, &s->temp[i]);
5562 if (res)
5563 return res;
5564 }
5565
5566 return n;
5567}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005568
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005569static void thermal_dump_all_sensors(void)
5570{
5571 int n, i;
5572 struct ibm_thermal_sensors_struct t;
5573
5574 n = thermal_get_sensors(&t);
5575 if (n <= 0)
5576 return;
5577
5578 printk(TPACPI_NOTICE
5579 "temperatures (Celsius):");
5580
5581 for (i = 0; i < n; i++) {
5582 if (t.temp[i] != TPACPI_THERMAL_SENSOR_NA)
5583 printk(KERN_CONT " %d", (int)(t.temp[i] / 1000));
5584 else
5585 printk(KERN_CONT " N/A");
5586 }
5587
5588 printk(KERN_CONT "\n");
5589}
5590
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005591/* sysfs temp##_input -------------------------------------------------- */
5592
5593static ssize_t thermal_temp_input_show(struct device *dev,
5594 struct device_attribute *attr,
5595 char *buf)
5596{
5597 struct sensor_device_attribute *sensor_attr =
5598 to_sensor_dev_attr(attr);
5599 int idx = sensor_attr->index;
5600 s32 value;
5601 int res;
5602
5603 res = thermal_get_sensor(idx, &value);
5604 if (res)
5605 return res;
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005606 if (value == TPACPI_THERMAL_SENSOR_NA)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005607 return -ENXIO;
5608
5609 return snprintf(buf, PAGE_SIZE, "%d\n", value);
5610}
5611
5612#define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005613 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5614 thermal_temp_input_show, NULL, _idxB)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005615
5616static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5617 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5618 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5619 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5620 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5621 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5622 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5623 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5624 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5625 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5626 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5627 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5628 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5629 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5630 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5631 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5632 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5633};
5634
5635#define THERMAL_ATTRS(X) \
5636 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5637
5638static struct attribute *thermal_temp_input_attr[] = {
5639 THERMAL_ATTRS(8),
5640 THERMAL_ATTRS(9),
5641 THERMAL_ATTRS(10),
5642 THERMAL_ATTRS(11),
5643 THERMAL_ATTRS(12),
5644 THERMAL_ATTRS(13),
5645 THERMAL_ATTRS(14),
5646 THERMAL_ATTRS(15),
5647 THERMAL_ATTRS(0),
5648 THERMAL_ATTRS(1),
5649 THERMAL_ATTRS(2),
5650 THERMAL_ATTRS(3),
5651 THERMAL_ATTRS(4),
5652 THERMAL_ATTRS(5),
5653 THERMAL_ATTRS(6),
5654 THERMAL_ATTRS(7),
5655 NULL
5656};
5657
5658static const struct attribute_group thermal_temp_input16_group = {
5659 .attrs = thermal_temp_input_attr
5660};
5661
5662static const struct attribute_group thermal_temp_input8_group = {
5663 .attrs = &thermal_temp_input_attr[8]
5664};
5665
5666#undef THERMAL_SENSOR_ATTR_TEMP
5667#undef THERMAL_ATTRS
5668
5669/* --------------------------------------------------------------------- */
5670
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005671static int __init thermal_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005672{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005673 u8 t, ta1, ta2;
5674 int i;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005675 int acpi_tmp7;
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005676 int res;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005677
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005678 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5679
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005680 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005681
Henrique de Moraes Holschuh3d6f99c2007-07-18 23:45:46 -03005682 if (thinkpad_id.ec_model) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005683 /*
5684 * Direct EC access mode: sensors at registers
5685 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5686 * non-implemented, thermal sensors return 0x80 when
5687 * not available
5688 */
5689
5690 ta1 = ta2 = 0;
5691 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03005692 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005693 ta1 |= t;
5694 } else {
5695 ta1 = 0;
5696 break;
5697 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03005698 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005699 ta2 |= t;
5700 } else {
5701 ta1 = 0;
5702 break;
5703 }
5704 }
5705 if (ta1 == 0) {
5706 /* This is sheer paranoia, but we handle it anyway */
5707 if (acpi_tmp7) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005708 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005709 "ThinkPad ACPI EC access misbehaving, "
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005710 "falling back to ACPI TMPx access "
5711 "mode\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005712 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005713 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005714 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005715 "ThinkPad ACPI EC access misbehaving, "
5716 "disabling thermal sensors access\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005717 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005718 }
5719 } else {
5720 thermal_read_mode =
5721 (ta2 != 0) ?
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005722 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005723 }
5724 } else if (acpi_tmp7) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005725 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
5726 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005727 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005728 } else {
5729 /* Standard ACPI TMPx access, max 8 sensors */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005730 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005731 }
5732 } else {
5733 /* temperatures not supported on 570, G4x, R30, R31, R32 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005734 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005735 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005736
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005737 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5738 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5739 thermal_read_mode);
5740
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005741 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005742 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005743 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005744 &thermal_temp_input16_group);
5745 if (res)
5746 return res;
5747 break;
5748 case TPACPI_THERMAL_TPEC_8:
5749 case TPACPI_THERMAL_ACPI_TMP07:
5750 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005751 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005752 &thermal_temp_input8_group);
5753 if (res)
5754 return res;
5755 break;
5756 case TPACPI_THERMAL_NONE:
5757 default:
5758 return 1;
5759 }
5760
5761 return 0;
5762}
5763
5764static void thermal_exit(void)
5765{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005766 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005767 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005768 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005769 &thermal_temp_input16_group);
5770 break;
5771 case TPACPI_THERMAL_TPEC_8:
5772 case TPACPI_THERMAL_ACPI_TMP07:
5773 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005774 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005775 &thermal_temp_input16_group);
5776 break;
5777 case TPACPI_THERMAL_NONE:
5778 default:
5779 break;
5780 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005781}
5782
5783static int thermal_read(char *p)
5784{
5785 int len = 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005786 int n, i;
5787 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005788
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005789 n = thermal_get_sensors(&t);
5790 if (unlikely(n < 0))
5791 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005792
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005793 len += sprintf(p + len, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005794
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005795 if (n > 0) {
5796 for (i = 0; i < (n - 1); i++)
5797 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
5798 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
5799 } else
5800 len += sprintf(p + len, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005801
5802 return len;
5803}
5804
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005805static struct ibm_struct thermal_driver_data = {
5806 .name = "thermal",
5807 .read = thermal_read,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005808 .exit = thermal_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005809};
5810
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005811/*************************************************************************
5812 * EC Dump subdriver
5813 */
5814
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005815static u8 ecdump_regs[256];
5816
5817static int ecdump_read(char *p)
5818{
5819 int len = 0;
5820 int i, j;
5821 u8 v;
5822
5823 len += sprintf(p + len, "EC "
5824 " +00 +01 +02 +03 +04 +05 +06 +07"
5825 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
5826 for (i = 0; i < 256; i += 16) {
5827 len += sprintf(p + len, "EC 0x%02x:", i);
5828 for (j = 0; j < 16; j++) {
5829 if (!acpi_ec_read(i + j, &v))
5830 break;
5831 if (v != ecdump_regs[i + j])
5832 len += sprintf(p + len, " *%02x", v);
5833 else
5834 len += sprintf(p + len, " %02x", v);
5835 ecdump_regs[i + j] = v;
5836 }
5837 len += sprintf(p + len, "\n");
5838 if (j != 16)
5839 break;
5840 }
5841
5842 /* These are way too dangerous to advertise openly... */
5843#if 0
5844 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
5845 " (<offset> is 00-ff, <value> is 00-ff)\n");
5846 len += sprintf(p + len, "commands:\t0x<offset> <value> "
5847 " (<offset> is 00-ff, <value> is 0-255)\n");
5848#endif
5849 return len;
5850}
5851
5852static int ecdump_write(char *buf)
5853{
5854 char *cmd;
5855 int i, v;
5856
5857 while ((cmd = next_cmd(&buf))) {
5858 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
5859 /* i and v set */
5860 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
5861 /* i and v set */
5862 } else
5863 return -EINVAL;
5864 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
5865 if (!acpi_ec_write(i, v))
5866 return -EIO;
5867 } else
5868 return -EINVAL;
5869 }
5870
5871 return 0;
5872}
5873
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005874static struct ibm_struct ecdump_driver_data = {
5875 .name = "ecdump",
5876 .read = ecdump_read,
5877 .write = ecdump_write,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03005878 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005879};
5880
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005881/*************************************************************************
5882 * Backlight/brightness subdriver
5883 */
Holger Macht8acb0252006-10-20 14:30:28 -07005884
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005885#define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5886
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005887/*
5888 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
5889 * CMOS NVRAM byte 0x5E, bits 0-3.
5890 *
5891 * EC HBRV (0x31) has the following layout
5892 * Bit 7: unknown function
5893 * Bit 6: unknown function
5894 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5895 * Bit 4: must be set to zero to avoid problems
5896 * Bit 3-0: backlight brightness level
5897 *
5898 * brightness_get_raw returns status data in the HBRV layout
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03005899 *
5900 * WARNING: The X61 has been verified to use HBRV for something else, so
5901 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
5902 * testing on the very early *60 Lenovo models...
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005903 */
5904
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005905enum {
5906 TP_EC_BACKLIGHT = 0x31,
5907
5908 /* TP_EC_BACKLIGHT bitmasks */
5909 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5910 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5911 TP_EC_BACKLIGHT_MAPSW = 0x20,
5912};
5913
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005914enum tpacpi_brightness_access_mode {
5915 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
5916 TPACPI_BRGHT_MODE_EC, /* EC control */
5917 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
5918 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
5919 TPACPI_BRGHT_MODE_MAX
5920};
5921
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03005922static struct backlight_device *ibm_backlight_device;
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005923
5924static enum tpacpi_brightness_access_mode brightness_mode =
5925 TPACPI_BRGHT_MODE_MAX;
5926
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005927static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5928
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005929static struct mutex brightness_mutex;
5930
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005931/* NVRAM brightness access,
5932 * call with brightness_mutex held! */
5933static unsigned int tpacpi_brightness_nvram_get(void)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005934{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005935 u8 lnvram;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005936
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005937 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5938 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5939 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
5940 lnvram &= (tp_features.bright_16levels) ? 0x0f : 0x07;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005941
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005942 return lnvram;
5943}
5944
5945static void tpacpi_brightness_checkpoint_nvram(void)
5946{
5947 u8 lec = 0;
5948 u8 b_nvram;
5949
5950 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
5951 return;
5952
5953 vdbg_printk(TPACPI_DBG_BRGHT,
5954 "trying to checkpoint backlight level to NVRAM...\n");
5955
5956 if (mutex_lock_killable(&brightness_mutex) < 0)
5957 return;
5958
5959 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5960 goto unlock;
5961 lec &= TP_EC_BACKLIGHT_LVLMSK;
5962 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
5963
5964 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5965 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
5966 /* NVRAM needs update */
5967 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
5968 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
5969 b_nvram |= lec;
5970 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
5971 dbg_printk(TPACPI_DBG_BRGHT,
5972 "updated NVRAM backlight level to %u (0x%02x)\n",
5973 (unsigned int) lec, (unsigned int) b_nvram);
5974 } else
5975 vdbg_printk(TPACPI_DBG_BRGHT,
5976 "NVRAM backlight level already is %u (0x%02x)\n",
5977 (unsigned int) lec, (unsigned int) b_nvram);
5978
5979unlock:
5980 mutex_unlock(&brightness_mutex);
5981}
5982
5983
5984/* call with brightness_mutex held! */
5985static int tpacpi_brightness_get_raw(int *status)
5986{
5987 u8 lec = 0;
5988
5989 switch (brightness_mode) {
5990 case TPACPI_BRGHT_MODE_UCMS_STEP:
5991 *status = tpacpi_brightness_nvram_get();
5992 return 0;
5993 case TPACPI_BRGHT_MODE_EC:
5994 case TPACPI_BRGHT_MODE_ECNVRAM:
5995 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
Henrique de Moraes Holschuh2d5e94d2008-04-26 01:02:20 -03005996 return -EIO;
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005997 *status = lec;
5998 return 0;
5999 default:
6000 return -ENXIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006001 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006002}
6003
6004/* call with brightness_mutex held! */
6005/* do NOT call with illegal backlight level value */
6006static int tpacpi_brightness_set_ec(unsigned int value)
6007{
6008 u8 lec = 0;
6009
6010 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6011 return -EIO;
6012
6013 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
6014 (lec & TP_EC_BACKLIGHT_CMDMSK) |
6015 (value & TP_EC_BACKLIGHT_LVLMSK))))
6016 return -EIO;
6017
6018 return 0;
6019}
6020
6021/* call with brightness_mutex held! */
6022static int tpacpi_brightness_set_ucmsstep(unsigned int value)
6023{
6024 int cmos_cmd, inc;
6025 unsigned int current_value, i;
6026
6027 current_value = tpacpi_brightness_nvram_get();
6028
6029 if (value == current_value)
6030 return 0;
6031
6032 cmos_cmd = (value > current_value) ?
6033 TP_CMOS_BRIGHTNESS_UP :
6034 TP_CMOS_BRIGHTNESS_DOWN;
6035 inc = (value > current_value) ? 1 : -1;
6036
6037 for (i = current_value; i != value; i += inc)
6038 if (issue_thinkpad_cmos_command(cmos_cmd))
6039 return -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006040
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006041 return 0;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006042}
6043
6044/* May return EINTR which can always be mapped to ERESTARTSYS */
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006045static int brightness_set(unsigned int value)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006046{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006047 int res;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006048
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006049 if (value > ((tp_features.bright_16levels)? 15 : 7) ||
6050 value < 0)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006051 return -EINVAL;
6052
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006053 vdbg_printk(TPACPI_DBG_BRGHT,
6054 "set backlight level to %d\n", value);
6055
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006056 res = mutex_lock_killable(&brightness_mutex);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006057 if (res < 0)
6058 return res;
6059
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006060 switch (brightness_mode) {
6061 case TPACPI_BRGHT_MODE_EC:
6062 case TPACPI_BRGHT_MODE_ECNVRAM:
6063 res = tpacpi_brightness_set_ec(value);
6064 break;
6065 case TPACPI_BRGHT_MODE_UCMS_STEP:
6066 res = tpacpi_brightness_set_ucmsstep(value);
6067 break;
6068 default:
6069 res = -ENXIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006070 }
6071
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006072 mutex_unlock(&brightness_mutex);
6073 return res;
6074}
6075
6076/* sysfs backlight class ----------------------------------------------- */
6077
6078static int brightness_update_status(struct backlight_device *bd)
6079{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006080 unsigned int level =
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006081 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
6082 bd->props.power == FB_BLANK_UNBLANK) ?
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006083 bd->props.brightness : 0;
6084
6085 dbg_printk(TPACPI_DBG_BRGHT,
6086 "backlight: attempt to set level to %d\n",
6087 level);
6088
6089 /* it is the backlight class's job (caller) to handle
6090 * EINTR and other errors properly */
6091 return brightness_set(level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006092}
Holger Macht8acb0252006-10-20 14:30:28 -07006093
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006094static int brightness_get(struct backlight_device *bd)
6095{
6096 int status, res;
6097
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006098 res = mutex_lock_killable(&brightness_mutex);
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006099 if (res < 0)
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006100 return 0;
6101
6102 res = tpacpi_brightness_get_raw(&status);
6103
6104 mutex_unlock(&brightness_mutex);
6105
6106 if (res < 0)
6107 return 0;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006108
6109 return status & TP_EC_BACKLIGHT_LVLMSK;
6110}
6111
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00006112static void tpacpi_brightness_notify_change(void)
6113{
6114 backlight_force_update(ibm_backlight_device,
6115 BACKLIGHT_UPDATE_HOTKEY);
6116}
6117
Richard Purdie599a52d2007-02-10 23:07:48 +00006118static struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006119 .get_brightness = brightness_get,
6120 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006121};
6122
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006123/* --------------------------------------------------------------------- */
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03006124
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006125/*
6126 * These are only useful for models that have only one possibility
6127 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6128 * these quirks.
6129 */
6130#define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6131#define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6132#define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6133
6134static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
6135 /* Models with ATI GPUs known to require ECNVRAM mode */
6136 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
6137
Henrique de Moraes Holschuh6da25bf2009-09-12 15:22:11 -03006138 /* Models with ATI GPUs that can use ECNVRAM */
6139 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC),
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006140 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6141 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6142 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6143
Henrique de Moraes Holschuh6da25bf2009-09-12 15:22:11 -03006144 /* Models with Intel Extreme Graphics 2 */
6145 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC),
Henrique de Moraes Holschuha9f8eac2009-12-09 01:36:21 +00006146 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6147 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006148
6149 /* Models with Intel GMA900 */
6150 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
6151 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
6152 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
6153};
6154
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006155static int __init brightness_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006156{
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006157 int b;
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006158 unsigned long quirks;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006159
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006160 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
6161
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03006162 mutex_init(&brightness_mutex);
6163
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006164 quirks = tpacpi_check_quirks(brightness_quirk_table,
6165 ARRAY_SIZE(brightness_quirk_table));
6166
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03006167 /*
6168 * We always attempt to detect acpi support, so as to switch
6169 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6170 * going to publish a backlight interface
6171 */
6172 b = tpacpi_check_std_acpi_brightness_support();
6173 if (b > 0) {
Thomas Renninger2dba1b52008-08-01 17:38:03 +02006174
6175 if (acpi_video_backlight_support()) {
6176 if (brightness_enable > 1) {
6177 printk(TPACPI_NOTICE
6178 "Standard ACPI backlight interface "
6179 "available, not loading native one.\n");
6180 return 1;
6181 } else if (brightness_enable == 1) {
6182 printk(TPACPI_NOTICE
6183 "Backlight control force enabled, even if standard "
6184 "ACPI backlight interface is available\n");
6185 }
6186 } else {
6187 if (brightness_enable > 1) {
6188 printk(TPACPI_NOTICE
6189 "Standard ACPI backlight interface not "
6190 "available, thinkpad_acpi native "
6191 "brightness control enabled\n");
6192 }
Henrique de Moraes Holschuhe11e2112007-10-30 17:46:22 -02006193 }
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02006194 }
6195
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03006196 if (!brightness_enable) {
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006197 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03006198 "brightness support disabled by "
6199 "module parameter\n");
6200 return 1;
6201 }
6202
6203 if (b > 16) {
6204 printk(TPACPI_ERR
6205 "Unsupported brightness interface, "
6206 "please contact %s\n", TPACPI_MAIL);
6207 return 1;
6208 }
6209 if (b == 16)
6210 tp_features.bright_16levels = 1;
6211
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006212 /*
6213 * Check for module parameter bogosity, note that we
6214 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
6215 * able to detect "unspecified"
6216 */
6217 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03006218 return -EINVAL;
6219
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006220 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
6221 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
6222 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006223 if (quirks & TPACPI_BRGHT_Q_EC)
6224 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
6225 else
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006226 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
6227
6228 dbg_printk(TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006229 "driver auto-selected brightness_mode=%d\n",
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006230 brightness_mode);
6231 }
6232
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03006233 /* Safety */
6234 if (thinkpad_id.vendor != PCI_VENDOR_ID_IBM &&
6235 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
6236 brightness_mode == TPACPI_BRGHT_MODE_EC))
6237 return -EINVAL;
6238
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006239 if (tpacpi_brightness_get_raw(&b) < 0)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03006240 return 1;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006241
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02006242 if (tp_features.bright_16levels)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006243 printk(TPACPI_INFO
6244 "detected a 16-level brightness capable ThinkPad\n");
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02006245
Henrique de Moraes Holschuh7d5a0152007-04-24 11:48:20 -03006246 ibm_backlight_device = backlight_device_register(
6247 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
6248 &ibm_backlight_data);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006249 if (IS_ERR(ibm_backlight_device)) {
Henrique de Moraes Holschuh435c47e2009-09-20 14:09:22 -03006250 int rc = PTR_ERR(ibm_backlight_device);
6251 ibm_backlight_device = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006252 printk(TPACPI_ERR "Could not register backlight device\n");
Henrique de Moraes Holschuh435c47e2009-09-20 14:09:22 -03006253 return rc;
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006254 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006255 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6256 "brightness is supported\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006257
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006258 if (quirks & TPACPI_BRGHT_Q_ASK) {
6259 printk(TPACPI_NOTICE
6260 "brightness: will use unverified default: "
6261 "brightness_mode=%d\n", brightness_mode);
6262 printk(TPACPI_NOTICE
6263 "brightness: please report to %s whether it works well "
6264 "or not on your ThinkPad\n", TPACPI_MAIL);
6265 }
6266
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02006267 ibm_backlight_device->props.max_brightness =
6268 (tp_features.bright_16levels)? 15 : 7;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006269 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006270 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00006271
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00006272 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6273 "brightness: registering brightness hotkeys "
6274 "as change notification\n");
6275 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6276 | TP_ACPI_HKEY_BRGHTUP_MASK
6277 | TP_ACPI_HKEY_BRGHTDWN_MASK);;
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006278 return 0;
6279}
6280
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006281static void brightness_suspend(pm_message_t state)
6282{
6283 tpacpi_brightness_checkpoint_nvram();
6284}
6285
6286static void brightness_shutdown(void)
6287{
6288 tpacpi_brightness_checkpoint_nvram();
6289}
6290
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006291static void brightness_exit(void)
6292{
6293 if (ibm_backlight_device) {
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006294 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006295 "calling backlight_device_unregister()\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006296 backlight_device_unregister(ibm_backlight_device);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006297 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006298
6299 tpacpi_brightness_checkpoint_nvram();
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006300}
6301
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006302static int brightness_read(char *p)
6303{
6304 int len = 0;
6305 int level;
6306
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006307 level = brightness_get(NULL);
6308 if (level < 0) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006309 len += sprintf(p + len, "level:\t\tunreadable\n");
6310 } else {
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02006311 len += sprintf(p + len, "level:\t\t%d\n", level);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006312 len += sprintf(p + len, "commands:\tup, down\n");
6313 len += sprintf(p + len, "commands:\tlevel <level>"
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02006314 " (<level> is 0-%d)\n",
6315 (tp_features.bright_16levels) ? 15 : 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006316 }
6317
6318 return len;
6319}
6320
6321static int brightness_write(char *buf)
6322{
6323 int level;
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006324 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006325 char *cmd;
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02006326 int max_level = (tp_features.bright_16levels) ? 15 : 7;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006327
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006328 level = brightness_get(NULL);
6329 if (level < 0)
6330 return level;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006331
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006332 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006333 if (strlencmp(cmd, "up") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006334 if (level < max_level)
6335 level++;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006336 } else if (strlencmp(cmd, "down") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006337 if (level > 0)
6338 level--;
6339 } else if (sscanf(cmd, "level %d", &level) == 1 &&
6340 level >= 0 && level <= max_level) {
6341 /* new level set */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006342 } else
6343 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006344 }
6345
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006346 tpacpi_disclose_usertask("procfs brightness",
6347 "set level to %d\n", level);
6348
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006349 /*
6350 * Now we know what the final level should be, so we try to set it.
6351 * Doing it this way makes the syscall restartable in case of EINTR
6352 */
6353 rc = brightness_set(level);
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00006354 if (!rc && ibm_backlight_device)
6355 backlight_force_update(ibm_backlight_device,
6356 BACKLIGHT_UPDATE_SYSFS);
Roel Kluin80a8d122009-11-20 19:48:23 +01006357 return (rc == -EINTR)? -ERESTARTSYS : rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006358}
6359
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006360static struct ibm_struct brightness_driver_data = {
6361 .name = "brightness",
6362 .read = brightness_read,
6363 .write = brightness_write,
6364 .exit = brightness_exit,
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006365 .suspend = brightness_suspend,
6366 .shutdown = brightness_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006367};
6368
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006369/*************************************************************************
6370 * Volume subdriver
6371 */
6372
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006373static int volume_offset = 0x30;
6374
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006375static int volume_read(char *p)
6376{
6377 int len = 0;
6378 u8 level;
6379
6380 if (!acpi_ec_read(volume_offset, &level)) {
6381 len += sprintf(p + len, "level:\t\tunreadable\n");
6382 } else {
6383 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
6384 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
6385 len += sprintf(p + len, "commands:\tup, down, mute\n");
6386 len += sprintf(p + len, "commands:\tlevel <level>"
6387 " (<level> is 0-15)\n");
6388 }
6389
6390 return len;
6391}
6392
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006393static int volume_write(char *buf)
6394{
6395 int cmos_cmd, inc, i;
6396 u8 level, mute;
6397 int new_level, new_mute;
6398 char *cmd;
6399
6400 while ((cmd = next_cmd(&buf))) {
6401 if (!acpi_ec_read(volume_offset, &level))
6402 return -EIO;
6403 new_mute = mute = level & 0x40;
6404 new_level = level = level & 0xf;
6405
6406 if (strlencmp(cmd, "up") == 0) {
6407 if (mute)
6408 new_mute = 0;
6409 else
6410 new_level = level == 15 ? 15 : level + 1;
6411 } else if (strlencmp(cmd, "down") == 0) {
6412 if (mute)
6413 new_mute = 0;
6414 else
6415 new_level = level == 0 ? 0 : level - 1;
6416 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
6417 new_level >= 0 && new_level <= 15) {
6418 /* new_level set */
6419 } else if (strlencmp(cmd, "mute") == 0) {
6420 new_mute = 0x40;
6421 } else
6422 return -EINVAL;
6423
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006424 if (new_level != level) {
6425 /* mute doesn't change */
6426
6427 cmos_cmd = (new_level > level) ?
6428 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006429 inc = new_level > level ? 1 : -1;
6430
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03006431 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006432 !acpi_ec_write(volume_offset, level)))
6433 return -EIO;
6434
6435 for (i = level; i != new_level; i += inc)
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03006436 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006437 !acpi_ec_write(volume_offset, i + inc))
6438 return -EIO;
6439
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006440 if (mute &&
6441 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
6442 !acpi_ec_write(volume_offset, new_level + mute))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006443 return -EIO;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006444 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006445 }
6446
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006447 if (new_mute != mute) {
6448 /* level doesn't change */
6449
6450 cmos_cmd = (new_mute) ?
6451 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006452
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03006453 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006454 !acpi_ec_write(volume_offset, level + new_mute))
6455 return -EIO;
6456 }
6457 }
6458
6459 return 0;
6460}
6461
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006462static struct ibm_struct volume_driver_data = {
6463 .name = "volume",
6464 .read = volume_read,
6465 .write = volume_write,
6466};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006467
6468/*************************************************************************
6469 * Fan subdriver
6470 */
6471
6472/*
6473 * FAN ACCESS MODES
6474 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006475 * TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006476 * ACPI GFAN method: returns fan level
6477 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006478 * see TPACPI_FAN_WR_ACPI_SFAN
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03006479 * EC 0x2f (HFSP) not available if GFAN exists
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006480 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006481 * TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006482 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
6483 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03006484 * EC 0x2f (HFSP) might be available *for reading*, but do not use
6485 * it for writing.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006486 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006487 * TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03006488 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
6489 * Supported on almost all ThinkPads
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006490 *
6491 * Fan speed changes of any sort (including those caused by the
6492 * disengaged mode) are usually done slowly by the firmware as the
6493 * maximum ammount of fan duty cycle change per second seems to be
6494 * limited.
6495 *
6496 * Reading is not available if GFAN exists.
6497 * Writing is not available if SFAN exists.
6498 *
6499 * Bits
6500 * 7 automatic mode engaged;
6501 * (default operation mode of the ThinkPad)
6502 * fan level is ignored in this mode.
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03006503 * 6 full speed mode (takes precedence over bit 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006504 * not available on all thinkpads. May disable
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03006505 * the tachometer while the fan controller ramps up
6506 * the speed (which can take up to a few *minutes*).
6507 * Speeds up fan to 100% duty-cycle, which is far above
6508 * the standard RPM levels. It is not impossible that
6509 * it could cause hardware damage.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006510 * 5-3 unused in some models. Extra bits for fan level
6511 * in others, but still useless as all values above
6512 * 7 map to the same speed as level 7 in these models.
6513 * 2-0 fan level (0..7 usually)
6514 * 0x00 = stop
6515 * 0x07 = max (set when temperatures critical)
6516 * Some ThinkPads may have other levels, see
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006517 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006518 *
6519 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
6520 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
6521 * does so, its initial value is meaningless (0x07).
6522 *
6523 * For firmware bugs, refer to:
6524 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
6525 *
6526 * ----
6527 *
6528 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
6529 * Main fan tachometer reading (in RPM)
6530 *
6531 * This register is present on all ThinkPads with a new-style EC, and
6532 * it is known not to be present on the A21m/e, and T22, as there is
6533 * something else in offset 0x84 according to the ACPI DSDT. Other
6534 * ThinkPads from this same time period (and earlier) probably lack the
6535 * tachometer as well.
6536 *
Nick Andrew877d0312009-01-26 11:06:57 +01006537 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006538 * was never fixed by IBM to report the EC firmware version string
6539 * probably support the tachometer (like the early X models), so
6540 * detecting it is quite hard. We need more data to know for sure.
6541 *
6542 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
6543 * might result.
6544 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03006545 * FIRMWARE BUG: may go stale while the EC is switching to full speed
6546 * mode.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006547 *
6548 * For firmware bugs, refer to:
6549 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
6550 *
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03006551 * ----
6552 *
6553 * ThinkPad EC register 0x31 bit 0 (only on select models)
6554 *
6555 * When bit 0 of EC register 0x31 is zero, the tachometer registers
6556 * show the speed of the main fan. When bit 0 of EC register 0x31
6557 * is one, the tachometer registers show the speed of the auxiliary
6558 * fan.
6559 *
6560 * Fan control seems to affect both fans, regardless of the state
6561 * of this bit.
6562 *
6563 * So far, only the firmware for the X60/X61 non-tablet versions
6564 * seem to support this (firmware TP-7M).
6565 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006566 * TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006567 * ThinkPad X31, X40, X41. Not available in the X60.
6568 *
6569 * FANS ACPI handle: takes three arguments: low speed, medium speed,
6570 * high speed. ACPI DSDT seems to map these three speeds to levels
6571 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
6572 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
6573 *
6574 * The speeds are stored on handles
6575 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
6576 *
6577 * There are three default speed sets, acessible as handles:
6578 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
6579 *
6580 * ACPI DSDT switches which set is in use depending on various
6581 * factors.
6582 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006583 * TPACPI_FAN_WR_TPEC is also available and should be used to
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006584 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
6585 * but the ACPI tables just mention level 7.
6586 */
6587
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006588enum { /* Fan control constants */
6589 fan_status_offset = 0x2f, /* EC register 0x2f */
6590 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
6591 * 0x84 must be read before 0x85 */
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03006592 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
6593 bit 0 selects which fan is active */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006594
6595 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
6596 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
6597
6598 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
6599};
6600
6601enum fan_status_access_mode {
6602 TPACPI_FAN_NONE = 0, /* No fan status or control */
6603 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
6604 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
6605};
6606
6607enum fan_control_access_mode {
6608 TPACPI_FAN_WR_NONE = 0, /* No fan control */
6609 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
6610 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
6611 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
6612};
6613
6614enum fan_control_commands {
6615 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
6616 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
6617 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
6618 * and also watchdog cmd */
6619};
6620
6621static int fan_control_allowed;
6622
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006623static enum fan_status_access_mode fan_status_access_mode;
6624static enum fan_control_access_mode fan_control_access_mode;
6625static enum fan_control_commands fan_control_commands;
6626
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02006627static u8 fan_control_initial_status;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006628static u8 fan_control_desired_level;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006629static u8 fan_control_resume_level;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02006630static int fan_watchdog_maxinterval;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006631
6632static struct mutex fan_mutex;
6633
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006634static void fan_watchdog_fire(struct work_struct *ignored);
Len Brown25c68a32006-12-08 04:43:41 -05006635static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02006636
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006637TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
6638TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006639 "\\FSPD", /* 600e/x, 770e, 770x */
6640 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006641TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006642 "JFNS", /* 770x-JL */
6643 ); /* all others */
6644
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006645/*
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006646 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
6647 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
6648 * be in auto mode (0x80).
6649 *
6650 * This is corrected by any write to HFSP either by the driver, or
6651 * by the firmware.
6652 *
6653 * We assume 0x07 really means auto mode while this quirk is active,
6654 * as this is far more likely than the ThinkPad being in level 7,
6655 * which is only used by the firmware during thermal emergencies.
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03006656 *
6657 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
6658 * TP-70 (T43, R52), which are known to be buggy.
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006659 */
6660
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03006661static void fan_quirk1_setup(void)
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006662{
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006663 if (fan_control_initial_status == 0x07) {
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03006664 printk(TPACPI_NOTICE
6665 "fan_init: initial fan status is unknown, "
6666 "assuming it is in auto mode\n");
6667 tp_features.fan_ctrl_status_undef = 1;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006668 }
6669}
6670
6671static void fan_quirk1_handle(u8 *fan_status)
6672{
6673 if (unlikely(tp_features.fan_ctrl_status_undef)) {
6674 if (*fan_status != fan_control_initial_status) {
6675 /* something changed the HFSP regisnter since
6676 * driver init time, so it is not undefined
6677 * anymore */
6678 tp_features.fan_ctrl_status_undef = 0;
6679 } else {
6680 /* Return most likely status. In fact, it
6681 * might be the only possible status */
6682 *fan_status = TP_EC_FAN_AUTO;
6683 }
6684 }
6685}
6686
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03006687/* Select main fan on X60/X61, NOOP on others */
6688static bool fan_select_fan1(void)
6689{
6690 if (tp_features.second_fan) {
6691 u8 val;
6692
6693 if (ec_read(fan_select_offset, &val) < 0)
6694 return false;
6695 val &= 0xFEU;
6696 if (ec_write(fan_select_offset, val) < 0)
6697 return false;
6698 }
6699 return true;
6700}
6701
6702/* Select secondary fan on X60/X61 */
6703static bool fan_select_fan2(void)
6704{
6705 u8 val;
6706
6707 if (!tp_features.second_fan)
6708 return false;
6709
6710 if (ec_read(fan_select_offset, &val) < 0)
6711 return false;
6712 val |= 0x01U;
6713 if (ec_write(fan_select_offset, val) < 0)
6714 return false;
6715
6716 return true;
6717}
6718
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006719/*
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006720 * Call with fan_mutex held
6721 */
6722static void fan_update_desired_level(u8 status)
6723{
6724 if ((status &
6725 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
6726 if (status > 7)
6727 fan_control_desired_level = 7;
6728 else
6729 fan_control_desired_level = status;
6730 }
6731}
6732
6733static int fan_get_status(u8 *status)
6734{
6735 u8 s;
6736
6737 /* TODO:
6738 * Add TPACPI_FAN_RD_ACPI_FANS ? */
6739
6740 switch (fan_status_access_mode) {
6741 case TPACPI_FAN_RD_ACPI_GFAN:
6742 /* 570, 600e/x, 770e, 770x */
6743
6744 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
6745 return -EIO;
6746
6747 if (likely(status))
6748 *status = s & 0x07;
6749
6750 break;
6751
6752 case TPACPI_FAN_RD_TPEC:
6753 /* all except 570, 600e/x, 770e, 770x */
6754 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
6755 return -EIO;
6756
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006757 if (likely(status)) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006758 *status = s;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006759 fan_quirk1_handle(status);
6760 }
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006761
6762 break;
6763
6764 default:
6765 return -ENXIO;
6766 }
6767
6768 return 0;
6769}
6770
6771static int fan_get_status_safe(u8 *status)
6772{
6773 int rc;
6774 u8 s;
6775
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006776 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006777 return -ERESTARTSYS;
6778 rc = fan_get_status(&s);
6779 if (!rc)
6780 fan_update_desired_level(s);
6781 mutex_unlock(&fan_mutex);
6782
6783 if (status)
6784 *status = s;
6785
6786 return rc;
6787}
6788
6789static int fan_get_speed(unsigned int *speed)
6790{
6791 u8 hi, lo;
6792
6793 switch (fan_status_access_mode) {
6794 case TPACPI_FAN_RD_TPEC:
6795 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03006796 if (unlikely(!fan_select_fan1()))
6797 return -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006798 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
6799 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
6800 return -EIO;
6801
6802 if (likely(speed))
6803 *speed = (hi << 8) | lo;
6804
6805 break;
6806
6807 default:
6808 return -ENXIO;
6809 }
6810
6811 return 0;
6812}
6813
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03006814static int fan2_get_speed(unsigned int *speed)
6815{
6816 u8 hi, lo;
6817 bool rc;
6818
6819 switch (fan_status_access_mode) {
6820 case TPACPI_FAN_RD_TPEC:
6821 /* all except 570, 600e/x, 770e, 770x */
6822 if (unlikely(!fan_select_fan2()))
6823 return -EIO;
6824 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
6825 !acpi_ec_read(fan_rpm_offset + 1, &hi);
6826 fan_select_fan1(); /* play it safe */
6827 if (rc)
6828 return -EIO;
6829
6830 if (likely(speed))
6831 *speed = (hi << 8) | lo;
6832
6833 break;
6834
6835 default:
6836 return -ENXIO;
6837 }
6838
6839 return 0;
6840}
6841
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006842static int fan_set_level(int level)
6843{
6844 if (!fan_control_allowed)
6845 return -EPERM;
6846
6847 switch (fan_control_access_mode) {
6848 case TPACPI_FAN_WR_ACPI_SFAN:
6849 if (level >= 0 && level <= 7) {
6850 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
6851 return -EIO;
6852 } else
6853 return -EINVAL;
6854 break;
6855
6856 case TPACPI_FAN_WR_ACPI_FANS:
6857 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006858 if (!(level & TP_EC_FAN_AUTO) &&
6859 !(level & TP_EC_FAN_FULLSPEED) &&
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006860 ((level < 0) || (level > 7)))
6861 return -EINVAL;
6862
6863 /* safety net should the EC not support AUTO
6864 * or FULLSPEED mode bits and just ignore them */
6865 if (level & TP_EC_FAN_FULLSPEED)
6866 level |= 7; /* safety min speed 7 */
Roel Kluin547266e2008-02-05 00:24:56 +01006867 else if (level & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006868 level |= 4; /* safety min speed 4 */
6869
6870 if (!acpi_ec_write(fan_status_offset, level))
6871 return -EIO;
6872 else
6873 tp_features.fan_ctrl_status_undef = 0;
6874 break;
6875
6876 default:
6877 return -ENXIO;
6878 }
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00006879
6880 vdbg_printk(TPACPI_DBG_FAN,
6881 "fan control: set fan control register to 0x%02x\n", level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006882 return 0;
6883}
6884
6885static int fan_set_level_safe(int level)
6886{
6887 int rc;
6888
6889 if (!fan_control_allowed)
6890 return -EPERM;
6891
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006892 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006893 return -ERESTARTSYS;
6894
6895 if (level == TPACPI_FAN_LAST_LEVEL)
6896 level = fan_control_desired_level;
6897
6898 rc = fan_set_level(level);
6899 if (!rc)
6900 fan_update_desired_level(level);
6901
6902 mutex_unlock(&fan_mutex);
6903 return rc;
6904}
6905
6906static int fan_set_enable(void)
6907{
6908 u8 s;
6909 int rc;
6910
6911 if (!fan_control_allowed)
6912 return -EPERM;
6913
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006914 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006915 return -ERESTARTSYS;
6916
6917 switch (fan_control_access_mode) {
6918 case TPACPI_FAN_WR_ACPI_FANS:
6919 case TPACPI_FAN_WR_TPEC:
6920 rc = fan_get_status(&s);
6921 if (rc < 0)
6922 break;
6923
6924 /* Don't go out of emergency fan mode */
6925 if (s != 7) {
6926 s &= 0x07;
6927 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
6928 }
6929
6930 if (!acpi_ec_write(fan_status_offset, s))
6931 rc = -EIO;
6932 else {
6933 tp_features.fan_ctrl_status_undef = 0;
6934 rc = 0;
6935 }
6936 break;
6937
6938 case TPACPI_FAN_WR_ACPI_SFAN:
6939 rc = fan_get_status(&s);
6940 if (rc < 0)
6941 break;
6942
6943 s &= 0x07;
6944
6945 /* Set fan to at least level 4 */
6946 s |= 4;
6947
6948 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006949 rc = -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006950 else
6951 rc = 0;
6952 break;
6953
6954 default:
6955 rc = -ENXIO;
6956 }
6957
6958 mutex_unlock(&fan_mutex);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00006959
6960 if (!rc)
6961 vdbg_printk(TPACPI_DBG_FAN,
6962 "fan control: set fan control register to 0x%02x\n",
6963 s);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006964 return rc;
6965}
6966
6967static int fan_set_disable(void)
6968{
6969 int rc;
6970
6971 if (!fan_control_allowed)
6972 return -EPERM;
6973
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006974 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006975 return -ERESTARTSYS;
6976
6977 rc = 0;
6978 switch (fan_control_access_mode) {
6979 case TPACPI_FAN_WR_ACPI_FANS:
6980 case TPACPI_FAN_WR_TPEC:
6981 if (!acpi_ec_write(fan_status_offset, 0x00))
6982 rc = -EIO;
6983 else {
6984 fan_control_desired_level = 0;
6985 tp_features.fan_ctrl_status_undef = 0;
6986 }
6987 break;
6988
6989 case TPACPI_FAN_WR_ACPI_SFAN:
6990 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
6991 rc = -EIO;
6992 else
6993 fan_control_desired_level = 0;
6994 break;
6995
6996 default:
6997 rc = -ENXIO;
6998 }
6999
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007000 if (!rc)
7001 vdbg_printk(TPACPI_DBG_FAN,
7002 "fan control: set fan control register to 0\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007003
7004 mutex_unlock(&fan_mutex);
7005 return rc;
7006}
7007
7008static int fan_set_speed(int speed)
7009{
7010 int rc;
7011
7012 if (!fan_control_allowed)
7013 return -EPERM;
7014
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007015 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007016 return -ERESTARTSYS;
7017
7018 rc = 0;
7019 switch (fan_control_access_mode) {
7020 case TPACPI_FAN_WR_ACPI_FANS:
7021 if (speed >= 0 && speed <= 65535) {
7022 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
7023 speed, speed, speed))
7024 rc = -EIO;
7025 } else
7026 rc = -EINVAL;
7027 break;
7028
7029 default:
7030 rc = -ENXIO;
7031 }
7032
7033 mutex_unlock(&fan_mutex);
7034 return rc;
7035}
7036
7037static void fan_watchdog_reset(void)
7038{
7039 static int fan_watchdog_active;
7040
7041 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
7042 return;
7043
7044 if (fan_watchdog_active)
7045 cancel_delayed_work(&fan_watchdog_task);
7046
7047 if (fan_watchdog_maxinterval > 0 &&
7048 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
7049 fan_watchdog_active = 1;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03007050 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007051 msecs_to_jiffies(fan_watchdog_maxinterval
7052 * 1000))) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007053 printk(TPACPI_ERR
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03007054 "failed to queue the fan watchdog, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007055 "watchdog will not trigger\n");
7056 }
7057 } else
7058 fan_watchdog_active = 0;
7059}
7060
7061static void fan_watchdog_fire(struct work_struct *ignored)
7062{
7063 int rc;
7064
7065 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
7066 return;
7067
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007068 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007069 rc = fan_set_enable();
7070 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007071 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007072 "will try again later...\n", -rc);
7073 /* reschedule for later */
7074 fan_watchdog_reset();
7075 }
7076}
7077
7078/*
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007079 * SYSFS fan layout: hwmon compatible (device)
7080 *
7081 * pwm*_enable:
7082 * 0: "disengaged" mode
7083 * 1: manual mode
7084 * 2: native EC "auto" mode (recommended, hardware default)
7085 *
7086 * pwm*: set speed in manual mode, ignored otherwise.
7087 * 0 is level 0; 255 is level 7. Intermediate points done with linear
7088 * interpolation.
7089 *
7090 * fan*_input: tachometer reading, RPM
7091 *
7092 *
7093 * SYSFS fan layout: extensions
7094 *
7095 * fan_watchdog (driver):
7096 * fan watchdog interval in seconds, 0 disables (default), max 120
7097 */
7098
7099/* sysfs fan pwm1_enable ----------------------------------------------- */
7100static ssize_t fan_pwm1_enable_show(struct device *dev,
7101 struct device_attribute *attr,
7102 char *buf)
7103{
7104 int res, mode;
7105 u8 status;
7106
7107 res = fan_get_status_safe(&status);
7108 if (res)
7109 return res;
7110
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007111 if (status & TP_EC_FAN_FULLSPEED) {
7112 mode = 0;
7113 } else if (status & TP_EC_FAN_AUTO) {
7114 mode = 2;
7115 } else
7116 mode = 1;
7117
7118 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
7119}
7120
7121static ssize_t fan_pwm1_enable_store(struct device *dev,
7122 struct device_attribute *attr,
7123 const char *buf, size_t count)
7124{
7125 unsigned long t;
7126 int res, level;
7127
7128 if (parse_strtoul(buf, 2, &t))
7129 return -EINVAL;
7130
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007131 tpacpi_disclose_usertask("hwmon pwm1_enable",
7132 "set fan mode to %lu\n", t);
7133
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007134 switch (t) {
7135 case 0:
7136 level = TP_EC_FAN_FULLSPEED;
7137 break;
7138 case 1:
7139 level = TPACPI_FAN_LAST_LEVEL;
7140 break;
7141 case 2:
7142 level = TP_EC_FAN_AUTO;
7143 break;
7144 case 3:
7145 /* reserved for software-controlled auto mode */
7146 return -ENOSYS;
7147 default:
7148 return -EINVAL;
7149 }
7150
7151 res = fan_set_level_safe(level);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03007152 if (res == -ENXIO)
7153 return -EINVAL;
7154 else if (res < 0)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007155 return res;
7156
7157 fan_watchdog_reset();
7158
7159 return count;
7160}
7161
7162static struct device_attribute dev_attr_fan_pwm1_enable =
7163 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
7164 fan_pwm1_enable_show, fan_pwm1_enable_store);
7165
7166/* sysfs fan pwm1 ------------------------------------------------------ */
7167static ssize_t fan_pwm1_show(struct device *dev,
7168 struct device_attribute *attr,
7169 char *buf)
7170{
7171 int res;
7172 u8 status;
7173
7174 res = fan_get_status_safe(&status);
7175 if (res)
7176 return res;
7177
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007178 if ((status &
7179 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
7180 status = fan_control_desired_level;
7181
7182 if (status > 7)
7183 status = 7;
7184
7185 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
7186}
7187
7188static ssize_t fan_pwm1_store(struct device *dev,
7189 struct device_attribute *attr,
7190 const char *buf, size_t count)
7191{
7192 unsigned long s;
7193 int rc;
7194 u8 status, newlevel;
7195
7196 if (parse_strtoul(buf, 255, &s))
7197 return -EINVAL;
7198
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007199 tpacpi_disclose_usertask("hwmon pwm1",
7200 "set fan speed to %lu\n", s);
7201
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007202 /* scale down from 0-255 to 0-7 */
7203 newlevel = (s >> 5) & 0x07;
7204
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007205 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02007206 return -ERESTARTSYS;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007207
7208 rc = fan_get_status(&status);
7209 if (!rc && (status &
7210 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7211 rc = fan_set_level(newlevel);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03007212 if (rc == -ENXIO)
7213 rc = -EINVAL;
7214 else if (!rc) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007215 fan_update_desired_level(newlevel);
Henrique de Moraes Holschuhca4ac2f2007-04-27 22:00:11 -03007216 fan_watchdog_reset();
7217 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007218 }
7219
7220 mutex_unlock(&fan_mutex);
7221 return (rc)? rc : count;
7222}
7223
7224static struct device_attribute dev_attr_fan_pwm1 =
7225 __ATTR(pwm1, S_IWUSR | S_IRUGO,
7226 fan_pwm1_show, fan_pwm1_store);
7227
7228/* sysfs fan fan1_input ------------------------------------------------ */
7229static ssize_t fan_fan1_input_show(struct device *dev,
7230 struct device_attribute *attr,
7231 char *buf)
7232{
7233 int res;
7234 unsigned int speed;
7235
7236 res = fan_get_speed(&speed);
7237 if (res < 0)
7238 return res;
7239
7240 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7241}
7242
7243static struct device_attribute dev_attr_fan_fan1_input =
7244 __ATTR(fan1_input, S_IRUGO,
7245 fan_fan1_input_show, NULL);
7246
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007247/* sysfs fan fan2_input ------------------------------------------------ */
7248static ssize_t fan_fan2_input_show(struct device *dev,
7249 struct device_attribute *attr,
7250 char *buf)
7251{
7252 int res;
7253 unsigned int speed;
7254
7255 res = fan2_get_speed(&speed);
7256 if (res < 0)
7257 return res;
7258
7259 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7260}
7261
7262static struct device_attribute dev_attr_fan_fan2_input =
7263 __ATTR(fan2_input, S_IRUGO,
7264 fan_fan2_input_show, NULL);
7265
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007266/* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007267static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
7268 char *buf)
7269{
7270 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
7271}
7272
7273static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
7274 const char *buf, size_t count)
7275{
7276 unsigned long t;
7277
7278 if (parse_strtoul(buf, 120, &t))
7279 return -EINVAL;
7280
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03007281 if (!fan_control_allowed)
7282 return -EPERM;
7283
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007284 fan_watchdog_maxinterval = t;
7285 fan_watchdog_reset();
7286
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007287 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
7288
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007289 return count;
7290}
7291
7292static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
7293 fan_fan_watchdog_show, fan_fan_watchdog_store);
7294
7295/* --------------------------------------------------------------------- */
7296static struct attribute *fan_attributes[] = {
7297 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
7298 &dev_attr_fan_fan1_input.attr,
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007299 NULL, /* for fan2_input */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007300 NULL
7301};
7302
7303static const struct attribute_group fan_attr_group = {
7304 .attrs = fan_attributes,
7305};
7306
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007307#define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
7308#define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007309
7310#define TPACPI_FAN_QI(__id1, __id2, __quirks) \
7311 { .vendor = PCI_VENDOR_ID_IBM, \
7312 .bios = TPACPI_MATCH_ANY, \
7313 .ec = TPID(__id1, __id2), \
7314 .quirks = __quirks }
7315
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007316#define TPACPI_FAN_QL(__id1, __id2, __quirks) \
7317 { .vendor = PCI_VENDOR_ID_LENOVO, \
7318 .bios = TPACPI_MATCH_ANY, \
7319 .ec = TPID(__id1, __id2), \
7320 .quirks = __quirks }
7321
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007322static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
7323 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
7324 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
7325 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
7326 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007327 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007328};
7329
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007330#undef TPACPI_FAN_QL
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007331#undef TPACPI_FAN_QI
7332
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007333static int __init fan_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007334{
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007335 int rc;
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007336 unsigned long quirks;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007337
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007338 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7339 "initializing fan subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007340
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03007341 mutex_init(&fan_mutex);
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007342 fan_status_access_mode = TPACPI_FAN_NONE;
7343 fan_control_access_mode = TPACPI_FAN_WR_NONE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007344 fan_control_commands = 0;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007345 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03007346 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007347 tp_features.second_fan = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007348 fan_control_desired_level = 7;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007349
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007350 TPACPI_ACPIHANDLE_INIT(fans);
7351 TPACPI_ACPIHANDLE_INIT(gfan);
7352 TPACPI_ACPIHANDLE_INIT(sfan);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007353
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007354 quirks = tpacpi_check_quirks(fan_quirk_table,
7355 ARRAY_SIZE(fan_quirk_table));
7356
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007357 if (gfan_handle) {
7358 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007359 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007360 } else {
7361 /* all other ThinkPads: note that even old-style
7362 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02007363 if (likely(acpi_ec_read(fan_status_offset,
7364 &fan_control_initial_status))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007365 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007366 if (quirks & TPACPI_FAN_Q1)
7367 fan_quirk1_setup();
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007368 if (quirks & TPACPI_FAN_2FAN) {
7369 tp_features.second_fan = 1;
7370 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7371 "secondary fan support enabled\n");
7372 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007373 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007374 printk(TPACPI_ERR
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007375 "ThinkPad ACPI EC access misbehaving, "
7376 "fan status and control unavailable\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007377 return 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007378 }
7379 }
7380
7381 if (sfan_handle) {
7382 /* 570, 770x-JL */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007383 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02007384 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007385 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007386 } else {
7387 if (!gfan_handle) {
7388 /* gfan without sfan means no fan control */
7389 /* all other models implement TP EC 0x2f control */
7390
7391 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02007392 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007393 fan_control_access_mode =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007394 TPACPI_FAN_WR_ACPI_FANS;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007395 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007396 TPACPI_FAN_CMD_SPEED |
7397 TPACPI_FAN_CMD_LEVEL |
7398 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007399 } else {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007400 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02007401 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007402 TPACPI_FAN_CMD_LEVEL |
7403 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007404 }
7405 }
7406 }
7407
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007408 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7409 "fan is %s, modes %d, %d\n",
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007410 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
7411 fan_control_access_mode != TPACPI_FAN_WR_NONE),
7412 fan_status_access_mode, fan_control_access_mode);
7413
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03007414 /* fan control master switch */
7415 if (!fan_control_allowed) {
7416 fan_control_access_mode = TPACPI_FAN_WR_NONE;
7417 fan_control_commands = 0;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007418 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03007419 "fan control features disabled by parameter\n");
7420 }
7421
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007422 /* update fan_control_desired_level */
7423 if (fan_status_access_mode != TPACPI_FAN_NONE)
7424 fan_get_status_safe(NULL);
7425
7426 if (fan_status_access_mode != TPACPI_FAN_NONE ||
7427 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007428 if (tp_features.second_fan) {
7429 /* attach second fan tachometer */
7430 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
7431 &dev_attr_fan_fan2_input.attr;
7432 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007433 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007434 &fan_attr_group);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007435 if (rc < 0)
7436 return rc;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03007437
7438 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
7439 &driver_attr_fan_watchdog);
7440 if (rc < 0) {
7441 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
7442 &fan_attr_group);
7443 return rc;
7444 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007445 return 0;
7446 } else
7447 return 1;
7448}
7449
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007450static void fan_exit(void)
7451{
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007452 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007453 "cancelling any pending fan watchdog tasks\n");
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007454
7455 /* FIXME: can we really do this unconditionally? */
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007456 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007457 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
7458 &driver_attr_fan_watchdog);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007459
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007460 cancel_delayed_work(&fan_watchdog_task);
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03007461 flush_workqueue(tpacpi_wq);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007462}
7463
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007464static void fan_suspend(pm_message_t state)
7465{
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007466 int rc;
7467
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007468 if (!fan_control_allowed)
7469 return;
7470
7471 /* Store fan status in cache */
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007472 fan_control_resume_level = 0;
7473 rc = fan_get_status_safe(&fan_control_resume_level);
7474 if (rc < 0)
7475 printk(TPACPI_NOTICE
7476 "failed to read fan level for later "
7477 "restore during resume: %d\n", rc);
7478
7479 /* if it is undefined, don't attempt to restore it.
7480 * KEEP THIS LAST */
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007481 if (tp_features.fan_ctrl_status_undef)
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007482 fan_control_resume_level = 0;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007483}
7484
7485static void fan_resume(void)
7486{
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007487 u8 current_level = 7;
7488 bool do_set = false;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007489 int rc;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007490
7491 /* DSDT *always* updates status on resume */
7492 tp_features.fan_ctrl_status_undef = 0;
7493
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007494 if (!fan_control_allowed ||
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007495 !fan_control_resume_level ||
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007496 (fan_get_status_safe(&current_level) < 0))
7497 return;
7498
7499 switch (fan_control_access_mode) {
7500 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007501 /* never decrease fan level */
7502 do_set = (fan_control_resume_level > current_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007503 break;
7504 case TPACPI_FAN_WR_ACPI_FANS:
7505 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007506 /* never decrease fan level, scale is:
7507 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
7508 *
7509 * We expect the firmware to set either 7 or AUTO, but we
7510 * handle FULLSPEED out of paranoia.
7511 *
7512 * So, we can safely only restore FULLSPEED or 7, anything
7513 * else could slow the fan. Restoring AUTO is useless, at
7514 * best that's exactly what the DSDT already set (it is the
7515 * slower it uses).
7516 *
7517 * Always keep in mind that the DSDT *will* have set the
7518 * fans to what the vendor supposes is the best level. We
7519 * muck with it only to speed the fan up.
7520 */
7521 if (fan_control_resume_level != 7 &&
7522 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
7523 return;
7524 else
7525 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
7526 (current_level != fan_control_resume_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007527 break;
7528 default:
7529 return;
7530 }
7531 if (do_set) {
7532 printk(TPACPI_NOTICE
7533 "restoring fan level to 0x%02x\n",
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007534 fan_control_resume_level);
7535 rc = fan_set_level_safe(fan_control_resume_level);
7536 if (rc < 0)
7537 printk(TPACPI_NOTICE
7538 "failed to restore fan level: %d\n", rc);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007539 }
7540}
7541
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007542static int fan_read(char *p)
7543{
7544 int len = 0;
7545 int rc;
7546 u8 status;
7547 unsigned int speed = 0;
7548
7549 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007550 case TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007551 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007552 rc = fan_get_status_safe(&status);
7553 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007554 return rc;
7555
7556 len += sprintf(p + len, "status:\t\t%s\n"
7557 "level:\t\t%d\n",
7558 (status != 0) ? "enabled" : "disabled", status);
7559 break;
7560
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007561 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007562 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007563 rc = fan_get_status_safe(&status);
7564 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007565 return rc;
7566
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007567 len += sprintf(p + len, "status:\t\t%s\n",
7568 (status != 0) ? "enabled" : "disabled");
7569
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007570 rc = fan_get_speed(&speed);
7571 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007572 return rc;
7573
7574 len += sprintf(p + len, "speed:\t\t%d\n", speed);
7575
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007576 if (status & TP_EC_FAN_FULLSPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007577 /* Disengaged mode takes precedence */
7578 len += sprintf(p + len, "level:\t\tdisengaged\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007579 else if (status & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007580 len += sprintf(p + len, "level:\t\tauto\n");
7581 else
7582 len += sprintf(p + len, "level:\t\t%d\n", status);
7583 break;
7584
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007585 case TPACPI_FAN_NONE:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007586 default:
7587 len += sprintf(p + len, "status:\t\tnot supported\n");
7588 }
7589
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007590 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007591 len += sprintf(p + len, "commands:\tlevel <level>");
7592
7593 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007594 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007595 len += sprintf(p + len, " (<level> is 0-7)\n");
7596 break;
7597
7598 default:
7599 len += sprintf(p + len, " (<level> is 0-7, "
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007600 "auto, disengaged, full-speed)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007601 break;
7602 }
7603 }
7604
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007605 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007606 len += sprintf(p + len, "commands:\tenable, disable\n"
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007607 "commands:\twatchdog <timeout> (<timeout> "
7608 "is 0 (off), 1-120 (seconds))\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007609
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007610 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007611 len += sprintf(p + len, "commands:\tspeed <speed>"
7612 " (<speed> is 0-65535)\n");
7613
7614 return len;
7615}
7616
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007617static int fan_write_cmd_level(const char *cmd, int *rc)
7618{
7619 int level;
7620
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02007621 if (strlencmp(cmd, "level auto") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007622 level = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007623 else if ((strlencmp(cmd, "level disengaged") == 0) |
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007624 (strlencmp(cmd, "level full-speed") == 0))
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007625 level = TP_EC_FAN_FULLSPEED;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02007626 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007627 return 0;
7628
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007629 *rc = fan_set_level_safe(level);
7630 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007631 printk(TPACPI_ERR "level command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007632 "access mode %d", fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007633 else if (!*rc)
7634 tpacpi_disclose_usertask("procfs fan",
7635 "set level to %d\n", level);
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007636
7637 return 1;
7638}
7639
7640static int fan_write_cmd_enable(const char *cmd, int *rc)
7641{
7642 if (strlencmp(cmd, "enable") != 0)
7643 return 0;
7644
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007645 *rc = fan_set_enable();
7646 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007647 printk(TPACPI_ERR "enable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007648 "access mode %d", fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007649 else if (!*rc)
7650 tpacpi_disclose_usertask("procfs fan", "enable\n");
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007651
7652 return 1;
7653}
7654
7655static int fan_write_cmd_disable(const char *cmd, int *rc)
7656{
7657 if (strlencmp(cmd, "disable") != 0)
7658 return 0;
7659
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007660 *rc = fan_set_disable();
7661 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007662 printk(TPACPI_ERR "disable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007663 "access mode %d", fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007664 else if (!*rc)
7665 tpacpi_disclose_usertask("procfs fan", "disable\n");
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007666
7667 return 1;
7668}
7669
7670static int fan_write_cmd_speed(const char *cmd, int *rc)
7671{
7672 int speed;
7673
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02007674 /* TODO:
7675 * Support speed <low> <medium> <high> ? */
7676
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007677 if (sscanf(cmd, "speed %d", &speed) != 1)
7678 return 0;
7679
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007680 *rc = fan_set_speed(speed);
7681 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007682 printk(TPACPI_ERR "speed command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007683 "access mode %d", fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007684 else if (!*rc)
7685 tpacpi_disclose_usertask("procfs fan",
7686 "set speed to %d\n", speed);
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007687
7688 return 1;
7689}
7690
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007691static int fan_write_cmd_watchdog(const char *cmd, int *rc)
7692{
7693 int interval;
7694
7695 if (sscanf(cmd, "watchdog %d", &interval) != 1)
7696 return 0;
7697
7698 if (interval < 0 || interval > 120)
7699 *rc = -EINVAL;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007700 else {
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007701 fan_watchdog_maxinterval = interval;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007702 tpacpi_disclose_usertask("procfs fan",
7703 "set watchdog timer to %d\n",
7704 interval);
7705 }
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007706
7707 return 1;
7708}
7709
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007710static int fan_write(char *buf)
7711{
7712 char *cmd;
7713 int rc = 0;
7714
7715 while (!rc && (cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007716 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007717 fan_write_cmd_level(cmd, &rc)) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007718 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007719 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007720 fan_write_cmd_disable(cmd, &rc) ||
7721 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007722 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007723 fan_write_cmd_speed(cmd, &rc))
7724 )
7725 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007726 else if (!rc)
7727 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007728 }
7729
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007730 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007731}
7732
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007733static struct ibm_struct fan_driver_data = {
7734 .name = "fan",
7735 .read = fan_read,
7736 .write = fan_write,
7737 .exit = fan_exit,
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007738 .suspend = fan_suspend,
7739 .resume = fan_resume,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007740};
7741
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007742/****************************************************************************
7743 ****************************************************************************
7744 *
7745 * Infrastructure
7746 *
7747 ****************************************************************************
7748 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07007749
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03007750/*
7751 * HKEY event callout for other subdrivers go here
7752 * (yes, it is ugly, but it is quick, safe, and gets the job done
7753 */
7754static void tpacpi_driver_event(const unsigned int hkey_event)
7755{
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00007756 if (ibm_backlight_device) {
7757 switch (hkey_event) {
7758 case TP_HKEY_EV_BRGHT_UP:
7759 case TP_HKEY_EV_BRGHT_DOWN:
7760 tpacpi_brightness_notify_change();
7761 }
7762 }
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03007763}
7764
7765
7766
7767static void hotkey_driver_event(const unsigned int scancode)
7768{
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03007769 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode);
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03007770}
7771
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007772/* sysfs name ---------------------------------------------------------- */
7773static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
7774 struct device_attribute *attr,
7775 char *buf)
7776{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007777 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007778}
7779
7780static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
7781 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
7782
7783/* --------------------------------------------------------------------- */
7784
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007785/* /proc support */
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03007786static struct proc_dir_entry *proc_dir;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007787
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007788/*
7789 * Module and infrastructure proble, init and exit handling
7790 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007791
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007792static int force_load;
7793
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007794#ifdef CONFIG_THINKPAD_ACPI_DEBUG
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007795static const char * __init str_supported(int is_supported)
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007796{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007797 static char text_unsupported[] __initdata = "not supported";
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007798
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007799 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007800}
7801#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
7802
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007803static void ibm_exit(struct ibm_struct *ibm)
7804{
7805 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
7806
7807 list_del_init(&ibm->all_drivers);
7808
7809 if (ibm->flags.acpi_notify_installed) {
7810 dbg_printk(TPACPI_DBG_EXIT,
7811 "%s: acpi_remove_notify_handler\n", ibm->name);
7812 BUG_ON(!ibm->acpi);
7813 acpi_remove_notify_handler(*ibm->acpi->handle,
7814 ibm->acpi->type,
7815 dispatch_acpi_notify);
7816 ibm->flags.acpi_notify_installed = 0;
7817 ibm->flags.acpi_notify_installed = 0;
7818 }
7819
7820 if (ibm->flags.proc_created) {
7821 dbg_printk(TPACPI_DBG_EXIT,
7822 "%s: remove_proc_entry\n", ibm->name);
7823 remove_proc_entry(ibm->name, proc_dir);
7824 ibm->flags.proc_created = 0;
7825 }
7826
7827 if (ibm->flags.acpi_driver_registered) {
7828 dbg_printk(TPACPI_DBG_EXIT,
7829 "%s: acpi_bus_unregister_driver\n", ibm->name);
7830 BUG_ON(!ibm->acpi);
7831 acpi_bus_unregister_driver(ibm->acpi->driver);
7832 kfree(ibm->acpi->driver);
7833 ibm->acpi->driver = NULL;
7834 ibm->flags.acpi_driver_registered = 0;
7835 }
7836
7837 if (ibm->flags.init_called && ibm->exit) {
7838 ibm->exit();
7839 ibm->flags.init_called = 0;
7840 }
7841
7842 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
7843}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007844
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007845static int __init ibm_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007846{
7847 int ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007848 struct ibm_struct *ibm = iibm->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007849 struct proc_dir_entry *entry;
7850
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007851 BUG_ON(ibm == NULL);
7852
7853 INIT_LIST_HEAD(&ibm->all_drivers);
7854
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03007855 if (ibm->flags.experimental && !experimental)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007856 return 0;
7857
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007858 dbg_printk(TPACPI_DBG_INIT,
7859 "probing for %s\n", ibm->name);
7860
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007861 if (iibm->init) {
7862 ret = iibm->init(iibm);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007863 if (ret > 0)
7864 return 0; /* probe failed */
7865 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007866 return ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007867
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03007868 ibm->flags.init_called = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007869 }
7870
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03007871 if (ibm->acpi) {
7872 if (ibm->acpi->hid) {
7873 ret = register_tpacpi_subdriver(ibm);
7874 if (ret)
7875 goto err_out;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007876 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03007877
7878 if (ibm->acpi->notify) {
7879 ret = setup_acpi_notify(ibm);
7880 if (ret == -ENODEV) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007881 printk(TPACPI_NOTICE "disabling subdriver %s\n",
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03007882 ibm->name);
7883 ret = 0;
7884 goto err_out;
7885 }
7886 if (ret < 0)
7887 goto err_out;
7888 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007889 }
7890
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007891 dbg_printk(TPACPI_DBG_INIT,
7892 "%s installed\n", ibm->name);
7893
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007894 if (ibm->read) {
7895 entry = create_proc_entry(ibm->name,
7896 S_IFREG | S_IRUGO | S_IWUSR,
7897 proc_dir);
7898 if (!entry) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007899 printk(TPACPI_ERR "unable to create proc entry %s\n",
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007900 ibm->name);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007901 ret = -ENODEV;
7902 goto err_out;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007903 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007904 entry->data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03007905 entry->read_proc = &dispatch_procfs_read;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007906 if (ibm->write)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03007907 entry->write_proc = &dispatch_procfs_write;
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03007908 ibm->flags.proc_created = 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007909 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007910
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007911 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
7912
Linus Torvalds1da177e2005-04-16 15:20:36 -07007913 return 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007914
7915err_out:
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007916 dbg_printk(TPACPI_DBG_INIT,
7917 "%s: at error exit path with result %d\n",
7918 ibm->name, ret);
7919
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007920 ibm_exit(ibm);
7921 return (ret < 0)? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007922}
7923
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007924/* Probing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007925
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03007926static bool __pure __init tpacpi_is_fw_digit(const char c)
7927{
7928 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
7929}
7930
7931/* Most models: xxyTkkWW (#.##c); Ancient 570/600 and -SL lacks (#.##c) */
7932static bool __pure __init tpacpi_is_valid_fw_id(const char* const s,
7933 const char t)
7934{
7935 return s && strlen(s) >= 8 &&
7936 tpacpi_is_fw_digit(s[0]) &&
7937 tpacpi_is_fw_digit(s[1]) &&
7938 s[2] == t && s[3] == 'T' &&
7939 tpacpi_is_fw_digit(s[4]) &&
7940 tpacpi_is_fw_digit(s[5]) &&
7941 s[6] == 'W' && s[7] == 'W';
7942}
7943
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007944/* returns 0 - probe ok, or < 0 - probe error.
7945 * Probe ok doesn't mean thinkpad found.
7946 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
7947static int __must_check __init get_thinkpad_model_data(
7948 struct thinkpad_id_data *tp)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02007949{
Jeff Garzik18552562007-10-03 15:15:40 -04007950 const struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02007951 char ec_fw_string[18];
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007952 char const *s;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02007953
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007954 if (!tp)
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007955 return -EINVAL;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007956
7957 memset(tp, 0, sizeof(*tp));
7958
7959 if (dmi_name_in_vendors("IBM"))
7960 tp->vendor = PCI_VENDOR_ID_IBM;
7961 else if (dmi_name_in_vendors("LENOVO"))
7962 tp->vendor = PCI_VENDOR_ID_LENOVO;
7963 else
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007964 return 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007965
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007966 s = dmi_get_system_info(DMI_BIOS_VERSION);
7967 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
7968 if (s && !tp->bios_version_str)
7969 return -ENOMEM;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03007970
7971 /* Really ancient ThinkPad 240X will fail this, which is fine */
7972 if (!tpacpi_is_valid_fw_id(tp->bios_version_str, 'E'))
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007973 return 0;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03007974
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007975 tp->bios_model = tp->bios_version_str[0]
7976 | (tp->bios_version_str[1] << 8);
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03007977 tp->bios_release = (tp->bios_version_str[4] << 8)
7978 | tp->bios_version_str[5];
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007979
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02007980 /*
7981 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
7982 * X32 or newer, all Z series; Some models must have an
7983 * up-to-date BIOS or they will not be detected.
7984 *
7985 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
7986 */
7987 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02007988 if (sscanf(dev->name,
7989 "IBM ThinkPad Embedded Controller -[%17c",
7990 ec_fw_string) == 1) {
7991 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
7992 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007993
7994 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007995 if (!tp->ec_version_str)
7996 return -ENOMEM;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03007997
7998 if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) {
7999 tp->ec_model = ec_fw_string[0]
8000 | (ec_fw_string[1] << 8);
8001 tp->ec_release = (ec_fw_string[4] << 8)
8002 | ec_fw_string[5];
8003 } else {
8004 printk(TPACPI_NOTICE
8005 "ThinkPad firmware release %s "
8006 "doesn't match the known patterns\n",
8007 ec_fw_string);
8008 printk(TPACPI_NOTICE
8009 "please report this to %s\n",
8010 TPACPI_MAIL);
8011 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008012 break;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02008013 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02008014 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008015
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008016 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
8017 if (s && !strnicmp(s, "ThinkPad", 8)) {
8018 tp->model_str = kstrdup(s, GFP_KERNEL);
8019 if (!tp->model_str)
8020 return -ENOMEM;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008021 }
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03008022
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008023 s = dmi_get_system_info(DMI_PRODUCT_NAME);
8024 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
8025 if (s && !tp->nummodel_str)
8026 return -ENOMEM;
8027
8028 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008029}
8030
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008031static int __init probe_for_thinkpad(void)
8032{
8033 int is_thinkpad;
8034
8035 if (acpi_disabled)
8036 return -ENODEV;
8037
8038 /*
8039 * Non-ancient models have better DMI tagging, but very old models
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03008040 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008041 */
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03008042 is_thinkpad = (thinkpad_id.model_str != NULL) ||
8043 (thinkpad_id.ec_model != 0) ||
8044 tpacpi_is_fw_known();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008045
8046 /* ec is required because many other handles are relative to it */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008047 TPACPI_ACPIHANDLE_INIT(ec);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008048 if (!ec_handle) {
8049 if (is_thinkpad)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008050 printk(TPACPI_ERR
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008051 "Not yet supported ThinkPad detected!\n");
8052 return -ENODEV;
8053 }
8054
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03008055 if (!is_thinkpad && !force_load)
8056 return -ENODEV;
8057
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008058 return 0;
8059}
8060
8061
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008062/* Module init, exit, parameters */
8063
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008064static struct ibm_init_struct ibms_init[] __initdata = {
8065 {
8066 .init = thinkpad_acpi_driver_init,
8067 .data = &thinkpad_acpi_driver_data,
8068 },
8069 {
8070 .init = hotkey_init,
8071 .data = &hotkey_driver_data,
8072 },
8073 {
8074 .init = bluetooth_init,
8075 .data = &bluetooth_driver_data,
8076 },
8077 {
8078 .init = wan_init,
8079 .data = &wan_driver_data,
8080 },
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02008081 {
8082 .init = uwb_init,
8083 .data = &uwb_driver_data,
8084 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02008085#ifdef CONFIG_THINKPAD_ACPI_VIDEO
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008086 {
8087 .init = video_init,
8088 .data = &video_driver_data,
8089 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02008090#endif
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008091 {
8092 .init = light_init,
8093 .data = &light_driver_data,
8094 },
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008095 {
8096 .init = cmos_init,
8097 .data = &cmos_driver_data,
8098 },
8099 {
8100 .init = led_init,
8101 .data = &led_driver_data,
8102 },
8103 {
8104 .init = beep_init,
8105 .data = &beep_driver_data,
8106 },
8107 {
8108 .init = thermal_init,
8109 .data = &thermal_driver_data,
8110 },
8111 {
8112 .data = &ecdump_driver_data,
8113 },
8114 {
8115 .init = brightness_init,
8116 .data = &brightness_driver_data,
8117 },
8118 {
8119 .data = &volume_driver_data,
8120 },
8121 {
8122 .init = fan_init,
8123 .data = &fan_driver_data,
8124 },
8125};
8126
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008127static int __init set_ibm_param(const char *val, struct kernel_param *kp)
8128{
8129 unsigned int i;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008130 struct ibm_struct *ibm;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008131
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02008132 if (!kp || !kp->name || !val)
8133 return -EINVAL;
8134
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008135 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
8136 ibm = ibms_init[i].data;
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02008137 WARN_ON(ibm == NULL);
8138
8139 if (!ibm || !ibm->name)
8140 continue;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008141
8142 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
8143 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008144 return -ENOSPC;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008145 strcpy(ibms_init[i].param, val);
8146 strcat(ibms_init[i].param, ",");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008147 return 0;
8148 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008149 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008150
8151 return -EINVAL;
8152}
8153
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008154module_param(experimental, int, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008155MODULE_PARM_DESC(experimental,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008156 "Enables experimental features when non-zero");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008157
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03008158module_param_named(debug, dbg_level, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008159MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03008160
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008161module_param(force_load, bool, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008162MODULE_PARM_DESC(force_load,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008163 "Attempts to load the driver even on a "
8164 "mis-identified ThinkPad when true");
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03008165
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008166module_param_named(fan_control, fan_control_allowed, bool, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008167MODULE_PARM_DESC(fan_control,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008168 "Enables setting fan parameters features when true");
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008169
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008170module_param_named(brightness_mode, brightness_mode, uint, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008171MODULE_PARM_DESC(brightness_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008172 "Selects brightness control strategy: "
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00008173 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03008174
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008175module_param(brightness_enable, uint, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008176MODULE_PARM_DESC(brightness_enable,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008177 "Enables backlight control when 1, disables when 0");
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02008178
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008179module_param(hotkey_report_mode, uint, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008180MODULE_PARM_DESC(hotkey_report_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008181 "used for backwards compatibility with userspace, "
8182 "see documentation");
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03008183
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008184#define TPACPI_PARAM(feature) \
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008185 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
Henrique de Moraes Holschuhcbb14842008-02-16 02:17:50 -02008186 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008187 "at module load, see documentation")
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008188
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008189TPACPI_PARAM(hotkey);
8190TPACPI_PARAM(bluetooth);
8191TPACPI_PARAM(video);
8192TPACPI_PARAM(light);
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008193TPACPI_PARAM(cmos);
8194TPACPI_PARAM(led);
8195TPACPI_PARAM(beep);
8196TPACPI_PARAM(ecdump);
8197TPACPI_PARAM(brightness);
8198TPACPI_PARAM(volume);
8199TPACPI_PARAM(fan);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008200
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02008201#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008202module_param(dbg_wlswemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02008203MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
8204module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
8205MODULE_PARM_DESC(wlsw_state,
8206 "Initial state of the emulated WLSW switch");
8207
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008208module_param(dbg_bluetoothemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02008209MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
8210module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
8211MODULE_PARM_DESC(bluetooth_state,
8212 "Initial state of the emulated bluetooth switch");
8213
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008214module_param(dbg_wwanemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02008215MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
8216module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
8217MODULE_PARM_DESC(wwan_state,
8218 "Initial state of the emulated WWAN switch");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02008219
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008220module_param(dbg_uwbemul, uint, 0444);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02008221MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
8222module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
8223MODULE_PARM_DESC(uwb_state,
8224 "Initial state of the emulated UWB switch");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02008225#endif
8226
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008227static void thinkpad_acpi_module_exit(void)
8228{
8229 struct ibm_struct *ibm, *itmp;
8230
8231 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
8232
8233 list_for_each_entry_safe_reverse(ibm, itmp,
8234 &tpacpi_all_drivers,
8235 all_drivers) {
8236 ibm_exit(ibm);
8237 }
8238
8239 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
8240
8241 if (tpacpi_inputdev) {
8242 if (tp_features.input_device_registered)
8243 input_unregister_device(tpacpi_inputdev);
8244 else
8245 input_free_device(tpacpi_inputdev);
8246 }
8247
8248 if (tpacpi_hwmon)
8249 hwmon_device_unregister(tpacpi_hwmon);
8250
8251 if (tp_features.sensors_pdev_attrs_registered)
8252 device_remove_file(&tpacpi_sensors_pdev->dev,
8253 &dev_attr_thinkpad_acpi_pdev_name);
8254 if (tpacpi_sensors_pdev)
8255 platform_device_unregister(tpacpi_sensors_pdev);
8256 if (tpacpi_pdev)
8257 platform_device_unregister(tpacpi_pdev);
8258
8259 if (tp_features.sensors_pdrv_attrs_registered)
8260 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
8261 if (tp_features.platform_drv_attrs_registered)
8262 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
8263
8264 if (tp_features.sensors_pdrv_registered)
8265 platform_driver_unregister(&tpacpi_hwmon_pdriver);
8266
8267 if (tp_features.platform_drv_registered)
8268 platform_driver_unregister(&tpacpi_pdriver);
8269
8270 if (proc_dir)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008271 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008272
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03008273 if (tpacpi_wq)
8274 destroy_workqueue(tpacpi_wq);
8275
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008276 kfree(thinkpad_id.bios_version_str);
8277 kfree(thinkpad_id.ec_version_str);
8278 kfree(thinkpad_id.model_str);
8279}
8280
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02008281
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03008282static int __init thinkpad_acpi_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008283{
8284 int ret, i;
8285
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03008286 tpacpi_lifecycle = TPACPI_LIFE_INIT;
8287
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03008288 /* Parameter checking */
8289 if (hotkey_report_mode > 2)
8290 return -EINVAL;
8291
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008292 /* Driver-level probe */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008293
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008294 ret = get_thinkpad_model_data(&thinkpad_id);
8295 if (ret) {
8296 printk(TPACPI_ERR
8297 "unable to get DMI data: %d\n", ret);
8298 thinkpad_acpi_module_exit();
8299 return ret;
8300 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008301 ret = probe_for_thinkpad();
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008302 if (ret) {
8303 thinkpad_acpi_module_exit();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008304 return ret;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008306
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008307 /* Driver initialization */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008308
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008309 TPACPI_ACPIHANDLE_INIT(ecrd);
8310 TPACPI_ACPIHANDLE_INIT(ecwr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008311
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03008312 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
8313 if (!tpacpi_wq) {
8314 thinkpad_acpi_module_exit();
8315 return -ENOMEM;
8316 }
8317
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008318 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008319 if (!proc_dir) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008320 printk(TPACPI_ERR
8321 "unable to create proc dir " TPACPI_PROC_DIR);
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03008322 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008323 return -ENODEV;
8324 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008325
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008326 ret = platform_driver_register(&tpacpi_pdriver);
8327 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008328 printk(TPACPI_ERR
8329 "unable to register main platform driver\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008330 thinkpad_acpi_module_exit();
8331 return ret;
8332 }
Henrique de Moraes Holschuhac363932007-07-27 17:04:40 -03008333 tp_features.platform_drv_registered = 1;
8334
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008335 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
8336 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008337 printk(TPACPI_ERR
8338 "unable to register hwmon platform driver\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008339 thinkpad_acpi_module_exit();
8340 return ret;
8341 }
8342 tp_features.sensors_pdrv_registered = 1;
8343
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03008344 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03008345 if (!ret) {
8346 tp_features.platform_drv_attrs_registered = 1;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008347 ret = tpacpi_create_driver_attributes(
8348 &tpacpi_hwmon_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03008349 }
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03008350 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008351 printk(TPACPI_ERR
8352 "unable to create sysfs driver attributes\n");
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03008353 thinkpad_acpi_module_exit();
8354 return ret;
8355 }
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03008356 tp_features.sensors_pdrv_attrs_registered = 1;
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03008357
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008358
8359 /* Device initialization */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008360 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008361 NULL, 0);
8362 if (IS_ERR(tpacpi_pdev)) {
8363 ret = PTR_ERR(tpacpi_pdev);
8364 tpacpi_pdev = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008365 printk(TPACPI_ERR "unable to register platform device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008366 thinkpad_acpi_module_exit();
8367 return ret;
8368 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008369 tpacpi_sensors_pdev = platform_device_register_simple(
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008370 TPACPI_HWMON_DRVR_NAME,
8371 -1, NULL, 0);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008372 if (IS_ERR(tpacpi_sensors_pdev)) {
8373 ret = PTR_ERR(tpacpi_sensors_pdev);
8374 tpacpi_sensors_pdev = NULL;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008375 printk(TPACPI_ERR
8376 "unable to register hwmon platform device\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008377 thinkpad_acpi_module_exit();
8378 return ret;
8379 }
8380 ret = device_create_file(&tpacpi_sensors_pdev->dev,
8381 &dev_attr_thinkpad_acpi_pdev_name);
8382 if (ret) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008383 printk(TPACPI_ERR
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008384 "unable to create sysfs hwmon device attributes\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008385 thinkpad_acpi_module_exit();
8386 return ret;
8387 }
8388 tp_features.sensors_pdev_attrs_registered = 1;
8389 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008390 if (IS_ERR(tpacpi_hwmon)) {
8391 ret = PTR_ERR(tpacpi_hwmon);
8392 tpacpi_hwmon = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008393 printk(TPACPI_ERR "unable to register hwmon device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008394 thinkpad_acpi_module_exit();
8395 return ret;
8396 }
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -03008397 mutex_init(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03008398 tpacpi_inputdev = input_allocate_device();
8399 if (!tpacpi_inputdev) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008400 printk(TPACPI_ERR "unable to allocate input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03008401 thinkpad_acpi_module_exit();
8402 return -ENOMEM;
8403 } else {
8404 /* Prepare input device, but don't register */
8405 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008406 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03008407 tpacpi_inputdev->id.bustype = BUS_HOST;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03008408 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
8409 thinkpad_id.vendor :
8410 PCI_VENDOR_ID_IBM;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03008411 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
8412 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
Henrique de Moraes Holschuhd112ef92009-12-09 01:36:26 +00008413 tpacpi_inputdev->dev.parent = &tpacpi_pdev->dev;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03008414 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008415 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
8416 ret = ibm_init(&ibms_init[i]);
8417 if (ret >= 0 && *ibms_init[i].param)
8418 ret = ibms_init[i].data->write(ibms_init[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008419 if (ret < 0) {
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03008420 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008421 return ret;
8422 }
8423 }
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03008424 ret = input_register_device(tpacpi_inputdev);
8425 if (ret < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008426 printk(TPACPI_ERR "unable to register input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03008427 thinkpad_acpi_module_exit();
8428 return ret;
8429 } else {
8430 tp_features.input_device_registered = 1;
8431 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008432
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03008433 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008434 return 0;
8435}
8436
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03008437MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
8438
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008439/*
Henrique de Moraes Holschuh922fe092009-04-14 02:44:13 +00008440 * This will autoload the driver in almost every ThinkPad
8441 * in widespread use.
8442 *
8443 * Only _VERY_ old models, like the 240, 240x and 570 lack
8444 * the HKEY event interface.
8445 */
8446MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
8447
8448/*
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008449 * DMI matching for module autoloading
8450 *
8451 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
8452 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
8453 *
8454 * Only models listed in thinkwiki will be supported, so add yours
8455 * if it is not there yet.
8456 */
8457#define IBM_BIOS_MODULE_ALIAS(__type) \
Mathieu Chouquet-Stringerb36a50f2009-03-14 16:35:26 +01008458 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008459
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008460/* Ancient thinkpad BIOSes have to be identified by
8461 * BIOS type or model number, and there are far less
8462 * BIOS types than model numbers... */
Henrique de Moraes Holschuh922fe092009-04-14 02:44:13 +00008463IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008464
Henrique de Moraes Holschuhf68f53a2009-04-14 02:44:12 +00008465MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
8466MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008467MODULE_DESCRIPTION(TPACPI_DESC);
8468MODULE_VERSION(TPACPI_VERSION);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008469MODULE_LICENSE("GPL");
8470
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03008471module_init(thinkpad_acpi_module_init);
8472module_exit(thinkpad_acpi_module_exit);