blob: cf61d6a8ef6f6410f8a684410fc1f03a9669d0fe [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 Holschuh0d922e32009-09-20 14:09:25 -030025#define TPACPI_SYSFS_VERSION 0x020500
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
André Goddard Rosae7d28602009-12-14 18:01:06 -08001009 *value = simple_strtoul(skip_spaces(buf), &endp, 0);
1010 endp = skip_spaces(endp);
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001011 if (*endp || *value > max)
1012 return -EINVAL;
1013
1014 return 0;
1015}
1016
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03001017static void tpacpi_disable_brightness_delay(void)
1018{
1019 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
1020 printk(TPACPI_NOTICE
1021 "ACPI backlight control delay disabled\n");
1022}
1023
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03001024static int __init tpacpi_query_bcl_levels(acpi_handle handle)
1025{
1026 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1027 union acpi_object *obj;
1028 int rc;
1029
1030 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
1031 obj = (union acpi_object *)buffer.pointer;
1032 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
1033 printk(TPACPI_ERR "Unknown _BCL data, "
1034 "please report this to %s\n", TPACPI_MAIL);
1035 rc = 0;
1036 } else {
1037 rc = obj->package.count;
1038 }
1039 } else {
1040 return 0;
1041 }
1042
1043 kfree(buffer.pointer);
1044 return rc;
1045}
1046
1047static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
1048 u32 lvl, void *context, void **rv)
1049{
1050 char name[ACPI_PATH_SEGMENT_LENGTH];
1051 struct acpi_buffer buffer = { sizeof(name), &name };
1052
1053 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
1054 !strncmp("_BCL", name, sizeof(name) - 1)) {
1055 BUG_ON(!rv || !*rv);
1056 **(int **)rv = tpacpi_query_bcl_levels(handle);
1057 return AE_CTRL_TERMINATE;
1058 } else {
1059 return AE_OK;
1060 }
1061}
1062
1063/*
1064 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
1065 */
1066static int __init tpacpi_check_std_acpi_brightness_support(void)
1067{
1068 int status;
1069 int bcl_levels = 0;
1070 void *bcl_ptr = &bcl_levels;
1071
1072 if (!vid_handle) {
1073 TPACPI_ACPIHANDLE_INIT(vid);
1074 }
1075 if (!vid_handle)
1076 return 0;
1077
1078 /*
1079 * Search for a _BCL method, and execute it. This is safe on all
1080 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
1081 * BIOS in ACPI backlight control mode. We do NOT have to care
1082 * about calling the _BCL method in an enabled video device, any
1083 * will do for our purposes.
1084 */
1085
1086 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
Lin Ming22635762009-11-13 10:06:08 +08001087 tpacpi_acpi_walk_find_bcl, NULL, NULL,
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03001088 &bcl_ptr);
1089
1090 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
1091 tp_features.bright_acpimode = 1;
1092 return (bcl_levels - 2);
1093 }
1094
1095 return 0;
1096}
1097
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00001098static void printk_deprecated_attribute(const char * const what,
1099 const char * const details)
1100{
1101 tpacpi_log_usertask("deprecated sysfs attribute");
1102 printk(TPACPI_WARN "WARNING: sysfs attribute %s is deprecated and "
1103 "will be removed. %s\n",
1104 what, details);
1105}
1106
Johannes Berg19d337d2009-06-02 13:01:37 +02001107/*************************************************************************
1108 * rfkill and radio control support helpers
1109 */
1110
1111/*
1112 * ThinkPad-ACPI firmware handling model:
1113 *
1114 * WLSW (master wireless switch) is event-driven, and is common to all
1115 * firmware-controlled radios. It cannot be controlled, just monitored,
1116 * as expected. It overrides all radio state in firmware
1117 *
1118 * The kernel, a masked-off hotkey, and WLSW can change the radio state
1119 * (TODO: verify how WLSW interacts with the returned radio state).
1120 *
1121 * The only time there are shadow radio state changes, is when
1122 * masked-off hotkeys are used.
1123 */
1124
1125/*
1126 * Internal driver API for radio state:
1127 *
1128 * int: < 0 = error, otherwise enum tpacpi_rfkill_state
1129 * bool: true means radio blocked (off)
1130 */
1131enum tpacpi_rfkill_state {
1132 TPACPI_RFK_RADIO_OFF = 0,
1133 TPACPI_RFK_RADIO_ON
1134};
1135
1136/* rfkill switches */
1137enum tpacpi_rfk_id {
1138 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
1139 TPACPI_RFK_WWAN_SW_ID,
1140 TPACPI_RFK_UWB_SW_ID,
1141 TPACPI_RFK_SW_MAX
1142};
1143
1144static const char *tpacpi_rfkill_names[] = {
1145 [TPACPI_RFK_BLUETOOTH_SW_ID] = "bluetooth",
1146 [TPACPI_RFK_WWAN_SW_ID] = "wwan",
1147 [TPACPI_RFK_UWB_SW_ID] = "uwb",
1148 [TPACPI_RFK_SW_MAX] = NULL
1149};
1150
1151/* ThinkPad-ACPI rfkill subdriver */
1152struct tpacpi_rfk {
1153 struct rfkill *rfkill;
1154 enum tpacpi_rfk_id id;
1155 const struct tpacpi_rfk_ops *ops;
1156};
1157
1158struct tpacpi_rfk_ops {
1159 /* firmware interface */
1160 int (*get_status)(void);
1161 int (*set_status)(const enum tpacpi_rfkill_state);
1162};
1163
1164static struct tpacpi_rfk *tpacpi_rfkill_switches[TPACPI_RFK_SW_MAX];
1165
1166/* Query FW and update rfkill sw state for a given rfkill switch */
1167static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk *tp_rfk)
1168{
1169 int status;
1170
1171 if (!tp_rfk)
1172 return -ENODEV;
1173
1174 status = (tp_rfk->ops->get_status)();
1175 if (status < 0)
1176 return status;
1177
1178 rfkill_set_sw_state(tp_rfk->rfkill,
1179 (status == TPACPI_RFK_RADIO_OFF));
1180
1181 return status;
1182}
1183
1184/* Query FW and update rfkill sw state for all rfkill switches */
1185static void tpacpi_rfk_update_swstate_all(void)
1186{
1187 unsigned int i;
1188
1189 for (i = 0; i < TPACPI_RFK_SW_MAX; i++)
1190 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[i]);
1191}
1192
1193/*
1194 * Sync the HW-blocking state of all rfkill switches,
1195 * do notice it causes the rfkill core to schedule uevents
1196 */
1197static void tpacpi_rfk_update_hwblock_state(bool blocked)
1198{
1199 unsigned int i;
1200 struct tpacpi_rfk *tp_rfk;
1201
1202 for (i = 0; i < TPACPI_RFK_SW_MAX; i++) {
1203 tp_rfk = tpacpi_rfkill_switches[i];
1204 if (tp_rfk) {
1205 if (rfkill_set_hw_state(tp_rfk->rfkill,
1206 blocked)) {
1207 /* ignore -- we track sw block */
1208 }
1209 }
1210 }
1211}
1212
1213/* Call to get the WLSW state from the firmware */
1214static int hotkey_get_wlsw(void);
1215
1216/* Call to query WLSW state and update all rfkill switches */
1217static bool tpacpi_rfk_check_hwblock_state(void)
1218{
1219 int res = hotkey_get_wlsw();
1220 int hw_blocked;
1221
1222 /* When unknown or unsupported, we have to assume it is unblocked */
1223 if (res < 0)
1224 return false;
1225
1226 hw_blocked = (res == TPACPI_RFK_RADIO_OFF);
1227 tpacpi_rfk_update_hwblock_state(hw_blocked);
1228
1229 return hw_blocked;
1230}
1231
1232static int tpacpi_rfk_hook_set_block(void *data, bool blocked)
1233{
1234 struct tpacpi_rfk *tp_rfk = data;
1235 int res;
1236
1237 dbg_printk(TPACPI_DBG_RFKILL,
1238 "request to change radio state to %s\n",
1239 blocked ? "blocked" : "unblocked");
1240
1241 /* try to set radio state */
1242 res = (tp_rfk->ops->set_status)(blocked ?
1243 TPACPI_RFK_RADIO_OFF : TPACPI_RFK_RADIO_ON);
1244
1245 /* and update the rfkill core with whatever the FW really did */
1246 tpacpi_rfk_update_swstate(tp_rfk);
1247
1248 return (res < 0) ? res : 0;
1249}
1250
1251static const struct rfkill_ops tpacpi_rfk_rfkill_ops = {
1252 .set_block = tpacpi_rfk_hook_set_block,
1253};
1254
1255static int __init tpacpi_new_rfkill(const enum tpacpi_rfk_id id,
1256 const struct tpacpi_rfk_ops *tp_rfkops,
1257 const enum rfkill_type rfktype,
1258 const char *name,
1259 const bool set_default)
1260{
1261 struct tpacpi_rfk *atp_rfk;
1262 int res;
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001263 bool sw_state = false;
1264 int sw_status;
Johannes Berg19d337d2009-06-02 13:01:37 +02001265
1266 BUG_ON(id >= TPACPI_RFK_SW_MAX || tpacpi_rfkill_switches[id]);
1267
Johannes Berg19d337d2009-06-02 13:01:37 +02001268 atp_rfk = kzalloc(sizeof(struct tpacpi_rfk), GFP_KERNEL);
1269 if (atp_rfk)
1270 atp_rfk->rfkill = rfkill_alloc(name,
1271 &tpacpi_pdev->dev,
1272 rfktype,
1273 &tpacpi_rfk_rfkill_ops,
1274 atp_rfk);
1275 if (!atp_rfk || !atp_rfk->rfkill) {
1276 printk(TPACPI_ERR
1277 "failed to allocate memory for rfkill class\n");
1278 kfree(atp_rfk);
1279 return -ENOMEM;
1280 }
1281
1282 atp_rfk->id = id;
1283 atp_rfk->ops = tp_rfkops;
1284
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001285 sw_status = (tp_rfkops->get_status)();
1286 if (sw_status < 0) {
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001287 printk(TPACPI_ERR
1288 "failed to read initial state for %s, error %d\n",
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001289 name, sw_status);
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001290 } else {
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001291 sw_state = (sw_status == TPACPI_RFK_RADIO_OFF);
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001292 if (set_default) {
1293 /* try to keep the initial state, since we ask the
1294 * firmware to preserve it across S5 in NVRAM */
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001295 rfkill_init_sw_state(atp_rfk->rfkill, sw_state);
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001296 }
1297 }
1298 rfkill_set_hw_state(atp_rfk->rfkill, tpacpi_rfk_check_hwblock_state());
Johannes Berg19d337d2009-06-02 13:01:37 +02001299
1300 res = rfkill_register(atp_rfk->rfkill);
1301 if (res < 0) {
1302 printk(TPACPI_ERR
1303 "failed to register %s rfkill switch: %d\n",
1304 name, res);
1305 rfkill_destroy(atp_rfk->rfkill);
1306 kfree(atp_rfk);
1307 return res;
1308 }
1309
1310 tpacpi_rfkill_switches[id] = atp_rfk;
1311 return 0;
1312}
1313
1314static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id)
1315{
1316 struct tpacpi_rfk *tp_rfk;
1317
1318 BUG_ON(id >= TPACPI_RFK_SW_MAX);
1319
1320 tp_rfk = tpacpi_rfkill_switches[id];
1321 if (tp_rfk) {
1322 rfkill_unregister(tp_rfk->rfkill);
Corentin Chary5f0dadb2009-09-14 12:43:52 +02001323 rfkill_destroy(tp_rfk->rfkill);
Johannes Berg19d337d2009-06-02 13:01:37 +02001324 tpacpi_rfkill_switches[id] = NULL;
1325 kfree(tp_rfk);
1326 }
1327}
1328
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00001329static void printk_deprecated_rfkill_attribute(const char * const what)
1330{
1331 printk_deprecated_attribute(what,
1332 "Please switch to generic rfkill before year 2010");
1333}
1334
Johannes Berg19d337d2009-06-02 13:01:37 +02001335/* sysfs <radio> enable ------------------------------------------------ */
1336static ssize_t tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id,
1337 struct device_attribute *attr,
1338 char *buf)
1339{
1340 int status;
1341
1342 printk_deprecated_rfkill_attribute(attr->attr.name);
1343
1344 /* This is in the ABI... */
1345 if (tpacpi_rfk_check_hwblock_state()) {
1346 status = TPACPI_RFK_RADIO_OFF;
1347 } else {
1348 status = tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1349 if (status < 0)
1350 return status;
1351 }
1352
1353 return snprintf(buf, PAGE_SIZE, "%d\n",
1354 (status == TPACPI_RFK_RADIO_ON) ? 1 : 0);
1355}
1356
1357static ssize_t tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id,
1358 struct device_attribute *attr,
1359 const char *buf, size_t count)
1360{
1361 unsigned long t;
1362 int res;
1363
1364 printk_deprecated_rfkill_attribute(attr->attr.name);
1365
1366 if (parse_strtoul(buf, 1, &t))
1367 return -EINVAL;
1368
1369 tpacpi_disclose_usertask(attr->attr.name, "set to %ld\n", t);
1370
1371 /* This is in the ABI... */
1372 if (tpacpi_rfk_check_hwblock_state() && !!t)
1373 return -EPERM;
1374
1375 res = tpacpi_rfkill_switches[id]->ops->set_status((!!t) ?
1376 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF);
1377 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1378
1379 return (res < 0) ? res : count;
1380}
1381
1382/* procfs -------------------------------------------------------------- */
1383static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id, char *p)
1384{
1385 int len = 0;
1386
1387 if (id >= TPACPI_RFK_SW_MAX)
1388 len += sprintf(p + len, "status:\t\tnot supported\n");
1389 else {
1390 int status;
1391
1392 /* This is in the ABI... */
1393 if (tpacpi_rfk_check_hwblock_state()) {
1394 status = TPACPI_RFK_RADIO_OFF;
1395 } else {
1396 status = tpacpi_rfk_update_swstate(
1397 tpacpi_rfkill_switches[id]);
1398 if (status < 0)
1399 return status;
1400 }
1401
1402 len += sprintf(p + len, "status:\t\t%s\n",
1403 (status == TPACPI_RFK_RADIO_ON) ?
1404 "enabled" : "disabled");
1405 len += sprintf(p + len, "commands:\tenable, disable\n");
1406 }
1407
1408 return len;
1409}
1410
1411static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id, char *buf)
1412{
1413 char *cmd;
1414 int status = -1;
1415 int res = 0;
1416
1417 if (id >= TPACPI_RFK_SW_MAX)
1418 return -ENODEV;
1419
1420 while ((cmd = next_cmd(&buf))) {
1421 if (strlencmp(cmd, "enable") == 0)
1422 status = TPACPI_RFK_RADIO_ON;
1423 else if (strlencmp(cmd, "disable") == 0)
1424 status = TPACPI_RFK_RADIO_OFF;
1425 else
1426 return -EINVAL;
1427 }
1428
1429 if (status != -1) {
1430 tpacpi_disclose_usertask("procfs", "attempt to %s %s\n",
1431 (status == TPACPI_RFK_RADIO_ON) ?
1432 "enable" : "disable",
1433 tpacpi_rfkill_names[id]);
1434 res = (tpacpi_rfkill_switches[id]->ops->set_status)(status);
1435 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1436 }
1437
1438 return res;
1439}
1440
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001441/*************************************************************************
1442 * thinkpad-acpi driver attributes
1443 */
1444
1445/* interface_version --------------------------------------------------- */
1446static ssize_t tpacpi_driver_interface_version_show(
1447 struct device_driver *drv,
1448 char *buf)
1449{
1450 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1451}
1452
1453static DRIVER_ATTR(interface_version, S_IRUGO,
1454 tpacpi_driver_interface_version_show, NULL);
1455
1456/* debug_level --------------------------------------------------------- */
1457static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1458 char *buf)
1459{
1460 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1461}
1462
1463static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1464 const char *buf, size_t count)
1465{
1466 unsigned long t;
1467
1468 if (parse_strtoul(buf, 0xffff, &t))
1469 return -EINVAL;
1470
1471 dbg_level = t;
1472
1473 return count;
1474}
1475
1476static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1477 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1478
1479/* version ------------------------------------------------------------- */
1480static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1481 char *buf)
1482{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001483 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1484 TPACPI_DESC, TPACPI_VERSION);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001485}
1486
1487static DRIVER_ATTR(version, S_IRUGO,
1488 tpacpi_driver_version_show, NULL);
1489
1490/* --------------------------------------------------------------------- */
1491
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001492#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1493
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001494/* wlsw_emulstate ------------------------------------------------------ */
1495static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1496 char *buf)
1497{
1498 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1499}
1500
1501static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1502 const char *buf, size_t count)
1503{
1504 unsigned long t;
1505
1506 if (parse_strtoul(buf, 1, &t))
1507 return -EINVAL;
1508
Johannes Berg19d337d2009-06-02 13:01:37 +02001509 if (tpacpi_wlsw_emulstate != !!t) {
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001510 tpacpi_wlsw_emulstate = !!t;
Johannes Berg19d337d2009-06-02 13:01:37 +02001511 tpacpi_rfk_update_hwblock_state(!t); /* negative logic */
1512 }
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001513
1514 return count;
1515}
1516
1517static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1518 tpacpi_driver_wlsw_emulstate_show,
1519 tpacpi_driver_wlsw_emulstate_store);
1520
1521/* bluetooth_emulstate ------------------------------------------------- */
1522static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1523 struct device_driver *drv,
1524 char *buf)
1525{
1526 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1527}
1528
1529static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1530 struct device_driver *drv,
1531 const char *buf, size_t count)
1532{
1533 unsigned long t;
1534
1535 if (parse_strtoul(buf, 1, &t))
1536 return -EINVAL;
1537
1538 tpacpi_bluetooth_emulstate = !!t;
1539
1540 return count;
1541}
1542
1543static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1544 tpacpi_driver_bluetooth_emulstate_show,
1545 tpacpi_driver_bluetooth_emulstate_store);
1546
1547/* wwan_emulstate ------------------------------------------------- */
1548static ssize_t tpacpi_driver_wwan_emulstate_show(
1549 struct device_driver *drv,
1550 char *buf)
1551{
1552 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1553}
1554
1555static ssize_t tpacpi_driver_wwan_emulstate_store(
1556 struct device_driver *drv,
1557 const char *buf, size_t count)
1558{
1559 unsigned long t;
1560
1561 if (parse_strtoul(buf, 1, &t))
1562 return -EINVAL;
1563
1564 tpacpi_wwan_emulstate = !!t;
1565
1566 return count;
1567}
1568
1569static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1570 tpacpi_driver_wwan_emulstate_show,
1571 tpacpi_driver_wwan_emulstate_store);
1572
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001573/* uwb_emulstate ------------------------------------------------- */
1574static ssize_t tpacpi_driver_uwb_emulstate_show(
1575 struct device_driver *drv,
1576 char *buf)
1577{
1578 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1579}
1580
1581static ssize_t tpacpi_driver_uwb_emulstate_store(
1582 struct device_driver *drv,
1583 const char *buf, size_t count)
1584{
1585 unsigned long t;
1586
1587 if (parse_strtoul(buf, 1, &t))
1588 return -EINVAL;
1589
1590 tpacpi_uwb_emulstate = !!t;
1591
1592 return count;
1593}
1594
1595static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
1596 tpacpi_driver_uwb_emulstate_show,
1597 tpacpi_driver_uwb_emulstate_store);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001598#endif
1599
1600/* --------------------------------------------------------------------- */
1601
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001602static struct driver_attribute *tpacpi_driver_attributes[] = {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001603 &driver_attr_debug_level, &driver_attr_version,
1604 &driver_attr_interface_version,
1605};
1606
1607static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1608{
1609 int i, res;
1610
1611 i = 0;
1612 res = 0;
1613 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1614 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1615 i++;
1616 }
1617
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001618#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1619 if (!res && dbg_wlswemul)
1620 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1621 if (!res && dbg_bluetoothemul)
1622 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1623 if (!res && dbg_wwanemul)
1624 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001625 if (!res && dbg_uwbemul)
1626 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001627#endif
1628
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001629 return res;
1630}
1631
1632static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1633{
1634 int i;
1635
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001636 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001637 driver_remove_file(drv, tpacpi_driver_attributes[i]);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001638
1639#ifdef THINKPAD_ACPI_DEBUGFACILITIES
1640 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1641 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1642 driver_remove_file(drv, &driver_attr_wwan_emulstate);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001643 driver_remove_file(drv, &driver_attr_uwb_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001644#endif
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001645}
1646
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001647/*************************************************************************
1648 * Firmware Data
1649 */
1650
1651/*
1652 * Table of recommended minimum BIOS versions
1653 *
1654 * Reasons for listing:
1655 * 1. Stable BIOS, listed because the unknown ammount of
1656 * bugs and bad ACPI behaviour on older versions
1657 *
1658 * 2. BIOS or EC fw with known bugs that trigger on Linux
1659 *
1660 * 3. BIOS with known reduced functionality in older versions
1661 *
1662 * We recommend the latest BIOS and EC version.
1663 * We only support the latest BIOS and EC fw version as a rule.
1664 *
1665 * Sources: IBM ThinkPad Public Web Documents (update changelogs),
1666 * Information from users in ThinkWiki
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03001667 *
1668 * WARNING: we use this table also to detect that the machine is
1669 * a ThinkPad in some cases, so don't remove entries lightly.
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001670 */
1671
1672#define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1673 { .vendor = (__v), \
1674 .bios = TPID(__id1, __id2), \
1675 .ec = TPACPI_MATCH_ANY, \
1676 .quirks = TPACPI_MATCH_ANY << 16 \
1677 | (__bv1) << 8 | (__bv2) }
1678
1679#define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001680 __eid, __ev1, __ev2) \
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001681 { .vendor = (__v), \
1682 .bios = TPID(__bid1, __bid2), \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001683 .ec = __eid, \
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001684 .quirks = (__ev1) << 24 | (__ev2) << 16 \
1685 | (__bv1) << 8 | (__bv2) }
1686
1687#define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1688 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1689
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001690/* Outdated IBM BIOSes often lack the EC id string */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001691#define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1692 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001693 __bv1, __bv2, TPID(__id1, __id2), \
1694 __ev1, __ev2), \
1695 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1696 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1697 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001698
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001699/* Outdated IBM BIOSes often lack the EC id string */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001700#define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1701 __eid1, __eid2, __ev1, __ev2) \
1702 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001703 __bv1, __bv2, TPID(__eid1, __eid2), \
1704 __ev1, __ev2), \
1705 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1706 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1707 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001708
1709#define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1710 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1711
1712#define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1713 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001714 __bv1, __bv2, TPID(__id1, __id2), \
1715 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001716
1717#define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1718 __eid1, __eid2, __ev1, __ev2) \
1719 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001720 __bv1, __bv2, TPID(__eid1, __eid2), \
1721 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001722
1723static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
1724 /* Numeric models ------------------ */
1725 /* FW MODEL BIOS VERS */
1726 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1727 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1728 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1729 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1730 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1731 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1732 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1733 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1734 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1735
1736 /* A-series ------------------------- */
1737 /* FW MODEL BIOS VERS EC VERS */
1738 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1739 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1740 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1741 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1742 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1743 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1744 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1745 TPV_QI0('1', '3', '2', '0'), /* A22m */
1746 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1747 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1748 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1749
1750 /* G-series ------------------------- */
1751 /* FW MODEL BIOS VERS */
1752 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1753 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1754
1755 /* R-series, T-series --------------- */
1756 /* FW MODEL BIOS VERS EC VERS */
1757 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1758 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1759 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1760 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1761 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1762 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1763 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1764 T40/p, T41/p, T42/p (1) */
1765 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1766 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1767 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1768 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1769
1770 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1771 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1772 TPV_QI0('1', '6', '3', '2'), /* T22 */
1773 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1774 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1775 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1776
1777 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1778 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
1779 TPV_QL0('7', 'E', 'D', '0'), /* R60e, R60i */
1780
1781 /* BIOS FW BIOS VERS EC FW EC VERS */
1782 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1783 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1784
1785 /* X-series ------------------------- */
1786 /* FW MODEL BIOS VERS EC VERS */
1787 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1788 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1789 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1790 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1791 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1792 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1793 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1794
1795 TPV_QL0('7', 'B', 'D', '7'), /* X60/s */
1796 TPV_QL0('7', 'J', '3', '0'), /* X60t */
1797
1798 /* (0) - older versions lack DMI EC fw string and functionality */
1799 /* (1) - older versions known to lack functionality */
1800};
1801
1802#undef TPV_QL1
1803#undef TPV_QL0
1804#undef TPV_QI2
1805#undef TPV_QI1
1806#undef TPV_QI0
1807#undef TPV_Q_X
1808#undef TPV_Q
1809
1810static void __init tpacpi_check_outdated_fw(void)
1811{
1812 unsigned long fwvers;
1813 u16 ec_version, bios_version;
1814
1815 fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
1816 ARRAY_SIZE(tpacpi_bios_version_qtable));
1817
1818 if (!fwvers)
1819 return;
1820
1821 bios_version = fwvers & 0xffffU;
1822 ec_version = (fwvers >> 16) & 0xffffU;
1823
1824 /* note that unknown versions are set to 0x0000 and we use that */
1825 if ((bios_version > thinkpad_id.bios_release) ||
1826 (ec_version > thinkpad_id.ec_release &&
1827 ec_version != TPACPI_MATCH_ANY)) {
1828 /*
1829 * The changelogs would let us track down the exact
1830 * reason, but it is just too much of a pain to track
1831 * it. We only list BIOSes that are either really
1832 * broken, or really stable to begin with, so it is
1833 * best if the user upgrades the firmware anyway.
1834 */
1835 printk(TPACPI_WARN
1836 "WARNING: Outdated ThinkPad BIOS/EC firmware\n");
1837 printk(TPACPI_WARN
1838 "WARNING: This firmware may be missing critical bug "
1839 "fixes and/or important features\n");
1840 }
1841}
1842
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03001843static bool __init tpacpi_is_fw_known(void)
1844{
1845 return tpacpi_check_quirks(tpacpi_bios_version_qtable,
1846 ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
1847}
1848
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03001849/****************************************************************************
1850 ****************************************************************************
1851 *
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001852 * Subdrivers
1853 *
1854 ****************************************************************************
1855 ****************************************************************************/
1856
1857/*************************************************************************
Henrique de Moraes Holschuh142cfc92007-04-21 11:08:26 -03001858 * thinkpad-acpi init subdriver
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001859 */
1860
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001861static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001863 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
1864 printk(TPACPI_INFO "%s\n", TPACPI_URL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001866 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03001867 (thinkpad_id.bios_version_str) ?
1868 thinkpad_id.bios_version_str : "unknown",
1869 (thinkpad_id.ec_version_str) ?
1870 thinkpad_id.ec_version_str : "unknown");
1871
1872 if (thinkpad_id.vendor && thinkpad_id.model_str)
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03001873 printk(TPACPI_INFO "%s %s, model %s\n",
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03001874 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
1875 "IBM" : ((thinkpad_id.vendor ==
1876 PCI_VENDOR_ID_LENOVO) ?
1877 "Lenovo" : "Unknown vendor"),
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03001878 thinkpad_id.model_str,
1879 (thinkpad_id.nummodel_str) ?
1880 thinkpad_id.nummodel_str : "unknown");
Henrique de Moraes Holschuh3945ac32007-02-06 19:13:44 -02001881
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001882 tpacpi_check_outdated_fw();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 return 0;
1884}
1885
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -03001886static int thinkpad_acpi_driver_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887{
1888 int len = 0;
1889
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001890 len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
1891 len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892
1893 return len;
1894}
1895
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001896static struct ibm_struct thinkpad_acpi_driver_data = {
1897 .name = "driver",
1898 .read = thinkpad_acpi_driver_read,
1899};
1900
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001901/*************************************************************************
1902 * Hotkey subdriver
1903 */
1904
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001905/*
1906 * ThinkPad firmware event model
1907 *
1908 * The ThinkPad firmware has two main event interfaces: normal ACPI
1909 * notifications (which follow the ACPI standard), and a private event
1910 * interface.
1911 *
1912 * The private event interface also issues events for the hotkeys. As
1913 * the driver gained features, the event handling code ended up being
1914 * built around the hotkey subdriver. This will need to be refactored
1915 * to a more formal event API eventually.
1916 *
1917 * Some "hotkeys" are actually supposed to be used as event reports,
1918 * such as "brightness has changed", "volume has changed", depending on
1919 * the ThinkPad model and how the firmware is operating.
1920 *
1921 * Unlike other classes, hotkey-class events have mask/unmask control on
1922 * non-ancient firmware. However, how it behaves changes a lot with the
1923 * firmware model and version.
1924 */
1925
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001926enum { /* hot key scan codes (derived from ACPI DSDT) */
1927 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1928 TP_ACPI_HOTKEYSCAN_FNF2,
1929 TP_ACPI_HOTKEYSCAN_FNF3,
1930 TP_ACPI_HOTKEYSCAN_FNF4,
1931 TP_ACPI_HOTKEYSCAN_FNF5,
1932 TP_ACPI_HOTKEYSCAN_FNF6,
1933 TP_ACPI_HOTKEYSCAN_FNF7,
1934 TP_ACPI_HOTKEYSCAN_FNF8,
1935 TP_ACPI_HOTKEYSCAN_FNF9,
1936 TP_ACPI_HOTKEYSCAN_FNF10,
1937 TP_ACPI_HOTKEYSCAN_FNF11,
1938 TP_ACPI_HOTKEYSCAN_FNF12,
1939 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1940 TP_ACPI_HOTKEYSCAN_FNINSERT,
1941 TP_ACPI_HOTKEYSCAN_FNDELETE,
1942 TP_ACPI_HOTKEYSCAN_FNHOME,
1943 TP_ACPI_HOTKEYSCAN_FNEND,
1944 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1945 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1946 TP_ACPI_HOTKEYSCAN_FNSPACE,
1947 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1948 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1949 TP_ACPI_HOTKEYSCAN_MUTE,
1950 TP_ACPI_HOTKEYSCAN_THINKPAD,
1951};
1952
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001953enum { /* Keys/events available through NVRAM polling */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001954 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1955 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1956};
1957
1958enum { /* Positions of some of the keys in hotkey masks */
1959 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1960 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1961 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1962 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1963 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1964 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1965 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1966 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1967 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1968 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1969 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1970};
1971
1972enum { /* NVRAM to ACPI HKEY group map */
1973 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1974 TP_ACPI_HKEY_ZOOM_MASK |
1975 TP_ACPI_HKEY_DISPSWTCH_MASK |
1976 TP_ACPI_HKEY_HIBERNATE_MASK,
1977 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1978 TP_ACPI_HKEY_BRGHTDWN_MASK,
1979 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1980 TP_ACPI_HKEY_VOLDWN_MASK |
1981 TP_ACPI_HKEY_MUTE_MASK,
1982};
1983
1984#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1985struct tp_nvram_state {
1986 u16 thinkpad_toggle:1;
1987 u16 zoom_toggle:1;
1988 u16 display_toggle:1;
1989 u16 thinklight_toggle:1;
1990 u16 hibernate_toggle:1;
1991 u16 displayexp_toggle:1;
1992 u16 display_state:1;
1993 u16 brightness_toggle:1;
1994 u16 volume_toggle:1;
1995 u16 mute:1;
1996
1997 u8 brightness_level;
1998 u8 volume_level;
1999};
2000
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002001/* kthread for the hotkey poller */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002002static struct task_struct *tpacpi_hotkey_task;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002003
2004/* Acquired while the poller kthread is running, use to sync start/stop */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002005static struct mutex hotkey_thread_mutex;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002006
2007/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002008 * Acquire mutex to write poller control variables as an
2009 * atomic block.
2010 *
2011 * Increment hotkey_config_change when changing them if you
2012 * want the kthread to forget old state.
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002013 *
2014 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
2015 */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002016static struct mutex hotkey_thread_data_mutex;
2017static unsigned int hotkey_config_change;
2018
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002019/*
2020 * hotkey poller control variables
2021 *
2022 * Must be atomic or readers will also need to acquire mutex
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002023 *
2024 * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
2025 * should be used only when the changes need to be taken as
2026 * a block, OR when one needs to force the kthread to forget
2027 * old state.
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002028 */
2029static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
2030static unsigned int hotkey_poll_freq = 10; /* Hz */
2031
2032#define HOTKEY_CONFIG_CRITICAL_START \
2033 do { \
2034 mutex_lock(&hotkey_thread_data_mutex); \
2035 hotkey_config_change++; \
2036 } while (0);
2037#define HOTKEY_CONFIG_CRITICAL_END \
2038 mutex_unlock(&hotkey_thread_data_mutex);
2039
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002040#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2041
2042#define hotkey_source_mask 0U
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002043#define HOTKEY_CONFIG_CRITICAL_START
2044#define HOTKEY_CONFIG_CRITICAL_END
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002045
2046#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2047
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002048static struct mutex hotkey_mutex;
2049
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002050static enum { /* Reasons for waking up */
2051 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
2052 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
2053 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
2054} hotkey_wakeup_reason;
2055
2056static int hotkey_autosleep_ack;
2057
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002058static u32 hotkey_orig_mask; /* events the BIOS had enabled */
2059static u32 hotkey_all_mask; /* all events supported in fw */
2060static u32 hotkey_reserved_mask; /* events better left disabled */
2061static u32 hotkey_driver_mask; /* events needed by the driver */
2062static u32 hotkey_user_mask; /* events visible to userspace */
2063static u32 hotkey_acpi_mask; /* events enabled in firmware */
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002064
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02002065static unsigned int hotkey_report_mode;
2066
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002067static u16 *hotkey_keycode_map;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002068
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03002069static struct attribute_set *hotkey_dev_attributes;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002070
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002071static void tpacpi_driver_event(const unsigned int hkey_event);
2072static void hotkey_driver_event(const unsigned int scancode);
2073
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002074/* HKEY.MHKG() return bits */
2075#define TP_HOTKEY_TABLET_MASK (1 << 3)
2076
Johannes Berg19d337d2009-06-02 13:01:37 +02002077static int hotkey_get_wlsw(void)
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002078{
Johannes Berg19d337d2009-06-02 13:01:37 +02002079 int status;
2080
2081 if (!tp_features.hotkey_wlsw)
2082 return -ENODEV;
2083
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02002084#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
Johannes Berg19d337d2009-06-02 13:01:37 +02002085 if (dbg_wlswemul)
2086 return (tpacpi_wlsw_emulstate) ?
2087 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02002088#endif
Johannes Berg19d337d2009-06-02 13:01:37 +02002089
2090 if (!acpi_evalf(hkey_handle, &status, "WLSW", "d"))
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002091 return -EIO;
Johannes Berg19d337d2009-06-02 13:01:37 +02002092
2093 return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002094}
2095
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002096static int hotkey_get_tablet_mode(int *status)
2097{
2098 int s;
2099
2100 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
2101 return -EIO;
2102
Henrique de Moraes Holschuhcee47f52008-03-04 14:29:21 -08002103 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
2104 return 0;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002105}
2106
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002107/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002108 * Reads current event mask from firmware, and updates
2109 * hotkey_acpi_mask accordingly. Also resets any bits
2110 * from hotkey_user_mask that are unavailable to be
2111 * delivered (shadow requirement of the userspace ABI).
2112 *
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002113 * Call with hotkey_mutex held
2114 */
2115static int hotkey_mask_get(void)
2116{
2117 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002118 u32 m = 0;
2119
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002120 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002121 return -EIO;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002122
2123 hotkey_acpi_mask = m;
2124 } else {
2125 /* no mask support doesn't mean no event support... */
2126 hotkey_acpi_mask = hotkey_all_mask;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002127 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002128
2129 /* sync userspace-visible mask */
2130 hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002131
2132 return 0;
2133}
2134
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002135void static hotkey_mask_warn_incomplete_mask(void)
2136{
2137 /* log only what the user can fix... */
2138 const u32 wantedmask = hotkey_driver_mask &
2139 ~(hotkey_acpi_mask | hotkey_source_mask) &
2140 (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
2141
2142 if (wantedmask)
2143 printk(TPACPI_NOTICE
2144 "required events 0x%08x not enabled!\n",
2145 wantedmask);
2146}
2147
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002148/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002149 * Set the firmware mask when supported
2150 *
2151 * Also calls hotkey_mask_get to update hotkey_acpi_mask.
2152 *
2153 * NOTE: does not set bits in hotkey_user_mask, but may reset them.
2154 *
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002155 * Call with hotkey_mutex held
2156 */
2157static int hotkey_mask_set(u32 mask)
2158{
2159 int i;
2160 int rc = 0;
2161
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002162 const u32 fwmask = mask & ~hotkey_source_mask;
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -03002163
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002164 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002165 for (i = 0; i < 32; i++) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002166 if (!acpi_evalf(hkey_handle,
2167 NULL, "MHKM", "vdd", i + 1,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002168 !!(mask & (1 << i)))) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002169 rc = -EIO;
2170 break;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002171 }
2172 }
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002173 }
2174
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002175 /*
2176 * We *must* make an inconditional call to hotkey_mask_get to
2177 * refresh hotkey_acpi_mask and update hotkey_user_mask
2178 *
2179 * Take the opportunity to also log when we cannot _enable_
2180 * a given event.
2181 */
2182 if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
2183 printk(TPACPI_NOTICE
2184 "asked for hotkey mask 0x%08x, but "
2185 "firmware forced it to 0x%08x\n",
2186 fwmask, hotkey_acpi_mask);
2187 }
2188
2189 hotkey_mask_warn_incomplete_mask();
2190
2191 return rc;
2192}
2193
2194/*
2195 * Sets hotkey_user_mask and tries to set the firmware mask
2196 *
2197 * Call with hotkey_mutex held
2198 */
2199static int hotkey_user_mask_set(const u32 mask)
2200{
2201 int rc;
2202
2203 /* Give people a chance to notice they are doing something that
2204 * is bound to go boom on their users sooner or later */
2205 if (!tp_warned.hotkey_mask_ff &&
2206 (mask == 0xffff || mask == 0xffffff ||
2207 mask == 0xffffffff)) {
2208 tp_warned.hotkey_mask_ff = 1;
2209 printk(TPACPI_NOTICE
2210 "setting the hotkey mask to 0x%08x is likely "
2211 "not the best way to go about it\n", mask);
2212 printk(TPACPI_NOTICE
2213 "please consider using the driver defaults, "
2214 "and refer to up-to-date thinkpad-acpi "
2215 "documentation\n");
2216 }
2217
2218 /* Try to enable what the user asked for, plus whatever we need.
2219 * this syncs everything but won't enable bits in hotkey_user_mask */
2220 rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
2221
2222 /* Enable the available bits in hotkey_user_mask */
2223 hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
2224
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002225 return rc;
2226}
2227
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002228/*
2229 * Sets the driver hotkey mask.
2230 *
2231 * Can be called even if the hotkey subdriver is inactive
2232 */
2233static int tpacpi_hotkey_driver_mask_set(const u32 mask)
2234{
2235 int rc;
2236
2237 /* Do the right thing if hotkey_init has not been called yet */
2238 if (!tp_features.hotkey) {
2239 hotkey_driver_mask = mask;
2240 return 0;
2241 }
2242
2243 mutex_lock(&hotkey_mutex);
2244
2245 HOTKEY_CONFIG_CRITICAL_START
2246 hotkey_driver_mask = mask;
Henrique de Moraes Holschuhb684a362009-09-26 21:42:49 -03002247#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002248 hotkey_source_mask |= (mask & ~hotkey_all_mask);
Henrique de Moraes Holschuhb684a362009-09-26 21:42:49 -03002249#endif
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002250 HOTKEY_CONFIG_CRITICAL_END
2251
2252 rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) &
2253 ~hotkey_source_mask);
2254 mutex_unlock(&hotkey_mutex);
2255
2256 return rc;
2257}
2258
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002259static int hotkey_status_get(int *status)
2260{
2261 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
2262 return -EIO;
2263
2264 return 0;
2265}
2266
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002267static int hotkey_status_set(bool enable)
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002268{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002269 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002270 return -EIO;
2271
2272 return 0;
2273}
2274
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002275static void tpacpi_input_send_tabletsw(void)
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002276{
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002277 int state;
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002278
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002279 if (tp_features.hotkey_tablet &&
2280 !hotkey_get_tablet_mode(&state)) {
2281 mutex_lock(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002282
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002283 input_report_switch(tpacpi_inputdev,
2284 SW_TABLET_MODE, !!state);
2285 input_sync(tpacpi_inputdev);
2286
2287 mutex_unlock(&tpacpi_inputdev_send_mutex);
2288 }
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002289}
2290
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002291/* Do NOT call without validating scancode first */
2292static void tpacpi_input_send_key(const unsigned int scancode)
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002293{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002294 const unsigned int keycode = hotkey_keycode_map[scancode];
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002295
2296 if (keycode != KEY_RESERVED) {
2297 mutex_lock(&tpacpi_inputdev_send_mutex);
2298
2299 input_report_key(tpacpi_inputdev, keycode, 1);
2300 if (keycode == KEY_UNKNOWN)
2301 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
2302 scancode);
2303 input_sync(tpacpi_inputdev);
2304
2305 input_report_key(tpacpi_inputdev, keycode, 0);
2306 if (keycode == KEY_UNKNOWN)
2307 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
2308 scancode);
2309 input_sync(tpacpi_inputdev);
2310
2311 mutex_unlock(&tpacpi_inputdev_send_mutex);
2312 }
2313}
2314
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002315/* Do NOT call without validating scancode first */
2316static void tpacpi_input_send_key_masked(const unsigned int scancode)
2317{
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002318 hotkey_driver_event(scancode);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002319 if (hotkey_user_mask & (1 << scancode))
2320 tpacpi_input_send_key(scancode);
2321}
2322
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002323#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2324static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
2325
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002326/* Do NOT call without validating scancode first */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002327static void tpacpi_hotkey_send_key(unsigned int scancode)
2328{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002329 tpacpi_input_send_key_masked(scancode);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002330 if (hotkey_report_mode < 2) {
2331 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03002332 0x80, TP_HKEY_EV_HOTKEY_BASE + scancode);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002333 }
2334}
2335
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002336static void hotkey_read_nvram(struct tp_nvram_state *n, const u32 m)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002337{
2338 u8 d;
2339
2340 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
2341 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
2342 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
2343 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
2344 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
2345 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
2346 }
2347 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
2348 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
2349 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
2350 }
2351 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
2352 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
2353 n->displayexp_toggle =
2354 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
2355 }
2356 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
2357 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
2358 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
2359 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
2360 n->brightness_toggle =
2361 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
2362 }
2363 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
2364 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
2365 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
2366 >> TP_NVRAM_POS_LEVEL_VOLUME;
2367 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2368 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2369 }
2370}
2371
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002372static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
2373 struct tp_nvram_state *newn,
2374 const u32 event_mask)
2375{
2376
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002377#define TPACPI_COMPARE_KEY(__scancode, __member) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002378 do { \
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002379 if ((event_mask & (1 << __scancode)) && \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002380 oldn->__member != newn->__member) \
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002381 tpacpi_hotkey_send_key(__scancode); \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002382 } while (0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002383
2384#define TPACPI_MAY_SEND_KEY(__scancode) \
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002385 do { \
2386 if (event_mask & (1 << __scancode)) \
2387 tpacpi_hotkey_send_key(__scancode); \
2388 } while (0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002389
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002390 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2391 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2392 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2393 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2394
2395 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2396
2397 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2398
2399 /* handle volume */
2400 if (oldn->volume_toggle != newn->volume_toggle) {
2401 if (oldn->mute != newn->mute) {
2402 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2403 }
2404 if (oldn->volume_level > newn->volume_level) {
2405 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2406 } else if (oldn->volume_level < newn->volume_level) {
2407 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2408 } else if (oldn->mute == newn->mute) {
2409 /* repeated key presses that didn't change state */
2410 if (newn->mute) {
2411 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2412 } else if (newn->volume_level != 0) {
2413 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2414 } else {
2415 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2416 }
2417 }
2418 }
2419
2420 /* handle brightness */
2421 if (oldn->brightness_toggle != newn->brightness_toggle) {
2422 if (oldn->brightness_level < newn->brightness_level) {
2423 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2424 } else if (oldn->brightness_level > newn->brightness_level) {
2425 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2426 } else {
2427 /* repeated key presses that didn't change state */
2428 if (newn->brightness_level != 0) {
2429 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2430 } else {
2431 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2432 }
2433 }
2434 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002435
2436#undef TPACPI_COMPARE_KEY
2437#undef TPACPI_MAY_SEND_KEY
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002438}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002439
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002440/*
2441 * Polling driver
2442 *
2443 * We track all events in hotkey_source_mask all the time, since
2444 * most of them are edge-based. We only issue those requested by
2445 * hotkey_user_mask or hotkey_driver_mask, though.
2446 */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002447static int hotkey_kthread(void *data)
2448{
2449 struct tp_nvram_state s[2];
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002450 u32 poll_mask, event_mask;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002451 unsigned int si, so;
2452 unsigned long t;
2453 unsigned int change_detector, must_reset;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002454 unsigned int poll_freq;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002455
2456 mutex_lock(&hotkey_thread_mutex);
2457
2458 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2459 goto exit;
2460
2461 set_freezable();
2462
2463 so = 0;
2464 si = 1;
2465 t = 0;
2466
2467 /* Initial state for compares */
2468 mutex_lock(&hotkey_thread_data_mutex);
2469 change_detector = hotkey_config_change;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002470 poll_mask = hotkey_source_mask;
2471 event_mask = hotkey_source_mask &
2472 (hotkey_driver_mask | hotkey_user_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002473 poll_freq = hotkey_poll_freq;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002474 mutex_unlock(&hotkey_thread_data_mutex);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002475 hotkey_read_nvram(&s[so], poll_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002476
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002477 while (!kthread_should_stop()) {
2478 if (t == 0) {
2479 if (likely(poll_freq))
2480 t = 1000/poll_freq;
2481 else
2482 t = 100; /* should never happen... */
2483 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002484 t = msleep_interruptible(t);
2485 if (unlikely(kthread_should_stop()))
2486 break;
2487 must_reset = try_to_freeze();
2488 if (t > 0 && !must_reset)
2489 continue;
2490
2491 mutex_lock(&hotkey_thread_data_mutex);
2492 if (must_reset || hotkey_config_change != change_detector) {
2493 /* forget old state on thaw or config change */
2494 si = so;
2495 t = 0;
2496 change_detector = hotkey_config_change;
2497 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002498 poll_mask = hotkey_source_mask;
2499 event_mask = hotkey_source_mask &
2500 (hotkey_driver_mask | hotkey_user_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002501 poll_freq = hotkey_poll_freq;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002502 mutex_unlock(&hotkey_thread_data_mutex);
2503
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002504 if (likely(poll_mask)) {
2505 hotkey_read_nvram(&s[si], poll_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002506 if (likely(si != so)) {
2507 hotkey_compare_and_issue_event(&s[so], &s[si],
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002508 event_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002509 }
2510 }
2511
2512 so = si;
2513 si ^= 1;
2514 }
2515
2516exit:
2517 mutex_unlock(&hotkey_thread_mutex);
2518 return 0;
2519}
2520
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002521/* call with hotkey_mutex held */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002522static void hotkey_poll_stop_sync(void)
2523{
2524 if (tpacpi_hotkey_task) {
2525 if (frozen(tpacpi_hotkey_task) ||
2526 freezing(tpacpi_hotkey_task))
2527 thaw_process(tpacpi_hotkey_task);
2528
2529 kthread_stop(tpacpi_hotkey_task);
2530 tpacpi_hotkey_task = NULL;
2531 mutex_lock(&hotkey_thread_mutex);
2532 /* at this point, the thread did exit */
2533 mutex_unlock(&hotkey_thread_mutex);
2534 }
2535}
2536
2537/* call with hotkey_mutex held */
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002538static void hotkey_poll_setup(bool may_warn)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002539{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002540 const u32 poll_driver_mask = hotkey_driver_mask & hotkey_source_mask;
2541 const u32 poll_user_mask = hotkey_user_mask & hotkey_source_mask;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002542
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002543 if (hotkey_poll_freq > 0 &&
2544 (poll_driver_mask ||
2545 (poll_user_mask && tpacpi_inputdev->users > 0))) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002546 if (!tpacpi_hotkey_task) {
2547 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03002548 NULL, TPACPI_NVRAM_KTHREAD_NAME);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002549 if (IS_ERR(tpacpi_hotkey_task)) {
2550 tpacpi_hotkey_task = NULL;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002551 printk(TPACPI_ERR
2552 "could not create kernel thread "
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002553 "for hotkey polling\n");
2554 }
2555 }
2556 } else {
2557 hotkey_poll_stop_sync();
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002558 if (may_warn && (poll_driver_mask || poll_user_mask) &&
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002559 hotkey_poll_freq == 0) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002560 printk(TPACPI_NOTICE
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002561 "hot keys 0x%08x and/or events 0x%08x "
2562 "require polling, which is currently "
2563 "disabled\n",
2564 poll_user_mask, poll_driver_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002565 }
2566 }
2567}
2568
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002569static void hotkey_poll_setup_safe(bool may_warn)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002570{
2571 mutex_lock(&hotkey_mutex);
2572 hotkey_poll_setup(may_warn);
2573 mutex_unlock(&hotkey_mutex);
2574}
2575
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002576/* call with hotkey_mutex held */
2577static void hotkey_poll_set_freq(unsigned int freq)
2578{
2579 if (!freq)
2580 hotkey_poll_stop_sync();
2581
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002582 hotkey_poll_freq = freq;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002583}
2584
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02002585#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2586
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002587static void hotkey_poll_setup_safe(bool __unused)
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02002588{
2589}
2590
2591#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2592
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002593static int hotkey_inputdev_open(struct input_dev *dev)
2594{
2595 switch (tpacpi_lifecycle) {
2596 case TPACPI_LIFE_INIT:
2597 /*
2598 * hotkey_init will call hotkey_poll_setup_safe
2599 * at the appropriate moment
2600 */
2601 return 0;
2602 case TPACPI_LIFE_EXITING:
2603 return -EBUSY;
2604 case TPACPI_LIFE_RUNNING:
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002605 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002606 return 0;
2607 }
2608
2609 /* Should only happen if tpacpi_lifecycle is corrupt */
2610 BUG();
2611 return -EBUSY;
2612}
2613
2614static void hotkey_inputdev_close(struct input_dev *dev)
2615{
2616 /* disable hotkey polling when possible */
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002617 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING &&
2618 !(hotkey_source_mask & hotkey_driver_mask))
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002619 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002620}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002621
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002622/* sysfs hotkey enable ------------------------------------------------- */
2623static ssize_t hotkey_enable_show(struct device *dev,
2624 struct device_attribute *attr,
2625 char *buf)
2626{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002627 int res, status;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002628
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002629 printk_deprecated_attribute("hotkey_enable",
2630 "Hotkey reporting is always enabled");
2631
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002632 res = hotkey_status_get(&status);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002633 if (res)
2634 return res;
2635
2636 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2637}
2638
2639static ssize_t hotkey_enable_store(struct device *dev,
2640 struct device_attribute *attr,
2641 const char *buf, size_t count)
2642{
2643 unsigned long t;
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002644
2645 printk_deprecated_attribute("hotkey_enable",
2646 "Hotkeys can be disabled through hotkey_mask");
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002647
2648 if (parse_strtoul(buf, 1, &t))
2649 return -EINVAL;
2650
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002651 if (t == 0)
2652 return -EPERM;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002653
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002654 return count;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002655}
2656
2657static struct device_attribute dev_attr_hotkey_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002658 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002659 hotkey_enable_show, hotkey_enable_store);
2660
2661/* sysfs hotkey mask --------------------------------------------------- */
2662static ssize_t hotkey_mask_show(struct device *dev,
2663 struct device_attribute *attr,
2664 char *buf)
2665{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002666 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_user_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002667}
2668
2669static ssize_t hotkey_mask_store(struct device *dev,
2670 struct device_attribute *attr,
2671 const char *buf, size_t count)
2672{
2673 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002674 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002675
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002676 if (parse_strtoul(buf, 0xffffffffUL, &t))
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002677 return -EINVAL;
2678
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002679 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002680 return -ERESTARTSYS;
2681
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002682 res = hotkey_user_mask_set(t);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002683
2684#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002685 hotkey_poll_setup(true);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002686#endif
2687
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002688 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002689
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002690 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2691
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002692 return (res) ? res : count;
2693}
2694
2695static struct device_attribute dev_attr_hotkey_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002696 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002697 hotkey_mask_show, hotkey_mask_store);
2698
2699/* sysfs hotkey bios_enabled ------------------------------------------- */
2700static ssize_t hotkey_bios_enabled_show(struct device *dev,
2701 struct device_attribute *attr,
2702 char *buf)
2703{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002704 return sprintf(buf, "0\n");
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002705}
2706
2707static struct device_attribute dev_attr_hotkey_bios_enabled =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002708 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002709
2710/* sysfs hotkey bios_mask ---------------------------------------------- */
2711static ssize_t hotkey_bios_mask_show(struct device *dev,
2712 struct device_attribute *attr,
2713 char *buf)
2714{
Henrique de Moraes Holschuh06777be2009-09-12 15:22:15 -03002715 printk_deprecated_attribute("hotkey_bios_mask",
2716 "This attribute is useless.");
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002717 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002718}
2719
2720static struct device_attribute dev_attr_hotkey_bios_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002721 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002722
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002723/* sysfs hotkey all_mask ----------------------------------------------- */
2724static ssize_t hotkey_all_mask_show(struct device *dev,
2725 struct device_attribute *attr,
2726 char *buf)
2727{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002728 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2729 hotkey_all_mask | hotkey_source_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002730}
2731
2732static struct device_attribute dev_attr_hotkey_all_mask =
2733 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
2734
2735/* sysfs hotkey recommended_mask --------------------------------------- */
2736static ssize_t hotkey_recommended_mask_show(struct device *dev,
2737 struct device_attribute *attr,
2738 char *buf)
2739{
2740 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002741 (hotkey_all_mask | hotkey_source_mask)
2742 & ~hotkey_reserved_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002743}
2744
2745static struct device_attribute dev_attr_hotkey_recommended_mask =
2746 __ATTR(hotkey_recommended_mask, S_IRUGO,
2747 hotkey_recommended_mask_show, NULL);
2748
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002749#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2750
2751/* sysfs hotkey hotkey_source_mask ------------------------------------- */
2752static ssize_t hotkey_source_mask_show(struct device *dev,
2753 struct device_attribute *attr,
2754 char *buf)
2755{
2756 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2757}
2758
2759static ssize_t hotkey_source_mask_store(struct device *dev,
2760 struct device_attribute *attr,
2761 const char *buf, size_t count)
2762{
2763 unsigned long t;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002764 u32 r_ev;
2765 int rc;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002766
2767 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2768 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2769 return -EINVAL;
2770
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002771 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002772 return -ERESTARTSYS;
2773
2774 HOTKEY_CONFIG_CRITICAL_START
2775 hotkey_source_mask = t;
2776 HOTKEY_CONFIG_CRITICAL_END
2777
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002778 rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
2779 ~hotkey_source_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002780 hotkey_poll_setup(true);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002781
2782 /* check if events needed by the driver got disabled */
2783 r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
2784 & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002785
2786 mutex_unlock(&hotkey_mutex);
2787
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002788 if (rc < 0)
2789 printk(TPACPI_ERR "hotkey_source_mask: failed to update the"
2790 "firmware event mask!\n");
2791
2792 if (r_ev)
2793 printk(TPACPI_NOTICE "hotkey_source_mask: "
2794 "some important events were disabled: "
2795 "0x%04x\n", r_ev);
2796
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002797 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2798
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002799 return (rc < 0) ? rc : count;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002800}
2801
2802static struct device_attribute dev_attr_hotkey_source_mask =
2803 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2804 hotkey_source_mask_show, hotkey_source_mask_store);
2805
2806/* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2807static ssize_t hotkey_poll_freq_show(struct device *dev,
2808 struct device_attribute *attr,
2809 char *buf)
2810{
2811 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2812}
2813
2814static ssize_t hotkey_poll_freq_store(struct device *dev,
2815 struct device_attribute *attr,
2816 const char *buf, size_t count)
2817{
2818 unsigned long t;
2819
2820 if (parse_strtoul(buf, 25, &t))
2821 return -EINVAL;
2822
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002823 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002824 return -ERESTARTSYS;
2825
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002826 hotkey_poll_set_freq(t);
2827 hotkey_poll_setup(true);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002828
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002829 mutex_unlock(&hotkey_mutex);
2830
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002831 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2832
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002833 return count;
2834}
2835
2836static struct device_attribute dev_attr_hotkey_poll_freq =
2837 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2838 hotkey_poll_freq_show, hotkey_poll_freq_store);
2839
2840#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2841
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002842/* sysfs hotkey radio_sw (pollable) ------------------------------------ */
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002843static ssize_t hotkey_radio_sw_show(struct device *dev,
2844 struct device_attribute *attr,
2845 char *buf)
2846{
Johannes Berg19d337d2009-06-02 13:01:37 +02002847 int res;
2848 res = hotkey_get_wlsw();
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002849 if (res < 0)
2850 return res;
2851
Johannes Berg19d337d2009-06-02 13:01:37 +02002852 /* Opportunistic update */
2853 tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
2854
2855 return snprintf(buf, PAGE_SIZE, "%d\n",
2856 (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002857}
2858
2859static struct device_attribute dev_attr_hotkey_radio_sw =
2860 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2861
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002862static void hotkey_radio_sw_notify_change(void)
2863{
2864 if (tp_features.hotkey_wlsw)
2865 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2866 "hotkey_radio_sw");
2867}
2868
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002869/* sysfs hotkey tablet mode (pollable) --------------------------------- */
2870static ssize_t hotkey_tablet_mode_show(struct device *dev,
2871 struct device_attribute *attr,
2872 char *buf)
2873{
2874 int res, s;
2875 res = hotkey_get_tablet_mode(&s);
2876 if (res < 0)
2877 return res;
2878
2879 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2880}
2881
2882static struct device_attribute dev_attr_hotkey_tablet_mode =
2883 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2884
2885static void hotkey_tablet_mode_notify_change(void)
2886{
2887 if (tp_features.hotkey_tablet)
2888 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2889 "hotkey_tablet_mode");
2890}
2891
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002892/* sysfs hotkey report_mode -------------------------------------------- */
2893static ssize_t hotkey_report_mode_show(struct device *dev,
2894 struct device_attribute *attr,
2895 char *buf)
2896{
2897 return snprintf(buf, PAGE_SIZE, "%d\n",
2898 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2899}
2900
2901static struct device_attribute dev_attr_hotkey_report_mode =
2902 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2903
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002904/* sysfs wakeup reason (pollable) -------------------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002905static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2906 struct device_attribute *attr,
2907 char *buf)
2908{
2909 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2910}
2911
2912static struct device_attribute dev_attr_hotkey_wakeup_reason =
2913 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2914
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02002915static void hotkey_wakeup_reason_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002916{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002917 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2918 "wakeup_reason");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002919}
2920
2921/* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002922static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2923 struct device_attribute *attr,
2924 char *buf)
2925{
2926 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2927}
2928
2929static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2930 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2931 hotkey_wakeup_hotunplug_complete_show, NULL);
2932
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02002933static void hotkey_wakeup_hotunplug_complete_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002934{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002935 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2936 "wakeup_hotunplug_complete");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002937}
2938
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002939/* --------------------------------------------------------------------- */
2940
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002941static struct attribute *hotkey_attributes[] __initdata = {
2942 &dev_attr_hotkey_enable.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002943 &dev_attr_hotkey_bios_enabled.attr,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002944 &dev_attr_hotkey_bios_mask.attr,
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002945 &dev_attr_hotkey_report_mode.attr,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002946 &dev_attr_hotkey_wakeup_reason.attr,
2947 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002948 &dev_attr_hotkey_mask.attr,
2949 &dev_attr_hotkey_all_mask.attr,
2950 &dev_attr_hotkey_recommended_mask.attr,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002951#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002952 &dev_attr_hotkey_source_mask.attr,
2953 &dev_attr_hotkey_poll_freq.attr,
2954#endif
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002955};
2956
Johannes Berg19d337d2009-06-02 13:01:37 +02002957/*
2958 * Sync both the hw and sw blocking state of all switches
2959 */
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002960static void tpacpi_send_radiosw_update(void)
2961{
2962 int wlsw;
2963
Johannes Berg19d337d2009-06-02 13:01:37 +02002964 /*
2965 * We must sync all rfkill controllers *before* issuing any
2966 * rfkill input events, or we will race the rfkill core input
2967 * handler.
2968 *
2969 * tpacpi_inputdev_send_mutex works as a syncronization point
2970 * for the above.
2971 *
2972 * We optimize to avoid numerous calls to hotkey_get_wlsw.
2973 */
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002974
Johannes Berg19d337d2009-06-02 13:01:37 +02002975 wlsw = hotkey_get_wlsw();
2976
2977 /* Sync hw blocking state first if it is hw-blocked */
2978 if (wlsw == TPACPI_RFK_RADIO_OFF)
2979 tpacpi_rfk_update_hwblock_state(true);
2980
2981 /* Sync sw blocking state */
2982 tpacpi_rfk_update_swstate_all();
2983
2984 /* Sync hw blocking state last if it is hw-unblocked */
2985 if (wlsw == TPACPI_RFK_RADIO_ON)
2986 tpacpi_rfk_update_hwblock_state(false);
2987
2988 /* Issue rfkill input event for WLSW switch */
2989 if (!(wlsw < 0)) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002990 mutex_lock(&tpacpi_inputdev_send_mutex);
2991
2992 input_report_switch(tpacpi_inputdev,
Johannes Berg19d337d2009-06-02 13:01:37 +02002993 SW_RFKILL_ALL, (wlsw > 0));
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002994 input_sync(tpacpi_inputdev);
2995
2996 mutex_unlock(&tpacpi_inputdev_send_mutex);
2997 }
Johannes Berg19d337d2009-06-02 13:01:37 +02002998
2999 /*
3000 * this can be unconditional, as we will poll state again
3001 * if userspace uses the notify to read data
3002 */
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003003 hotkey_radio_sw_notify_change();
3004}
3005
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003006static void hotkey_exit(void)
3007{
3008#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003009 mutex_lock(&hotkey_mutex);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003010 hotkey_poll_stop_sync();
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003011 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003012#endif
3013
3014 if (hotkey_dev_attributes)
3015 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3016
3017 kfree(hotkey_keycode_map);
3018
Henrique de Moraes Holschuh4be73002009-09-20 14:09:23 -03003019 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003020 "restoring original HKEY status and mask\n");
3021 /* yes, there is a bitwise or below, we want the
3022 * functions to be called even if one of them fail */
3023 if (((tp_features.hotkey_mask &&
3024 hotkey_mask_set(hotkey_orig_mask)) |
3025 hotkey_status_set(false)) != 0)
Henrique de Moraes Holschuh4be73002009-09-20 14:09:23 -03003026 printk(TPACPI_ERR
3027 "failed to restore hot key mask "
3028 "to BIOS defaults\n");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003029}
3030
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003031static void __init hotkey_unmap(const unsigned int scancode)
3032{
3033 if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
3034 clear_bit(hotkey_keycode_map[scancode],
3035 tpacpi_inputdev->keybit);
3036 hotkey_keycode_map[scancode] = KEY_RESERVED;
3037 }
3038}
3039
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003040/*
3041 * HKEY quirks:
3042 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
3043 */
3044
3045#define TPACPI_HK_Q_INIMASK 0x0001
3046
3047static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
3048 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
3049 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
3050 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
3051 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
3052 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
3053 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
3054 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
3055 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
3056 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
3057 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
3058 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
3059 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
3060 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
3061 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
3062 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
3063 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
3064 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
3065 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
3066 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
3067};
3068
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003069static int __init hotkey_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003070{
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003071 /* Requirements for changing the default keymaps:
3072 *
3073 * 1. Many of the keys are mapped to KEY_RESERVED for very
3074 * good reasons. Do not change them unless you have deep
3075 * knowledge on the IBM and Lenovo ThinkPad firmware for
3076 * the various ThinkPad models. The driver behaves
3077 * differently for KEY_RESERVED: such keys have their
3078 * hot key mask *unset* in mask_recommended, and also
3079 * in the initial hot key mask programmed into the
3080 * firmware at driver load time, which means the firm-
3081 * ware may react very differently if you change them to
3082 * something else;
3083 *
3084 * 2. You must be subscribed to the linux-thinkpad and
3085 * ibm-acpi-devel mailing lists, and you should read the
3086 * list archives since 2007 if you want to change the
3087 * keymaps. This requirement exists so that you will
3088 * know the past history of problems with the thinkpad-
3089 * acpi driver keymaps, and also that you will be
3090 * listening to any bug reports;
3091 *
3092 * 3. Do not send thinkpad-acpi specific patches directly to
3093 * for merging, *ever*. Send them to the linux-acpi
3094 * mailinglist for comments. Merging is to be done only
3095 * through acpi-test and the ACPI maintainer.
3096 *
3097 * If the above is too much to ask, don't change the keymap.
3098 * Ask the thinkpad-acpi maintainer to do it, instead.
3099 */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003100 static u16 ibm_keycode_map[] __initdata = {
3101 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3102 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
3103 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3104 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003105
3106 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003107 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3108 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3109 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003110
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003111 /* brightness: firmware always reacts to them */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003112 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003113 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003114
3115 /* Thinklight: firmware always react to it */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003116 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003117
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003118 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3119 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003120
3121 /* Volume: firmware always react to it and reprograms
3122 * the built-in *extra* mixer. Never map it to control
3123 * another mixer by default. */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003124 KEY_RESERVED, /* 0x14: VOLUME UP */
3125 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3126 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003127
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003128 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003129
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003130 /* (assignments unknown, please report if found) */
3131 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3132 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3133 };
3134 static u16 lenovo_keycode_map[] __initdata = {
3135 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3136 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
3137 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3138 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003139
3140 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003141 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3142 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3143 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003144
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003145 /* These should be enabled --only-- when ACPI video
3146 * is disabled (i.e. in "vendor" mode), and are handled
3147 * in a special way by the init code */
3148 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
3149 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003150
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003151 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003152
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003153 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3154 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003155
3156 /* Volume: z60/z61, T60 (BIOS version?): firmware always
3157 * react to it and reprograms the built-in *extra* mixer.
3158 * Never map it to control another mixer by default.
3159 *
3160 * T60?, T61, R60?, R61: firmware and EC tries to send
3161 * these over the regular keyboard, so these are no-ops,
3162 * but there are still weird bugs re. MUTE, so do not
3163 * change unless you get test reports from all Lenovo
3164 * models. May cause the BIOS to interfere with the
3165 * HDA mixer.
3166 */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003167 KEY_RESERVED, /* 0x14: VOLUME UP */
3168 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3169 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003170
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003171 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003172
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003173 /* (assignments unknown, please report if found) */
3174 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3175 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3176 };
3177
3178#define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
3179#define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
3180#define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
3181
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003182 int res, i;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003183 int status;
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03003184 int hkeyv;
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03003185
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003186 unsigned long quirks;
3187
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003188 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3189 "initializing hotkey subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003190
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003191 BUG_ON(!tpacpi_inputdev);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003192 BUG_ON(tpacpi_inputdev->open != NULL ||
3193 tpacpi_inputdev->close != NULL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003194
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003195 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003196 mutex_init(&hotkey_mutex);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003197
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003198#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3199 mutex_init(&hotkey_thread_mutex);
3200 mutex_init(&hotkey_thread_data_mutex);
3201#endif
3202
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003203 /* hotkey not supported on 570 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003204 tp_features.hotkey = hkey_handle != NULL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003205
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003206 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3207 "hotkeys are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003208 str_supported(tp_features.hotkey));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003209
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003210 if (!tp_features.hotkey)
3211 return 1;
3212
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003213 quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
3214 ARRAY_SIZE(tpacpi_hotkey_qtable));
3215
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03003216 tpacpi_disable_brightness_delay();
3217
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003218 /* MUST have enough space for all attributes to be added to
3219 * hotkey_dev_attributes */
3220 hotkey_dev_attributes = create_attr_set(
3221 ARRAY_SIZE(hotkey_attributes) + 2,
3222 NULL);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003223 if (!hotkey_dev_attributes)
3224 return -ENOMEM;
3225 res = add_many_to_attr_set(hotkey_dev_attributes,
3226 hotkey_attributes,
3227 ARRAY_SIZE(hotkey_attributes));
3228 if (res)
3229 goto err_exit;
3230
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003231 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003232 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
3233 for HKEY interface version 0x100 */
3234 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
3235 if ((hkeyv >> 8) != 1) {
3236 printk(TPACPI_ERR "unknown version of the "
3237 "HKEY interface: 0x%x\n", hkeyv);
3238 printk(TPACPI_ERR "please report this to %s\n",
3239 TPACPI_MAIL);
3240 } else {
3241 /*
3242 * MHKV 0x100 in A31, R40, R40e,
3243 * T4x, X31, and later
3244 */
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003245 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3246 "firmware HKEY interface version: 0x%x\n",
3247 hkeyv);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003248
3249 /* Paranoia check AND init hotkey_all_mask */
3250 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3251 "MHKA", "qd")) {
3252 printk(TPACPI_ERR
3253 "missing MHKA handler, "
3254 "please report this to %s\n",
3255 TPACPI_MAIL);
3256 /* Fallback: pre-init for FN+F3,F4,F12 */
3257 hotkey_all_mask = 0x080cU;
3258 } else {
3259 tp_features.hotkey_mask = 1;
3260 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003261 }
3262 }
3263
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003264 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3265 "hotkey masks are %s\n",
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003266 str_supported(tp_features.hotkey_mask));
3267
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003268 /* Init hotkey_all_mask if not initialized yet */
3269 if (!tp_features.hotkey_mask && !hotkey_all_mask &&
3270 (quirks & TPACPI_HK_Q_INIMASK))
3271 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003272
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003273 /* Init hotkey_acpi_mask and hotkey_orig_mask */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003274 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003275 /* hotkey_source_mask *must* be zero for
3276 * the first hotkey_mask_get to return hotkey_orig_mask */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003277 res = hotkey_mask_get();
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003278 if (res)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003279 goto err_exit;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003280
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003281 hotkey_orig_mask = hotkey_acpi_mask;
3282 } else {
3283 hotkey_orig_mask = hotkey_all_mask;
3284 hotkey_acpi_mask = hotkey_all_mask;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003285 }
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003286
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003287#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3288 if (dbg_wlswemul) {
3289 tp_features.hotkey_wlsw = 1;
3290 printk(TPACPI_INFO
3291 "radio switch emulation enabled\n");
3292 } else
3293#endif
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003294 /* Not all thinkpads have a hardware radio switch */
3295 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
3296 tp_features.hotkey_wlsw = 1;
3297 printk(TPACPI_INFO
3298 "radio switch found; radios are %s\n",
3299 enabled(status, 0));
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003300 }
3301 if (tp_features.hotkey_wlsw)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003302 res = add_to_attr_set(hotkey_dev_attributes,
3303 &dev_attr_hotkey_radio_sw.attr);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003304
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003305 /* For X41t, X60t, X61t Tablets... */
3306 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
3307 tp_features.hotkey_tablet = 1;
3308 printk(TPACPI_INFO
3309 "possible tablet mode switch found; "
3310 "ThinkPad in %s mode\n",
3311 (status & TP_HOTKEY_TABLET_MASK)?
3312 "tablet" : "laptop");
3313 res = add_to_attr_set(hotkey_dev_attributes,
3314 &dev_attr_hotkey_tablet_mode.attr);
3315 }
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02003316
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003317 if (!res)
3318 res = register_attr_set_with_sysfs(
3319 hotkey_dev_attributes,
3320 &tpacpi_pdev->dev.kobj);
3321 if (res)
3322 goto err_exit;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003323
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003324 /* Set up key map */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003325
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003326 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
3327 GFP_KERNEL);
3328 if (!hotkey_keycode_map) {
3329 printk(TPACPI_ERR
3330 "failed to allocate memory for key map\n");
3331 res = -ENOMEM;
3332 goto err_exit;
3333 }
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003334
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003335 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003336 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003337 "using Lenovo default hot key map\n");
3338 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
3339 TPACPI_HOTKEY_MAP_SIZE);
3340 } else {
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003341 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003342 "using IBM default hot key map\n");
3343 memcpy(hotkey_keycode_map, &ibm_keycode_map,
3344 TPACPI_HOTKEY_MAP_SIZE);
3345 }
3346
3347 set_bit(EV_KEY, tpacpi_inputdev->evbit);
3348 set_bit(EV_MSC, tpacpi_inputdev->evbit);
3349 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
3350 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
3351 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3352 tpacpi_inputdev->keycode = hotkey_keycode_map;
3353 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3354 if (hotkey_keycode_map[i] != KEY_RESERVED) {
3355 set_bit(hotkey_keycode_map[i],
3356 tpacpi_inputdev->keybit);
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003357 } else {
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003358 if (i < sizeof(hotkey_reserved_mask)*8)
3359 hotkey_reserved_mask |= 1 << i;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003360 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003361 }
3362
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003363 if (tp_features.hotkey_wlsw) {
3364 set_bit(EV_SW, tpacpi_inputdev->evbit);
3365 set_bit(SW_RFKILL_ALL, tpacpi_inputdev->swbit);
3366 }
3367 if (tp_features.hotkey_tablet) {
3368 set_bit(EV_SW, tpacpi_inputdev->evbit);
3369 set_bit(SW_TABLET_MODE, tpacpi_inputdev->swbit);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003370 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003371
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003372 /* Do not issue duplicate brightness change events to
3373 * userspace */
3374 if (!tp_features.bright_acpimode)
3375 /* update bright_acpimode... */
3376 tpacpi_check_std_acpi_brightness_support();
3377
Henrique de Moraes Holschuh8bf3d4c2009-05-30 13:25:09 -03003378 if (tp_features.bright_acpimode && acpi_video_backlight_support()) {
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003379 printk(TPACPI_INFO
3380 "This ThinkPad has standard ACPI backlight "
3381 "brightness control, supported by the ACPI "
3382 "video driver\n");
3383 printk(TPACPI_NOTICE
3384 "Disabling thinkpad-acpi brightness events "
3385 "by default...\n");
3386
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003387 /* Disable brightness up/down on Lenovo thinkpads when
3388 * ACPI is handling them, otherwise it is plain impossible
3389 * for userspace to do something even remotely sane */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003390 hotkey_reserved_mask |=
3391 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3392 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003393 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
3394 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003395 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003396
Henrique de Moraes Holschuh230d8cf252009-09-12 15:22:17 -03003397#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003398 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3399 & ~hotkey_all_mask
3400 & ~hotkey_reserved_mask;
Henrique de Moraes Holschuh230d8cf252009-09-12 15:22:17 -03003401
3402 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3403 "hotkey source mask 0x%08x, polling freq %u\n",
3404 hotkey_source_mask, hotkey_poll_freq);
3405#endif
3406
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003407 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3408 "enabling firmware HKEY event interface...\n");
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003409 res = hotkey_status_set(true);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003410 if (res) {
3411 hotkey_exit();
3412 return res;
3413 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003414 res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
3415 | hotkey_driver_mask)
3416 & ~hotkey_source_mask);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003417 if (res < 0 && res != -ENXIO) {
3418 hotkey_exit();
3419 return res;
3420 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003421 hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
3422 & ~hotkey_reserved_mask;
3423 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3424 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3425 hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003426
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003427 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3428 "legacy ibm/hotkey event reporting over procfs %s\n",
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003429 (hotkey_report_mode < 2) ?
3430 "enabled" : "disabled");
3431
3432 tpacpi_inputdev->open = &hotkey_inputdev_open;
3433 tpacpi_inputdev->close = &hotkey_inputdev_close;
3434
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003435 hotkey_poll_setup_safe(true);
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003436 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003437 tpacpi_input_send_tabletsw();
3438
3439 return 0;
3440
3441err_exit:
3442 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3443 hotkey_dev_attributes = NULL;
3444
3445 return (res < 0)? res : 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003446}
3447
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003448static bool hotkey_notify_hotkey(const u32 hkey,
3449 bool *send_acpi_ev,
3450 bool *ignore_acpi_ev)
3451{
3452 /* 0x1000-0x1FFF: key presses */
3453 unsigned int scancode = hkey & 0xfff;
3454 *send_acpi_ev = true;
3455 *ignore_acpi_ev = false;
3456
3457 if (scancode > 0 && scancode < 0x21) {
3458 scancode--;
3459 if (!(hotkey_source_mask & (1 << scancode))) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003460 tpacpi_input_send_key_masked(scancode);
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003461 *send_acpi_ev = false;
3462 } else {
3463 *ignore_acpi_ev = true;
3464 }
3465 return true;
3466 }
3467 return false;
3468}
3469
3470static bool hotkey_notify_wakeup(const u32 hkey,
3471 bool *send_acpi_ev,
3472 bool *ignore_acpi_ev)
3473{
3474 /* 0x2000-0x2FFF: Wakeup reason */
3475 *send_acpi_ev = true;
3476 *ignore_acpi_ev = false;
3477
3478 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003479 case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */
3480 case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003481 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3482 *ignore_acpi_ev = true;
3483 break;
3484
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003485 case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */
3486 case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003487 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3488 *ignore_acpi_ev = true;
3489 break;
3490
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003491 case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */
3492 case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003493 printk(TPACPI_ALERT
3494 "EMERGENCY WAKEUP: battery almost empty\n");
3495 /* how to auto-heal: */
3496 /* 2313: woke up from S3, go to S4/S5 */
3497 /* 2413: woke up from S4, go to S5 */
3498 break;
3499
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003500 default:
3501 return false;
3502 }
3503
3504 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
3505 printk(TPACPI_INFO
3506 "woke up due to a hot-unplug "
3507 "request...\n");
3508 hotkey_wakeup_reason_notify_change();
3509 }
3510 return true;
3511}
3512
3513static bool hotkey_notify_usrevent(const u32 hkey,
3514 bool *send_acpi_ev,
3515 bool *ignore_acpi_ev)
3516{
3517 /* 0x5000-0x5FFF: human interface helpers */
3518 *send_acpi_ev = true;
3519 *ignore_acpi_ev = false;
3520
3521 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003522 case TP_HKEY_EV_PEN_INSERTED: /* X61t: tablet pen inserted into bay */
3523 case TP_HKEY_EV_PEN_REMOVED: /* X61t: tablet pen removed from bay */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003524 return true;
3525
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003526 case TP_HKEY_EV_TABLET_TABLET: /* X41t-X61t: tablet mode */
3527 case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003528 tpacpi_input_send_tabletsw();
3529 hotkey_tablet_mode_notify_change();
3530 *send_acpi_ev = false;
3531 return true;
3532
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003533 case TP_HKEY_EV_LID_CLOSE: /* Lid closed */
3534 case TP_HKEY_EV_LID_OPEN: /* Lid opened */
3535 case TP_HKEY_EV_BRGHT_CHANGED: /* brightness changed */
Henrique de Moraes Holschuh176dd982009-09-20 14:09:24 -03003536 /* do not propagate these events */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003537 *ignore_acpi_ev = true;
3538 return true;
3539
3540 default:
3541 return false;
3542 }
3543}
3544
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003545static bool hotkey_notify_thermal(const u32 hkey,
3546 bool *send_acpi_ev,
3547 bool *ignore_acpi_ev)
3548{
3549 /* 0x6000-0x6FFF: thermal alarms */
3550 *send_acpi_ev = true;
3551 *ignore_acpi_ev = false;
3552
3553 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003554 case TP_HKEY_EV_ALARM_BAT_HOT:
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003555 printk(TPACPI_CRIT
3556 "THERMAL ALARM: battery is too hot!\n");
3557 /* recommended action: warn user through gui */
3558 return true;
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003559 case TP_HKEY_EV_ALARM_BAT_XHOT:
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003560 printk(TPACPI_ALERT
3561 "THERMAL EMERGENCY: battery is extremely hot!\n");
3562 /* recommended action: immediate sleep/hibernate */
3563 return true;
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003564 case TP_HKEY_EV_ALARM_SENSOR_HOT:
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003565 printk(TPACPI_CRIT
3566 "THERMAL ALARM: "
3567 "a sensor reports something is too hot!\n");
3568 /* recommended action: warn user through gui, that */
3569 /* some internal component is too hot */
3570 return true;
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003571 case TP_HKEY_EV_ALARM_SENSOR_XHOT:
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003572 printk(TPACPI_ALERT
3573 "THERMAL EMERGENCY: "
3574 "a sensor reports something is extremely hot!\n");
3575 /* recommended action: immediate sleep/hibernate */
3576 return true;
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003577 case TP_HKEY_EV_THM_TABLE_CHANGED:
Henrique de Moraes Holschuh54926ce2009-01-11 03:01:09 -02003578 printk(TPACPI_INFO
3579 "EC reports that Thermal Table has changed\n");
3580 /* recommended action: do nothing, we don't have
3581 * Lenovo ATM information */
3582 return true;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003583 default:
3584 printk(TPACPI_ALERT
3585 "THERMAL ALERT: unknown thermal alarm received\n");
3586 return false;
3587 }
3588}
3589
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003590static void hotkey_notify(struct ibm_struct *ibm, u32 event)
3591{
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003592 u32 hkey;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003593 bool send_acpi_ev;
3594 bool ignore_acpi_ev;
3595 bool known_ev;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003596
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003597 if (event != 0x80) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003598 printk(TPACPI_ERR
3599 "unknown HKEY notification event %d\n", event);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003600 /* forward it to userspace, maybe it knows how to handle it */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003601 acpi_bus_generate_netlink_event(
3602 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02003603 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003604 event, 0);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003605 return;
3606 }
3607
3608 while (1) {
3609 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003610 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003611 return;
3612 }
3613
3614 if (hkey == 0) {
3615 /* queue empty */
3616 return;
3617 }
3618
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003619 send_acpi_ev = true;
3620 ignore_acpi_ev = false;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003621
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003622 switch (hkey >> 12) {
3623 case 1:
3624 /* 0x1000-0x1FFF: key presses */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003625 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
3626 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003627 break;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003628 case 2:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003629 /* 0x2000-0x2FFF: Wakeup reason */
3630 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
3631 &ignore_acpi_ev);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003632 break;
3633 case 3:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003634 /* 0x3000-0x3FFF: bay-related wakeups */
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003635 if (hkey == TP_HKEY_EV_BAYEJ_ACK) {
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003636 hotkey_autosleep_ack = 1;
3637 printk(TPACPI_INFO
3638 "bay ejected\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003639 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003640 known_ev = true;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003641 } else {
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003642 known_ev = false;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003643 }
3644 break;
3645 case 4:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003646 /* 0x4000-0x4FFF: dock-related wakeups */
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003647 if (hkey == TP_HKEY_EV_UNDOCK_ACK) {
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003648 hotkey_autosleep_ack = 1;
3649 printk(TPACPI_INFO
3650 "undocked\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003651 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003652 known_ev = true;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003653 } else {
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003654 known_ev = false;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003655 }
3656 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003657 case 5:
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02003658 /* 0x5000-0x5FFF: human interface helpers */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003659 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
3660 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003661 break;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003662 case 6:
3663 /* 0x6000-0x6FFF: thermal alarms */
3664 known_ev = hotkey_notify_thermal(hkey, &send_acpi_ev,
3665 &ignore_acpi_ev);
3666 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003667 case 7:
3668 /* 0x7000-0x7FFF: misc */
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003669 if (tp_features.hotkey_wlsw &&
3670 hkey == TP_HKEY_EV_RFKILL_CHANGED) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003671 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02003672 send_acpi_ev = 0;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003673 known_ev = true;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003674 break;
3675 }
3676 /* fallthrough to default */
3677 default:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003678 known_ev = false;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02003679 }
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003680 if (!known_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003681 printk(TPACPI_NOTICE
3682 "unhandled HKEY event 0x%04x\n", hkey);
Henrique de Moraes Holschuhcb429352009-01-11 03:01:07 -02003683 printk(TPACPI_NOTICE
3684 "please report the conditions when this "
3685 "event happened to %s\n", TPACPI_MAIL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003686 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003687
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003688 /* Legacy events */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003689 if (!ignore_acpi_ev &&
3690 (send_acpi_ev || hotkey_report_mode < 2)) {
3691 acpi_bus_generate_proc_event(ibm->acpi->device,
3692 event, hkey);
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03003693 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003694
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003695 /* netlink events */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03003696 if (!ignore_acpi_ev && send_acpi_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003697 acpi_bus_generate_netlink_event(
3698 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02003699 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003700 event, hkey);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003701 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003702 }
3703}
3704
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003705static void hotkey_suspend(pm_message_t state)
3706{
3707 /* Do these on suspend, we get the events on early resume! */
3708 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
3709 hotkey_autosleep_ack = 0;
3710}
3711
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03003712static void hotkey_resume(void)
3713{
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03003714 tpacpi_disable_brightness_delay();
3715
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003716 if (hotkey_status_set(true) < 0 ||
3717 hotkey_mask_set(hotkey_acpi_mask) < 0)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003718 printk(TPACPI_ERR
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003719 "error while attempting to reset the event "
3720 "firmware interface\n");
3721
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003722 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02003723 hotkey_tablet_mode_notify_change();
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003724 hotkey_wakeup_reason_notify_change();
3725 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003726 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03003727}
3728
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003729/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003730static int hotkey_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03003732 int res, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733 int len = 0;
3734
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003735 if (!tp_features.hotkey) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003736 len += sprintf(p + len, "status:\t\tnot supported\n");
3737 return len;
3738 }
3739
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02003740 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02003741 return -ERESTARTSYS;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02003742 res = hotkey_status_get(&status);
3743 if (!res)
3744 res = hotkey_mask_get();
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003745 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03003746 if (res)
3747 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748
3749 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003750 if (hotkey_all_mask) {
3751 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_user_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752 len += sprintf(p + len,
3753 "commands:\tenable, disable, reset, <mask>\n");
3754 } else {
3755 len += sprintf(p + len, "mask:\t\tnot supported\n");
3756 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
3757 }
3758
3759 return len;
3760}
3761
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003762static void hotkey_enabledisable_warn(bool enable)
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003763{
3764 tpacpi_log_usertask("procfs hotkey enable/disable");
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003765 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
3766 TPACPI_WARN
3767 "hotkey enable/disable functionality has been "
3768 "removed from the driver. Hotkeys are always "
3769 "enabled\n"))
3770 printk(TPACPI_ERR
3771 "Please remove the hotkey=enable module "
3772 "parameter, it is deprecated. Hotkeys are always "
3773 "enabled\n");
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003774}
3775
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003776static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003778 int res;
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03003779 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003782 if (!tp_features.hotkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783 return -ENODEV;
3784
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02003785 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02003786 return -ERESTARTSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003787
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003788 mask = hotkey_user_mask;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003789
3790 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791 while ((cmd = next_cmd(&buf))) {
3792 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003793 hotkey_enabledisable_warn(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003795 hotkey_enabledisable_warn(0);
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003796 res = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797 } else if (strlencmp(cmd, "reset") == 0) {
Henrique de Moraes Holschuh20c9aa42009-09-12 15:22:16 -03003798 mask = (hotkey_all_mask | hotkey_source_mask)
3799 & ~hotkey_reserved_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
3801 /* mask set */
3802 } else if (sscanf(cmd, "%x", &mask) == 1) {
3803 /* mask set */
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003804 } else {
3805 res = -EINVAL;
3806 goto errexit;
3807 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808 }
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003809
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003810 if (!res) {
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003811 tpacpi_disclose_usertask("procfs hotkey",
3812 "set mask to 0x%08x\n", mask);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003813 res = hotkey_user_mask_set(mask);
3814 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003816errexit:
3817 mutex_unlock(&hotkey_mutex);
3818 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003819}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003821static const struct acpi_device_id ibm_htk_device_ids[] = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003822 {TPACPI_ACPI_HKEY_HID, 0},
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003823 {"", 0},
3824};
3825
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003826static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003827 .hid = ibm_htk_device_ids,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003828 .notify = hotkey_notify,
3829 .handle = &hkey_handle,
3830 .type = ACPI_DEVICE_NOTIFY,
3831};
3832
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003833static struct ibm_struct hotkey_driver_data = {
3834 .name = "hotkey",
3835 .read = hotkey_read,
3836 .write = hotkey_write,
3837 .exit = hotkey_exit,
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03003838 .resume = hotkey_resume,
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003839 .suspend = hotkey_suspend,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003840 .acpi = &ibm_hotkey_acpidriver,
3841};
3842
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003843/*************************************************************************
3844 * Bluetooth subdriver
3845 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003846
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003847enum {
3848 /* ACPI GBDC/SBDC bits */
3849 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
3850 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003851 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
3852 off / last state */
3853};
3854
3855enum {
3856 /* ACPI \BLTH commands */
3857 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
3858 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
3859 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
3860 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
3861 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003862};
3863
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003864#define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
3865
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003866static void bluetooth_suspend(pm_message_t state)
3867{
3868 /* Try to make sure radio will resume powered off */
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003869 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3870 TP_ACPI_BLTH_PWR_OFF_ON_RESUME))
3871 vdbg_printk(TPACPI_DBG_RFKILL,
3872 "bluetooth power down on resume request failed\n");
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003873}
3874
Johannes Berg19d337d2009-06-02 13:01:37 +02003875static int bluetooth_get_status(void)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003876{
3877 int status;
3878
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003879#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3880 if (dbg_bluetoothemul)
3881 return (tpacpi_bluetooth_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02003882 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003883#endif
3884
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003885 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
3886 return -EIO;
3887
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003888 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02003889 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003890}
3891
Johannes Berg19d337d2009-06-02 13:01:37 +02003892static int bluetooth_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003893{
3894 int status;
3895
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003896 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02003897 "will attempt to %s bluetooth\n",
3898 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003899
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003900#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3901 if (dbg_bluetoothemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02003902 tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003903 return 0;
3904 }
3905#endif
3906
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003907 /* We make sure to keep TP_ACPI_BLUETOOTH_RESUMECTRL off */
Johannes Berg19d337d2009-06-02 13:01:37 +02003908 if (state == TPACPI_RFK_RADIO_ON)
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003909 status = TP_ACPI_BLUETOOTH_RADIOSSW;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003910 else
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003911 status = 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02003912
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003913 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
3914 return -EIO;
3915
3916 return 0;
3917}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003918
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003919/* sysfs bluetooth enable ---------------------------------------------- */
3920static ssize_t bluetooth_enable_show(struct device *dev,
3921 struct device_attribute *attr,
3922 char *buf)
3923{
Johannes Berg19d337d2009-06-02 13:01:37 +02003924 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID,
3925 attr, buf);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003926}
3927
3928static ssize_t bluetooth_enable_store(struct device *dev,
3929 struct device_attribute *attr,
3930 const char *buf, size_t count)
3931{
Johannes Berg19d337d2009-06-02 13:01:37 +02003932 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID,
3933 attr, buf, count);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003934}
3935
3936static struct device_attribute dev_attr_bluetooth_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03003937 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003938 bluetooth_enable_show, bluetooth_enable_store);
3939
3940/* --------------------------------------------------------------------- */
3941
3942static struct attribute *bluetooth_attributes[] = {
3943 &dev_attr_bluetooth_enable.attr,
3944 NULL
3945};
3946
3947static const struct attribute_group bluetooth_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003948 .attrs = bluetooth_attributes,
3949};
3950
Johannes Berg19d337d2009-06-02 13:01:37 +02003951static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = {
3952 .get_status = bluetooth_get_status,
3953 .set_status = bluetooth_set_status,
3954};
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003955
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003956static void bluetooth_shutdown(void)
3957{
3958 /* Order firmware to save current state to NVRAM */
3959 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3960 TP_ACPI_BLTH_SAVE_STATE))
3961 printk(TPACPI_NOTICE
3962 "failed to save bluetooth state to NVRAM\n");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003963 else
3964 vdbg_printk(TPACPI_DBG_RFKILL,
3965 "bluestooth state saved to NVRAM\n");
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003966}
3967
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003968static void bluetooth_exit(void)
3969{
3970 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3971 &bluetooth_attr_group);
Johannes Berg19d337d2009-06-02 13:01:37 +02003972
3973 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
3974
3975 bluetooth_shutdown();
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003976}
3977
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003978static int __init bluetooth_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003980 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003981 int status = 0;
3982
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003983 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3984 "initializing bluetooth subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003985
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003986 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003987
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003988 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3989 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003990 tp_features.bluetooth = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003991 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003993 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3994 "bluetooth is %s, status 0x%02x\n",
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003995 str_supported(tp_features.bluetooth),
3996 status);
3997
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003998#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3999 if (dbg_bluetoothemul) {
4000 tp_features.bluetooth = 1;
4001 printk(TPACPI_INFO
4002 "bluetooth switch emulation enabled\n");
4003 } else
4004#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004005 if (tp_features.bluetooth &&
4006 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
4007 /* no bluetooth hardware present in system */
4008 tp_features.bluetooth = 0;
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004009 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004010 "bluetooth hardware not installed\n");
4011 }
4012
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004013 if (!tp_features.bluetooth)
4014 return 1;
4015
Johannes Berg19d337d2009-06-02 13:01:37 +02004016 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
4017 &bluetooth_tprfk_ops,
4018 RFKILL_TYPE_BLUETOOTH,
4019 TPACPI_RFK_BLUETOOTH_SW_NAME,
4020 true);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004021 if (res)
4022 return res;
4023
Johannes Berg19d337d2009-06-02 13:01:37 +02004024 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4025 &bluetooth_attr_group);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004026 if (res) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004027 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004028 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004029 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004030
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004031 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032}
4033
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004034/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004035static int bluetooth_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036{
Johannes Berg19d337d2009-06-02 13:01:37 +02004037 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038}
4039
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004040static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041{
Johannes Berg19d337d2009-06-02 13:01:37 +02004042 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043}
4044
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004045static struct ibm_struct bluetooth_driver_data = {
4046 .name = "bluetooth",
4047 .read = bluetooth_read,
4048 .write = bluetooth_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004049 .exit = bluetooth_exit,
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02004050 .suspend = bluetooth_suspend,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004051 .shutdown = bluetooth_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004052};
4053
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004054/*************************************************************************
4055 * Wan subdriver
4056 */
4057
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004058enum {
4059 /* ACPI GWAN/SWAN bits */
4060 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
4061 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02004062 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
4063 off / last state */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004064};
4065
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004066#define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
4067
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02004068static void wan_suspend(pm_message_t state)
4069{
4070 /* Try to make sure radio will resume powered off */
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004071 if (!acpi_evalf(NULL, NULL, "\\WGSV", "qvd",
4072 TP_ACPI_WGSV_PWR_OFF_ON_RESUME))
4073 vdbg_printk(TPACPI_DBG_RFKILL,
4074 "WWAN power down on resume request failed\n");
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02004075}
4076
Johannes Berg19d337d2009-06-02 13:01:37 +02004077static int wan_get_status(void)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004078{
4079 int status;
4080
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004081#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4082 if (dbg_wwanemul)
4083 return (tpacpi_wwan_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004084 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004085#endif
4086
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004087 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
4088 return -EIO;
4089
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004090 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004091 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004092}
4093
Johannes Berg19d337d2009-06-02 13:01:37 +02004094static int wan_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004095{
4096 int status;
4097
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004098 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02004099 "will attempt to %s wwan\n",
4100 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004101
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004102#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4103 if (dbg_wwanemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004104 tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004105 return 0;
4106 }
4107#endif
4108
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02004109 /* We make sure to keep TP_ACPI_WANCARD_RESUMECTRL off */
Johannes Berg19d337d2009-06-02 13:01:37 +02004110 if (state == TPACPI_RFK_RADIO_ON)
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02004111 status = TP_ACPI_WANCARD_RADIOSSW;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004112 else
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02004113 status = 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02004114
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004115 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
4116 return -EIO;
4117
4118 return 0;
4119}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004120
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004121/* sysfs wan enable ---------------------------------------------------- */
4122static ssize_t wan_enable_show(struct device *dev,
4123 struct device_attribute *attr,
4124 char *buf)
4125{
Johannes Berg19d337d2009-06-02 13:01:37 +02004126 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
4127 attr, buf);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004128}
4129
4130static ssize_t wan_enable_store(struct device *dev,
4131 struct device_attribute *attr,
4132 const char *buf, size_t count)
4133{
Johannes Berg19d337d2009-06-02 13:01:37 +02004134 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
4135 attr, buf, count);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004136}
4137
4138static struct device_attribute dev_attr_wan_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03004139 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004140 wan_enable_show, wan_enable_store);
4141
4142/* --------------------------------------------------------------------- */
4143
4144static struct attribute *wan_attributes[] = {
4145 &dev_attr_wan_enable.attr,
4146 NULL
4147};
4148
4149static const struct attribute_group wan_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004150 .attrs = wan_attributes,
4151};
4152
Johannes Berg19d337d2009-06-02 13:01:37 +02004153static const struct tpacpi_rfk_ops wan_tprfk_ops = {
4154 .get_status = wan_get_status,
4155 .set_status = wan_set_status,
4156};
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004157
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004158static void wan_shutdown(void)
4159{
4160 /* Order firmware to save current state to NVRAM */
4161 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
4162 TP_ACPI_WGSV_SAVE_STATE))
4163 printk(TPACPI_NOTICE
4164 "failed to save WWAN state to NVRAM\n");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004165 else
4166 vdbg_printk(TPACPI_DBG_RFKILL,
4167 "WWAN state saved to NVRAM\n");
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004168}
4169
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004170static void wan_exit(void)
4171{
4172 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4173 &wan_attr_group);
Johannes Berg19d337d2009-06-02 13:01:37 +02004174
4175 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4176
4177 wan_shutdown();
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004178}
4179
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004180static int __init wan_init(struct ibm_init_struct *iibm)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004181{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004182 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004183 int status = 0;
4184
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004185 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4186 "initializing wan subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004187
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004188 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004189
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004190 tp_features.wan = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004191 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004192
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004193 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4194 "wan is %s, status 0x%02x\n",
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004195 str_supported(tp_features.wan),
4196 status);
4197
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004198#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4199 if (dbg_wwanemul) {
4200 tp_features.wan = 1;
4201 printk(TPACPI_INFO
4202 "wwan switch emulation enabled\n");
4203 } else
4204#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004205 if (tp_features.wan &&
4206 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
4207 /* no wan hardware present in system */
4208 tp_features.wan = 0;
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004209 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004210 "wan hardware not installed\n");
4211 }
4212
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004213 if (!tp_features.wan)
4214 return 1;
4215
Johannes Berg19d337d2009-06-02 13:01:37 +02004216 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
4217 &wan_tprfk_ops,
4218 RFKILL_TYPE_WWAN,
4219 TPACPI_RFK_WWAN_SW_NAME,
4220 true);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004221 if (res)
4222 return res;
4223
Johannes Berg19d337d2009-06-02 13:01:37 +02004224 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4225 &wan_attr_group);
4226
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004227 if (res) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004228 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004229 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004230 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004231
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004232 return 0;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004233}
4234
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004235/* procfs -------------------------------------------------------------- */
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004236static int wan_read(char *p)
4237{
Johannes Berg19d337d2009-06-02 13:01:37 +02004238 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, p);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004239}
4240
4241static int wan_write(char *buf)
4242{
Johannes Berg19d337d2009-06-02 13:01:37 +02004243 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004244}
4245
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004246static struct ibm_struct wan_driver_data = {
4247 .name = "wan",
4248 .read = wan_read,
4249 .write = wan_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004250 .exit = wan_exit,
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02004251 .suspend = wan_suspend,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004252 .shutdown = wan_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004253};
4254
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004255/*************************************************************************
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004256 * UWB subdriver
4257 */
4258
4259enum {
4260 /* ACPI GUWB/SUWB bits */
4261 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4262 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4263};
4264
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004265#define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4266
Johannes Berg19d337d2009-06-02 13:01:37 +02004267static int uwb_get_status(void)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004268{
4269 int status;
4270
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004271#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4272 if (dbg_uwbemul)
4273 return (tpacpi_uwb_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004274 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004275#endif
4276
4277 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4278 return -EIO;
4279
4280 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004281 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004282}
4283
Johannes Berg19d337d2009-06-02 13:01:37 +02004284static int uwb_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004285{
4286 int status;
4287
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004288 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02004289 "will attempt to %s UWB\n",
4290 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004291
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004292#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4293 if (dbg_uwbemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004294 tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004295 return 0;
4296 }
4297#endif
4298
Johannes Berg19d337d2009-06-02 13:01:37 +02004299 if (state == TPACPI_RFK_RADIO_ON)
4300 status = TP_ACPI_UWB_RADIOSSW;
4301 else
4302 status = 0;
4303
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004304 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4305 return -EIO;
4306
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004307 return 0;
4308}
4309
4310/* --------------------------------------------------------------------- */
4311
Johannes Berg19d337d2009-06-02 13:01:37 +02004312static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
4313 .get_status = uwb_get_status,
4314 .set_status = uwb_set_status,
4315};
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004316
4317static void uwb_exit(void)
4318{
Johannes Berg19d337d2009-06-02 13:01:37 +02004319 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004320}
4321
4322static int __init uwb_init(struct ibm_init_struct *iibm)
4323{
4324 int res;
4325 int status = 0;
4326
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004327 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4328 "initializing uwb subdriver\n");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004329
4330 TPACPI_ACPIHANDLE_INIT(hkey);
4331
4332 tp_features.uwb = hkey_handle &&
4333 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4334
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004335 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4336 "uwb is %s, status 0x%02x\n",
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004337 str_supported(tp_features.uwb),
4338 status);
4339
4340#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4341 if (dbg_uwbemul) {
4342 tp_features.uwb = 1;
4343 printk(TPACPI_INFO
4344 "uwb switch emulation enabled\n");
4345 } else
4346#endif
4347 if (tp_features.uwb &&
4348 !(status & TP_ACPI_UWB_HWPRESENT)) {
4349 /* no uwb hardware present in system */
4350 tp_features.uwb = 0;
4351 dbg_printk(TPACPI_DBG_INIT,
4352 "uwb hardware not installed\n");
4353 }
4354
4355 if (!tp_features.uwb)
4356 return 1;
4357
4358 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
Johannes Berg19d337d2009-06-02 13:01:37 +02004359 &uwb_tprfk_ops,
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004360 RFKILL_TYPE_UWB,
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004361 TPACPI_RFK_UWB_SW_NAME,
Johannes Berg19d337d2009-06-02 13:01:37 +02004362 false);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004363 return res;
4364}
4365
4366static struct ibm_struct uwb_driver_data = {
4367 .name = "uwb",
4368 .exit = uwb_exit,
4369 .flags.experimental = 1,
4370};
4371
4372/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004373 * Video subdriver
4374 */
4375
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02004376#ifdef CONFIG_THINKPAD_ACPI_VIDEO
4377
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004378enum video_access_mode {
4379 TPACPI_VIDEO_NONE = 0,
4380 TPACPI_VIDEO_570, /* 570 */
4381 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4382 TPACPI_VIDEO_NEW, /* all others */
4383};
4384
4385enum { /* video status flags, based on VIDEO_570 */
4386 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4387 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4388 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4389};
4390
4391enum { /* TPACPI_VIDEO_570 constants */
4392 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4393 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4394 * video_status_flags */
4395 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4396 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4397};
4398
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02004399static enum video_access_mode video_supported;
4400static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004401
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004402static int video_autosw_get(void);
4403static int video_autosw_set(int enable);
4404
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004405TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004406
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004407static int __init video_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004409 int ivga;
4410
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004411 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4412
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004413 TPACPI_ACPIHANDLE_INIT(vid);
4414 TPACPI_ACPIHANDLE_INIT(vid2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004415
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004416 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4417 /* G41, assume IVGA doesn't change */
4418 vid_handle = vid2_handle;
4419
4420 if (!vid_handle)
4421 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004422 video_supported = TPACPI_VIDEO_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004423 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
4424 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004425 video_supported = TPACPI_VIDEO_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004426 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
4427 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004428 video_supported = TPACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004429 else
4430 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004431 video_supported = TPACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004433 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
4434 str_supported(video_supported != TPACPI_VIDEO_NONE),
4435 video_supported);
4436
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004437 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438}
4439
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004440static void video_exit(void)
4441{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004442 dbg_printk(TPACPI_DBG_EXIT,
4443 "restoring original video autoswitch mode\n");
4444 if (video_autosw_set(video_orig_autosw))
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004445 printk(TPACPI_ERR "error while trying to restore original "
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004446 "video autoswitch mode\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004447}
4448
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004449static int video_outputsw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450{
4451 int status = 0;
4452 int i;
4453
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004454 switch (video_supported) {
4455 case TPACPI_VIDEO_570:
4456 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
4457 TP_ACPI_VIDEO_570_PHSCMD))
4458 return -EIO;
4459 status = i & TP_ACPI_VIDEO_570_PHSMASK;
4460 break;
4461 case TPACPI_VIDEO_770:
4462 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4463 return -EIO;
4464 if (i)
4465 status |= TP_ACPI_VIDEO_S_LCD;
4466 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4467 return -EIO;
4468 if (i)
4469 status |= TP_ACPI_VIDEO_S_CRT;
4470 break;
4471 case TPACPI_VIDEO_NEW:
4472 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4473 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4474 return -EIO;
4475 if (i)
4476 status |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004478 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4479 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4480 return -EIO;
4481 if (i)
4482 status |= TP_ACPI_VIDEO_S_LCD;
4483 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4484 return -EIO;
4485 if (i)
4486 status |= TP_ACPI_VIDEO_S_DVI;
4487 break;
4488 default:
4489 return -ENOSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004490 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491
4492 return status;
4493}
4494
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004495static int video_outputsw_set(int status)
4496{
4497 int autosw;
4498 int res = 0;
4499
4500 switch (video_supported) {
4501 case TPACPI_VIDEO_570:
4502 res = acpi_evalf(NULL, NULL,
4503 "\\_SB.PHS2", "vdd",
4504 TP_ACPI_VIDEO_570_PHS2CMD,
4505 status | TP_ACPI_VIDEO_570_PHS2SET);
4506 break;
4507 case TPACPI_VIDEO_770:
4508 autosw = video_autosw_get();
4509 if (autosw < 0)
4510 return autosw;
4511
4512 res = video_autosw_set(1);
4513 if (res)
4514 return res;
4515 res = acpi_evalf(vid_handle, NULL,
4516 "ASWT", "vdd", status * 0x100, 0);
4517 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004518 printk(TPACPI_ERR
4519 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004520 return -EIO;
4521 }
4522 break;
4523 case TPACPI_VIDEO_NEW:
4524 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004525 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004526 break;
4527 default:
4528 return -ENOSYS;
4529 }
4530
4531 return (res)? 0 : -EIO;
4532}
4533
4534static int video_autosw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004536 int autosw = 0;
4537
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004538 switch (video_supported) {
4539 case TPACPI_VIDEO_570:
4540 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
4541 return -EIO;
4542 break;
4543 case TPACPI_VIDEO_770:
4544 case TPACPI_VIDEO_NEW:
4545 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4546 return -EIO;
4547 break;
4548 default:
4549 return -ENOSYS;
4550 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004551
4552 return autosw & 1;
4553}
4554
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004555static int video_autosw_set(int enable)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004556{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004557 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004558 return -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004559 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004560}
4561
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004562static int video_outputsw_cycle(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004563{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004564 int autosw = video_autosw_get();
4565 int res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004566
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004567 if (autosw < 0)
4568 return autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004569
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004570 switch (video_supported) {
4571 case TPACPI_VIDEO_570:
4572 res = video_autosw_set(1);
4573 if (res)
4574 return res;
4575 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4576 break;
4577 case TPACPI_VIDEO_770:
4578 case TPACPI_VIDEO_NEW:
4579 res = video_autosw_set(1);
4580 if (res)
4581 return res;
4582 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4583 break;
4584 default:
4585 return -ENOSYS;
4586 }
4587 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004588 printk(TPACPI_ERR
4589 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004590 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004591 }
4592
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004593 return (res)? 0 : -EIO;
4594}
4595
4596static int video_expand_toggle(void)
4597{
4598 switch (video_supported) {
4599 case TPACPI_VIDEO_570:
4600 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
4601 0 : -EIO;
4602 case TPACPI_VIDEO_770:
4603 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
4604 0 : -EIO;
4605 case TPACPI_VIDEO_NEW:
4606 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
4607 0 : -EIO;
4608 default:
4609 return -ENOSYS;
4610 }
4611 /* not reached */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004612}
4613
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004614static int video_read(char *p)
4615{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004616 int status, autosw;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004617 int len = 0;
4618
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004619 if (video_supported == TPACPI_VIDEO_NONE) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004620 len += sprintf(p + len, "status:\t\tnot supported\n");
4621 return len;
4622 }
4623
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004624 status = video_outputsw_get();
4625 if (status < 0)
4626 return status;
4627
4628 autosw = video_autosw_get();
4629 if (autosw < 0)
4630 return autosw;
4631
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004632 len += sprintf(p + len, "status:\t\tsupported\n");
4633 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
4634 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004635 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004636 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
4637 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
4638 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
4639 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004640 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004641 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
4642 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
4643 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
4644
4645 return len;
4646}
4647
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004648static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649{
4650 char *cmd;
4651 int enable, disable, status;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004652 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004654 if (video_supported == TPACPI_VIDEO_NONE)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004655 return -ENODEV;
4656
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004657 enable = 0;
4658 disable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004659
4660 while ((cmd = next_cmd(&buf))) {
4661 if (strlencmp(cmd, "lcd_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004662 enable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663 } else if (strlencmp(cmd, "lcd_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004664 disable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665 } else if (strlencmp(cmd, "crt_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004666 enable |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667 } else if (strlencmp(cmd, "crt_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004668 disable |= TP_ACPI_VIDEO_S_CRT;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004669 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004670 strlencmp(cmd, "dvi_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004671 enable |= TP_ACPI_VIDEO_S_DVI;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004672 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004673 strlencmp(cmd, "dvi_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004674 disable |= TP_ACPI_VIDEO_S_DVI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004675 } else if (strlencmp(cmd, "auto_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004676 res = video_autosw_set(1);
4677 if (res)
4678 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679 } else if (strlencmp(cmd, "auto_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004680 res = video_autosw_set(0);
4681 if (res)
4682 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683 } else if (strlencmp(cmd, "video_switch") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004684 res = video_outputsw_cycle();
4685 if (res)
4686 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004688 res = video_expand_toggle();
4689 if (res)
4690 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004691 } else
4692 return -EINVAL;
4693 }
4694
4695 if (enable || disable) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004696 status = video_outputsw_get();
4697 if (status < 0)
4698 return status;
4699 res = video_outputsw_set((status & ~disable) | enable);
4700 if (res)
4701 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004702 }
4703
4704 return 0;
4705}
4706
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004707static struct ibm_struct video_driver_data = {
4708 .name = "video",
4709 .read = video_read,
4710 .write = video_write,
4711 .exit = video_exit,
4712};
4713
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02004714#endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4715
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004716/*************************************************************************
4717 * Light (thinklight) subdriver
4718 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004720TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4721TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004722
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004723static int light_get_status(void)
4724{
4725 int status = 0;
4726
4727 if (tp_features.light_status) {
4728 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4729 return -EIO;
4730 return (!!status);
4731 }
4732
4733 return -ENXIO;
4734}
4735
4736static int light_set_status(int status)
4737{
4738 int rc;
4739
4740 if (tp_features.light) {
4741 if (cmos_handle) {
4742 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4743 (status)?
4744 TP_CMOS_THINKLIGHT_ON :
4745 TP_CMOS_THINKLIGHT_OFF);
4746 } else {
4747 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4748 (status)? 1 : 0);
4749 }
4750 return (rc)? 0 : -EIO;
4751 }
4752
4753 return -ENXIO;
4754}
4755
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004756static void light_set_status_worker(struct work_struct *work)
4757{
4758 struct tpacpi_led_classdev *data =
4759 container_of(work, struct tpacpi_led_classdev, work);
4760
4761 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00004762 light_set_status((data->new_state != TPACPI_LED_OFF));
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004763}
4764
4765static void light_sysfs_set(struct led_classdev *led_cdev,
4766 enum led_brightness brightness)
4767{
4768 struct tpacpi_led_classdev *data =
4769 container_of(led_cdev,
4770 struct tpacpi_led_classdev,
4771 led_classdev);
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00004772 data->new_state = (brightness != LED_OFF) ?
4773 TPACPI_LED_ON : TPACPI_LED_OFF;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004774 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004775}
4776
4777static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4778{
4779 return (light_get_status() == 1)? LED_FULL : LED_OFF;
4780}
4781
4782static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4783 .led_classdev = {
4784 .name = "tpacpi::thinklight",
4785 .brightness_set = &light_sysfs_set,
4786 .brightness_get = &light_sysfs_get,
4787 }
4788};
4789
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004790static int __init light_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791{
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004792 int rc;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004793
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004794 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4795
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004796 TPACPI_ACPIHANDLE_INIT(ledb);
4797 TPACPI_ACPIHANDLE_INIT(lght);
4798 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004799 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004800
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004801 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004802 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004803
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004804 if (tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004805 /* light status not supported on
4806 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004807 tp_features.light_status =
4808 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004810 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4811 str_supported(tp_features.light),
4812 str_supported(tp_features.light_status));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004813
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004814 if (!tp_features.light)
4815 return 1;
4816
4817 rc = led_classdev_register(&tpacpi_pdev->dev,
4818 &tpacpi_led_thinklight.led_classdev);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004819
4820 if (rc < 0) {
4821 tp_features.light = 0;
4822 tp_features.light_status = 0;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004823 } else {
4824 rc = 0;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004825 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004826
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004827 return rc;
4828}
4829
4830static void light_exit(void)
4831{
4832 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
4833 if (work_pending(&tpacpi_led_thinklight.work))
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004834 flush_workqueue(tpacpi_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004835}
4836
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004837static int light_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004838{
4839 int len = 0;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004840 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004842 if (!tp_features.light) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004843 len += sprintf(p + len, "status:\t\tnot supported\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004844 } else if (!tp_features.light_status) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004845 len += sprintf(p + len, "status:\t\tunknown\n");
4846 len += sprintf(p + len, "commands:\ton, off\n");
4847 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004848 status = light_get_status();
4849 if (status < 0)
4850 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004851 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004852 len += sprintf(p + len, "commands:\ton, off\n");
4853 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854
4855 return len;
4856}
4857
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004858static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004859{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004861 int newstatus = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004862
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004863 if (!tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004864 return -ENODEV;
4865
Linus Torvalds1da177e2005-04-16 15:20:36 -07004866 while ((cmd = next_cmd(&buf))) {
4867 if (strlencmp(cmd, "on") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004868 newstatus = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004869 } else if (strlencmp(cmd, "off") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004870 newstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871 } else
4872 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873 }
4874
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004875 return light_set_status(newstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876}
4877
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004878static struct ibm_struct light_driver_data = {
4879 .name = "light",
4880 .read = light_read,
4881 .write = light_write,
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004882 .exit = light_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004883};
4884
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004885/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004886 * CMOS subdriver
4887 */
4888
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004889/* sysfs cmos_command -------------------------------------------------- */
4890static ssize_t cmos_command_store(struct device *dev,
4891 struct device_attribute *attr,
4892 const char *buf, size_t count)
4893{
4894 unsigned long cmos_cmd;
4895 int res;
4896
4897 if (parse_strtoul(buf, 21, &cmos_cmd))
4898 return -EINVAL;
4899
4900 res = issue_thinkpad_cmos_command(cmos_cmd);
4901 return (res)? res : count;
4902}
4903
4904static struct device_attribute dev_attr_cmos_command =
4905 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4906
4907/* --------------------------------------------------------------------- */
4908
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004909static int __init cmos_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004910{
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004911 int res;
4912
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004913 vdbg_printk(TPACPI_DBG_INIT,
4914 "initializing cmos commands subdriver\n");
4915
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004916 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004917
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004918 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4919 str_supported(cmos_handle != NULL));
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004920
4921 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4922 if (res)
4923 return res;
4924
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004925 return (cmos_handle)? 0 : 1;
4926}
4927
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004928static void cmos_exit(void)
4929{
4930 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4931}
4932
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004933static int cmos_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004934{
4935 int len = 0;
4936
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004937 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4938 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004939 if (!cmos_handle)
4940 len += sprintf(p + len, "status:\t\tnot supported\n");
4941 else {
4942 len += sprintf(p + len, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004943 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944 }
4945
4946 return len;
4947}
4948
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004949static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004950{
4951 char *cmd;
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004952 int cmos_cmd, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004953
4954 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004955 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
4956 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004957 /* cmos_cmd set */
4958 } else
4959 return -EINVAL;
4960
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004961 res = issue_thinkpad_cmos_command(cmos_cmd);
4962 if (res)
4963 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004964 }
4965
4966 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004967}
4968
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004969static struct ibm_struct cmos_driver_data = {
4970 .name = "cmos",
4971 .read = cmos_read,
4972 .write = cmos_write,
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004973 .exit = cmos_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004974};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004975
4976/*************************************************************************
4977 * LED subdriver
4978 */
4979
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004980enum led_access_mode {
4981 TPACPI_LED_NONE = 0,
4982 TPACPI_LED_570, /* 570 */
4983 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4984 TPACPI_LED_NEW, /* all others */
4985};
4986
4987enum { /* For TPACPI_LED_OLD */
4988 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
4989 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
4990 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
4991};
4992
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02004993static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004994
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004995TPACPI_HANDLE(led, ec, "SLED", /* 570 */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004996 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
4997 /* T20-22, X20-21 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004998 "LED", /* all others */
4999 ); /* R30, R31 */
5000
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005001#define TPACPI_LED_NUMLEDS 16
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005002static struct tpacpi_led_classdev *tpacpi_leds;
5003static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
Harvey Harrisone3aa51f2008-05-29 17:51:57 -07005004static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005005 /* there's a limit of 19 chars + NULL before 2.6.26 */
5006 "tpacpi::power",
5007 "tpacpi:orange:batt",
5008 "tpacpi:green:batt",
5009 "tpacpi::dock_active",
5010 "tpacpi::bay_active",
5011 "tpacpi::dock_batt",
5012 "tpacpi::unknown_led",
5013 "tpacpi::standby",
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005014 "tpacpi::dock_status1",
5015 "tpacpi::dock_status2",
5016 "tpacpi::unknown_led2",
5017 "tpacpi::unknown_led3",
5018 "tpacpi::thinkvantage",
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005019};
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005020#define TPACPI_SAFE_LEDS 0x1081U
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005021
5022static inline bool tpacpi_is_led_restricted(const unsigned int led)
5023{
5024#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5025 return false;
5026#else
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005027 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005028#endif
5029}
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005030
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005031static int led_get_status(const unsigned int led)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005032{
5033 int status;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005034 enum led_status_t led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005035
5036 switch (led_supported) {
5037 case TPACPI_LED_570:
5038 if (!acpi_evalf(ec_handle,
5039 &status, "GLED", "dd", 1 << led))
5040 return -EIO;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005041 led_s = (status == 0)?
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005042 TPACPI_LED_OFF :
5043 ((status == 1)?
5044 TPACPI_LED_ON :
5045 TPACPI_LED_BLINK);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005046 tpacpi_led_state_cache[led] = led_s;
5047 return led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005048 default:
5049 return -ENXIO;
5050 }
5051
5052 /* not reached */
5053}
5054
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005055static int led_set_status(const unsigned int led,
5056 const enum led_status_t ledstatus)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005057{
5058 /* off, on, blink. Index is led_status_t */
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005059 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
5060 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005061
5062 int rc = 0;
5063
5064 switch (led_supported) {
5065 case TPACPI_LED_570:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005066 /* 570 */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005067 if (unlikely(led > 7))
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005068 return -EINVAL;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005069 if (unlikely(tpacpi_is_led_restricted(led)))
5070 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005071 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5072 (1 << led), led_sled_arg1[ledstatus]))
5073 rc = -EIO;
5074 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005075 case TPACPI_LED_OLD:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005076 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005077 if (unlikely(led > 7))
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005078 return -EINVAL;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005079 if (unlikely(tpacpi_is_led_restricted(led)))
5080 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005081 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
5082 if (rc >= 0)
5083 rc = ec_write(TPACPI_LED_EC_HLBL,
5084 (ledstatus == TPACPI_LED_BLINK) << led);
5085 if (rc >= 0)
5086 rc = ec_write(TPACPI_LED_EC_HLCL,
5087 (ledstatus != TPACPI_LED_OFF) << led);
5088 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005089 case TPACPI_LED_NEW:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005090 /* all others */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005091 if (unlikely(led >= TPACPI_LED_NUMLEDS))
5092 return -EINVAL;
5093 if (unlikely(tpacpi_is_led_restricted(led)))
5094 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005095 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5096 led, led_led_arg1[ledstatus]))
5097 rc = -EIO;
5098 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005099 default:
5100 rc = -ENXIO;
5101 }
5102
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005103 if (!rc)
5104 tpacpi_led_state_cache[led] = ledstatus;
5105
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005106 return rc;
5107}
5108
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005109static void led_set_status_worker(struct work_struct *work)
5110{
5111 struct tpacpi_led_classdev *data =
5112 container_of(work, struct tpacpi_led_classdev, work);
5113
5114 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005115 led_set_status(data->led, data->new_state);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005116}
5117
5118static void led_sysfs_set(struct led_classdev *led_cdev,
5119 enum led_brightness brightness)
5120{
5121 struct tpacpi_led_classdev *data = container_of(led_cdev,
5122 struct tpacpi_led_classdev, led_classdev);
5123
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005124 if (brightness == LED_OFF)
5125 data->new_state = TPACPI_LED_OFF;
5126 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
5127 data->new_state = TPACPI_LED_ON;
5128 else
5129 data->new_state = TPACPI_LED_BLINK;
5130
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03005131 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005132}
5133
5134static int led_sysfs_blink_set(struct led_classdev *led_cdev,
5135 unsigned long *delay_on, unsigned long *delay_off)
5136{
5137 struct tpacpi_led_classdev *data = container_of(led_cdev,
5138 struct tpacpi_led_classdev, led_classdev);
5139
5140 /* Can we choose the flash rate? */
5141 if (*delay_on == 0 && *delay_off == 0) {
5142 /* yes. set them to the hardware blink rate (1 Hz) */
5143 *delay_on = 500; /* ms */
5144 *delay_off = 500; /* ms */
5145 } else if ((*delay_on != 500) || (*delay_off != 500))
5146 return -EINVAL;
5147
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005148 data->new_state = TPACPI_LED_BLINK;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03005149 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005150
5151 return 0;
5152}
5153
5154static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
5155{
5156 int rc;
5157
5158 struct tpacpi_led_classdev *data = container_of(led_cdev,
5159 struct tpacpi_led_classdev, led_classdev);
5160
5161 rc = led_get_status(data->led);
5162
5163 if (rc == TPACPI_LED_OFF || rc < 0)
5164 rc = LED_OFF; /* no error handling in led class :( */
5165 else
5166 rc = LED_FULL;
5167
5168 return rc;
5169}
5170
5171static void led_exit(void)
5172{
5173 unsigned int i;
5174
5175 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5176 if (tpacpi_leds[i].led_classdev.name)
5177 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
5178 }
5179
5180 kfree(tpacpi_leds);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005181}
5182
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005183static int __init tpacpi_init_led(unsigned int led)
5184{
5185 int rc;
5186
5187 tpacpi_leds[led].led = led;
5188
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005189 /* LEDs with no name don't get registered */
5190 if (!tpacpi_led_names[led])
5191 return 0;
5192
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005193 tpacpi_leds[led].led_classdev.brightness_set = &led_sysfs_set;
5194 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5195 if (led_supported == TPACPI_LED_570)
5196 tpacpi_leds[led].led_classdev.brightness_get =
5197 &led_sysfs_get;
5198
5199 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5200
5201 INIT_WORK(&tpacpi_leds[led].work, led_set_status_worker);
5202
5203 rc = led_classdev_register(&tpacpi_pdev->dev,
5204 &tpacpi_leds[led].led_classdev);
5205 if (rc < 0)
5206 tpacpi_leds[led].led_classdev.name = NULL;
5207
5208 return rc;
5209}
5210
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005211static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
5212 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5213 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5214 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5215
5216 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5217 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5218 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5219 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5220 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5221 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5222 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5223 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5224
5225 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5226 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5227 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5228 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5229 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5230
5231 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5232 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5233 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5234 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5235
5236 /* (1) - may have excess leds enabled on MSB */
5237
5238 /* Defaults (order matters, keep last, don't reorder!) */
5239 { /* Lenovo */
5240 .vendor = PCI_VENDOR_ID_LENOVO,
5241 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5242 .quirks = 0x1fffU,
5243 },
5244 { /* IBM ThinkPads with no EC version string */
5245 .vendor = PCI_VENDOR_ID_IBM,
5246 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
5247 .quirks = 0x00ffU,
5248 },
5249 { /* IBM ThinkPads with EC version string */
5250 .vendor = PCI_VENDOR_ID_IBM,
5251 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5252 .quirks = 0x00bfU,
5253 },
5254};
5255
5256#undef TPACPI_LEDQ_IBM
5257#undef TPACPI_LEDQ_LNV
5258
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005259static int __init led_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005260{
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005261 unsigned int i;
5262 int rc;
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005263 unsigned long useful_leds;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005264
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005265 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
5266
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005267 TPACPI_ACPIHANDLE_INIT(led);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005268
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005269 if (!led_handle)
5270 /* led not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005271 led_supported = TPACPI_LED_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005272 else if (strlencmp(led_path, "SLED") == 0)
5273 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005274 led_supported = TPACPI_LED_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005275 else if (strlencmp(led_path, "SYSL") == 0)
5276 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005277 led_supported = TPACPI_LED_OLD;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005278 else
5279 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005280 led_supported = TPACPI_LED_NEW;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005281
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005282 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
5283 str_supported(led_supported), led_supported);
5284
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005285 if (led_supported == TPACPI_LED_NONE)
5286 return 1;
5287
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005288 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
5289 GFP_KERNEL);
5290 if (!tpacpi_leds) {
5291 printk(TPACPI_ERR "Out of memory for LED data\n");
5292 return -ENOMEM;
5293 }
5294
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005295 useful_leds = tpacpi_check_quirks(led_useful_qtable,
5296 ARRAY_SIZE(led_useful_qtable));
5297
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005298 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005299 if (!tpacpi_is_led_restricted(i) &&
5300 test_bit(i, &useful_leds)) {
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005301 rc = tpacpi_init_led(i);
5302 if (rc < 0) {
5303 led_exit();
5304 return rc;
5305 }
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005306 }
5307 }
5308
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005309#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005310 printk(TPACPI_NOTICE
5311 "warning: userspace override of important "
5312 "firmware LEDs is enabled\n");
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005313#endif
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005314 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005315}
5316
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005317#define str_led_status(s) \
5318 ((s) == TPACPI_LED_OFF ? "off" : \
5319 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005320
5321static int led_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005322{
5323 int len = 0;
5324
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005325 if (!led_supported) {
5326 len += sprintf(p + len, "status:\t\tnot supported\n");
5327 return len;
5328 }
5329 len += sprintf(p + len, "status:\t\tsupported\n");
5330
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005331 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005332 /* 570 */
5333 int i, status;
5334 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005335 status = led_get_status(i);
5336 if (status < 0)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005337 return -EIO;
5338 len += sprintf(p + len, "%d:\t\t%s\n",
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005339 i, str_led_status(status));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005340 }
5341 }
5342
Linus Torvalds1da177e2005-04-16 15:20:36 -07005343 len += sprintf(p + len, "commands:\t"
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005344 "<led> on, <led> off, <led> blink (<led> is 0-15)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005345
5346 return len;
5347}
5348
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005349static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005350{
5351 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005352 int led, rc;
5353 enum led_status_t s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005354
5355 if (!led_supported)
5356 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005357
5358 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005359 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 15)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005360 return -EINVAL;
5361
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005362 if (strstr(cmd, "off")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005363 s = TPACPI_LED_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005364 } else if (strstr(cmd, "on")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005365 s = TPACPI_LED_ON;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005366 } else if (strstr(cmd, "blink")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005367 s = TPACPI_LED_BLINK;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005368 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005369 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005370 }
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005371
5372 rc = led_set_status(led, s);
5373 if (rc < 0)
5374 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005375 }
5376
5377 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005378}
5379
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005380static struct ibm_struct led_driver_data = {
5381 .name = "led",
5382 .read = led_read,
5383 .write = led_write,
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005384 .exit = led_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005385};
5386
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005387/*************************************************************************
5388 * Beep subdriver
5389 */
5390
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005391TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005392
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005393#define TPACPI_BEEP_Q1 0x0001
5394
5395static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
5396 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
5397 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
5398};
5399
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005400static int __init beep_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005401{
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005402 unsigned long quirks;
5403
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005404 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
5405
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005406 TPACPI_ACPIHANDLE_INIT(beep);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005407
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005408 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
5409 str_supported(beep_handle != NULL));
5410
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005411 quirks = tpacpi_check_quirks(beep_quirk_table,
5412 ARRAY_SIZE(beep_quirk_table));
5413
5414 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
5415
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005416 return (beep_handle)? 0 : 1;
5417}
5418
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005419static int beep_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005420{
5421 int len = 0;
5422
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005423 if (!beep_handle)
5424 len += sprintf(p + len, "status:\t\tnot supported\n");
5425 else {
5426 len += sprintf(p + len, "status:\t\tsupported\n");
5427 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
5428 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005429
5430 return len;
5431}
5432
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005433static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005434{
5435 char *cmd;
5436 int beep_cmd;
5437
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005438 if (!beep_handle)
5439 return -ENODEV;
5440
Linus Torvalds1da177e2005-04-16 15:20:36 -07005441 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005442 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
5443 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005444 /* beep_cmd set */
5445 } else
5446 return -EINVAL;
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005447 if (tp_features.beep_needs_two_args) {
5448 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
5449 beep_cmd, 0))
5450 return -EIO;
5451 } else {
5452 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
5453 beep_cmd))
5454 return -EIO;
5455 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005456 }
5457
5458 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005459}
5460
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005461static struct ibm_struct beep_driver_data = {
5462 .name = "beep",
5463 .read = beep_read,
5464 .write = beep_write,
5465};
5466
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005467/*************************************************************************
5468 * Thermal subdriver
5469 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005470
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005471enum thermal_access_mode {
5472 TPACPI_THERMAL_NONE = 0, /* No thermal support */
5473 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
5474 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
5475 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
5476 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
5477};
5478
5479enum { /* TPACPI_THERMAL_TPEC_* */
5480 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
5481 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
5482 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
5483};
5484
5485#define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5486struct ibm_thermal_sensors_struct {
5487 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
5488};
5489
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005490static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005491
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005492/* idx is zero-based */
5493static int thermal_get_sensor(int idx, s32 *value)
5494{
5495 int t;
5496 s8 tmp;
5497 char tmpi[5];
5498
5499 t = TP_EC_THERMAL_TMP0;
5500
5501 switch (thermal_read_mode) {
5502#if TPACPI_MAX_THERMAL_SENSORS >= 16
5503 case TPACPI_THERMAL_TPEC_16:
5504 if (idx >= 8 && idx <= 15) {
5505 t = TP_EC_THERMAL_TMP8;
5506 idx -= 8;
5507 }
5508 /* fallthrough */
5509#endif
5510 case TPACPI_THERMAL_TPEC_8:
5511 if (idx <= 7) {
5512 if (!acpi_ec_read(t + idx, &tmp))
5513 return -EIO;
5514 *value = tmp * 1000;
5515 return 0;
5516 }
5517 break;
5518
5519 case TPACPI_THERMAL_ACPI_UPDT:
5520 if (idx <= 7) {
5521 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5522 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
5523 return -EIO;
5524 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5525 return -EIO;
5526 *value = (t - 2732) * 100;
5527 return 0;
5528 }
5529 break;
5530
5531 case TPACPI_THERMAL_ACPI_TMP07:
5532 if (idx <= 7) {
5533 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5534 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5535 return -EIO;
5536 if (t > 127 || t < -127)
5537 t = TP_EC_THERMAL_TMP_NA;
5538 *value = t * 1000;
5539 return 0;
5540 }
5541 break;
5542
5543 case TPACPI_THERMAL_NONE:
5544 default:
5545 return -ENOSYS;
5546 }
5547
5548 return -EINVAL;
5549}
5550
5551static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5552{
5553 int res, i;
5554 int n;
5555
5556 n = 8;
5557 i = 0;
5558
5559 if (!s)
5560 return -EINVAL;
5561
5562 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5563 n = 16;
5564
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005565 for (i = 0 ; i < n; i++) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005566 res = thermal_get_sensor(i, &s->temp[i]);
5567 if (res)
5568 return res;
5569 }
5570
5571 return n;
5572}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005573
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005574/* sysfs temp##_input -------------------------------------------------- */
5575
5576static ssize_t thermal_temp_input_show(struct device *dev,
5577 struct device_attribute *attr,
5578 char *buf)
5579{
5580 struct sensor_device_attribute *sensor_attr =
5581 to_sensor_dev_attr(attr);
5582 int idx = sensor_attr->index;
5583 s32 value;
5584 int res;
5585
5586 res = thermal_get_sensor(idx, &value);
5587 if (res)
5588 return res;
5589 if (value == TP_EC_THERMAL_TMP_NA * 1000)
5590 return -ENXIO;
5591
5592 return snprintf(buf, PAGE_SIZE, "%d\n", value);
5593}
5594
5595#define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005596 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5597 thermal_temp_input_show, NULL, _idxB)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005598
5599static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5600 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5601 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5602 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5603 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5604 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5605 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5606 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5607 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5608 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5609 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5610 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5611 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5612 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5613 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5614 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5615 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5616};
5617
5618#define THERMAL_ATTRS(X) \
5619 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5620
5621static struct attribute *thermal_temp_input_attr[] = {
5622 THERMAL_ATTRS(8),
5623 THERMAL_ATTRS(9),
5624 THERMAL_ATTRS(10),
5625 THERMAL_ATTRS(11),
5626 THERMAL_ATTRS(12),
5627 THERMAL_ATTRS(13),
5628 THERMAL_ATTRS(14),
5629 THERMAL_ATTRS(15),
5630 THERMAL_ATTRS(0),
5631 THERMAL_ATTRS(1),
5632 THERMAL_ATTRS(2),
5633 THERMAL_ATTRS(3),
5634 THERMAL_ATTRS(4),
5635 THERMAL_ATTRS(5),
5636 THERMAL_ATTRS(6),
5637 THERMAL_ATTRS(7),
5638 NULL
5639};
5640
5641static const struct attribute_group thermal_temp_input16_group = {
5642 .attrs = thermal_temp_input_attr
5643};
5644
5645static const struct attribute_group thermal_temp_input8_group = {
5646 .attrs = &thermal_temp_input_attr[8]
5647};
5648
5649#undef THERMAL_SENSOR_ATTR_TEMP
5650#undef THERMAL_ATTRS
5651
5652/* --------------------------------------------------------------------- */
5653
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005654static int __init thermal_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005655{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005656 u8 t, ta1, ta2;
5657 int i;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005658 int acpi_tmp7;
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005659 int res;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005660
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005661 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5662
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005663 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005664
Henrique de Moraes Holschuh3d6f99c2007-07-18 23:45:46 -03005665 if (thinkpad_id.ec_model) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005666 /*
5667 * Direct EC access mode: sensors at registers
5668 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5669 * non-implemented, thermal sensors return 0x80 when
5670 * not available
5671 */
5672
5673 ta1 = ta2 = 0;
5674 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03005675 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005676 ta1 |= t;
5677 } else {
5678 ta1 = 0;
5679 break;
5680 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03005681 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005682 ta2 |= t;
5683 } else {
5684 ta1 = 0;
5685 break;
5686 }
5687 }
5688 if (ta1 == 0) {
5689 /* This is sheer paranoia, but we handle it anyway */
5690 if (acpi_tmp7) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005691 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005692 "ThinkPad ACPI EC access misbehaving, "
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005693 "falling back to ACPI TMPx access "
5694 "mode\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005695 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005696 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005697 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005698 "ThinkPad ACPI EC access misbehaving, "
5699 "disabling thermal sensors access\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005700 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005701 }
5702 } else {
5703 thermal_read_mode =
5704 (ta2 != 0) ?
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005705 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005706 }
5707 } else if (acpi_tmp7) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005708 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
5709 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005710 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005711 } else {
5712 /* Standard ACPI TMPx access, max 8 sensors */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005713 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005714 }
5715 } else {
5716 /* temperatures not supported on 570, G4x, R30, R31, R32 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005717 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005718 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005719
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005720 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5721 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5722 thermal_read_mode);
5723
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005724 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005725 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005726 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005727 &thermal_temp_input16_group);
5728 if (res)
5729 return res;
5730 break;
5731 case TPACPI_THERMAL_TPEC_8:
5732 case TPACPI_THERMAL_ACPI_TMP07:
5733 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005734 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005735 &thermal_temp_input8_group);
5736 if (res)
5737 return res;
5738 break;
5739 case TPACPI_THERMAL_NONE:
5740 default:
5741 return 1;
5742 }
5743
5744 return 0;
5745}
5746
5747static void thermal_exit(void)
5748{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005749 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005750 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005751 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005752 &thermal_temp_input16_group);
5753 break;
5754 case TPACPI_THERMAL_TPEC_8:
5755 case TPACPI_THERMAL_ACPI_TMP07:
5756 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005757 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005758 &thermal_temp_input16_group);
5759 break;
5760 case TPACPI_THERMAL_NONE:
5761 default:
5762 break;
5763 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005764}
5765
5766static int thermal_read(char *p)
5767{
5768 int len = 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005769 int n, i;
5770 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005771
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005772 n = thermal_get_sensors(&t);
5773 if (unlikely(n < 0))
5774 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005775
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005776 len += sprintf(p + len, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005777
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005778 if (n > 0) {
5779 for (i = 0; i < (n - 1); i++)
5780 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
5781 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
5782 } else
5783 len += sprintf(p + len, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005784
5785 return len;
5786}
5787
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005788static struct ibm_struct thermal_driver_data = {
5789 .name = "thermal",
5790 .read = thermal_read,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005791 .exit = thermal_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005792};
5793
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005794/*************************************************************************
5795 * EC Dump subdriver
5796 */
5797
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005798static u8 ecdump_regs[256];
5799
5800static int ecdump_read(char *p)
5801{
5802 int len = 0;
5803 int i, j;
5804 u8 v;
5805
5806 len += sprintf(p + len, "EC "
5807 " +00 +01 +02 +03 +04 +05 +06 +07"
5808 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
5809 for (i = 0; i < 256; i += 16) {
5810 len += sprintf(p + len, "EC 0x%02x:", i);
5811 for (j = 0; j < 16; j++) {
5812 if (!acpi_ec_read(i + j, &v))
5813 break;
5814 if (v != ecdump_regs[i + j])
5815 len += sprintf(p + len, " *%02x", v);
5816 else
5817 len += sprintf(p + len, " %02x", v);
5818 ecdump_regs[i + j] = v;
5819 }
5820 len += sprintf(p + len, "\n");
5821 if (j != 16)
5822 break;
5823 }
5824
5825 /* These are way too dangerous to advertise openly... */
5826#if 0
5827 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
5828 " (<offset> is 00-ff, <value> is 00-ff)\n");
5829 len += sprintf(p + len, "commands:\t0x<offset> <value> "
5830 " (<offset> is 00-ff, <value> is 0-255)\n");
5831#endif
5832 return len;
5833}
5834
5835static int ecdump_write(char *buf)
5836{
5837 char *cmd;
5838 int i, v;
5839
5840 while ((cmd = next_cmd(&buf))) {
5841 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
5842 /* i and v set */
5843 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
5844 /* i and v set */
5845 } else
5846 return -EINVAL;
5847 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
5848 if (!acpi_ec_write(i, v))
5849 return -EIO;
5850 } else
5851 return -EINVAL;
5852 }
5853
5854 return 0;
5855}
5856
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005857static struct ibm_struct ecdump_driver_data = {
5858 .name = "ecdump",
5859 .read = ecdump_read,
5860 .write = ecdump_write,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03005861 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005862};
5863
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005864/*************************************************************************
5865 * Backlight/brightness subdriver
5866 */
Holger Macht8acb0252006-10-20 14:30:28 -07005867
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005868#define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5869
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005870/*
5871 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
5872 * CMOS NVRAM byte 0x5E, bits 0-3.
5873 *
5874 * EC HBRV (0x31) has the following layout
5875 * Bit 7: unknown function
5876 * Bit 6: unknown function
5877 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5878 * Bit 4: must be set to zero to avoid problems
5879 * Bit 3-0: backlight brightness level
5880 *
5881 * brightness_get_raw returns status data in the HBRV layout
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03005882 *
5883 * WARNING: The X61 has been verified to use HBRV for something else, so
5884 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
5885 * testing on the very early *60 Lenovo models...
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005886 */
5887
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005888enum {
5889 TP_EC_BACKLIGHT = 0x31,
5890
5891 /* TP_EC_BACKLIGHT bitmasks */
5892 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5893 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5894 TP_EC_BACKLIGHT_MAPSW = 0x20,
5895};
5896
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005897enum tpacpi_brightness_access_mode {
5898 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
5899 TPACPI_BRGHT_MODE_EC, /* EC control */
5900 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
5901 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
5902 TPACPI_BRGHT_MODE_MAX
5903};
5904
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03005905static struct backlight_device *ibm_backlight_device;
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005906
5907static enum tpacpi_brightness_access_mode brightness_mode =
5908 TPACPI_BRGHT_MODE_MAX;
5909
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005910static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5911
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005912static struct mutex brightness_mutex;
5913
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005914/* NVRAM brightness access,
5915 * call with brightness_mutex held! */
5916static unsigned int tpacpi_brightness_nvram_get(void)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005917{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005918 u8 lnvram;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005919
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005920 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5921 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5922 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
5923 lnvram &= (tp_features.bright_16levels) ? 0x0f : 0x07;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005924
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005925 return lnvram;
5926}
5927
5928static void tpacpi_brightness_checkpoint_nvram(void)
5929{
5930 u8 lec = 0;
5931 u8 b_nvram;
5932
5933 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
5934 return;
5935
5936 vdbg_printk(TPACPI_DBG_BRGHT,
5937 "trying to checkpoint backlight level to NVRAM...\n");
5938
5939 if (mutex_lock_killable(&brightness_mutex) < 0)
5940 return;
5941
5942 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5943 goto unlock;
5944 lec &= TP_EC_BACKLIGHT_LVLMSK;
5945 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
5946
5947 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5948 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
5949 /* NVRAM needs update */
5950 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
5951 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
5952 b_nvram |= lec;
5953 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
5954 dbg_printk(TPACPI_DBG_BRGHT,
5955 "updated NVRAM backlight level to %u (0x%02x)\n",
5956 (unsigned int) lec, (unsigned int) b_nvram);
5957 } else
5958 vdbg_printk(TPACPI_DBG_BRGHT,
5959 "NVRAM backlight level already is %u (0x%02x)\n",
5960 (unsigned int) lec, (unsigned int) b_nvram);
5961
5962unlock:
5963 mutex_unlock(&brightness_mutex);
5964}
5965
5966
5967/* call with brightness_mutex held! */
5968static int tpacpi_brightness_get_raw(int *status)
5969{
5970 u8 lec = 0;
5971
5972 switch (brightness_mode) {
5973 case TPACPI_BRGHT_MODE_UCMS_STEP:
5974 *status = tpacpi_brightness_nvram_get();
5975 return 0;
5976 case TPACPI_BRGHT_MODE_EC:
5977 case TPACPI_BRGHT_MODE_ECNVRAM:
5978 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
Henrique de Moraes Holschuh2d5e94d2008-04-26 01:02:20 -03005979 return -EIO;
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005980 *status = lec;
5981 return 0;
5982 default:
5983 return -ENXIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005984 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005985}
5986
5987/* call with brightness_mutex held! */
5988/* do NOT call with illegal backlight level value */
5989static int tpacpi_brightness_set_ec(unsigned int value)
5990{
5991 u8 lec = 0;
5992
5993 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5994 return -EIO;
5995
5996 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
5997 (lec & TP_EC_BACKLIGHT_CMDMSK) |
5998 (value & TP_EC_BACKLIGHT_LVLMSK))))
5999 return -EIO;
6000
6001 return 0;
6002}
6003
6004/* call with brightness_mutex held! */
6005static int tpacpi_brightness_set_ucmsstep(unsigned int value)
6006{
6007 int cmos_cmd, inc;
6008 unsigned int current_value, i;
6009
6010 current_value = tpacpi_brightness_nvram_get();
6011
6012 if (value == current_value)
6013 return 0;
6014
6015 cmos_cmd = (value > current_value) ?
6016 TP_CMOS_BRIGHTNESS_UP :
6017 TP_CMOS_BRIGHTNESS_DOWN;
6018 inc = (value > current_value) ? 1 : -1;
6019
6020 for (i = current_value; i != value; i += inc)
6021 if (issue_thinkpad_cmos_command(cmos_cmd))
6022 return -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006023
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006024 return 0;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006025}
6026
6027/* May return EINTR which can always be mapped to ERESTARTSYS */
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006028static int brightness_set(unsigned int value)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006029{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006030 int res;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006031
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006032 if (value > ((tp_features.bright_16levels)? 15 : 7) ||
6033 value < 0)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006034 return -EINVAL;
6035
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006036 vdbg_printk(TPACPI_DBG_BRGHT,
6037 "set backlight level to %d\n", value);
6038
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006039 res = mutex_lock_killable(&brightness_mutex);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006040 if (res < 0)
6041 return res;
6042
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006043 switch (brightness_mode) {
6044 case TPACPI_BRGHT_MODE_EC:
6045 case TPACPI_BRGHT_MODE_ECNVRAM:
6046 res = tpacpi_brightness_set_ec(value);
6047 break;
6048 case TPACPI_BRGHT_MODE_UCMS_STEP:
6049 res = tpacpi_brightness_set_ucmsstep(value);
6050 break;
6051 default:
6052 res = -ENXIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006053 }
6054
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006055 mutex_unlock(&brightness_mutex);
6056 return res;
6057}
6058
6059/* sysfs backlight class ----------------------------------------------- */
6060
6061static int brightness_update_status(struct backlight_device *bd)
6062{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006063 unsigned int level =
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006064 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
6065 bd->props.power == FB_BLANK_UNBLANK) ?
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006066 bd->props.brightness : 0;
6067
6068 dbg_printk(TPACPI_DBG_BRGHT,
6069 "backlight: attempt to set level to %d\n",
6070 level);
6071
6072 /* it is the backlight class's job (caller) to handle
6073 * EINTR and other errors properly */
6074 return brightness_set(level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006075}
Holger Macht8acb0252006-10-20 14:30:28 -07006076
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006077static int brightness_get(struct backlight_device *bd)
6078{
6079 int status, res;
6080
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006081 res = mutex_lock_killable(&brightness_mutex);
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006082 if (res < 0)
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006083 return 0;
6084
6085 res = tpacpi_brightness_get_raw(&status);
6086
6087 mutex_unlock(&brightness_mutex);
6088
6089 if (res < 0)
6090 return 0;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006091
6092 return status & TP_EC_BACKLIGHT_LVLMSK;
6093}
6094
Richard Purdie599a52d2007-02-10 23:07:48 +00006095static struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006096 .get_brightness = brightness_get,
6097 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006098};
6099
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006100/* --------------------------------------------------------------------- */
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03006101
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006102/*
6103 * These are only useful for models that have only one possibility
6104 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6105 * these quirks.
6106 */
6107#define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6108#define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6109#define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6110
6111static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
6112 /* Models with ATI GPUs known to require ECNVRAM mode */
6113 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
6114
Henrique de Moraes Holschuh6da25bf2009-09-12 15:22:11 -03006115 /* Models with ATI GPUs that can use ECNVRAM */
6116 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC),
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006117 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6118 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6119 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6120
Henrique de Moraes Holschuh6da25bf2009-09-12 15:22:11 -03006121 /* Models with Intel Extreme Graphics 2 */
6122 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC),
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006123 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_NOEC),
6124 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_NOEC),
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006125
6126 /* Models with Intel GMA900 */
6127 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
6128 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
6129 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
6130};
6131
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006132static int __init brightness_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006133{
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006134 int b;
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006135 unsigned long quirks;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006136
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006137 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
6138
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03006139 mutex_init(&brightness_mutex);
6140
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006141 quirks = tpacpi_check_quirks(brightness_quirk_table,
6142 ARRAY_SIZE(brightness_quirk_table));
6143
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03006144 /*
6145 * We always attempt to detect acpi support, so as to switch
6146 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6147 * going to publish a backlight interface
6148 */
6149 b = tpacpi_check_std_acpi_brightness_support();
6150 if (b > 0) {
Thomas Renninger2dba1b52008-08-01 17:38:03 +02006151
6152 if (acpi_video_backlight_support()) {
6153 if (brightness_enable > 1) {
6154 printk(TPACPI_NOTICE
6155 "Standard ACPI backlight interface "
6156 "available, not loading native one.\n");
6157 return 1;
6158 } else if (brightness_enable == 1) {
6159 printk(TPACPI_NOTICE
6160 "Backlight control force enabled, even if standard "
6161 "ACPI backlight interface is available\n");
6162 }
6163 } else {
6164 if (brightness_enable > 1) {
6165 printk(TPACPI_NOTICE
6166 "Standard ACPI backlight interface not "
6167 "available, thinkpad_acpi native "
6168 "brightness control enabled\n");
6169 }
Henrique de Moraes Holschuhe11e2112007-10-30 17:46:22 -02006170 }
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02006171 }
6172
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03006173 if (!brightness_enable) {
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006174 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03006175 "brightness support disabled by "
6176 "module parameter\n");
6177 return 1;
6178 }
6179
6180 if (b > 16) {
6181 printk(TPACPI_ERR
6182 "Unsupported brightness interface, "
6183 "please contact %s\n", TPACPI_MAIL);
6184 return 1;
6185 }
6186 if (b == 16)
6187 tp_features.bright_16levels = 1;
6188
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006189 /*
6190 * Check for module parameter bogosity, note that we
6191 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
6192 * able to detect "unspecified"
6193 */
6194 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03006195 return -EINVAL;
6196
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006197 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
6198 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
6199 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006200 if (quirks & TPACPI_BRGHT_Q_EC)
6201 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
6202 else
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006203 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
6204
6205 dbg_printk(TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006206 "driver auto-selected brightness_mode=%d\n",
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006207 brightness_mode);
6208 }
6209
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03006210 /* Safety */
6211 if (thinkpad_id.vendor != PCI_VENDOR_ID_IBM &&
6212 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
6213 brightness_mode == TPACPI_BRGHT_MODE_EC))
6214 return -EINVAL;
6215
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006216 if (tpacpi_brightness_get_raw(&b) < 0)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03006217 return 1;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006218
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02006219 if (tp_features.bright_16levels)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006220 printk(TPACPI_INFO
6221 "detected a 16-level brightness capable ThinkPad\n");
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02006222
Henrique de Moraes Holschuh7d5a0152007-04-24 11:48:20 -03006223 ibm_backlight_device = backlight_device_register(
6224 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
6225 &ibm_backlight_data);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006226 if (IS_ERR(ibm_backlight_device)) {
Henrique de Moraes Holschuh435c47e2009-09-20 14:09:22 -03006227 int rc = PTR_ERR(ibm_backlight_device);
6228 ibm_backlight_device = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006229 printk(TPACPI_ERR "Could not register backlight device\n");
Henrique de Moraes Holschuh435c47e2009-09-20 14:09:22 -03006230 return rc;
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006231 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006232 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6233 "brightness is supported\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006234
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006235 if (quirks & TPACPI_BRGHT_Q_ASK) {
6236 printk(TPACPI_NOTICE
6237 "brightness: will use unverified default: "
6238 "brightness_mode=%d\n", brightness_mode);
6239 printk(TPACPI_NOTICE
6240 "brightness: please report to %s whether it works well "
6241 "or not on your ThinkPad\n", TPACPI_MAIL);
6242 }
6243
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02006244 ibm_backlight_device->props.max_brightness =
6245 (tp_features.bright_16levels)? 15 : 7;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006246 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006247 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00006248
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006249 return 0;
6250}
6251
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006252static void brightness_suspend(pm_message_t state)
6253{
6254 tpacpi_brightness_checkpoint_nvram();
6255}
6256
6257static void brightness_shutdown(void)
6258{
6259 tpacpi_brightness_checkpoint_nvram();
6260}
6261
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006262static void brightness_exit(void)
6263{
6264 if (ibm_backlight_device) {
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006265 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006266 "calling backlight_device_unregister()\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006267 backlight_device_unregister(ibm_backlight_device);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006268 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006269
6270 tpacpi_brightness_checkpoint_nvram();
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006271}
6272
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006273static int brightness_read(char *p)
6274{
6275 int len = 0;
6276 int level;
6277
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006278 level = brightness_get(NULL);
6279 if (level < 0) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006280 len += sprintf(p + len, "level:\t\tunreadable\n");
6281 } else {
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02006282 len += sprintf(p + len, "level:\t\t%d\n", level);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006283 len += sprintf(p + len, "commands:\tup, down\n");
6284 len += sprintf(p + len, "commands:\tlevel <level>"
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02006285 " (<level> is 0-%d)\n",
6286 (tp_features.bright_16levels) ? 15 : 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006287 }
6288
6289 return len;
6290}
6291
6292static int brightness_write(char *buf)
6293{
6294 int level;
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006295 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006296 char *cmd;
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02006297 int max_level = (tp_features.bright_16levels) ? 15 : 7;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006298
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006299 level = brightness_get(NULL);
6300 if (level < 0)
6301 return level;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006302
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006303 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006304 if (strlencmp(cmd, "up") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006305 if (level < max_level)
6306 level++;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006307 } else if (strlencmp(cmd, "down") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006308 if (level > 0)
6309 level--;
6310 } else if (sscanf(cmd, "level %d", &level) == 1 &&
6311 level >= 0 && level <= max_level) {
6312 /* new level set */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006313 } else
6314 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006315 }
6316
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006317 tpacpi_disclose_usertask("procfs brightness",
6318 "set level to %d\n", level);
6319
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006320 /*
6321 * Now we know what the final level should be, so we try to set it.
6322 * Doing it this way makes the syscall restartable in case of EINTR
6323 */
6324 rc = brightness_set(level);
Roel Kluin80a8d122009-11-20 19:48:23 +01006325 return (rc == -EINTR)? -ERESTARTSYS : rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006326}
6327
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006328static struct ibm_struct brightness_driver_data = {
6329 .name = "brightness",
6330 .read = brightness_read,
6331 .write = brightness_write,
6332 .exit = brightness_exit,
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006333 .suspend = brightness_suspend,
6334 .shutdown = brightness_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006335};
6336
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006337/*************************************************************************
6338 * Volume subdriver
6339 */
6340
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006341static int volume_offset = 0x30;
6342
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006343static int volume_read(char *p)
6344{
6345 int len = 0;
6346 u8 level;
6347
6348 if (!acpi_ec_read(volume_offset, &level)) {
6349 len += sprintf(p + len, "level:\t\tunreadable\n");
6350 } else {
6351 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
6352 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
6353 len += sprintf(p + len, "commands:\tup, down, mute\n");
6354 len += sprintf(p + len, "commands:\tlevel <level>"
6355 " (<level> is 0-15)\n");
6356 }
6357
6358 return len;
6359}
6360
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006361static int volume_write(char *buf)
6362{
6363 int cmos_cmd, inc, i;
6364 u8 level, mute;
6365 int new_level, new_mute;
6366 char *cmd;
6367
6368 while ((cmd = next_cmd(&buf))) {
6369 if (!acpi_ec_read(volume_offset, &level))
6370 return -EIO;
6371 new_mute = mute = level & 0x40;
6372 new_level = level = level & 0xf;
6373
6374 if (strlencmp(cmd, "up") == 0) {
6375 if (mute)
6376 new_mute = 0;
6377 else
6378 new_level = level == 15 ? 15 : level + 1;
6379 } else if (strlencmp(cmd, "down") == 0) {
6380 if (mute)
6381 new_mute = 0;
6382 else
6383 new_level = level == 0 ? 0 : level - 1;
6384 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
6385 new_level >= 0 && new_level <= 15) {
6386 /* new_level set */
6387 } else if (strlencmp(cmd, "mute") == 0) {
6388 new_mute = 0x40;
6389 } else
6390 return -EINVAL;
6391
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006392 if (new_level != level) {
6393 /* mute doesn't change */
6394
6395 cmos_cmd = (new_level > level) ?
6396 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006397 inc = new_level > level ? 1 : -1;
6398
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03006399 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006400 !acpi_ec_write(volume_offset, level)))
6401 return -EIO;
6402
6403 for (i = level; i != new_level; i += inc)
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03006404 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006405 !acpi_ec_write(volume_offset, i + inc))
6406 return -EIO;
6407
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006408 if (mute &&
6409 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
6410 !acpi_ec_write(volume_offset, new_level + mute))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006411 return -EIO;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006412 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006413 }
6414
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006415 if (new_mute != mute) {
6416 /* level doesn't change */
6417
6418 cmos_cmd = (new_mute) ?
6419 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006420
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03006421 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006422 !acpi_ec_write(volume_offset, level + new_mute))
6423 return -EIO;
6424 }
6425 }
6426
6427 return 0;
6428}
6429
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006430static struct ibm_struct volume_driver_data = {
6431 .name = "volume",
6432 .read = volume_read,
6433 .write = volume_write,
6434};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006435
6436/*************************************************************************
6437 * Fan subdriver
6438 */
6439
6440/*
6441 * FAN ACCESS MODES
6442 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006443 * TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006444 * ACPI GFAN method: returns fan level
6445 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006446 * see TPACPI_FAN_WR_ACPI_SFAN
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03006447 * EC 0x2f (HFSP) not available if GFAN exists
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006448 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006449 * TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006450 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
6451 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03006452 * EC 0x2f (HFSP) might be available *for reading*, but do not use
6453 * it for writing.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006454 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006455 * TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03006456 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
6457 * Supported on almost all ThinkPads
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006458 *
6459 * Fan speed changes of any sort (including those caused by the
6460 * disengaged mode) are usually done slowly by the firmware as the
6461 * maximum ammount of fan duty cycle change per second seems to be
6462 * limited.
6463 *
6464 * Reading is not available if GFAN exists.
6465 * Writing is not available if SFAN exists.
6466 *
6467 * Bits
6468 * 7 automatic mode engaged;
6469 * (default operation mode of the ThinkPad)
6470 * fan level is ignored in this mode.
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03006471 * 6 full speed mode (takes precedence over bit 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006472 * not available on all thinkpads. May disable
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03006473 * the tachometer while the fan controller ramps up
6474 * the speed (which can take up to a few *minutes*).
6475 * Speeds up fan to 100% duty-cycle, which is far above
6476 * the standard RPM levels. It is not impossible that
6477 * it could cause hardware damage.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006478 * 5-3 unused in some models. Extra bits for fan level
6479 * in others, but still useless as all values above
6480 * 7 map to the same speed as level 7 in these models.
6481 * 2-0 fan level (0..7 usually)
6482 * 0x00 = stop
6483 * 0x07 = max (set when temperatures critical)
6484 * Some ThinkPads may have other levels, see
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006485 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006486 *
6487 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
6488 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
6489 * does so, its initial value is meaningless (0x07).
6490 *
6491 * For firmware bugs, refer to:
6492 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
6493 *
6494 * ----
6495 *
6496 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
6497 * Main fan tachometer reading (in RPM)
6498 *
6499 * This register is present on all ThinkPads with a new-style EC, and
6500 * it is known not to be present on the A21m/e, and T22, as there is
6501 * something else in offset 0x84 according to the ACPI DSDT. Other
6502 * ThinkPads from this same time period (and earlier) probably lack the
6503 * tachometer as well.
6504 *
Nick Andrew877d0312009-01-26 11:06:57 +01006505 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006506 * was never fixed by IBM to report the EC firmware version string
6507 * probably support the tachometer (like the early X models), so
6508 * detecting it is quite hard. We need more data to know for sure.
6509 *
6510 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
6511 * might result.
6512 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03006513 * FIRMWARE BUG: may go stale while the EC is switching to full speed
6514 * mode.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006515 *
6516 * For firmware bugs, refer to:
6517 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
6518 *
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03006519 * ----
6520 *
6521 * ThinkPad EC register 0x31 bit 0 (only on select models)
6522 *
6523 * When bit 0 of EC register 0x31 is zero, the tachometer registers
6524 * show the speed of the main fan. When bit 0 of EC register 0x31
6525 * is one, the tachometer registers show the speed of the auxiliary
6526 * fan.
6527 *
6528 * Fan control seems to affect both fans, regardless of the state
6529 * of this bit.
6530 *
6531 * So far, only the firmware for the X60/X61 non-tablet versions
6532 * seem to support this (firmware TP-7M).
6533 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006534 * TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006535 * ThinkPad X31, X40, X41. Not available in the X60.
6536 *
6537 * FANS ACPI handle: takes three arguments: low speed, medium speed,
6538 * high speed. ACPI DSDT seems to map these three speeds to levels
6539 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
6540 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
6541 *
6542 * The speeds are stored on handles
6543 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
6544 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006545 * There are three default speed sets, accessible as handles:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006546 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
6547 *
6548 * ACPI DSDT switches which set is in use depending on various
6549 * factors.
6550 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006551 * TPACPI_FAN_WR_TPEC is also available and should be used to
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006552 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
6553 * but the ACPI tables just mention level 7.
6554 */
6555
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006556enum { /* Fan control constants */
6557 fan_status_offset = 0x2f, /* EC register 0x2f */
6558 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
6559 * 0x84 must be read before 0x85 */
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03006560 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
6561 bit 0 selects which fan is active */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006562
6563 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
6564 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
6565
6566 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
6567};
6568
6569enum fan_status_access_mode {
6570 TPACPI_FAN_NONE = 0, /* No fan status or control */
6571 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
6572 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
6573};
6574
6575enum fan_control_access_mode {
6576 TPACPI_FAN_WR_NONE = 0, /* No fan control */
6577 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
6578 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
6579 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
6580};
6581
6582enum fan_control_commands {
6583 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
6584 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
6585 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
6586 * and also watchdog cmd */
6587};
6588
6589static int fan_control_allowed;
6590
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006591static enum fan_status_access_mode fan_status_access_mode;
6592static enum fan_control_access_mode fan_control_access_mode;
6593static enum fan_control_commands fan_control_commands;
6594
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02006595static u8 fan_control_initial_status;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006596static u8 fan_control_desired_level;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006597static u8 fan_control_resume_level;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02006598static int fan_watchdog_maxinterval;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006599
6600static struct mutex fan_mutex;
6601
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006602static void fan_watchdog_fire(struct work_struct *ignored);
Len Brown25c68a32006-12-08 04:43:41 -05006603static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02006604
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006605TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
6606TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006607 "\\FSPD", /* 600e/x, 770e, 770x */
6608 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006609TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006610 "JFNS", /* 770x-JL */
6611 ); /* all others */
6612
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006613/*
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006614 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
6615 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
6616 * be in auto mode (0x80).
6617 *
6618 * This is corrected by any write to HFSP either by the driver, or
6619 * by the firmware.
6620 *
6621 * We assume 0x07 really means auto mode while this quirk is active,
6622 * as this is far more likely than the ThinkPad being in level 7,
6623 * which is only used by the firmware during thermal emergencies.
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03006624 *
6625 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
6626 * TP-70 (T43, R52), which are known to be buggy.
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006627 */
6628
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03006629static void fan_quirk1_setup(void)
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006630{
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006631 if (fan_control_initial_status == 0x07) {
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03006632 printk(TPACPI_NOTICE
6633 "fan_init: initial fan status is unknown, "
6634 "assuming it is in auto mode\n");
6635 tp_features.fan_ctrl_status_undef = 1;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006636 }
6637}
6638
6639static void fan_quirk1_handle(u8 *fan_status)
6640{
6641 if (unlikely(tp_features.fan_ctrl_status_undef)) {
6642 if (*fan_status != fan_control_initial_status) {
6643 /* something changed the HFSP regisnter since
6644 * driver init time, so it is not undefined
6645 * anymore */
6646 tp_features.fan_ctrl_status_undef = 0;
6647 } else {
6648 /* Return most likely status. In fact, it
6649 * might be the only possible status */
6650 *fan_status = TP_EC_FAN_AUTO;
6651 }
6652 }
6653}
6654
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03006655/* Select main fan on X60/X61, NOOP on others */
6656static bool fan_select_fan1(void)
6657{
6658 if (tp_features.second_fan) {
6659 u8 val;
6660
6661 if (ec_read(fan_select_offset, &val) < 0)
6662 return false;
6663 val &= 0xFEU;
6664 if (ec_write(fan_select_offset, val) < 0)
6665 return false;
6666 }
6667 return true;
6668}
6669
6670/* Select secondary fan on X60/X61 */
6671static bool fan_select_fan2(void)
6672{
6673 u8 val;
6674
6675 if (!tp_features.second_fan)
6676 return false;
6677
6678 if (ec_read(fan_select_offset, &val) < 0)
6679 return false;
6680 val |= 0x01U;
6681 if (ec_write(fan_select_offset, val) < 0)
6682 return false;
6683
6684 return true;
6685}
6686
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006687/*
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006688 * Call with fan_mutex held
6689 */
6690static void fan_update_desired_level(u8 status)
6691{
6692 if ((status &
6693 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
6694 if (status > 7)
6695 fan_control_desired_level = 7;
6696 else
6697 fan_control_desired_level = status;
6698 }
6699}
6700
6701static int fan_get_status(u8 *status)
6702{
6703 u8 s;
6704
6705 /* TODO:
6706 * Add TPACPI_FAN_RD_ACPI_FANS ? */
6707
6708 switch (fan_status_access_mode) {
6709 case TPACPI_FAN_RD_ACPI_GFAN:
6710 /* 570, 600e/x, 770e, 770x */
6711
6712 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
6713 return -EIO;
6714
6715 if (likely(status))
6716 *status = s & 0x07;
6717
6718 break;
6719
6720 case TPACPI_FAN_RD_TPEC:
6721 /* all except 570, 600e/x, 770e, 770x */
6722 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
6723 return -EIO;
6724
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006725 if (likely(status)) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006726 *status = s;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006727 fan_quirk1_handle(status);
6728 }
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006729
6730 break;
6731
6732 default:
6733 return -ENXIO;
6734 }
6735
6736 return 0;
6737}
6738
6739static int fan_get_status_safe(u8 *status)
6740{
6741 int rc;
6742 u8 s;
6743
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006744 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006745 return -ERESTARTSYS;
6746 rc = fan_get_status(&s);
6747 if (!rc)
6748 fan_update_desired_level(s);
6749 mutex_unlock(&fan_mutex);
6750
6751 if (status)
6752 *status = s;
6753
6754 return rc;
6755}
6756
6757static int fan_get_speed(unsigned int *speed)
6758{
6759 u8 hi, lo;
6760
6761 switch (fan_status_access_mode) {
6762 case TPACPI_FAN_RD_TPEC:
6763 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03006764 if (unlikely(!fan_select_fan1()))
6765 return -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006766 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
6767 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
6768 return -EIO;
6769
6770 if (likely(speed))
6771 *speed = (hi << 8) | lo;
6772
6773 break;
6774
6775 default:
6776 return -ENXIO;
6777 }
6778
6779 return 0;
6780}
6781
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03006782static int fan2_get_speed(unsigned int *speed)
6783{
6784 u8 hi, lo;
6785 bool rc;
6786
6787 switch (fan_status_access_mode) {
6788 case TPACPI_FAN_RD_TPEC:
6789 /* all except 570, 600e/x, 770e, 770x */
6790 if (unlikely(!fan_select_fan2()))
6791 return -EIO;
6792 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
6793 !acpi_ec_read(fan_rpm_offset + 1, &hi);
6794 fan_select_fan1(); /* play it safe */
6795 if (rc)
6796 return -EIO;
6797
6798 if (likely(speed))
6799 *speed = (hi << 8) | lo;
6800
6801 break;
6802
6803 default:
6804 return -ENXIO;
6805 }
6806
6807 return 0;
6808}
6809
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006810static int fan_set_level(int level)
6811{
6812 if (!fan_control_allowed)
6813 return -EPERM;
6814
6815 switch (fan_control_access_mode) {
6816 case TPACPI_FAN_WR_ACPI_SFAN:
6817 if (level >= 0 && level <= 7) {
6818 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
6819 return -EIO;
6820 } else
6821 return -EINVAL;
6822 break;
6823
6824 case TPACPI_FAN_WR_ACPI_FANS:
6825 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006826 if (!(level & TP_EC_FAN_AUTO) &&
6827 !(level & TP_EC_FAN_FULLSPEED) &&
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006828 ((level < 0) || (level > 7)))
6829 return -EINVAL;
6830
6831 /* safety net should the EC not support AUTO
6832 * or FULLSPEED mode bits and just ignore them */
6833 if (level & TP_EC_FAN_FULLSPEED)
6834 level |= 7; /* safety min speed 7 */
Roel Kluin547266e2008-02-05 00:24:56 +01006835 else if (level & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006836 level |= 4; /* safety min speed 4 */
6837
6838 if (!acpi_ec_write(fan_status_offset, level))
6839 return -EIO;
6840 else
6841 tp_features.fan_ctrl_status_undef = 0;
6842 break;
6843
6844 default:
6845 return -ENXIO;
6846 }
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00006847
6848 vdbg_printk(TPACPI_DBG_FAN,
6849 "fan control: set fan control register to 0x%02x\n", level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006850 return 0;
6851}
6852
6853static int fan_set_level_safe(int level)
6854{
6855 int rc;
6856
6857 if (!fan_control_allowed)
6858 return -EPERM;
6859
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006860 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006861 return -ERESTARTSYS;
6862
6863 if (level == TPACPI_FAN_LAST_LEVEL)
6864 level = fan_control_desired_level;
6865
6866 rc = fan_set_level(level);
6867 if (!rc)
6868 fan_update_desired_level(level);
6869
6870 mutex_unlock(&fan_mutex);
6871 return rc;
6872}
6873
6874static int fan_set_enable(void)
6875{
6876 u8 s;
6877 int rc;
6878
6879 if (!fan_control_allowed)
6880 return -EPERM;
6881
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006882 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006883 return -ERESTARTSYS;
6884
6885 switch (fan_control_access_mode) {
6886 case TPACPI_FAN_WR_ACPI_FANS:
6887 case TPACPI_FAN_WR_TPEC:
6888 rc = fan_get_status(&s);
6889 if (rc < 0)
6890 break;
6891
6892 /* Don't go out of emergency fan mode */
6893 if (s != 7) {
6894 s &= 0x07;
6895 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
6896 }
6897
6898 if (!acpi_ec_write(fan_status_offset, s))
6899 rc = -EIO;
6900 else {
6901 tp_features.fan_ctrl_status_undef = 0;
6902 rc = 0;
6903 }
6904 break;
6905
6906 case TPACPI_FAN_WR_ACPI_SFAN:
6907 rc = fan_get_status(&s);
6908 if (rc < 0)
6909 break;
6910
6911 s &= 0x07;
6912
6913 /* Set fan to at least level 4 */
6914 s |= 4;
6915
6916 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006917 rc = -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006918 else
6919 rc = 0;
6920 break;
6921
6922 default:
6923 rc = -ENXIO;
6924 }
6925
6926 mutex_unlock(&fan_mutex);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00006927
6928 if (!rc)
6929 vdbg_printk(TPACPI_DBG_FAN,
6930 "fan control: set fan control register to 0x%02x\n",
6931 s);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006932 return rc;
6933}
6934
6935static int fan_set_disable(void)
6936{
6937 int rc;
6938
6939 if (!fan_control_allowed)
6940 return -EPERM;
6941
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006942 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006943 return -ERESTARTSYS;
6944
6945 rc = 0;
6946 switch (fan_control_access_mode) {
6947 case TPACPI_FAN_WR_ACPI_FANS:
6948 case TPACPI_FAN_WR_TPEC:
6949 if (!acpi_ec_write(fan_status_offset, 0x00))
6950 rc = -EIO;
6951 else {
6952 fan_control_desired_level = 0;
6953 tp_features.fan_ctrl_status_undef = 0;
6954 }
6955 break;
6956
6957 case TPACPI_FAN_WR_ACPI_SFAN:
6958 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
6959 rc = -EIO;
6960 else
6961 fan_control_desired_level = 0;
6962 break;
6963
6964 default:
6965 rc = -ENXIO;
6966 }
6967
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00006968 if (!rc)
6969 vdbg_printk(TPACPI_DBG_FAN,
6970 "fan control: set fan control register to 0\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006971
6972 mutex_unlock(&fan_mutex);
6973 return rc;
6974}
6975
6976static int fan_set_speed(int speed)
6977{
6978 int rc;
6979
6980 if (!fan_control_allowed)
6981 return -EPERM;
6982
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006983 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006984 return -ERESTARTSYS;
6985
6986 rc = 0;
6987 switch (fan_control_access_mode) {
6988 case TPACPI_FAN_WR_ACPI_FANS:
6989 if (speed >= 0 && speed <= 65535) {
6990 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
6991 speed, speed, speed))
6992 rc = -EIO;
6993 } else
6994 rc = -EINVAL;
6995 break;
6996
6997 default:
6998 rc = -ENXIO;
6999 }
7000
7001 mutex_unlock(&fan_mutex);
7002 return rc;
7003}
7004
7005static void fan_watchdog_reset(void)
7006{
7007 static int fan_watchdog_active;
7008
7009 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
7010 return;
7011
7012 if (fan_watchdog_active)
7013 cancel_delayed_work(&fan_watchdog_task);
7014
7015 if (fan_watchdog_maxinterval > 0 &&
7016 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
7017 fan_watchdog_active = 1;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03007018 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007019 msecs_to_jiffies(fan_watchdog_maxinterval
7020 * 1000))) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007021 printk(TPACPI_ERR
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03007022 "failed to queue the fan watchdog, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007023 "watchdog will not trigger\n");
7024 }
7025 } else
7026 fan_watchdog_active = 0;
7027}
7028
7029static void fan_watchdog_fire(struct work_struct *ignored)
7030{
7031 int rc;
7032
7033 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
7034 return;
7035
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007036 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007037 rc = fan_set_enable();
7038 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007039 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007040 "will try again later...\n", -rc);
7041 /* reschedule for later */
7042 fan_watchdog_reset();
7043 }
7044}
7045
7046/*
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007047 * SYSFS fan layout: hwmon compatible (device)
7048 *
7049 * pwm*_enable:
7050 * 0: "disengaged" mode
7051 * 1: manual mode
7052 * 2: native EC "auto" mode (recommended, hardware default)
7053 *
7054 * pwm*: set speed in manual mode, ignored otherwise.
7055 * 0 is level 0; 255 is level 7. Intermediate points done with linear
7056 * interpolation.
7057 *
7058 * fan*_input: tachometer reading, RPM
7059 *
7060 *
7061 * SYSFS fan layout: extensions
7062 *
7063 * fan_watchdog (driver):
7064 * fan watchdog interval in seconds, 0 disables (default), max 120
7065 */
7066
7067/* sysfs fan pwm1_enable ----------------------------------------------- */
7068static ssize_t fan_pwm1_enable_show(struct device *dev,
7069 struct device_attribute *attr,
7070 char *buf)
7071{
7072 int res, mode;
7073 u8 status;
7074
7075 res = fan_get_status_safe(&status);
7076 if (res)
7077 return res;
7078
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007079 if (status & TP_EC_FAN_FULLSPEED) {
7080 mode = 0;
7081 } else if (status & TP_EC_FAN_AUTO) {
7082 mode = 2;
7083 } else
7084 mode = 1;
7085
7086 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
7087}
7088
7089static ssize_t fan_pwm1_enable_store(struct device *dev,
7090 struct device_attribute *attr,
7091 const char *buf, size_t count)
7092{
7093 unsigned long t;
7094 int res, level;
7095
7096 if (parse_strtoul(buf, 2, &t))
7097 return -EINVAL;
7098
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007099 tpacpi_disclose_usertask("hwmon pwm1_enable",
7100 "set fan mode to %lu\n", t);
7101
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007102 switch (t) {
7103 case 0:
7104 level = TP_EC_FAN_FULLSPEED;
7105 break;
7106 case 1:
7107 level = TPACPI_FAN_LAST_LEVEL;
7108 break;
7109 case 2:
7110 level = TP_EC_FAN_AUTO;
7111 break;
7112 case 3:
7113 /* reserved for software-controlled auto mode */
7114 return -ENOSYS;
7115 default:
7116 return -EINVAL;
7117 }
7118
7119 res = fan_set_level_safe(level);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03007120 if (res == -ENXIO)
7121 return -EINVAL;
7122 else if (res < 0)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007123 return res;
7124
7125 fan_watchdog_reset();
7126
7127 return count;
7128}
7129
7130static struct device_attribute dev_attr_fan_pwm1_enable =
7131 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
7132 fan_pwm1_enable_show, fan_pwm1_enable_store);
7133
7134/* sysfs fan pwm1 ------------------------------------------------------ */
7135static ssize_t fan_pwm1_show(struct device *dev,
7136 struct device_attribute *attr,
7137 char *buf)
7138{
7139 int res;
7140 u8 status;
7141
7142 res = fan_get_status_safe(&status);
7143 if (res)
7144 return res;
7145
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007146 if ((status &
7147 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
7148 status = fan_control_desired_level;
7149
7150 if (status > 7)
7151 status = 7;
7152
7153 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
7154}
7155
7156static ssize_t fan_pwm1_store(struct device *dev,
7157 struct device_attribute *attr,
7158 const char *buf, size_t count)
7159{
7160 unsigned long s;
7161 int rc;
7162 u8 status, newlevel;
7163
7164 if (parse_strtoul(buf, 255, &s))
7165 return -EINVAL;
7166
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007167 tpacpi_disclose_usertask("hwmon pwm1",
7168 "set fan speed to %lu\n", s);
7169
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007170 /* scale down from 0-255 to 0-7 */
7171 newlevel = (s >> 5) & 0x07;
7172
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007173 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02007174 return -ERESTARTSYS;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007175
7176 rc = fan_get_status(&status);
7177 if (!rc && (status &
7178 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7179 rc = fan_set_level(newlevel);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03007180 if (rc == -ENXIO)
7181 rc = -EINVAL;
7182 else if (!rc) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007183 fan_update_desired_level(newlevel);
Henrique de Moraes Holschuhca4ac2f2007-04-27 22:00:11 -03007184 fan_watchdog_reset();
7185 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007186 }
7187
7188 mutex_unlock(&fan_mutex);
7189 return (rc)? rc : count;
7190}
7191
7192static struct device_attribute dev_attr_fan_pwm1 =
7193 __ATTR(pwm1, S_IWUSR | S_IRUGO,
7194 fan_pwm1_show, fan_pwm1_store);
7195
7196/* sysfs fan fan1_input ------------------------------------------------ */
7197static ssize_t fan_fan1_input_show(struct device *dev,
7198 struct device_attribute *attr,
7199 char *buf)
7200{
7201 int res;
7202 unsigned int speed;
7203
7204 res = fan_get_speed(&speed);
7205 if (res < 0)
7206 return res;
7207
7208 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7209}
7210
7211static struct device_attribute dev_attr_fan_fan1_input =
7212 __ATTR(fan1_input, S_IRUGO,
7213 fan_fan1_input_show, NULL);
7214
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007215/* sysfs fan fan2_input ------------------------------------------------ */
7216static ssize_t fan_fan2_input_show(struct device *dev,
7217 struct device_attribute *attr,
7218 char *buf)
7219{
7220 int res;
7221 unsigned int speed;
7222
7223 res = fan2_get_speed(&speed);
7224 if (res < 0)
7225 return res;
7226
7227 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7228}
7229
7230static struct device_attribute dev_attr_fan_fan2_input =
7231 __ATTR(fan2_input, S_IRUGO,
7232 fan_fan2_input_show, NULL);
7233
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007234/* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007235static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
7236 char *buf)
7237{
7238 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
7239}
7240
7241static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
7242 const char *buf, size_t count)
7243{
7244 unsigned long t;
7245
7246 if (parse_strtoul(buf, 120, &t))
7247 return -EINVAL;
7248
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03007249 if (!fan_control_allowed)
7250 return -EPERM;
7251
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007252 fan_watchdog_maxinterval = t;
7253 fan_watchdog_reset();
7254
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007255 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
7256
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007257 return count;
7258}
7259
7260static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
7261 fan_fan_watchdog_show, fan_fan_watchdog_store);
7262
7263/* --------------------------------------------------------------------- */
7264static struct attribute *fan_attributes[] = {
7265 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
7266 &dev_attr_fan_fan1_input.attr,
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007267 NULL, /* for fan2_input */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007268 NULL
7269};
7270
7271static const struct attribute_group fan_attr_group = {
7272 .attrs = fan_attributes,
7273};
7274
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007275#define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
7276#define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007277
7278#define TPACPI_FAN_QI(__id1, __id2, __quirks) \
7279 { .vendor = PCI_VENDOR_ID_IBM, \
7280 .bios = TPACPI_MATCH_ANY, \
7281 .ec = TPID(__id1, __id2), \
7282 .quirks = __quirks }
7283
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007284#define TPACPI_FAN_QL(__id1, __id2, __quirks) \
7285 { .vendor = PCI_VENDOR_ID_LENOVO, \
7286 .bios = TPACPI_MATCH_ANY, \
7287 .ec = TPID(__id1, __id2), \
7288 .quirks = __quirks }
7289
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007290static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
7291 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
7292 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
7293 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
7294 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007295 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007296};
7297
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007298#undef TPACPI_FAN_QL
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007299#undef TPACPI_FAN_QI
7300
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007301static int __init fan_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007302{
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007303 int rc;
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007304 unsigned long quirks;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007305
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007306 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7307 "initializing fan subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007308
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03007309 mutex_init(&fan_mutex);
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007310 fan_status_access_mode = TPACPI_FAN_NONE;
7311 fan_control_access_mode = TPACPI_FAN_WR_NONE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007312 fan_control_commands = 0;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007313 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03007314 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007315 tp_features.second_fan = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007316 fan_control_desired_level = 7;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007317
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007318 TPACPI_ACPIHANDLE_INIT(fans);
7319 TPACPI_ACPIHANDLE_INIT(gfan);
7320 TPACPI_ACPIHANDLE_INIT(sfan);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007321
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007322 quirks = tpacpi_check_quirks(fan_quirk_table,
7323 ARRAY_SIZE(fan_quirk_table));
7324
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007325 if (gfan_handle) {
7326 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007327 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007328 } else {
7329 /* all other ThinkPads: note that even old-style
7330 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02007331 if (likely(acpi_ec_read(fan_status_offset,
7332 &fan_control_initial_status))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007333 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007334 if (quirks & TPACPI_FAN_Q1)
7335 fan_quirk1_setup();
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007336 if (quirks & TPACPI_FAN_2FAN) {
7337 tp_features.second_fan = 1;
7338 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7339 "secondary fan support enabled\n");
7340 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007341 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007342 printk(TPACPI_ERR
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007343 "ThinkPad ACPI EC access misbehaving, "
7344 "fan status and control unavailable\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007345 return 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007346 }
7347 }
7348
7349 if (sfan_handle) {
7350 /* 570, 770x-JL */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007351 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02007352 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007353 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007354 } else {
7355 if (!gfan_handle) {
7356 /* gfan without sfan means no fan control */
7357 /* all other models implement TP EC 0x2f control */
7358
7359 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02007360 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007361 fan_control_access_mode =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007362 TPACPI_FAN_WR_ACPI_FANS;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007363 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007364 TPACPI_FAN_CMD_SPEED |
7365 TPACPI_FAN_CMD_LEVEL |
7366 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007367 } else {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007368 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02007369 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007370 TPACPI_FAN_CMD_LEVEL |
7371 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007372 }
7373 }
7374 }
7375
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007376 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7377 "fan is %s, modes %d, %d\n",
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007378 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
7379 fan_control_access_mode != TPACPI_FAN_WR_NONE),
7380 fan_status_access_mode, fan_control_access_mode);
7381
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03007382 /* fan control master switch */
7383 if (!fan_control_allowed) {
7384 fan_control_access_mode = TPACPI_FAN_WR_NONE;
7385 fan_control_commands = 0;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007386 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03007387 "fan control features disabled by parameter\n");
7388 }
7389
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007390 /* update fan_control_desired_level */
7391 if (fan_status_access_mode != TPACPI_FAN_NONE)
7392 fan_get_status_safe(NULL);
7393
7394 if (fan_status_access_mode != TPACPI_FAN_NONE ||
7395 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007396 if (tp_features.second_fan) {
7397 /* attach second fan tachometer */
7398 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
7399 &dev_attr_fan_fan2_input.attr;
7400 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007401 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007402 &fan_attr_group);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007403 if (rc < 0)
7404 return rc;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03007405
7406 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
7407 &driver_attr_fan_watchdog);
7408 if (rc < 0) {
7409 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
7410 &fan_attr_group);
7411 return rc;
7412 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007413 return 0;
7414 } else
7415 return 1;
7416}
7417
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007418static void fan_exit(void)
7419{
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007420 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007421 "cancelling any pending fan watchdog tasks\n");
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007422
7423 /* FIXME: can we really do this unconditionally? */
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007424 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007425 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
7426 &driver_attr_fan_watchdog);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007427
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007428 cancel_delayed_work(&fan_watchdog_task);
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03007429 flush_workqueue(tpacpi_wq);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007430}
7431
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007432static void fan_suspend(pm_message_t state)
7433{
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007434 int rc;
7435
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007436 if (!fan_control_allowed)
7437 return;
7438
7439 /* Store fan status in cache */
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007440 fan_control_resume_level = 0;
7441 rc = fan_get_status_safe(&fan_control_resume_level);
7442 if (rc < 0)
7443 printk(TPACPI_NOTICE
7444 "failed to read fan level for later "
7445 "restore during resume: %d\n", rc);
7446
7447 /* if it is undefined, don't attempt to restore it.
7448 * KEEP THIS LAST */
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007449 if (tp_features.fan_ctrl_status_undef)
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007450 fan_control_resume_level = 0;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007451}
7452
7453static void fan_resume(void)
7454{
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007455 u8 current_level = 7;
7456 bool do_set = false;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007457 int rc;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007458
7459 /* DSDT *always* updates status on resume */
7460 tp_features.fan_ctrl_status_undef = 0;
7461
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007462 if (!fan_control_allowed ||
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007463 !fan_control_resume_level ||
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007464 (fan_get_status_safe(&current_level) < 0))
7465 return;
7466
7467 switch (fan_control_access_mode) {
7468 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007469 /* never decrease fan level */
7470 do_set = (fan_control_resume_level > current_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007471 break;
7472 case TPACPI_FAN_WR_ACPI_FANS:
7473 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007474 /* never decrease fan level, scale is:
7475 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
7476 *
7477 * We expect the firmware to set either 7 or AUTO, but we
7478 * handle FULLSPEED out of paranoia.
7479 *
7480 * So, we can safely only restore FULLSPEED or 7, anything
7481 * else could slow the fan. Restoring AUTO is useless, at
7482 * best that's exactly what the DSDT already set (it is the
7483 * slower it uses).
7484 *
7485 * Always keep in mind that the DSDT *will* have set the
7486 * fans to what the vendor supposes is the best level. We
7487 * muck with it only to speed the fan up.
7488 */
7489 if (fan_control_resume_level != 7 &&
7490 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
7491 return;
7492 else
7493 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
7494 (current_level != fan_control_resume_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007495 break;
7496 default:
7497 return;
7498 }
7499 if (do_set) {
7500 printk(TPACPI_NOTICE
7501 "restoring fan level to 0x%02x\n",
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007502 fan_control_resume_level);
7503 rc = fan_set_level_safe(fan_control_resume_level);
7504 if (rc < 0)
7505 printk(TPACPI_NOTICE
7506 "failed to restore fan level: %d\n", rc);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007507 }
7508}
7509
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007510static int fan_read(char *p)
7511{
7512 int len = 0;
7513 int rc;
7514 u8 status;
7515 unsigned int speed = 0;
7516
7517 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007518 case TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007519 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007520 rc = fan_get_status_safe(&status);
7521 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007522 return rc;
7523
7524 len += sprintf(p + len, "status:\t\t%s\n"
7525 "level:\t\t%d\n",
7526 (status != 0) ? "enabled" : "disabled", status);
7527 break;
7528
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007529 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007530 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007531 rc = fan_get_status_safe(&status);
7532 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007533 return rc;
7534
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007535 len += sprintf(p + len, "status:\t\t%s\n",
7536 (status != 0) ? "enabled" : "disabled");
7537
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007538 rc = fan_get_speed(&speed);
7539 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007540 return rc;
7541
7542 len += sprintf(p + len, "speed:\t\t%d\n", speed);
7543
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007544 if (status & TP_EC_FAN_FULLSPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007545 /* Disengaged mode takes precedence */
7546 len += sprintf(p + len, "level:\t\tdisengaged\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007547 else if (status & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007548 len += sprintf(p + len, "level:\t\tauto\n");
7549 else
7550 len += sprintf(p + len, "level:\t\t%d\n", status);
7551 break;
7552
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007553 case TPACPI_FAN_NONE:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007554 default:
7555 len += sprintf(p + len, "status:\t\tnot supported\n");
7556 }
7557
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007558 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007559 len += sprintf(p + len, "commands:\tlevel <level>");
7560
7561 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007562 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007563 len += sprintf(p + len, " (<level> is 0-7)\n");
7564 break;
7565
7566 default:
7567 len += sprintf(p + len, " (<level> is 0-7, "
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007568 "auto, disengaged, full-speed)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007569 break;
7570 }
7571 }
7572
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007573 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007574 len += sprintf(p + len, "commands:\tenable, disable\n"
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007575 "commands:\twatchdog <timeout> (<timeout> "
7576 "is 0 (off), 1-120 (seconds))\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007577
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007578 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007579 len += sprintf(p + len, "commands:\tspeed <speed>"
7580 " (<speed> is 0-65535)\n");
7581
7582 return len;
7583}
7584
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007585static int fan_write_cmd_level(const char *cmd, int *rc)
7586{
7587 int level;
7588
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02007589 if (strlencmp(cmd, "level auto") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007590 level = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007591 else if ((strlencmp(cmd, "level disengaged") == 0) |
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007592 (strlencmp(cmd, "level full-speed") == 0))
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007593 level = TP_EC_FAN_FULLSPEED;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02007594 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007595 return 0;
7596
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007597 *rc = fan_set_level_safe(level);
7598 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007599 printk(TPACPI_ERR "level command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007600 "access mode %d", fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007601 else if (!*rc)
7602 tpacpi_disclose_usertask("procfs fan",
7603 "set level to %d\n", level);
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007604
7605 return 1;
7606}
7607
7608static int fan_write_cmd_enable(const char *cmd, int *rc)
7609{
7610 if (strlencmp(cmd, "enable") != 0)
7611 return 0;
7612
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007613 *rc = fan_set_enable();
7614 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007615 printk(TPACPI_ERR "enable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007616 "access mode %d", fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007617 else if (!*rc)
7618 tpacpi_disclose_usertask("procfs fan", "enable\n");
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007619
7620 return 1;
7621}
7622
7623static int fan_write_cmd_disable(const char *cmd, int *rc)
7624{
7625 if (strlencmp(cmd, "disable") != 0)
7626 return 0;
7627
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007628 *rc = fan_set_disable();
7629 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007630 printk(TPACPI_ERR "disable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007631 "access mode %d", fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007632 else if (!*rc)
7633 tpacpi_disclose_usertask("procfs fan", "disable\n");
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007634
7635 return 1;
7636}
7637
7638static int fan_write_cmd_speed(const char *cmd, int *rc)
7639{
7640 int speed;
7641
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02007642 /* TODO:
7643 * Support speed <low> <medium> <high> ? */
7644
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007645 if (sscanf(cmd, "speed %d", &speed) != 1)
7646 return 0;
7647
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007648 *rc = fan_set_speed(speed);
7649 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007650 printk(TPACPI_ERR "speed command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007651 "access mode %d", fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007652 else if (!*rc)
7653 tpacpi_disclose_usertask("procfs fan",
7654 "set speed to %d\n", speed);
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007655
7656 return 1;
7657}
7658
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007659static int fan_write_cmd_watchdog(const char *cmd, int *rc)
7660{
7661 int interval;
7662
7663 if (sscanf(cmd, "watchdog %d", &interval) != 1)
7664 return 0;
7665
7666 if (interval < 0 || interval > 120)
7667 *rc = -EINVAL;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007668 else {
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007669 fan_watchdog_maxinterval = interval;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007670 tpacpi_disclose_usertask("procfs fan",
7671 "set watchdog timer to %d\n",
7672 interval);
7673 }
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007674
7675 return 1;
7676}
7677
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007678static int fan_write(char *buf)
7679{
7680 char *cmd;
7681 int rc = 0;
7682
7683 while (!rc && (cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007684 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007685 fan_write_cmd_level(cmd, &rc)) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007686 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007687 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007688 fan_write_cmd_disable(cmd, &rc) ||
7689 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007690 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007691 fan_write_cmd_speed(cmd, &rc))
7692 )
7693 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007694 else if (!rc)
7695 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007696 }
7697
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007698 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007699}
7700
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007701static struct ibm_struct fan_driver_data = {
7702 .name = "fan",
7703 .read = fan_read,
7704 .write = fan_write,
7705 .exit = fan_exit,
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007706 .suspend = fan_suspend,
7707 .resume = fan_resume,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007708};
7709
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007710/****************************************************************************
7711 ****************************************************************************
7712 *
7713 * Infrastructure
7714 *
7715 ****************************************************************************
7716 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07007717
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03007718/*
7719 * HKEY event callout for other subdrivers go here
7720 * (yes, it is ugly, but it is quick, safe, and gets the job done
7721 */
7722static void tpacpi_driver_event(const unsigned int hkey_event)
7723{
7724}
7725
7726
7727
7728static void hotkey_driver_event(const unsigned int scancode)
7729{
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03007730 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode);
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03007731}
7732
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007733/* sysfs name ---------------------------------------------------------- */
7734static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
7735 struct device_attribute *attr,
7736 char *buf)
7737{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007738 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007739}
7740
7741static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
7742 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
7743
7744/* --------------------------------------------------------------------- */
7745
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007746/* /proc support */
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03007747static struct proc_dir_entry *proc_dir;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007748
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007749/*
7750 * Module and infrastructure proble, init and exit handling
7751 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007752
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007753static int force_load;
7754
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007755#ifdef CONFIG_THINKPAD_ACPI_DEBUG
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007756static const char * __init str_supported(int is_supported)
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007757{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007758 static char text_unsupported[] __initdata = "not supported";
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007759
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007760 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007761}
7762#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
7763
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007764static void ibm_exit(struct ibm_struct *ibm)
7765{
7766 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
7767
7768 list_del_init(&ibm->all_drivers);
7769
7770 if (ibm->flags.acpi_notify_installed) {
7771 dbg_printk(TPACPI_DBG_EXIT,
7772 "%s: acpi_remove_notify_handler\n", ibm->name);
7773 BUG_ON(!ibm->acpi);
7774 acpi_remove_notify_handler(*ibm->acpi->handle,
7775 ibm->acpi->type,
7776 dispatch_acpi_notify);
7777 ibm->flags.acpi_notify_installed = 0;
7778 ibm->flags.acpi_notify_installed = 0;
7779 }
7780
7781 if (ibm->flags.proc_created) {
7782 dbg_printk(TPACPI_DBG_EXIT,
7783 "%s: remove_proc_entry\n", ibm->name);
7784 remove_proc_entry(ibm->name, proc_dir);
7785 ibm->flags.proc_created = 0;
7786 }
7787
7788 if (ibm->flags.acpi_driver_registered) {
7789 dbg_printk(TPACPI_DBG_EXIT,
7790 "%s: acpi_bus_unregister_driver\n", ibm->name);
7791 BUG_ON(!ibm->acpi);
7792 acpi_bus_unregister_driver(ibm->acpi->driver);
7793 kfree(ibm->acpi->driver);
7794 ibm->acpi->driver = NULL;
7795 ibm->flags.acpi_driver_registered = 0;
7796 }
7797
7798 if (ibm->flags.init_called && ibm->exit) {
7799 ibm->exit();
7800 ibm->flags.init_called = 0;
7801 }
7802
7803 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
7804}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007805
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007806static int __init ibm_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007807{
7808 int ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007809 struct ibm_struct *ibm = iibm->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007810 struct proc_dir_entry *entry;
7811
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007812 BUG_ON(ibm == NULL);
7813
7814 INIT_LIST_HEAD(&ibm->all_drivers);
7815
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03007816 if (ibm->flags.experimental && !experimental)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007817 return 0;
7818
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007819 dbg_printk(TPACPI_DBG_INIT,
7820 "probing for %s\n", ibm->name);
7821
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007822 if (iibm->init) {
7823 ret = iibm->init(iibm);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007824 if (ret > 0)
7825 return 0; /* probe failed */
7826 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007827 return ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007828
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03007829 ibm->flags.init_called = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007830 }
7831
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03007832 if (ibm->acpi) {
7833 if (ibm->acpi->hid) {
7834 ret = register_tpacpi_subdriver(ibm);
7835 if (ret)
7836 goto err_out;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007837 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03007838
7839 if (ibm->acpi->notify) {
7840 ret = setup_acpi_notify(ibm);
7841 if (ret == -ENODEV) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007842 printk(TPACPI_NOTICE "disabling subdriver %s\n",
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03007843 ibm->name);
7844 ret = 0;
7845 goto err_out;
7846 }
7847 if (ret < 0)
7848 goto err_out;
7849 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007850 }
7851
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007852 dbg_printk(TPACPI_DBG_INIT,
7853 "%s installed\n", ibm->name);
7854
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007855 if (ibm->read) {
7856 entry = create_proc_entry(ibm->name,
7857 S_IFREG | S_IRUGO | S_IWUSR,
7858 proc_dir);
7859 if (!entry) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007860 printk(TPACPI_ERR "unable to create proc entry %s\n",
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007861 ibm->name);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007862 ret = -ENODEV;
7863 goto err_out;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007864 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007865 entry->data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03007866 entry->read_proc = &dispatch_procfs_read;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007867 if (ibm->write)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03007868 entry->write_proc = &dispatch_procfs_write;
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03007869 ibm->flags.proc_created = 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007870 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007871
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007872 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
7873
Linus Torvalds1da177e2005-04-16 15:20:36 -07007874 return 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007875
7876err_out:
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007877 dbg_printk(TPACPI_DBG_INIT,
7878 "%s: at error exit path with result %d\n",
7879 ibm->name, ret);
7880
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007881 ibm_exit(ibm);
7882 return (ret < 0)? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007883}
7884
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007885/* Probing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007886
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03007887static bool __pure __init tpacpi_is_fw_digit(const char c)
7888{
7889 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
7890}
7891
7892/* Most models: xxyTkkWW (#.##c); Ancient 570/600 and -SL lacks (#.##c) */
7893static bool __pure __init tpacpi_is_valid_fw_id(const char* const s,
7894 const char t)
7895{
7896 return s && strlen(s) >= 8 &&
7897 tpacpi_is_fw_digit(s[0]) &&
7898 tpacpi_is_fw_digit(s[1]) &&
7899 s[2] == t && s[3] == 'T' &&
7900 tpacpi_is_fw_digit(s[4]) &&
7901 tpacpi_is_fw_digit(s[5]) &&
7902 s[6] == 'W' && s[7] == 'W';
7903}
7904
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007905/* returns 0 - probe ok, or < 0 - probe error.
7906 * Probe ok doesn't mean thinkpad found.
7907 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
7908static int __must_check __init get_thinkpad_model_data(
7909 struct thinkpad_id_data *tp)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02007910{
Jeff Garzik18552562007-10-03 15:15:40 -04007911 const struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02007912 char ec_fw_string[18];
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007913 char const *s;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02007914
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007915 if (!tp)
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007916 return -EINVAL;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007917
7918 memset(tp, 0, sizeof(*tp));
7919
7920 if (dmi_name_in_vendors("IBM"))
7921 tp->vendor = PCI_VENDOR_ID_IBM;
7922 else if (dmi_name_in_vendors("LENOVO"))
7923 tp->vendor = PCI_VENDOR_ID_LENOVO;
7924 else
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007925 return 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007926
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007927 s = dmi_get_system_info(DMI_BIOS_VERSION);
7928 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
7929 if (s && !tp->bios_version_str)
7930 return -ENOMEM;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03007931
7932 /* Really ancient ThinkPad 240X will fail this, which is fine */
7933 if (!tpacpi_is_valid_fw_id(tp->bios_version_str, 'E'))
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007934 return 0;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03007935
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007936 tp->bios_model = tp->bios_version_str[0]
7937 | (tp->bios_version_str[1] << 8);
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03007938 tp->bios_release = (tp->bios_version_str[4] << 8)
7939 | tp->bios_version_str[5];
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007940
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02007941 /*
7942 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
7943 * X32 or newer, all Z series; Some models must have an
7944 * up-to-date BIOS or they will not be detected.
7945 *
7946 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
7947 */
7948 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02007949 if (sscanf(dev->name,
7950 "IBM ThinkPad Embedded Controller -[%17c",
7951 ec_fw_string) == 1) {
7952 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
7953 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007954
7955 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007956 if (!tp->ec_version_str)
7957 return -ENOMEM;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03007958
7959 if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) {
7960 tp->ec_model = ec_fw_string[0]
7961 | (ec_fw_string[1] << 8);
7962 tp->ec_release = (ec_fw_string[4] << 8)
7963 | ec_fw_string[5];
7964 } else {
7965 printk(TPACPI_NOTICE
7966 "ThinkPad firmware release %s "
7967 "doesn't match the known patterns\n",
7968 ec_fw_string);
7969 printk(TPACPI_NOTICE
7970 "please report this to %s\n",
7971 TPACPI_MAIL);
7972 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007973 break;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02007974 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02007975 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007976
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007977 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
7978 if (s && !strnicmp(s, "ThinkPad", 8)) {
7979 tp->model_str = kstrdup(s, GFP_KERNEL);
7980 if (!tp->model_str)
7981 return -ENOMEM;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007982 }
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03007983
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007984 s = dmi_get_system_info(DMI_PRODUCT_NAME);
7985 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
7986 if (s && !tp->nummodel_str)
7987 return -ENOMEM;
7988
7989 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007990}
7991
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007992static int __init probe_for_thinkpad(void)
7993{
7994 int is_thinkpad;
7995
7996 if (acpi_disabled)
7997 return -ENODEV;
7998
7999 /*
8000 * Non-ancient models have better DMI tagging, but very old models
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03008001 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008002 */
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03008003 is_thinkpad = (thinkpad_id.model_str != NULL) ||
8004 (thinkpad_id.ec_model != 0) ||
8005 tpacpi_is_fw_known();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008006
8007 /* ec is required because many other handles are relative to it */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008008 TPACPI_ACPIHANDLE_INIT(ec);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008009 if (!ec_handle) {
8010 if (is_thinkpad)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008011 printk(TPACPI_ERR
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008012 "Not yet supported ThinkPad detected!\n");
8013 return -ENODEV;
8014 }
8015
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03008016 if (!is_thinkpad && !force_load)
8017 return -ENODEV;
8018
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008019 return 0;
8020}
8021
8022
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008023/* Module init, exit, parameters */
8024
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008025static struct ibm_init_struct ibms_init[] __initdata = {
8026 {
8027 .init = thinkpad_acpi_driver_init,
8028 .data = &thinkpad_acpi_driver_data,
8029 },
8030 {
8031 .init = hotkey_init,
8032 .data = &hotkey_driver_data,
8033 },
8034 {
8035 .init = bluetooth_init,
8036 .data = &bluetooth_driver_data,
8037 },
8038 {
8039 .init = wan_init,
8040 .data = &wan_driver_data,
8041 },
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02008042 {
8043 .init = uwb_init,
8044 .data = &uwb_driver_data,
8045 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02008046#ifdef CONFIG_THINKPAD_ACPI_VIDEO
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008047 {
8048 .init = video_init,
8049 .data = &video_driver_data,
8050 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02008051#endif
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008052 {
8053 .init = light_init,
8054 .data = &light_driver_data,
8055 },
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008056 {
8057 .init = cmos_init,
8058 .data = &cmos_driver_data,
8059 },
8060 {
8061 .init = led_init,
8062 .data = &led_driver_data,
8063 },
8064 {
8065 .init = beep_init,
8066 .data = &beep_driver_data,
8067 },
8068 {
8069 .init = thermal_init,
8070 .data = &thermal_driver_data,
8071 },
8072 {
8073 .data = &ecdump_driver_data,
8074 },
8075 {
8076 .init = brightness_init,
8077 .data = &brightness_driver_data,
8078 },
8079 {
8080 .data = &volume_driver_data,
8081 },
8082 {
8083 .init = fan_init,
8084 .data = &fan_driver_data,
8085 },
8086};
8087
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008088static int __init set_ibm_param(const char *val, struct kernel_param *kp)
8089{
8090 unsigned int i;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008091 struct ibm_struct *ibm;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008092
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02008093 if (!kp || !kp->name || !val)
8094 return -EINVAL;
8095
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008096 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
8097 ibm = ibms_init[i].data;
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02008098 WARN_ON(ibm == NULL);
8099
8100 if (!ibm || !ibm->name)
8101 continue;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008102
8103 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
8104 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008105 return -ENOSPC;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008106 strcpy(ibms_init[i].param, val);
8107 strcat(ibms_init[i].param, ",");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008108 return 0;
8109 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008110 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008111
8112 return -EINVAL;
8113}
8114
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008115module_param(experimental, int, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008116MODULE_PARM_DESC(experimental,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008117 "Enables experimental features when non-zero");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008118
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03008119module_param_named(debug, dbg_level, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008120MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03008121
Henrique de Moraes Holschuh86cc9442007-07-18 23:45:41 -03008122module_param(force_load, bool, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008123MODULE_PARM_DESC(force_load,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008124 "Attempts to load the driver even on a "
8125 "mis-identified ThinkPad when true");
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03008126
Henrique de Moraes Holschuh86cc9442007-07-18 23:45:41 -03008127module_param_named(fan_control, fan_control_allowed, bool, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008128MODULE_PARM_DESC(fan_control,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008129 "Enables setting fan parameters features when true");
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008130
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00008131module_param_named(brightness_mode, brightness_mode, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008132MODULE_PARM_DESC(brightness_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008133 "Selects brightness control strategy: "
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00008134 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03008135
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02008136module_param(brightness_enable, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008137MODULE_PARM_DESC(brightness_enable,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008138 "Enables backlight control when 1, disables when 0");
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02008139
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03008140module_param(hotkey_report_mode, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008141MODULE_PARM_DESC(hotkey_report_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008142 "used for backwards compatibility with userspace, "
8143 "see documentation");
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03008144
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008145#define TPACPI_PARAM(feature) \
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008146 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
Henrique de Moraes Holschuhcbb14842008-02-16 02:17:50 -02008147 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008148 "at module load, see documentation")
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008149
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008150TPACPI_PARAM(hotkey);
8151TPACPI_PARAM(bluetooth);
8152TPACPI_PARAM(video);
8153TPACPI_PARAM(light);
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008154TPACPI_PARAM(cmos);
8155TPACPI_PARAM(led);
8156TPACPI_PARAM(beep);
8157TPACPI_PARAM(ecdump);
8158TPACPI_PARAM(brightness);
8159TPACPI_PARAM(volume);
8160TPACPI_PARAM(fan);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008161
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02008162#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
8163module_param(dbg_wlswemul, uint, 0);
8164MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
8165module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
8166MODULE_PARM_DESC(wlsw_state,
8167 "Initial state of the emulated WLSW switch");
8168
8169module_param(dbg_bluetoothemul, uint, 0);
8170MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
8171module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
8172MODULE_PARM_DESC(bluetooth_state,
8173 "Initial state of the emulated bluetooth switch");
8174
8175module_param(dbg_wwanemul, uint, 0);
8176MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
8177module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
8178MODULE_PARM_DESC(wwan_state,
8179 "Initial state of the emulated WWAN switch");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02008180
8181module_param(dbg_uwbemul, uint, 0);
8182MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
8183module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
8184MODULE_PARM_DESC(uwb_state,
8185 "Initial state of the emulated UWB switch");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02008186#endif
8187
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008188static void thinkpad_acpi_module_exit(void)
8189{
8190 struct ibm_struct *ibm, *itmp;
8191
8192 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
8193
8194 list_for_each_entry_safe_reverse(ibm, itmp,
8195 &tpacpi_all_drivers,
8196 all_drivers) {
8197 ibm_exit(ibm);
8198 }
8199
8200 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
8201
8202 if (tpacpi_inputdev) {
8203 if (tp_features.input_device_registered)
8204 input_unregister_device(tpacpi_inputdev);
8205 else
8206 input_free_device(tpacpi_inputdev);
8207 }
8208
8209 if (tpacpi_hwmon)
8210 hwmon_device_unregister(tpacpi_hwmon);
8211
8212 if (tp_features.sensors_pdev_attrs_registered)
8213 device_remove_file(&tpacpi_sensors_pdev->dev,
8214 &dev_attr_thinkpad_acpi_pdev_name);
8215 if (tpacpi_sensors_pdev)
8216 platform_device_unregister(tpacpi_sensors_pdev);
8217 if (tpacpi_pdev)
8218 platform_device_unregister(tpacpi_pdev);
8219
8220 if (tp_features.sensors_pdrv_attrs_registered)
8221 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
8222 if (tp_features.platform_drv_attrs_registered)
8223 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
8224
8225 if (tp_features.sensors_pdrv_registered)
8226 platform_driver_unregister(&tpacpi_hwmon_pdriver);
8227
8228 if (tp_features.platform_drv_registered)
8229 platform_driver_unregister(&tpacpi_pdriver);
8230
8231 if (proc_dir)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008232 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008233
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03008234 if (tpacpi_wq)
8235 destroy_workqueue(tpacpi_wq);
8236
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008237 kfree(thinkpad_id.bios_version_str);
8238 kfree(thinkpad_id.ec_version_str);
8239 kfree(thinkpad_id.model_str);
8240}
8241
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02008242
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03008243static int __init thinkpad_acpi_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008244{
8245 int ret, i;
8246
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03008247 tpacpi_lifecycle = TPACPI_LIFE_INIT;
8248
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03008249 /* Parameter checking */
8250 if (hotkey_report_mode > 2)
8251 return -EINVAL;
8252
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008253 /* Driver-level probe */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008254
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008255 ret = get_thinkpad_model_data(&thinkpad_id);
8256 if (ret) {
8257 printk(TPACPI_ERR
8258 "unable to get DMI data: %d\n", ret);
8259 thinkpad_acpi_module_exit();
8260 return ret;
8261 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008262 ret = probe_for_thinkpad();
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008263 if (ret) {
8264 thinkpad_acpi_module_exit();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008265 return ret;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008266 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008267
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008268 /* Driver initialization */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008269
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008270 TPACPI_ACPIHANDLE_INIT(ecrd);
8271 TPACPI_ACPIHANDLE_INIT(ecwr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008272
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03008273 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
8274 if (!tpacpi_wq) {
8275 thinkpad_acpi_module_exit();
8276 return -ENOMEM;
8277 }
8278
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008279 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008280 if (!proc_dir) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008281 printk(TPACPI_ERR
8282 "unable to create proc dir " TPACPI_PROC_DIR);
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03008283 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008284 return -ENODEV;
8285 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008286
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008287 ret = platform_driver_register(&tpacpi_pdriver);
8288 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008289 printk(TPACPI_ERR
8290 "unable to register main platform driver\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008291 thinkpad_acpi_module_exit();
8292 return ret;
8293 }
Henrique de Moraes Holschuhac363932007-07-27 17:04:40 -03008294 tp_features.platform_drv_registered = 1;
8295
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008296 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
8297 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008298 printk(TPACPI_ERR
8299 "unable to register hwmon platform driver\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008300 thinkpad_acpi_module_exit();
8301 return ret;
8302 }
8303 tp_features.sensors_pdrv_registered = 1;
8304
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03008305 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03008306 if (!ret) {
8307 tp_features.platform_drv_attrs_registered = 1;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008308 ret = tpacpi_create_driver_attributes(
8309 &tpacpi_hwmon_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03008310 }
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03008311 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008312 printk(TPACPI_ERR
8313 "unable to create sysfs driver attributes\n");
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03008314 thinkpad_acpi_module_exit();
8315 return ret;
8316 }
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03008317 tp_features.sensors_pdrv_attrs_registered = 1;
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03008318
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008319
8320 /* Device initialization */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008321 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008322 NULL, 0);
8323 if (IS_ERR(tpacpi_pdev)) {
8324 ret = PTR_ERR(tpacpi_pdev);
8325 tpacpi_pdev = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008326 printk(TPACPI_ERR "unable to register platform device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008327 thinkpad_acpi_module_exit();
8328 return ret;
8329 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008330 tpacpi_sensors_pdev = platform_device_register_simple(
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008331 TPACPI_HWMON_DRVR_NAME,
8332 -1, NULL, 0);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008333 if (IS_ERR(tpacpi_sensors_pdev)) {
8334 ret = PTR_ERR(tpacpi_sensors_pdev);
8335 tpacpi_sensors_pdev = NULL;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008336 printk(TPACPI_ERR
8337 "unable to register hwmon platform device\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008338 thinkpad_acpi_module_exit();
8339 return ret;
8340 }
8341 ret = device_create_file(&tpacpi_sensors_pdev->dev,
8342 &dev_attr_thinkpad_acpi_pdev_name);
8343 if (ret) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008344 printk(TPACPI_ERR
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008345 "unable to create sysfs hwmon device attributes\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008346 thinkpad_acpi_module_exit();
8347 return ret;
8348 }
8349 tp_features.sensors_pdev_attrs_registered = 1;
8350 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008351 if (IS_ERR(tpacpi_hwmon)) {
8352 ret = PTR_ERR(tpacpi_hwmon);
8353 tpacpi_hwmon = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008354 printk(TPACPI_ERR "unable to register hwmon device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008355 thinkpad_acpi_module_exit();
8356 return ret;
8357 }
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -03008358 mutex_init(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03008359 tpacpi_inputdev = input_allocate_device();
8360 if (!tpacpi_inputdev) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008361 printk(TPACPI_ERR "unable to allocate input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03008362 thinkpad_acpi_module_exit();
8363 return -ENOMEM;
8364 } else {
8365 /* Prepare input device, but don't register */
8366 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008367 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03008368 tpacpi_inputdev->id.bustype = BUS_HOST;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03008369 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
8370 thinkpad_id.vendor :
8371 PCI_VENDOR_ID_IBM;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03008372 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
8373 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
8374 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008375 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
8376 ret = ibm_init(&ibms_init[i]);
8377 if (ret >= 0 && *ibms_init[i].param)
8378 ret = ibms_init[i].data->write(ibms_init[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008379 if (ret < 0) {
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03008380 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008381 return ret;
8382 }
8383 }
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03008384 ret = input_register_device(tpacpi_inputdev);
8385 if (ret < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008386 printk(TPACPI_ERR "unable to register input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03008387 thinkpad_acpi_module_exit();
8388 return ret;
8389 } else {
8390 tp_features.input_device_registered = 1;
8391 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008392
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03008393 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008394 return 0;
8395}
8396
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03008397MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
8398
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008399/*
Henrique de Moraes Holschuh922fe092009-04-14 02:44:13 +00008400 * This will autoload the driver in almost every ThinkPad
8401 * in widespread use.
8402 *
8403 * Only _VERY_ old models, like the 240, 240x and 570 lack
8404 * the HKEY event interface.
8405 */
8406MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
8407
8408/*
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008409 * DMI matching for module autoloading
8410 *
8411 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
8412 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
8413 *
8414 * Only models listed in thinkwiki will be supported, so add yours
8415 * if it is not there yet.
8416 */
8417#define IBM_BIOS_MODULE_ALIAS(__type) \
Mathieu Chouquet-Stringerb36a50f2009-03-14 16:35:26 +01008418 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008419
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008420/* Ancient thinkpad BIOSes have to be identified by
8421 * BIOS type or model number, and there are far less
8422 * BIOS types than model numbers... */
Henrique de Moraes Holschuh922fe092009-04-14 02:44:13 +00008423IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008424
Henrique de Moraes Holschuhf68f53a2009-04-14 02:44:12 +00008425MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
8426MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008427MODULE_DESCRIPTION(TPACPI_DESC);
8428MODULE_VERSION(TPACPI_VERSION);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008429MODULE_LICENSE("GPL");
8430
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03008431module_init(thinkpad_acpi_module_init);
8432module_exit(thinkpad_acpi_module_exit);