blob: 3910f2f3eada1af94ce24e05c8b106da4bb2701a [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
Henrique de Moraes Holschuh32afbf02007-10-08 10:12:56 -03001009 while (*buf && isspace(*buf))
1010 buf++;
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001011 *value = simple_strtoul(buf, &endp, 0);
1012 while (*endp && isspace(*endp))
1013 endp++;
1014 if (*endp || *value > max)
1015 return -EINVAL;
1016
1017 return 0;
1018}
1019
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03001020static void tpacpi_disable_brightness_delay(void)
1021{
1022 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
1023 printk(TPACPI_NOTICE
1024 "ACPI backlight control delay disabled\n");
1025}
1026
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03001027static int __init tpacpi_query_bcl_levels(acpi_handle handle)
1028{
1029 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1030 union acpi_object *obj;
1031 int rc;
1032
1033 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
1034 obj = (union acpi_object *)buffer.pointer;
1035 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
1036 printk(TPACPI_ERR "Unknown _BCL data, "
1037 "please report this to %s\n", TPACPI_MAIL);
1038 rc = 0;
1039 } else {
1040 rc = obj->package.count;
1041 }
1042 } else {
1043 return 0;
1044 }
1045
1046 kfree(buffer.pointer);
1047 return rc;
1048}
1049
1050static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
1051 u32 lvl, void *context, void **rv)
1052{
1053 char name[ACPI_PATH_SEGMENT_LENGTH];
1054 struct acpi_buffer buffer = { sizeof(name), &name };
1055
1056 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
1057 !strncmp("_BCL", name, sizeof(name) - 1)) {
1058 BUG_ON(!rv || !*rv);
1059 **(int **)rv = tpacpi_query_bcl_levels(handle);
1060 return AE_CTRL_TERMINATE;
1061 } else {
1062 return AE_OK;
1063 }
1064}
1065
1066/*
1067 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
1068 */
1069static int __init tpacpi_check_std_acpi_brightness_support(void)
1070{
1071 int status;
1072 int bcl_levels = 0;
1073 void *bcl_ptr = &bcl_levels;
1074
1075 if (!vid_handle) {
1076 TPACPI_ACPIHANDLE_INIT(vid);
1077 }
1078 if (!vid_handle)
1079 return 0;
1080
1081 /*
1082 * Search for a _BCL method, and execute it. This is safe on all
1083 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
1084 * BIOS in ACPI backlight control mode. We do NOT have to care
1085 * about calling the _BCL method in an enabled video device, any
1086 * will do for our purposes.
1087 */
1088
1089 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
1090 tpacpi_acpi_walk_find_bcl, NULL,
1091 &bcl_ptr);
1092
1093 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
1094 tp_features.bright_acpimode = 1;
1095 return (bcl_levels - 2);
1096 }
1097
1098 return 0;
1099}
1100
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00001101static void printk_deprecated_attribute(const char * const what,
1102 const char * const details)
1103{
1104 tpacpi_log_usertask("deprecated sysfs attribute");
1105 printk(TPACPI_WARN "WARNING: sysfs attribute %s is deprecated and "
1106 "will be removed. %s\n",
1107 what, details);
1108}
1109
Johannes Berg19d337d2009-06-02 13:01:37 +02001110/*************************************************************************
1111 * rfkill and radio control support helpers
1112 */
1113
1114/*
1115 * ThinkPad-ACPI firmware handling model:
1116 *
1117 * WLSW (master wireless switch) is event-driven, and is common to all
1118 * firmware-controlled radios. It cannot be controlled, just monitored,
1119 * as expected. It overrides all radio state in firmware
1120 *
1121 * The kernel, a masked-off hotkey, and WLSW can change the radio state
1122 * (TODO: verify how WLSW interacts with the returned radio state).
1123 *
1124 * The only time there are shadow radio state changes, is when
1125 * masked-off hotkeys are used.
1126 */
1127
1128/*
1129 * Internal driver API for radio state:
1130 *
1131 * int: < 0 = error, otherwise enum tpacpi_rfkill_state
1132 * bool: true means radio blocked (off)
1133 */
1134enum tpacpi_rfkill_state {
1135 TPACPI_RFK_RADIO_OFF = 0,
1136 TPACPI_RFK_RADIO_ON
1137};
1138
1139/* rfkill switches */
1140enum tpacpi_rfk_id {
1141 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
1142 TPACPI_RFK_WWAN_SW_ID,
1143 TPACPI_RFK_UWB_SW_ID,
1144 TPACPI_RFK_SW_MAX
1145};
1146
1147static const char *tpacpi_rfkill_names[] = {
1148 [TPACPI_RFK_BLUETOOTH_SW_ID] = "bluetooth",
1149 [TPACPI_RFK_WWAN_SW_ID] = "wwan",
1150 [TPACPI_RFK_UWB_SW_ID] = "uwb",
1151 [TPACPI_RFK_SW_MAX] = NULL
1152};
1153
1154/* ThinkPad-ACPI rfkill subdriver */
1155struct tpacpi_rfk {
1156 struct rfkill *rfkill;
1157 enum tpacpi_rfk_id id;
1158 const struct tpacpi_rfk_ops *ops;
1159};
1160
1161struct tpacpi_rfk_ops {
1162 /* firmware interface */
1163 int (*get_status)(void);
1164 int (*set_status)(const enum tpacpi_rfkill_state);
1165};
1166
1167static struct tpacpi_rfk *tpacpi_rfkill_switches[TPACPI_RFK_SW_MAX];
1168
1169/* Query FW and update rfkill sw state for a given rfkill switch */
1170static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk *tp_rfk)
1171{
1172 int status;
1173
1174 if (!tp_rfk)
1175 return -ENODEV;
1176
1177 status = (tp_rfk->ops->get_status)();
1178 if (status < 0)
1179 return status;
1180
1181 rfkill_set_sw_state(tp_rfk->rfkill,
1182 (status == TPACPI_RFK_RADIO_OFF));
1183
1184 return status;
1185}
1186
1187/* Query FW and update rfkill sw state for all rfkill switches */
1188static void tpacpi_rfk_update_swstate_all(void)
1189{
1190 unsigned int i;
1191
1192 for (i = 0; i < TPACPI_RFK_SW_MAX; i++)
1193 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[i]);
1194}
1195
1196/*
1197 * Sync the HW-blocking state of all rfkill switches,
1198 * do notice it causes the rfkill core to schedule uevents
1199 */
1200static void tpacpi_rfk_update_hwblock_state(bool blocked)
1201{
1202 unsigned int i;
1203 struct tpacpi_rfk *tp_rfk;
1204
1205 for (i = 0; i < TPACPI_RFK_SW_MAX; i++) {
1206 tp_rfk = tpacpi_rfkill_switches[i];
1207 if (tp_rfk) {
1208 if (rfkill_set_hw_state(tp_rfk->rfkill,
1209 blocked)) {
1210 /* ignore -- we track sw block */
1211 }
1212 }
1213 }
1214}
1215
1216/* Call to get the WLSW state from the firmware */
1217static int hotkey_get_wlsw(void);
1218
1219/* Call to query WLSW state and update all rfkill switches */
1220static bool tpacpi_rfk_check_hwblock_state(void)
1221{
1222 int res = hotkey_get_wlsw();
1223 int hw_blocked;
1224
1225 /* When unknown or unsupported, we have to assume it is unblocked */
1226 if (res < 0)
1227 return false;
1228
1229 hw_blocked = (res == TPACPI_RFK_RADIO_OFF);
1230 tpacpi_rfk_update_hwblock_state(hw_blocked);
1231
1232 return hw_blocked;
1233}
1234
1235static int tpacpi_rfk_hook_set_block(void *data, bool blocked)
1236{
1237 struct tpacpi_rfk *tp_rfk = data;
1238 int res;
1239
1240 dbg_printk(TPACPI_DBG_RFKILL,
1241 "request to change radio state to %s\n",
1242 blocked ? "blocked" : "unblocked");
1243
1244 /* try to set radio state */
1245 res = (tp_rfk->ops->set_status)(blocked ?
1246 TPACPI_RFK_RADIO_OFF : TPACPI_RFK_RADIO_ON);
1247
1248 /* and update the rfkill core with whatever the FW really did */
1249 tpacpi_rfk_update_swstate(tp_rfk);
1250
1251 return (res < 0) ? res : 0;
1252}
1253
1254static const struct rfkill_ops tpacpi_rfk_rfkill_ops = {
1255 .set_block = tpacpi_rfk_hook_set_block,
1256};
1257
1258static int __init tpacpi_new_rfkill(const enum tpacpi_rfk_id id,
1259 const struct tpacpi_rfk_ops *tp_rfkops,
1260 const enum rfkill_type rfktype,
1261 const char *name,
1262 const bool set_default)
1263{
1264 struct tpacpi_rfk *atp_rfk;
1265 int res;
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001266 bool sw_state = false;
1267 int sw_status;
Johannes Berg19d337d2009-06-02 13:01:37 +02001268
1269 BUG_ON(id >= TPACPI_RFK_SW_MAX || tpacpi_rfkill_switches[id]);
1270
Johannes Berg19d337d2009-06-02 13:01:37 +02001271 atp_rfk = kzalloc(sizeof(struct tpacpi_rfk), GFP_KERNEL);
1272 if (atp_rfk)
1273 atp_rfk->rfkill = rfkill_alloc(name,
1274 &tpacpi_pdev->dev,
1275 rfktype,
1276 &tpacpi_rfk_rfkill_ops,
1277 atp_rfk);
1278 if (!atp_rfk || !atp_rfk->rfkill) {
1279 printk(TPACPI_ERR
1280 "failed to allocate memory for rfkill class\n");
1281 kfree(atp_rfk);
1282 return -ENOMEM;
1283 }
1284
1285 atp_rfk->id = id;
1286 atp_rfk->ops = tp_rfkops;
1287
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001288 sw_status = (tp_rfkops->get_status)();
1289 if (sw_status < 0) {
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001290 printk(TPACPI_ERR
1291 "failed to read initial state for %s, error %d\n",
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001292 name, sw_status);
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001293 } else {
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001294 sw_state = (sw_status == TPACPI_RFK_RADIO_OFF);
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001295 if (set_default) {
1296 /* try to keep the initial state, since we ask the
1297 * firmware to preserve it across S5 in NVRAM */
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001298 rfkill_init_sw_state(atp_rfk->rfkill, sw_state);
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001299 }
1300 }
1301 rfkill_set_hw_state(atp_rfk->rfkill, tpacpi_rfk_check_hwblock_state());
Johannes Berg19d337d2009-06-02 13:01:37 +02001302
1303 res = rfkill_register(atp_rfk->rfkill);
1304 if (res < 0) {
1305 printk(TPACPI_ERR
1306 "failed to register %s rfkill switch: %d\n",
1307 name, res);
1308 rfkill_destroy(atp_rfk->rfkill);
1309 kfree(atp_rfk);
1310 return res;
1311 }
1312
1313 tpacpi_rfkill_switches[id] = atp_rfk;
1314 return 0;
1315}
1316
1317static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id)
1318{
1319 struct tpacpi_rfk *tp_rfk;
1320
1321 BUG_ON(id >= TPACPI_RFK_SW_MAX);
1322
1323 tp_rfk = tpacpi_rfkill_switches[id];
1324 if (tp_rfk) {
1325 rfkill_unregister(tp_rfk->rfkill);
Corentin Chary5f0dadb2009-09-14 12:43:52 +02001326 rfkill_destroy(tp_rfk->rfkill);
Johannes Berg19d337d2009-06-02 13:01:37 +02001327 tpacpi_rfkill_switches[id] = NULL;
1328 kfree(tp_rfk);
1329 }
1330}
1331
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00001332static void printk_deprecated_rfkill_attribute(const char * const what)
1333{
1334 printk_deprecated_attribute(what,
1335 "Please switch to generic rfkill before year 2010");
1336}
1337
Johannes Berg19d337d2009-06-02 13:01:37 +02001338/* sysfs <radio> enable ------------------------------------------------ */
1339static ssize_t tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id,
1340 struct device_attribute *attr,
1341 char *buf)
1342{
1343 int status;
1344
1345 printk_deprecated_rfkill_attribute(attr->attr.name);
1346
1347 /* This is in the ABI... */
1348 if (tpacpi_rfk_check_hwblock_state()) {
1349 status = TPACPI_RFK_RADIO_OFF;
1350 } else {
1351 status = tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1352 if (status < 0)
1353 return status;
1354 }
1355
1356 return snprintf(buf, PAGE_SIZE, "%d\n",
1357 (status == TPACPI_RFK_RADIO_ON) ? 1 : 0);
1358}
1359
1360static ssize_t tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id,
1361 struct device_attribute *attr,
1362 const char *buf, size_t count)
1363{
1364 unsigned long t;
1365 int res;
1366
1367 printk_deprecated_rfkill_attribute(attr->attr.name);
1368
1369 if (parse_strtoul(buf, 1, &t))
1370 return -EINVAL;
1371
1372 tpacpi_disclose_usertask(attr->attr.name, "set to %ld\n", t);
1373
1374 /* This is in the ABI... */
1375 if (tpacpi_rfk_check_hwblock_state() && !!t)
1376 return -EPERM;
1377
1378 res = tpacpi_rfkill_switches[id]->ops->set_status((!!t) ?
1379 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF);
1380 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1381
1382 return (res < 0) ? res : count;
1383}
1384
1385/* procfs -------------------------------------------------------------- */
1386static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id, char *p)
1387{
1388 int len = 0;
1389
1390 if (id >= TPACPI_RFK_SW_MAX)
1391 len += sprintf(p + len, "status:\t\tnot supported\n");
1392 else {
1393 int status;
1394
1395 /* This is in the ABI... */
1396 if (tpacpi_rfk_check_hwblock_state()) {
1397 status = TPACPI_RFK_RADIO_OFF;
1398 } else {
1399 status = tpacpi_rfk_update_swstate(
1400 tpacpi_rfkill_switches[id]);
1401 if (status < 0)
1402 return status;
1403 }
1404
1405 len += sprintf(p + len, "status:\t\t%s\n",
1406 (status == TPACPI_RFK_RADIO_ON) ?
1407 "enabled" : "disabled");
1408 len += sprintf(p + len, "commands:\tenable, disable\n");
1409 }
1410
1411 return len;
1412}
1413
1414static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id, char *buf)
1415{
1416 char *cmd;
1417 int status = -1;
1418 int res = 0;
1419
1420 if (id >= TPACPI_RFK_SW_MAX)
1421 return -ENODEV;
1422
1423 while ((cmd = next_cmd(&buf))) {
1424 if (strlencmp(cmd, "enable") == 0)
1425 status = TPACPI_RFK_RADIO_ON;
1426 else if (strlencmp(cmd, "disable") == 0)
1427 status = TPACPI_RFK_RADIO_OFF;
1428 else
1429 return -EINVAL;
1430 }
1431
1432 if (status != -1) {
1433 tpacpi_disclose_usertask("procfs", "attempt to %s %s\n",
1434 (status == TPACPI_RFK_RADIO_ON) ?
1435 "enable" : "disable",
1436 tpacpi_rfkill_names[id]);
1437 res = (tpacpi_rfkill_switches[id]->ops->set_status)(status);
1438 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1439 }
1440
1441 return res;
1442}
1443
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001444/*************************************************************************
1445 * thinkpad-acpi driver attributes
1446 */
1447
1448/* interface_version --------------------------------------------------- */
1449static ssize_t tpacpi_driver_interface_version_show(
1450 struct device_driver *drv,
1451 char *buf)
1452{
1453 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1454}
1455
1456static DRIVER_ATTR(interface_version, S_IRUGO,
1457 tpacpi_driver_interface_version_show, NULL);
1458
1459/* debug_level --------------------------------------------------------- */
1460static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1461 char *buf)
1462{
1463 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1464}
1465
1466static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1467 const char *buf, size_t count)
1468{
1469 unsigned long t;
1470
1471 if (parse_strtoul(buf, 0xffff, &t))
1472 return -EINVAL;
1473
1474 dbg_level = t;
1475
1476 return count;
1477}
1478
1479static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1480 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1481
1482/* version ------------------------------------------------------------- */
1483static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1484 char *buf)
1485{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001486 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1487 TPACPI_DESC, TPACPI_VERSION);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001488}
1489
1490static DRIVER_ATTR(version, S_IRUGO,
1491 tpacpi_driver_version_show, NULL);
1492
1493/* --------------------------------------------------------------------- */
1494
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001495#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1496
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001497/* wlsw_emulstate ------------------------------------------------------ */
1498static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1499 char *buf)
1500{
1501 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1502}
1503
1504static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1505 const char *buf, size_t count)
1506{
1507 unsigned long t;
1508
1509 if (parse_strtoul(buf, 1, &t))
1510 return -EINVAL;
1511
Johannes Berg19d337d2009-06-02 13:01:37 +02001512 if (tpacpi_wlsw_emulstate != !!t) {
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001513 tpacpi_wlsw_emulstate = !!t;
Johannes Berg19d337d2009-06-02 13:01:37 +02001514 tpacpi_rfk_update_hwblock_state(!t); /* negative logic */
1515 }
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001516
1517 return count;
1518}
1519
1520static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1521 tpacpi_driver_wlsw_emulstate_show,
1522 tpacpi_driver_wlsw_emulstate_store);
1523
1524/* bluetooth_emulstate ------------------------------------------------- */
1525static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1526 struct device_driver *drv,
1527 char *buf)
1528{
1529 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1530}
1531
1532static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1533 struct device_driver *drv,
1534 const char *buf, size_t count)
1535{
1536 unsigned long t;
1537
1538 if (parse_strtoul(buf, 1, &t))
1539 return -EINVAL;
1540
1541 tpacpi_bluetooth_emulstate = !!t;
1542
1543 return count;
1544}
1545
1546static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1547 tpacpi_driver_bluetooth_emulstate_show,
1548 tpacpi_driver_bluetooth_emulstate_store);
1549
1550/* wwan_emulstate ------------------------------------------------- */
1551static ssize_t tpacpi_driver_wwan_emulstate_show(
1552 struct device_driver *drv,
1553 char *buf)
1554{
1555 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1556}
1557
1558static ssize_t tpacpi_driver_wwan_emulstate_store(
1559 struct device_driver *drv,
1560 const char *buf, size_t count)
1561{
1562 unsigned long t;
1563
1564 if (parse_strtoul(buf, 1, &t))
1565 return -EINVAL;
1566
1567 tpacpi_wwan_emulstate = !!t;
1568
1569 return count;
1570}
1571
1572static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1573 tpacpi_driver_wwan_emulstate_show,
1574 tpacpi_driver_wwan_emulstate_store);
1575
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001576/* uwb_emulstate ------------------------------------------------- */
1577static ssize_t tpacpi_driver_uwb_emulstate_show(
1578 struct device_driver *drv,
1579 char *buf)
1580{
1581 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1582}
1583
1584static ssize_t tpacpi_driver_uwb_emulstate_store(
1585 struct device_driver *drv,
1586 const char *buf, size_t count)
1587{
1588 unsigned long t;
1589
1590 if (parse_strtoul(buf, 1, &t))
1591 return -EINVAL;
1592
1593 tpacpi_uwb_emulstate = !!t;
1594
1595 return count;
1596}
1597
1598static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
1599 tpacpi_driver_uwb_emulstate_show,
1600 tpacpi_driver_uwb_emulstate_store);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001601#endif
1602
1603/* --------------------------------------------------------------------- */
1604
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001605static struct driver_attribute *tpacpi_driver_attributes[] = {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001606 &driver_attr_debug_level, &driver_attr_version,
1607 &driver_attr_interface_version,
1608};
1609
1610static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1611{
1612 int i, res;
1613
1614 i = 0;
1615 res = 0;
1616 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1617 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1618 i++;
1619 }
1620
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001621#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1622 if (!res && dbg_wlswemul)
1623 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1624 if (!res && dbg_bluetoothemul)
1625 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1626 if (!res && dbg_wwanemul)
1627 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001628 if (!res && dbg_uwbemul)
1629 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001630#endif
1631
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001632 return res;
1633}
1634
1635static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1636{
1637 int i;
1638
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001639 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001640 driver_remove_file(drv, tpacpi_driver_attributes[i]);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001641
1642#ifdef THINKPAD_ACPI_DEBUGFACILITIES
1643 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1644 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1645 driver_remove_file(drv, &driver_attr_wwan_emulstate);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001646 driver_remove_file(drv, &driver_attr_uwb_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001647#endif
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001648}
1649
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001650/*************************************************************************
1651 * Firmware Data
1652 */
1653
1654/*
1655 * Table of recommended minimum BIOS versions
1656 *
1657 * Reasons for listing:
1658 * 1. Stable BIOS, listed because the unknown ammount of
1659 * bugs and bad ACPI behaviour on older versions
1660 *
1661 * 2. BIOS or EC fw with known bugs that trigger on Linux
1662 *
1663 * 3. BIOS with known reduced functionality in older versions
1664 *
1665 * We recommend the latest BIOS and EC version.
1666 * We only support the latest BIOS and EC fw version as a rule.
1667 *
1668 * Sources: IBM ThinkPad Public Web Documents (update changelogs),
1669 * Information from users in ThinkWiki
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03001670 *
1671 * WARNING: we use this table also to detect that the machine is
1672 * a ThinkPad in some cases, so don't remove entries lightly.
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001673 */
1674
1675#define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1676 { .vendor = (__v), \
1677 .bios = TPID(__id1, __id2), \
1678 .ec = TPACPI_MATCH_ANY, \
1679 .quirks = TPACPI_MATCH_ANY << 16 \
1680 | (__bv1) << 8 | (__bv2) }
1681
1682#define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
1683 __eid1, __eid2, __ev1, __ev2) \
1684 { .vendor = (__v), \
1685 .bios = TPID(__bid1, __bid2), \
1686 .ec = TPID(__eid1, __eid2), \
1687 .quirks = (__ev1) << 24 | (__ev2) << 16 \
1688 | (__bv1) << 8 | (__bv2) }
1689
1690#define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1691 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1692
1693#define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1694 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1695 __bv1, __bv2, __id1, __id2, __ev1, __ev2)
1696
1697#define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1698 __eid1, __eid2, __ev1, __ev2) \
1699 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1700 __bv1, __bv2, __eid1, __eid2, __ev1, __ev2)
1701
1702#define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1703 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1704
1705#define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1706 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
1707 __bv1, __bv2, __id1, __id2, __ev1, __ev2)
1708
1709#define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1710 __eid1, __eid2, __ev1, __ev2) \
1711 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
1712 __bv1, __bv2, __eid1, __eid2, __ev1, __ev2)
1713
1714static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
1715 /* Numeric models ------------------ */
1716 /* FW MODEL BIOS VERS */
1717 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1718 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1719 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1720 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1721 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1722 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1723 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1724 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1725 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1726
1727 /* A-series ------------------------- */
1728 /* FW MODEL BIOS VERS EC VERS */
1729 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1730 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1731 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1732 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1733 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1734 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1735 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1736 TPV_QI0('1', '3', '2', '0'), /* A22m */
1737 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1738 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1739 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1740
1741 /* G-series ------------------------- */
1742 /* FW MODEL BIOS VERS */
1743 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1744 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1745
1746 /* R-series, T-series --------------- */
1747 /* FW MODEL BIOS VERS EC VERS */
1748 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1749 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1750 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1751 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1752 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1753 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1754 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1755 T40/p, T41/p, T42/p (1) */
1756 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1757 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1758 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1759 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1760
1761 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1762 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1763 TPV_QI0('1', '6', '3', '2'), /* T22 */
1764 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1765 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1766 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1767
1768 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1769 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
1770 TPV_QL0('7', 'E', 'D', '0'), /* R60e, R60i */
1771
1772 /* BIOS FW BIOS VERS EC FW EC VERS */
1773 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1774 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1775
1776 /* X-series ------------------------- */
1777 /* FW MODEL BIOS VERS EC VERS */
1778 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1779 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1780 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1781 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1782 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1783 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1784 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1785
1786 TPV_QL0('7', 'B', 'D', '7'), /* X60/s */
1787 TPV_QL0('7', 'J', '3', '0'), /* X60t */
1788
1789 /* (0) - older versions lack DMI EC fw string and functionality */
1790 /* (1) - older versions known to lack functionality */
1791};
1792
1793#undef TPV_QL1
1794#undef TPV_QL0
1795#undef TPV_QI2
1796#undef TPV_QI1
1797#undef TPV_QI0
1798#undef TPV_Q_X
1799#undef TPV_Q
1800
1801static void __init tpacpi_check_outdated_fw(void)
1802{
1803 unsigned long fwvers;
1804 u16 ec_version, bios_version;
1805
1806 fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
1807 ARRAY_SIZE(tpacpi_bios_version_qtable));
1808
1809 if (!fwvers)
1810 return;
1811
1812 bios_version = fwvers & 0xffffU;
1813 ec_version = (fwvers >> 16) & 0xffffU;
1814
1815 /* note that unknown versions are set to 0x0000 and we use that */
1816 if ((bios_version > thinkpad_id.bios_release) ||
1817 (ec_version > thinkpad_id.ec_release &&
1818 ec_version != TPACPI_MATCH_ANY)) {
1819 /*
1820 * The changelogs would let us track down the exact
1821 * reason, but it is just too much of a pain to track
1822 * it. We only list BIOSes that are either really
1823 * broken, or really stable to begin with, so it is
1824 * best if the user upgrades the firmware anyway.
1825 */
1826 printk(TPACPI_WARN
1827 "WARNING: Outdated ThinkPad BIOS/EC firmware\n");
1828 printk(TPACPI_WARN
1829 "WARNING: This firmware may be missing critical bug "
1830 "fixes and/or important features\n");
1831 }
1832}
1833
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03001834static bool __init tpacpi_is_fw_known(void)
1835{
1836 return tpacpi_check_quirks(tpacpi_bios_version_qtable,
1837 ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
1838}
1839
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03001840/****************************************************************************
1841 ****************************************************************************
1842 *
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001843 * Subdrivers
1844 *
1845 ****************************************************************************
1846 ****************************************************************************/
1847
1848/*************************************************************************
Henrique de Moraes Holschuh142cfc92007-04-21 11:08:26 -03001849 * thinkpad-acpi init subdriver
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001850 */
1851
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001852static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001854 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
1855 printk(TPACPI_INFO "%s\n", TPACPI_URL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001857 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03001858 (thinkpad_id.bios_version_str) ?
1859 thinkpad_id.bios_version_str : "unknown",
1860 (thinkpad_id.ec_version_str) ?
1861 thinkpad_id.ec_version_str : "unknown");
1862
1863 if (thinkpad_id.vendor && thinkpad_id.model_str)
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03001864 printk(TPACPI_INFO "%s %s, model %s\n",
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03001865 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
1866 "IBM" : ((thinkpad_id.vendor ==
1867 PCI_VENDOR_ID_LENOVO) ?
1868 "Lenovo" : "Unknown vendor"),
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03001869 thinkpad_id.model_str,
1870 (thinkpad_id.nummodel_str) ?
1871 thinkpad_id.nummodel_str : "unknown");
Henrique de Moraes Holschuh3945ac32007-02-06 19:13:44 -02001872
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001873 tpacpi_check_outdated_fw();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 return 0;
1875}
1876
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -03001877static int thinkpad_acpi_driver_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878{
1879 int len = 0;
1880
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001881 len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
1882 len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883
1884 return len;
1885}
1886
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001887static struct ibm_struct thinkpad_acpi_driver_data = {
1888 .name = "driver",
1889 .read = thinkpad_acpi_driver_read,
1890};
1891
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001892/*************************************************************************
1893 * Hotkey subdriver
1894 */
1895
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001896/*
1897 * ThinkPad firmware event model
1898 *
1899 * The ThinkPad firmware has two main event interfaces: normal ACPI
1900 * notifications (which follow the ACPI standard), and a private event
1901 * interface.
1902 *
1903 * The private event interface also issues events for the hotkeys. As
1904 * the driver gained features, the event handling code ended up being
1905 * built around the hotkey subdriver. This will need to be refactored
1906 * to a more formal event API eventually.
1907 *
1908 * Some "hotkeys" are actually supposed to be used as event reports,
1909 * such as "brightness has changed", "volume has changed", depending on
1910 * the ThinkPad model and how the firmware is operating.
1911 *
1912 * Unlike other classes, hotkey-class events have mask/unmask control on
1913 * non-ancient firmware. However, how it behaves changes a lot with the
1914 * firmware model and version.
1915 */
1916
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001917enum { /* hot key scan codes (derived from ACPI DSDT) */
1918 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1919 TP_ACPI_HOTKEYSCAN_FNF2,
1920 TP_ACPI_HOTKEYSCAN_FNF3,
1921 TP_ACPI_HOTKEYSCAN_FNF4,
1922 TP_ACPI_HOTKEYSCAN_FNF5,
1923 TP_ACPI_HOTKEYSCAN_FNF6,
1924 TP_ACPI_HOTKEYSCAN_FNF7,
1925 TP_ACPI_HOTKEYSCAN_FNF8,
1926 TP_ACPI_HOTKEYSCAN_FNF9,
1927 TP_ACPI_HOTKEYSCAN_FNF10,
1928 TP_ACPI_HOTKEYSCAN_FNF11,
1929 TP_ACPI_HOTKEYSCAN_FNF12,
1930 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1931 TP_ACPI_HOTKEYSCAN_FNINSERT,
1932 TP_ACPI_HOTKEYSCAN_FNDELETE,
1933 TP_ACPI_HOTKEYSCAN_FNHOME,
1934 TP_ACPI_HOTKEYSCAN_FNEND,
1935 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1936 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1937 TP_ACPI_HOTKEYSCAN_FNSPACE,
1938 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1939 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1940 TP_ACPI_HOTKEYSCAN_MUTE,
1941 TP_ACPI_HOTKEYSCAN_THINKPAD,
1942};
1943
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001944enum { /* Keys/events available through NVRAM polling */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001945 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1946 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1947};
1948
1949enum { /* Positions of some of the keys in hotkey masks */
1950 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1951 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1952 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1953 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1954 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1955 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1956 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1957 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1958 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1959 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1960 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1961};
1962
1963enum { /* NVRAM to ACPI HKEY group map */
1964 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1965 TP_ACPI_HKEY_ZOOM_MASK |
1966 TP_ACPI_HKEY_DISPSWTCH_MASK |
1967 TP_ACPI_HKEY_HIBERNATE_MASK,
1968 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1969 TP_ACPI_HKEY_BRGHTDWN_MASK,
1970 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1971 TP_ACPI_HKEY_VOLDWN_MASK |
1972 TP_ACPI_HKEY_MUTE_MASK,
1973};
1974
1975#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1976struct tp_nvram_state {
1977 u16 thinkpad_toggle:1;
1978 u16 zoom_toggle:1;
1979 u16 display_toggle:1;
1980 u16 thinklight_toggle:1;
1981 u16 hibernate_toggle:1;
1982 u16 displayexp_toggle:1;
1983 u16 display_state:1;
1984 u16 brightness_toggle:1;
1985 u16 volume_toggle:1;
1986 u16 mute:1;
1987
1988 u8 brightness_level;
1989 u8 volume_level;
1990};
1991
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001992/* kthread for the hotkey poller */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001993static struct task_struct *tpacpi_hotkey_task;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001994
1995/* Acquired while the poller kthread is running, use to sync start/stop */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001996static struct mutex hotkey_thread_mutex;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001997
1998/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001999 * Acquire mutex to write poller control variables as an
2000 * atomic block.
2001 *
2002 * Increment hotkey_config_change when changing them if you
2003 * want the kthread to forget old state.
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002004 *
2005 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
2006 */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002007static struct mutex hotkey_thread_data_mutex;
2008static unsigned int hotkey_config_change;
2009
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002010/*
2011 * hotkey poller control variables
2012 *
2013 * Must be atomic or readers will also need to acquire mutex
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002014 *
2015 * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
2016 * should be used only when the changes need to be taken as
2017 * a block, OR when one needs to force the kthread to forget
2018 * old state.
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002019 */
2020static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
2021static unsigned int hotkey_poll_freq = 10; /* Hz */
2022
2023#define HOTKEY_CONFIG_CRITICAL_START \
2024 do { \
2025 mutex_lock(&hotkey_thread_data_mutex); \
2026 hotkey_config_change++; \
2027 } while (0);
2028#define HOTKEY_CONFIG_CRITICAL_END \
2029 mutex_unlock(&hotkey_thread_data_mutex);
2030
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002031#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2032
2033#define hotkey_source_mask 0U
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002034#define HOTKEY_CONFIG_CRITICAL_START
2035#define HOTKEY_CONFIG_CRITICAL_END
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002036
2037#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2038
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002039static struct mutex hotkey_mutex;
2040
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002041static enum { /* Reasons for waking up */
2042 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
2043 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
2044 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
2045} hotkey_wakeup_reason;
2046
2047static int hotkey_autosleep_ack;
2048
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002049static u32 hotkey_orig_mask; /* events the BIOS had enabled */
2050static u32 hotkey_all_mask; /* all events supported in fw */
2051static u32 hotkey_reserved_mask; /* events better left disabled */
2052static u32 hotkey_driver_mask; /* events needed by the driver */
2053static u32 hotkey_user_mask; /* events visible to userspace */
2054static u32 hotkey_acpi_mask; /* events enabled in firmware */
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002055
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02002056static unsigned int hotkey_report_mode;
2057
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002058static u16 *hotkey_keycode_map;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002059
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03002060static struct attribute_set *hotkey_dev_attributes;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002061
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002062static void tpacpi_driver_event(const unsigned int hkey_event);
2063static void hotkey_driver_event(const unsigned int scancode);
2064
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002065/* HKEY.MHKG() return bits */
2066#define TP_HOTKEY_TABLET_MASK (1 << 3)
2067
Johannes Berg19d337d2009-06-02 13:01:37 +02002068static int hotkey_get_wlsw(void)
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002069{
Johannes Berg19d337d2009-06-02 13:01:37 +02002070 int status;
2071
2072 if (!tp_features.hotkey_wlsw)
2073 return -ENODEV;
2074
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02002075#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
Johannes Berg19d337d2009-06-02 13:01:37 +02002076 if (dbg_wlswemul)
2077 return (tpacpi_wlsw_emulstate) ?
2078 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02002079#endif
Johannes Berg19d337d2009-06-02 13:01:37 +02002080
2081 if (!acpi_evalf(hkey_handle, &status, "WLSW", "d"))
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002082 return -EIO;
Johannes Berg19d337d2009-06-02 13:01:37 +02002083
2084 return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002085}
2086
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002087static int hotkey_get_tablet_mode(int *status)
2088{
2089 int s;
2090
2091 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
2092 return -EIO;
2093
Henrique de Moraes Holschuhcee47f52008-03-04 14:29:21 -08002094 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
2095 return 0;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002096}
2097
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002098/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002099 * Reads current event mask from firmware, and updates
2100 * hotkey_acpi_mask accordingly. Also resets any bits
2101 * from hotkey_user_mask that are unavailable to be
2102 * delivered (shadow requirement of the userspace ABI).
2103 *
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002104 * Call with hotkey_mutex held
2105 */
2106static int hotkey_mask_get(void)
2107{
2108 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002109 u32 m = 0;
2110
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002111 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002112 return -EIO;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002113
2114 hotkey_acpi_mask = m;
2115 } else {
2116 /* no mask support doesn't mean no event support... */
2117 hotkey_acpi_mask = hotkey_all_mask;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002118 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002119
2120 /* sync userspace-visible mask */
2121 hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002122
2123 return 0;
2124}
2125
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002126void static hotkey_mask_warn_incomplete_mask(void)
2127{
2128 /* log only what the user can fix... */
2129 const u32 wantedmask = hotkey_driver_mask &
2130 ~(hotkey_acpi_mask | hotkey_source_mask) &
2131 (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
2132
2133 if (wantedmask)
2134 printk(TPACPI_NOTICE
2135 "required events 0x%08x not enabled!\n",
2136 wantedmask);
2137}
2138
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002139/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002140 * Set the firmware mask when supported
2141 *
2142 * Also calls hotkey_mask_get to update hotkey_acpi_mask.
2143 *
2144 * NOTE: does not set bits in hotkey_user_mask, but may reset them.
2145 *
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002146 * Call with hotkey_mutex held
2147 */
2148static int hotkey_mask_set(u32 mask)
2149{
2150 int i;
2151 int rc = 0;
2152
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002153 const u32 fwmask = mask & ~hotkey_source_mask;
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -03002154
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002155 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002156 for (i = 0; i < 32; i++) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002157 if (!acpi_evalf(hkey_handle,
2158 NULL, "MHKM", "vdd", i + 1,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002159 !!(mask & (1 << i)))) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002160 rc = -EIO;
2161 break;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002162 }
2163 }
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002164 }
2165
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002166 /*
2167 * We *must* make an inconditional call to hotkey_mask_get to
2168 * refresh hotkey_acpi_mask and update hotkey_user_mask
2169 *
2170 * Take the opportunity to also log when we cannot _enable_
2171 * a given event.
2172 */
2173 if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
2174 printk(TPACPI_NOTICE
2175 "asked for hotkey mask 0x%08x, but "
2176 "firmware forced it to 0x%08x\n",
2177 fwmask, hotkey_acpi_mask);
2178 }
2179
2180 hotkey_mask_warn_incomplete_mask();
2181
2182 return rc;
2183}
2184
2185/*
2186 * Sets hotkey_user_mask and tries to set the firmware mask
2187 *
2188 * Call with hotkey_mutex held
2189 */
2190static int hotkey_user_mask_set(const u32 mask)
2191{
2192 int rc;
2193
2194 /* Give people a chance to notice they are doing something that
2195 * is bound to go boom on their users sooner or later */
2196 if (!tp_warned.hotkey_mask_ff &&
2197 (mask == 0xffff || mask == 0xffffff ||
2198 mask == 0xffffffff)) {
2199 tp_warned.hotkey_mask_ff = 1;
2200 printk(TPACPI_NOTICE
2201 "setting the hotkey mask to 0x%08x is likely "
2202 "not the best way to go about it\n", mask);
2203 printk(TPACPI_NOTICE
2204 "please consider using the driver defaults, "
2205 "and refer to up-to-date thinkpad-acpi "
2206 "documentation\n");
2207 }
2208
2209 /* Try to enable what the user asked for, plus whatever we need.
2210 * this syncs everything but won't enable bits in hotkey_user_mask */
2211 rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
2212
2213 /* Enable the available bits in hotkey_user_mask */
2214 hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
2215
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002216 return rc;
2217}
2218
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002219/*
2220 * Sets the driver hotkey mask.
2221 *
2222 * Can be called even if the hotkey subdriver is inactive
2223 */
2224static int tpacpi_hotkey_driver_mask_set(const u32 mask)
2225{
2226 int rc;
2227
2228 /* Do the right thing if hotkey_init has not been called yet */
2229 if (!tp_features.hotkey) {
2230 hotkey_driver_mask = mask;
2231 return 0;
2232 }
2233
2234 mutex_lock(&hotkey_mutex);
2235
2236 HOTKEY_CONFIG_CRITICAL_START
2237 hotkey_driver_mask = mask;
2238 hotkey_source_mask |= (mask & ~hotkey_all_mask);
2239 HOTKEY_CONFIG_CRITICAL_END
2240
2241 rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) &
2242 ~hotkey_source_mask);
2243 mutex_unlock(&hotkey_mutex);
2244
2245 return rc;
2246}
2247
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002248static int hotkey_status_get(int *status)
2249{
2250 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
2251 return -EIO;
2252
2253 return 0;
2254}
2255
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002256static int hotkey_status_set(bool enable)
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002257{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002258 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002259 return -EIO;
2260
2261 return 0;
2262}
2263
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002264static void tpacpi_input_send_tabletsw(void)
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002265{
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002266 int state;
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002267
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002268 if (tp_features.hotkey_tablet &&
2269 !hotkey_get_tablet_mode(&state)) {
2270 mutex_lock(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002271
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002272 input_report_switch(tpacpi_inputdev,
2273 SW_TABLET_MODE, !!state);
2274 input_sync(tpacpi_inputdev);
2275
2276 mutex_unlock(&tpacpi_inputdev_send_mutex);
2277 }
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002278}
2279
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002280/* Do NOT call without validating scancode first */
2281static void tpacpi_input_send_key(const unsigned int scancode)
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002282{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002283 const unsigned int keycode = hotkey_keycode_map[scancode];
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002284
2285 if (keycode != KEY_RESERVED) {
2286 mutex_lock(&tpacpi_inputdev_send_mutex);
2287
2288 input_report_key(tpacpi_inputdev, keycode, 1);
2289 if (keycode == KEY_UNKNOWN)
2290 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
2291 scancode);
2292 input_sync(tpacpi_inputdev);
2293
2294 input_report_key(tpacpi_inputdev, keycode, 0);
2295 if (keycode == KEY_UNKNOWN)
2296 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
2297 scancode);
2298 input_sync(tpacpi_inputdev);
2299
2300 mutex_unlock(&tpacpi_inputdev_send_mutex);
2301 }
2302}
2303
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002304/* Do NOT call without validating scancode first */
2305static void tpacpi_input_send_key_masked(const unsigned int scancode)
2306{
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002307 hotkey_driver_event(scancode);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002308 if (hotkey_user_mask & (1 << scancode))
2309 tpacpi_input_send_key(scancode);
2310}
2311
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002312#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2313static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
2314
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002315/* Do NOT call without validating scancode first */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002316static void tpacpi_hotkey_send_key(unsigned int scancode)
2317{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002318 tpacpi_input_send_key_masked(scancode);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002319 if (hotkey_report_mode < 2) {
2320 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03002321 0x80, TP_HKEY_EV_HOTKEY_BASE + scancode);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002322 }
2323}
2324
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002325static void hotkey_read_nvram(struct tp_nvram_state *n, const u32 m)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002326{
2327 u8 d;
2328
2329 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
2330 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
2331 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
2332 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
2333 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
2334 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
2335 }
2336 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
2337 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
2338 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
2339 }
2340 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
2341 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
2342 n->displayexp_toggle =
2343 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
2344 }
2345 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
2346 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
2347 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
2348 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
2349 n->brightness_toggle =
2350 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
2351 }
2352 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
2353 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
2354 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
2355 >> TP_NVRAM_POS_LEVEL_VOLUME;
2356 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2357 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2358 }
2359}
2360
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002361static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
2362 struct tp_nvram_state *newn,
2363 const u32 event_mask)
2364{
2365
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002366#define TPACPI_COMPARE_KEY(__scancode, __member) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002367 do { \
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002368 if ((event_mask & (1 << __scancode)) && \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002369 oldn->__member != newn->__member) \
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002370 tpacpi_hotkey_send_key(__scancode); \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002371 } while (0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002372
2373#define TPACPI_MAY_SEND_KEY(__scancode) \
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002374 do { \
2375 if (event_mask & (1 << __scancode)) \
2376 tpacpi_hotkey_send_key(__scancode); \
2377 } while (0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002378
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002379 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2380 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2381 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2382 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2383
2384 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2385
2386 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2387
2388 /* handle volume */
2389 if (oldn->volume_toggle != newn->volume_toggle) {
2390 if (oldn->mute != newn->mute) {
2391 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2392 }
2393 if (oldn->volume_level > newn->volume_level) {
2394 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2395 } else if (oldn->volume_level < newn->volume_level) {
2396 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2397 } else if (oldn->mute == newn->mute) {
2398 /* repeated key presses that didn't change state */
2399 if (newn->mute) {
2400 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2401 } else if (newn->volume_level != 0) {
2402 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2403 } else {
2404 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2405 }
2406 }
2407 }
2408
2409 /* handle brightness */
2410 if (oldn->brightness_toggle != newn->brightness_toggle) {
2411 if (oldn->brightness_level < newn->brightness_level) {
2412 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2413 } else if (oldn->brightness_level > newn->brightness_level) {
2414 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2415 } else {
2416 /* repeated key presses that didn't change state */
2417 if (newn->brightness_level != 0) {
2418 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2419 } else {
2420 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2421 }
2422 }
2423 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002424
2425#undef TPACPI_COMPARE_KEY
2426#undef TPACPI_MAY_SEND_KEY
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002427}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002428
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002429/*
2430 * Polling driver
2431 *
2432 * We track all events in hotkey_source_mask all the time, since
2433 * most of them are edge-based. We only issue those requested by
2434 * hotkey_user_mask or hotkey_driver_mask, though.
2435 */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002436static int hotkey_kthread(void *data)
2437{
2438 struct tp_nvram_state s[2];
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002439 u32 poll_mask, event_mask;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002440 unsigned int si, so;
2441 unsigned long t;
2442 unsigned int change_detector, must_reset;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002443 unsigned int poll_freq;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002444
2445 mutex_lock(&hotkey_thread_mutex);
2446
2447 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2448 goto exit;
2449
2450 set_freezable();
2451
2452 so = 0;
2453 si = 1;
2454 t = 0;
2455
2456 /* Initial state for compares */
2457 mutex_lock(&hotkey_thread_data_mutex);
2458 change_detector = hotkey_config_change;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002459 poll_mask = hotkey_source_mask;
2460 event_mask = hotkey_source_mask &
2461 (hotkey_driver_mask | hotkey_user_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002462 poll_freq = hotkey_poll_freq;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002463 mutex_unlock(&hotkey_thread_data_mutex);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002464 hotkey_read_nvram(&s[so], poll_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002465
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002466 while (!kthread_should_stop()) {
2467 if (t == 0) {
2468 if (likely(poll_freq))
2469 t = 1000/poll_freq;
2470 else
2471 t = 100; /* should never happen... */
2472 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002473 t = msleep_interruptible(t);
2474 if (unlikely(kthread_should_stop()))
2475 break;
2476 must_reset = try_to_freeze();
2477 if (t > 0 && !must_reset)
2478 continue;
2479
2480 mutex_lock(&hotkey_thread_data_mutex);
2481 if (must_reset || hotkey_config_change != change_detector) {
2482 /* forget old state on thaw or config change */
2483 si = so;
2484 t = 0;
2485 change_detector = hotkey_config_change;
2486 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002487 poll_mask = hotkey_source_mask;
2488 event_mask = hotkey_source_mask &
2489 (hotkey_driver_mask | hotkey_user_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002490 poll_freq = hotkey_poll_freq;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002491 mutex_unlock(&hotkey_thread_data_mutex);
2492
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002493 if (likely(poll_mask)) {
2494 hotkey_read_nvram(&s[si], poll_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002495 if (likely(si != so)) {
2496 hotkey_compare_and_issue_event(&s[so], &s[si],
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002497 event_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002498 }
2499 }
2500
2501 so = si;
2502 si ^= 1;
2503 }
2504
2505exit:
2506 mutex_unlock(&hotkey_thread_mutex);
2507 return 0;
2508}
2509
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002510/* call with hotkey_mutex held */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002511static void hotkey_poll_stop_sync(void)
2512{
2513 if (tpacpi_hotkey_task) {
2514 if (frozen(tpacpi_hotkey_task) ||
2515 freezing(tpacpi_hotkey_task))
2516 thaw_process(tpacpi_hotkey_task);
2517
2518 kthread_stop(tpacpi_hotkey_task);
2519 tpacpi_hotkey_task = NULL;
2520 mutex_lock(&hotkey_thread_mutex);
2521 /* at this point, the thread did exit */
2522 mutex_unlock(&hotkey_thread_mutex);
2523 }
2524}
2525
2526/* call with hotkey_mutex held */
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002527static void hotkey_poll_setup(bool may_warn)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002528{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002529 const u32 poll_driver_mask = hotkey_driver_mask & hotkey_source_mask;
2530 const u32 poll_user_mask = hotkey_user_mask & hotkey_source_mask;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002531
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002532 if (hotkey_poll_freq > 0 &&
2533 (poll_driver_mask ||
2534 (poll_user_mask && tpacpi_inputdev->users > 0))) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002535 if (!tpacpi_hotkey_task) {
2536 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03002537 NULL, TPACPI_NVRAM_KTHREAD_NAME);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002538 if (IS_ERR(tpacpi_hotkey_task)) {
2539 tpacpi_hotkey_task = NULL;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002540 printk(TPACPI_ERR
2541 "could not create kernel thread "
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002542 "for hotkey polling\n");
2543 }
2544 }
2545 } else {
2546 hotkey_poll_stop_sync();
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002547 if (may_warn && (poll_driver_mask || poll_user_mask) &&
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002548 hotkey_poll_freq == 0) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002549 printk(TPACPI_NOTICE
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002550 "hot keys 0x%08x and/or events 0x%08x "
2551 "require polling, which is currently "
2552 "disabled\n",
2553 poll_user_mask, poll_driver_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002554 }
2555 }
2556}
2557
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002558static void hotkey_poll_setup_safe(bool may_warn)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002559{
2560 mutex_lock(&hotkey_mutex);
2561 hotkey_poll_setup(may_warn);
2562 mutex_unlock(&hotkey_mutex);
2563}
2564
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002565/* call with hotkey_mutex held */
2566static void hotkey_poll_set_freq(unsigned int freq)
2567{
2568 if (!freq)
2569 hotkey_poll_stop_sync();
2570
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002571 hotkey_poll_freq = freq;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002572}
2573
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02002574#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2575
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002576static void hotkey_poll_setup_safe(bool __unused)
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02002577{
2578}
2579
2580#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2581
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002582static int hotkey_inputdev_open(struct input_dev *dev)
2583{
2584 switch (tpacpi_lifecycle) {
2585 case TPACPI_LIFE_INIT:
2586 /*
2587 * hotkey_init will call hotkey_poll_setup_safe
2588 * at the appropriate moment
2589 */
2590 return 0;
2591 case TPACPI_LIFE_EXITING:
2592 return -EBUSY;
2593 case TPACPI_LIFE_RUNNING:
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002594 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002595 return 0;
2596 }
2597
2598 /* Should only happen if tpacpi_lifecycle is corrupt */
2599 BUG();
2600 return -EBUSY;
2601}
2602
2603static void hotkey_inputdev_close(struct input_dev *dev)
2604{
2605 /* disable hotkey polling when possible */
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002606 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING &&
2607 !(hotkey_source_mask & hotkey_driver_mask))
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002608 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002609}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002610
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002611/* sysfs hotkey enable ------------------------------------------------- */
2612static ssize_t hotkey_enable_show(struct device *dev,
2613 struct device_attribute *attr,
2614 char *buf)
2615{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002616 int res, status;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002617
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002618 printk_deprecated_attribute("hotkey_enable",
2619 "Hotkey reporting is always enabled");
2620
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002621 res = hotkey_status_get(&status);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002622 if (res)
2623 return res;
2624
2625 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2626}
2627
2628static ssize_t hotkey_enable_store(struct device *dev,
2629 struct device_attribute *attr,
2630 const char *buf, size_t count)
2631{
2632 unsigned long t;
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002633
2634 printk_deprecated_attribute("hotkey_enable",
2635 "Hotkeys can be disabled through hotkey_mask");
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002636
2637 if (parse_strtoul(buf, 1, &t))
2638 return -EINVAL;
2639
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002640 if (t == 0)
2641 return -EPERM;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002642
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002643 return count;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002644}
2645
2646static struct device_attribute dev_attr_hotkey_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002647 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002648 hotkey_enable_show, hotkey_enable_store);
2649
2650/* sysfs hotkey mask --------------------------------------------------- */
2651static ssize_t hotkey_mask_show(struct device *dev,
2652 struct device_attribute *attr,
2653 char *buf)
2654{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002655 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_user_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002656}
2657
2658static ssize_t hotkey_mask_store(struct device *dev,
2659 struct device_attribute *attr,
2660 const char *buf, size_t count)
2661{
2662 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002663 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002664
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002665 if (parse_strtoul(buf, 0xffffffffUL, &t))
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002666 return -EINVAL;
2667
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002668 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002669 return -ERESTARTSYS;
2670
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002671 res = hotkey_user_mask_set(t);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002672
2673#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002674 hotkey_poll_setup(true);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002675#endif
2676
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002677 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002678
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002679 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2680
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002681 return (res) ? res : count;
2682}
2683
2684static struct device_attribute dev_attr_hotkey_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002685 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002686 hotkey_mask_show, hotkey_mask_store);
2687
2688/* sysfs hotkey bios_enabled ------------------------------------------- */
2689static ssize_t hotkey_bios_enabled_show(struct device *dev,
2690 struct device_attribute *attr,
2691 char *buf)
2692{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002693 return sprintf(buf, "0\n");
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002694}
2695
2696static struct device_attribute dev_attr_hotkey_bios_enabled =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002697 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002698
2699/* sysfs hotkey bios_mask ---------------------------------------------- */
2700static ssize_t hotkey_bios_mask_show(struct device *dev,
2701 struct device_attribute *attr,
2702 char *buf)
2703{
Henrique de Moraes Holschuh06777be2009-09-12 15:22:15 -03002704 printk_deprecated_attribute("hotkey_bios_mask",
2705 "This attribute is useless.");
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002706 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002707}
2708
2709static struct device_attribute dev_attr_hotkey_bios_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002710 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002711
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002712/* sysfs hotkey all_mask ----------------------------------------------- */
2713static ssize_t hotkey_all_mask_show(struct device *dev,
2714 struct device_attribute *attr,
2715 char *buf)
2716{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002717 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2718 hotkey_all_mask | hotkey_source_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002719}
2720
2721static struct device_attribute dev_attr_hotkey_all_mask =
2722 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
2723
2724/* sysfs hotkey recommended_mask --------------------------------------- */
2725static ssize_t hotkey_recommended_mask_show(struct device *dev,
2726 struct device_attribute *attr,
2727 char *buf)
2728{
2729 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002730 (hotkey_all_mask | hotkey_source_mask)
2731 & ~hotkey_reserved_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002732}
2733
2734static struct device_attribute dev_attr_hotkey_recommended_mask =
2735 __ATTR(hotkey_recommended_mask, S_IRUGO,
2736 hotkey_recommended_mask_show, NULL);
2737
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002738#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2739
2740/* sysfs hotkey hotkey_source_mask ------------------------------------- */
2741static ssize_t hotkey_source_mask_show(struct device *dev,
2742 struct device_attribute *attr,
2743 char *buf)
2744{
2745 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2746}
2747
2748static ssize_t hotkey_source_mask_store(struct device *dev,
2749 struct device_attribute *attr,
2750 const char *buf, size_t count)
2751{
2752 unsigned long t;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002753 u32 r_ev;
2754 int rc;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002755
2756 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2757 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2758 return -EINVAL;
2759
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002760 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002761 return -ERESTARTSYS;
2762
2763 HOTKEY_CONFIG_CRITICAL_START
2764 hotkey_source_mask = t;
2765 HOTKEY_CONFIG_CRITICAL_END
2766
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002767 rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
2768 ~hotkey_source_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002769 hotkey_poll_setup(true);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002770
2771 /* check if events needed by the driver got disabled */
2772 r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
2773 & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002774
2775 mutex_unlock(&hotkey_mutex);
2776
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002777 if (rc < 0)
2778 printk(TPACPI_ERR "hotkey_source_mask: failed to update the"
2779 "firmware event mask!\n");
2780
2781 if (r_ev)
2782 printk(TPACPI_NOTICE "hotkey_source_mask: "
2783 "some important events were disabled: "
2784 "0x%04x\n", r_ev);
2785
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002786 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2787
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002788 return (rc < 0) ? rc : count;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002789}
2790
2791static struct device_attribute dev_attr_hotkey_source_mask =
2792 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2793 hotkey_source_mask_show, hotkey_source_mask_store);
2794
2795/* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2796static ssize_t hotkey_poll_freq_show(struct device *dev,
2797 struct device_attribute *attr,
2798 char *buf)
2799{
2800 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2801}
2802
2803static ssize_t hotkey_poll_freq_store(struct device *dev,
2804 struct device_attribute *attr,
2805 const char *buf, size_t count)
2806{
2807 unsigned long t;
2808
2809 if (parse_strtoul(buf, 25, &t))
2810 return -EINVAL;
2811
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002812 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002813 return -ERESTARTSYS;
2814
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002815 hotkey_poll_set_freq(t);
2816 hotkey_poll_setup(true);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002817
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002818 mutex_unlock(&hotkey_mutex);
2819
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002820 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2821
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002822 return count;
2823}
2824
2825static struct device_attribute dev_attr_hotkey_poll_freq =
2826 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2827 hotkey_poll_freq_show, hotkey_poll_freq_store);
2828
2829#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2830
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002831/* sysfs hotkey radio_sw (pollable) ------------------------------------ */
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002832static ssize_t hotkey_radio_sw_show(struct device *dev,
2833 struct device_attribute *attr,
2834 char *buf)
2835{
Johannes Berg19d337d2009-06-02 13:01:37 +02002836 int res;
2837 res = hotkey_get_wlsw();
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002838 if (res < 0)
2839 return res;
2840
Johannes Berg19d337d2009-06-02 13:01:37 +02002841 /* Opportunistic update */
2842 tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
2843
2844 return snprintf(buf, PAGE_SIZE, "%d\n",
2845 (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002846}
2847
2848static struct device_attribute dev_attr_hotkey_radio_sw =
2849 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2850
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002851static void hotkey_radio_sw_notify_change(void)
2852{
2853 if (tp_features.hotkey_wlsw)
2854 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2855 "hotkey_radio_sw");
2856}
2857
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002858/* sysfs hotkey tablet mode (pollable) --------------------------------- */
2859static ssize_t hotkey_tablet_mode_show(struct device *dev,
2860 struct device_attribute *attr,
2861 char *buf)
2862{
2863 int res, s;
2864 res = hotkey_get_tablet_mode(&s);
2865 if (res < 0)
2866 return res;
2867
2868 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2869}
2870
2871static struct device_attribute dev_attr_hotkey_tablet_mode =
2872 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2873
2874static void hotkey_tablet_mode_notify_change(void)
2875{
2876 if (tp_features.hotkey_tablet)
2877 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2878 "hotkey_tablet_mode");
2879}
2880
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002881/* sysfs hotkey report_mode -------------------------------------------- */
2882static ssize_t hotkey_report_mode_show(struct device *dev,
2883 struct device_attribute *attr,
2884 char *buf)
2885{
2886 return snprintf(buf, PAGE_SIZE, "%d\n",
2887 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2888}
2889
2890static struct device_attribute dev_attr_hotkey_report_mode =
2891 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2892
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002893/* sysfs wakeup reason (pollable) -------------------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002894static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2895 struct device_attribute *attr,
2896 char *buf)
2897{
2898 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2899}
2900
2901static struct device_attribute dev_attr_hotkey_wakeup_reason =
2902 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2903
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02002904static void hotkey_wakeup_reason_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002905{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002906 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2907 "wakeup_reason");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002908}
2909
2910/* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002911static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2912 struct device_attribute *attr,
2913 char *buf)
2914{
2915 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2916}
2917
2918static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2919 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2920 hotkey_wakeup_hotunplug_complete_show, NULL);
2921
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02002922static void hotkey_wakeup_hotunplug_complete_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002923{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002924 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2925 "wakeup_hotunplug_complete");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002926}
2927
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002928/* --------------------------------------------------------------------- */
2929
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002930static struct attribute *hotkey_attributes[] __initdata = {
2931 &dev_attr_hotkey_enable.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002932 &dev_attr_hotkey_bios_enabled.attr,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002933 &dev_attr_hotkey_bios_mask.attr,
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002934 &dev_attr_hotkey_report_mode.attr,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002935 &dev_attr_hotkey_wakeup_reason.attr,
2936 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002937 &dev_attr_hotkey_mask.attr,
2938 &dev_attr_hotkey_all_mask.attr,
2939 &dev_attr_hotkey_recommended_mask.attr,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002940#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002941 &dev_attr_hotkey_source_mask.attr,
2942 &dev_attr_hotkey_poll_freq.attr,
2943#endif
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002944};
2945
Johannes Berg19d337d2009-06-02 13:01:37 +02002946/*
2947 * Sync both the hw and sw blocking state of all switches
2948 */
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002949static void tpacpi_send_radiosw_update(void)
2950{
2951 int wlsw;
2952
Johannes Berg19d337d2009-06-02 13:01:37 +02002953 /*
2954 * We must sync all rfkill controllers *before* issuing any
2955 * rfkill input events, or we will race the rfkill core input
2956 * handler.
2957 *
2958 * tpacpi_inputdev_send_mutex works as a syncronization point
2959 * for the above.
2960 *
2961 * We optimize to avoid numerous calls to hotkey_get_wlsw.
2962 */
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002963
Johannes Berg19d337d2009-06-02 13:01:37 +02002964 wlsw = hotkey_get_wlsw();
2965
2966 /* Sync hw blocking state first if it is hw-blocked */
2967 if (wlsw == TPACPI_RFK_RADIO_OFF)
2968 tpacpi_rfk_update_hwblock_state(true);
2969
2970 /* Sync sw blocking state */
2971 tpacpi_rfk_update_swstate_all();
2972
2973 /* Sync hw blocking state last if it is hw-unblocked */
2974 if (wlsw == TPACPI_RFK_RADIO_ON)
2975 tpacpi_rfk_update_hwblock_state(false);
2976
2977 /* Issue rfkill input event for WLSW switch */
2978 if (!(wlsw < 0)) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002979 mutex_lock(&tpacpi_inputdev_send_mutex);
2980
2981 input_report_switch(tpacpi_inputdev,
Johannes Berg19d337d2009-06-02 13:01:37 +02002982 SW_RFKILL_ALL, (wlsw > 0));
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002983 input_sync(tpacpi_inputdev);
2984
2985 mutex_unlock(&tpacpi_inputdev_send_mutex);
2986 }
Johannes Berg19d337d2009-06-02 13:01:37 +02002987
2988 /*
2989 * this can be unconditional, as we will poll state again
2990 * if userspace uses the notify to read data
2991 */
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002992 hotkey_radio_sw_notify_change();
2993}
2994
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002995static void hotkey_exit(void)
2996{
2997#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002998 mutex_lock(&hotkey_mutex);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002999 hotkey_poll_stop_sync();
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003000 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003001#endif
3002
3003 if (hotkey_dev_attributes)
3004 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3005
3006 kfree(hotkey_keycode_map);
3007
Henrique de Moraes Holschuh4be73002009-09-20 14:09:23 -03003008 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003009 "restoring original HKEY status and mask\n");
3010 /* yes, there is a bitwise or below, we want the
3011 * functions to be called even if one of them fail */
3012 if (((tp_features.hotkey_mask &&
3013 hotkey_mask_set(hotkey_orig_mask)) |
3014 hotkey_status_set(false)) != 0)
Henrique de Moraes Holschuh4be73002009-09-20 14:09:23 -03003015 printk(TPACPI_ERR
3016 "failed to restore hot key mask "
3017 "to BIOS defaults\n");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003018}
3019
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003020static void __init hotkey_unmap(const unsigned int scancode)
3021{
3022 if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
3023 clear_bit(hotkey_keycode_map[scancode],
3024 tpacpi_inputdev->keybit);
3025 hotkey_keycode_map[scancode] = KEY_RESERVED;
3026 }
3027}
3028
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003029/*
3030 * HKEY quirks:
3031 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
3032 */
3033
3034#define TPACPI_HK_Q_INIMASK 0x0001
3035
3036static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
3037 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
3038 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
3039 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
3040 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
3041 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
3042 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
3043 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
3044 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
3045 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
3046 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
3047 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
3048 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
3049 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
3050 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
3051 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
3052 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
3053 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
3054 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
3055 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
3056};
3057
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003058static int __init hotkey_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003059{
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003060 /* Requirements for changing the default keymaps:
3061 *
3062 * 1. Many of the keys are mapped to KEY_RESERVED for very
3063 * good reasons. Do not change them unless you have deep
3064 * knowledge on the IBM and Lenovo ThinkPad firmware for
3065 * the various ThinkPad models. The driver behaves
3066 * differently for KEY_RESERVED: such keys have their
3067 * hot key mask *unset* in mask_recommended, and also
3068 * in the initial hot key mask programmed into the
3069 * firmware at driver load time, which means the firm-
3070 * ware may react very differently if you change them to
3071 * something else;
3072 *
3073 * 2. You must be subscribed to the linux-thinkpad and
3074 * ibm-acpi-devel mailing lists, and you should read the
3075 * list archives since 2007 if you want to change the
3076 * keymaps. This requirement exists so that you will
3077 * know the past history of problems with the thinkpad-
3078 * acpi driver keymaps, and also that you will be
3079 * listening to any bug reports;
3080 *
3081 * 3. Do not send thinkpad-acpi specific patches directly to
3082 * for merging, *ever*. Send them to the linux-acpi
3083 * mailinglist for comments. Merging is to be done only
3084 * through acpi-test and the ACPI maintainer.
3085 *
3086 * If the above is too much to ask, don't change the keymap.
3087 * Ask the thinkpad-acpi maintainer to do it, instead.
3088 */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003089 static u16 ibm_keycode_map[] __initdata = {
3090 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3091 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
3092 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3093 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003094
3095 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003096 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3097 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3098 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003099
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003100 /* brightness: firmware always reacts to them */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003101 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003102 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003103
3104 /* Thinklight: firmware always react to it */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003105 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003106
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003107 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3108 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003109
3110 /* Volume: firmware always react to it and reprograms
3111 * the built-in *extra* mixer. Never map it to control
3112 * another mixer by default. */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003113 KEY_RESERVED, /* 0x14: VOLUME UP */
3114 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3115 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003116
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003117 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003118
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003119 /* (assignments unknown, please report if found) */
3120 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3121 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3122 };
3123 static u16 lenovo_keycode_map[] __initdata = {
3124 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3125 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
3126 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3127 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003128
3129 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003130 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3131 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3132 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003133
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003134 /* These should be enabled --only-- when ACPI video
3135 * is disabled (i.e. in "vendor" mode), and are handled
3136 * in a special way by the init code */
3137 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
3138 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003139
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003140 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003141
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003142 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3143 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003144
3145 /* Volume: z60/z61, T60 (BIOS version?): firmware always
3146 * react to it and reprograms the built-in *extra* mixer.
3147 * Never map it to control another mixer by default.
3148 *
3149 * T60?, T61, R60?, R61: firmware and EC tries to send
3150 * these over the regular keyboard, so these are no-ops,
3151 * but there are still weird bugs re. MUTE, so do not
3152 * change unless you get test reports from all Lenovo
3153 * models. May cause the BIOS to interfere with the
3154 * HDA mixer.
3155 */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003156 KEY_RESERVED, /* 0x14: VOLUME UP */
3157 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3158 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003159
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003160 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003161
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003162 /* (assignments unknown, please report if found) */
3163 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3164 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3165 };
3166
3167#define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
3168#define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
3169#define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
3170
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003171 int res, i;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003172 int status;
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03003173 int hkeyv;
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03003174
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003175 unsigned long quirks;
3176
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003177 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3178 "initializing hotkey subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003179
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003180 BUG_ON(!tpacpi_inputdev);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003181 BUG_ON(tpacpi_inputdev->open != NULL ||
3182 tpacpi_inputdev->close != NULL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003183
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003184 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003185 mutex_init(&hotkey_mutex);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003186
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003187#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3188 mutex_init(&hotkey_thread_mutex);
3189 mutex_init(&hotkey_thread_data_mutex);
3190#endif
3191
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003192 /* hotkey not supported on 570 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003193 tp_features.hotkey = hkey_handle != NULL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003194
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003195 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3196 "hotkeys are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003197 str_supported(tp_features.hotkey));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003198
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003199 if (!tp_features.hotkey)
3200 return 1;
3201
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003202 quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
3203 ARRAY_SIZE(tpacpi_hotkey_qtable));
3204
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03003205 tpacpi_disable_brightness_delay();
3206
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003207 /* MUST have enough space for all attributes to be added to
3208 * hotkey_dev_attributes */
3209 hotkey_dev_attributes = create_attr_set(
3210 ARRAY_SIZE(hotkey_attributes) + 2,
3211 NULL);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003212 if (!hotkey_dev_attributes)
3213 return -ENOMEM;
3214 res = add_many_to_attr_set(hotkey_dev_attributes,
3215 hotkey_attributes,
3216 ARRAY_SIZE(hotkey_attributes));
3217 if (res)
3218 goto err_exit;
3219
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003220 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003221 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
3222 for HKEY interface version 0x100 */
3223 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
3224 if ((hkeyv >> 8) != 1) {
3225 printk(TPACPI_ERR "unknown version of the "
3226 "HKEY interface: 0x%x\n", hkeyv);
3227 printk(TPACPI_ERR "please report this to %s\n",
3228 TPACPI_MAIL);
3229 } else {
3230 /*
3231 * MHKV 0x100 in A31, R40, R40e,
3232 * T4x, X31, and later
3233 */
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003234 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3235 "firmware HKEY interface version: 0x%x\n",
3236 hkeyv);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003237
3238 /* Paranoia check AND init hotkey_all_mask */
3239 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3240 "MHKA", "qd")) {
3241 printk(TPACPI_ERR
3242 "missing MHKA handler, "
3243 "please report this to %s\n",
3244 TPACPI_MAIL);
3245 /* Fallback: pre-init for FN+F3,F4,F12 */
3246 hotkey_all_mask = 0x080cU;
3247 } else {
3248 tp_features.hotkey_mask = 1;
3249 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003250 }
3251 }
3252
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003253 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3254 "hotkey masks are %s\n",
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003255 str_supported(tp_features.hotkey_mask));
3256
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003257 /* Init hotkey_all_mask if not initialized yet */
3258 if (!tp_features.hotkey_mask && !hotkey_all_mask &&
3259 (quirks & TPACPI_HK_Q_INIMASK))
3260 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003261
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003262 /* Init hotkey_acpi_mask and hotkey_orig_mask */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003263 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003264 /* hotkey_source_mask *must* be zero for
3265 * the first hotkey_mask_get to return hotkey_orig_mask */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003266 res = hotkey_mask_get();
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003267 if (res)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003268 goto err_exit;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003269
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003270 hotkey_orig_mask = hotkey_acpi_mask;
3271 } else {
3272 hotkey_orig_mask = hotkey_all_mask;
3273 hotkey_acpi_mask = hotkey_all_mask;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003274 }
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003275
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003276#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3277 if (dbg_wlswemul) {
3278 tp_features.hotkey_wlsw = 1;
3279 printk(TPACPI_INFO
3280 "radio switch emulation enabled\n");
3281 } else
3282#endif
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003283 /* Not all thinkpads have a hardware radio switch */
3284 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
3285 tp_features.hotkey_wlsw = 1;
3286 printk(TPACPI_INFO
3287 "radio switch found; radios are %s\n",
3288 enabled(status, 0));
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003289 }
3290 if (tp_features.hotkey_wlsw)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003291 res = add_to_attr_set(hotkey_dev_attributes,
3292 &dev_attr_hotkey_radio_sw.attr);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003293
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003294 /* For X41t, X60t, X61t Tablets... */
3295 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
3296 tp_features.hotkey_tablet = 1;
3297 printk(TPACPI_INFO
3298 "possible tablet mode switch found; "
3299 "ThinkPad in %s mode\n",
3300 (status & TP_HOTKEY_TABLET_MASK)?
3301 "tablet" : "laptop");
3302 res = add_to_attr_set(hotkey_dev_attributes,
3303 &dev_attr_hotkey_tablet_mode.attr);
3304 }
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02003305
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003306 if (!res)
3307 res = register_attr_set_with_sysfs(
3308 hotkey_dev_attributes,
3309 &tpacpi_pdev->dev.kobj);
3310 if (res)
3311 goto err_exit;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003312
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003313 /* Set up key map */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003314
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003315 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
3316 GFP_KERNEL);
3317 if (!hotkey_keycode_map) {
3318 printk(TPACPI_ERR
3319 "failed to allocate memory for key map\n");
3320 res = -ENOMEM;
3321 goto err_exit;
3322 }
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003323
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003324 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003325 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003326 "using Lenovo default hot key map\n");
3327 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
3328 TPACPI_HOTKEY_MAP_SIZE);
3329 } else {
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003330 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003331 "using IBM default hot key map\n");
3332 memcpy(hotkey_keycode_map, &ibm_keycode_map,
3333 TPACPI_HOTKEY_MAP_SIZE);
3334 }
3335
3336 set_bit(EV_KEY, tpacpi_inputdev->evbit);
3337 set_bit(EV_MSC, tpacpi_inputdev->evbit);
3338 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
3339 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
3340 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3341 tpacpi_inputdev->keycode = hotkey_keycode_map;
3342 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3343 if (hotkey_keycode_map[i] != KEY_RESERVED) {
3344 set_bit(hotkey_keycode_map[i],
3345 tpacpi_inputdev->keybit);
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003346 } else {
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003347 if (i < sizeof(hotkey_reserved_mask)*8)
3348 hotkey_reserved_mask |= 1 << i;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003349 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003350 }
3351
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003352 if (tp_features.hotkey_wlsw) {
3353 set_bit(EV_SW, tpacpi_inputdev->evbit);
3354 set_bit(SW_RFKILL_ALL, tpacpi_inputdev->swbit);
3355 }
3356 if (tp_features.hotkey_tablet) {
3357 set_bit(EV_SW, tpacpi_inputdev->evbit);
3358 set_bit(SW_TABLET_MODE, tpacpi_inputdev->swbit);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003359 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003360
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003361 /* Do not issue duplicate brightness change events to
3362 * userspace */
3363 if (!tp_features.bright_acpimode)
3364 /* update bright_acpimode... */
3365 tpacpi_check_std_acpi_brightness_support();
3366
Henrique de Moraes Holschuh8bf3d4c2009-05-30 13:25:09 -03003367 if (tp_features.bright_acpimode && acpi_video_backlight_support()) {
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003368 printk(TPACPI_INFO
3369 "This ThinkPad has standard ACPI backlight "
3370 "brightness control, supported by the ACPI "
3371 "video driver\n");
3372 printk(TPACPI_NOTICE
3373 "Disabling thinkpad-acpi brightness events "
3374 "by default...\n");
3375
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003376 /* Disable brightness up/down on Lenovo thinkpads when
3377 * ACPI is handling them, otherwise it is plain impossible
3378 * for userspace to do something even remotely sane */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003379 hotkey_reserved_mask |=
3380 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3381 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003382 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
3383 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003384 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003385
Henrique de Moraes Holschuh230d8cf252009-09-12 15:22:17 -03003386#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003387 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3388 & ~hotkey_all_mask
3389 & ~hotkey_reserved_mask;
Henrique de Moraes Holschuh230d8cf252009-09-12 15:22:17 -03003390
3391 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3392 "hotkey source mask 0x%08x, polling freq %u\n",
3393 hotkey_source_mask, hotkey_poll_freq);
3394#endif
3395
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003396 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3397 "enabling firmware HKEY event interface...\n");
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003398 res = hotkey_status_set(true);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003399 if (res) {
3400 hotkey_exit();
3401 return res;
3402 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003403 res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
3404 | hotkey_driver_mask)
3405 & ~hotkey_source_mask);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003406 if (res < 0 && res != -ENXIO) {
3407 hotkey_exit();
3408 return res;
3409 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003410 hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
3411 & ~hotkey_reserved_mask;
3412 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3413 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3414 hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003415
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003416 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3417 "legacy ibm/hotkey event reporting over procfs %s\n",
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003418 (hotkey_report_mode < 2) ?
3419 "enabled" : "disabled");
3420
3421 tpacpi_inputdev->open = &hotkey_inputdev_open;
3422 tpacpi_inputdev->close = &hotkey_inputdev_close;
3423
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003424 hotkey_poll_setup_safe(true);
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003425 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003426 tpacpi_input_send_tabletsw();
3427
3428 return 0;
3429
3430err_exit:
3431 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3432 hotkey_dev_attributes = NULL;
3433
3434 return (res < 0)? res : 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003435}
3436
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003437static bool hotkey_notify_hotkey(const u32 hkey,
3438 bool *send_acpi_ev,
3439 bool *ignore_acpi_ev)
3440{
3441 /* 0x1000-0x1FFF: key presses */
3442 unsigned int scancode = hkey & 0xfff;
3443 *send_acpi_ev = true;
3444 *ignore_acpi_ev = false;
3445
3446 if (scancode > 0 && scancode < 0x21) {
3447 scancode--;
3448 if (!(hotkey_source_mask & (1 << scancode))) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003449 tpacpi_input_send_key_masked(scancode);
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003450 *send_acpi_ev = false;
3451 } else {
3452 *ignore_acpi_ev = true;
3453 }
3454 return true;
3455 }
3456 return false;
3457}
3458
3459static bool hotkey_notify_wakeup(const u32 hkey,
3460 bool *send_acpi_ev,
3461 bool *ignore_acpi_ev)
3462{
3463 /* 0x2000-0x2FFF: Wakeup reason */
3464 *send_acpi_ev = true;
3465 *ignore_acpi_ev = false;
3466
3467 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003468 case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */
3469 case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003470 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3471 *ignore_acpi_ev = true;
3472 break;
3473
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003474 case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */
3475 case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003476 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3477 *ignore_acpi_ev = true;
3478 break;
3479
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003480 case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */
3481 case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003482 printk(TPACPI_ALERT
3483 "EMERGENCY WAKEUP: battery almost empty\n");
3484 /* how to auto-heal: */
3485 /* 2313: woke up from S3, go to S4/S5 */
3486 /* 2413: woke up from S4, go to S5 */
3487 break;
3488
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003489 default:
3490 return false;
3491 }
3492
3493 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
3494 printk(TPACPI_INFO
3495 "woke up due to a hot-unplug "
3496 "request...\n");
3497 hotkey_wakeup_reason_notify_change();
3498 }
3499 return true;
3500}
3501
3502static bool hotkey_notify_usrevent(const u32 hkey,
3503 bool *send_acpi_ev,
3504 bool *ignore_acpi_ev)
3505{
3506 /* 0x5000-0x5FFF: human interface helpers */
3507 *send_acpi_ev = true;
3508 *ignore_acpi_ev = false;
3509
3510 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003511 case TP_HKEY_EV_PEN_INSERTED: /* X61t: tablet pen inserted into bay */
3512 case TP_HKEY_EV_PEN_REMOVED: /* X61t: tablet pen removed from bay */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003513 return true;
3514
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003515 case TP_HKEY_EV_TABLET_TABLET: /* X41t-X61t: tablet mode */
3516 case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003517 tpacpi_input_send_tabletsw();
3518 hotkey_tablet_mode_notify_change();
3519 *send_acpi_ev = false;
3520 return true;
3521
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003522 case TP_HKEY_EV_LID_CLOSE: /* Lid closed */
3523 case TP_HKEY_EV_LID_OPEN: /* Lid opened */
3524 case TP_HKEY_EV_BRGHT_CHANGED: /* brightness changed */
Henrique de Moraes Holschuh176dd982009-09-20 14:09:24 -03003525 /* do not propagate these events */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003526 *ignore_acpi_ev = true;
3527 return true;
3528
3529 default:
3530 return false;
3531 }
3532}
3533
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003534static bool hotkey_notify_thermal(const u32 hkey,
3535 bool *send_acpi_ev,
3536 bool *ignore_acpi_ev)
3537{
3538 /* 0x6000-0x6FFF: thermal alarms */
3539 *send_acpi_ev = true;
3540 *ignore_acpi_ev = false;
3541
3542 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003543 case TP_HKEY_EV_ALARM_BAT_HOT:
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003544 printk(TPACPI_CRIT
3545 "THERMAL ALARM: battery is too hot!\n");
3546 /* recommended action: warn user through gui */
3547 return true;
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003548 case TP_HKEY_EV_ALARM_BAT_XHOT:
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003549 printk(TPACPI_ALERT
3550 "THERMAL EMERGENCY: battery is extremely hot!\n");
3551 /* recommended action: immediate sleep/hibernate */
3552 return true;
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003553 case TP_HKEY_EV_ALARM_SENSOR_HOT:
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003554 printk(TPACPI_CRIT
3555 "THERMAL ALARM: "
3556 "a sensor reports something is too hot!\n");
3557 /* recommended action: warn user through gui, that */
3558 /* some internal component is too hot */
3559 return true;
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003560 case TP_HKEY_EV_ALARM_SENSOR_XHOT:
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003561 printk(TPACPI_ALERT
3562 "THERMAL EMERGENCY: "
3563 "a sensor reports something is extremely hot!\n");
3564 /* recommended action: immediate sleep/hibernate */
3565 return true;
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003566 case TP_HKEY_EV_THM_TABLE_CHANGED:
Henrique de Moraes Holschuh54926ce2009-01-11 03:01:09 -02003567 printk(TPACPI_INFO
3568 "EC reports that Thermal Table has changed\n");
3569 /* recommended action: do nothing, we don't have
3570 * Lenovo ATM information */
3571 return true;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003572 default:
3573 printk(TPACPI_ALERT
3574 "THERMAL ALERT: unknown thermal alarm received\n");
3575 return false;
3576 }
3577}
3578
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003579static void hotkey_notify(struct ibm_struct *ibm, u32 event)
3580{
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003581 u32 hkey;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003582 bool send_acpi_ev;
3583 bool ignore_acpi_ev;
3584 bool known_ev;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003585
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003586 if (event != 0x80) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003587 printk(TPACPI_ERR
3588 "unknown HKEY notification event %d\n", event);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003589 /* forward it to userspace, maybe it knows how to handle it */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003590 acpi_bus_generate_netlink_event(
3591 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02003592 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003593 event, 0);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003594 return;
3595 }
3596
3597 while (1) {
3598 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003599 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003600 return;
3601 }
3602
3603 if (hkey == 0) {
3604 /* queue empty */
3605 return;
3606 }
3607
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003608 send_acpi_ev = true;
3609 ignore_acpi_ev = false;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003610
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003611 switch (hkey >> 12) {
3612 case 1:
3613 /* 0x1000-0x1FFF: key presses */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003614 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
3615 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003616 break;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003617 case 2:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003618 /* 0x2000-0x2FFF: Wakeup reason */
3619 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
3620 &ignore_acpi_ev);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003621 break;
3622 case 3:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003623 /* 0x3000-0x3FFF: bay-related wakeups */
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003624 if (hkey == TP_HKEY_EV_BAYEJ_ACK) {
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003625 hotkey_autosleep_ack = 1;
3626 printk(TPACPI_INFO
3627 "bay ejected\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003628 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003629 known_ev = true;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003630 } else {
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003631 known_ev = false;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003632 }
3633 break;
3634 case 4:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003635 /* 0x4000-0x4FFF: dock-related wakeups */
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003636 if (hkey == TP_HKEY_EV_UNDOCK_ACK) {
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003637 hotkey_autosleep_ack = 1;
3638 printk(TPACPI_INFO
3639 "undocked\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003640 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003641 known_ev = true;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003642 } else {
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003643 known_ev = false;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003644 }
3645 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003646 case 5:
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02003647 /* 0x5000-0x5FFF: human interface helpers */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003648 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
3649 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003650 break;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003651 case 6:
3652 /* 0x6000-0x6FFF: thermal alarms */
3653 known_ev = hotkey_notify_thermal(hkey, &send_acpi_ev,
3654 &ignore_acpi_ev);
3655 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003656 case 7:
3657 /* 0x7000-0x7FFF: misc */
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003658 if (tp_features.hotkey_wlsw &&
3659 hkey == TP_HKEY_EV_RFKILL_CHANGED) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003660 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02003661 send_acpi_ev = 0;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003662 known_ev = true;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003663 break;
3664 }
3665 /* fallthrough to default */
3666 default:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003667 known_ev = false;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02003668 }
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003669 if (!known_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003670 printk(TPACPI_NOTICE
3671 "unhandled HKEY event 0x%04x\n", hkey);
Henrique de Moraes Holschuhcb429352009-01-11 03:01:07 -02003672 printk(TPACPI_NOTICE
3673 "please report the conditions when this "
3674 "event happened to %s\n", TPACPI_MAIL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003675 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003676
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003677 /* Legacy events */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003678 if (!ignore_acpi_ev &&
3679 (send_acpi_ev || hotkey_report_mode < 2)) {
3680 acpi_bus_generate_proc_event(ibm->acpi->device,
3681 event, hkey);
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03003682 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003683
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003684 /* netlink events */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03003685 if (!ignore_acpi_ev && send_acpi_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003686 acpi_bus_generate_netlink_event(
3687 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02003688 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003689 event, hkey);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003690 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003691 }
3692}
3693
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003694static void hotkey_suspend(pm_message_t state)
3695{
3696 /* Do these on suspend, we get the events on early resume! */
3697 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
3698 hotkey_autosleep_ack = 0;
3699}
3700
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03003701static void hotkey_resume(void)
3702{
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03003703 tpacpi_disable_brightness_delay();
3704
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003705 if (hotkey_status_set(true) < 0 ||
3706 hotkey_mask_set(hotkey_acpi_mask) < 0)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003707 printk(TPACPI_ERR
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003708 "error while attempting to reset the event "
3709 "firmware interface\n");
3710
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003711 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02003712 hotkey_tablet_mode_notify_change();
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003713 hotkey_wakeup_reason_notify_change();
3714 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003715 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03003716}
3717
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003718/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003719static int hotkey_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03003721 int res, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722 int len = 0;
3723
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003724 if (!tp_features.hotkey) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003725 len += sprintf(p + len, "status:\t\tnot supported\n");
3726 return len;
3727 }
3728
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02003729 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02003730 return -ERESTARTSYS;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02003731 res = hotkey_status_get(&status);
3732 if (!res)
3733 res = hotkey_mask_get();
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003734 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03003735 if (res)
3736 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003737
3738 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003739 if (hotkey_all_mask) {
3740 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_user_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741 len += sprintf(p + len,
3742 "commands:\tenable, disable, reset, <mask>\n");
3743 } else {
3744 len += sprintf(p + len, "mask:\t\tnot supported\n");
3745 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
3746 }
3747
3748 return len;
3749}
3750
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003751static void hotkey_enabledisable_warn(bool enable)
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003752{
3753 tpacpi_log_usertask("procfs hotkey enable/disable");
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003754 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
3755 TPACPI_WARN
3756 "hotkey enable/disable functionality has been "
3757 "removed from the driver. Hotkeys are always "
3758 "enabled\n"))
3759 printk(TPACPI_ERR
3760 "Please remove the hotkey=enable module "
3761 "parameter, it is deprecated. Hotkeys are always "
3762 "enabled\n");
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003763}
3764
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003765static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003767 int res;
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03003768 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003771 if (!tp_features.hotkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772 return -ENODEV;
3773
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02003774 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02003775 return -ERESTARTSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003776
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003777 mask = hotkey_user_mask;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003778
3779 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780 while ((cmd = next_cmd(&buf))) {
3781 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003782 hotkey_enabledisable_warn(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003784 hotkey_enabledisable_warn(0);
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003785 res = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786 } else if (strlencmp(cmd, "reset") == 0) {
Henrique de Moraes Holschuh20c9aa42009-09-12 15:22:16 -03003787 mask = (hotkey_all_mask | hotkey_source_mask)
3788 & ~hotkey_reserved_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
3790 /* mask set */
3791 } else if (sscanf(cmd, "%x", &mask) == 1) {
3792 /* mask set */
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003793 } else {
3794 res = -EINVAL;
3795 goto errexit;
3796 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797 }
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003798
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003799 if (!res) {
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003800 tpacpi_disclose_usertask("procfs hotkey",
3801 "set mask to 0x%08x\n", mask);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003802 res = hotkey_user_mask_set(mask);
3803 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003805errexit:
3806 mutex_unlock(&hotkey_mutex);
3807 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003808}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003810static const struct acpi_device_id ibm_htk_device_ids[] = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003811 {TPACPI_ACPI_HKEY_HID, 0},
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003812 {"", 0},
3813};
3814
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003815static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003816 .hid = ibm_htk_device_ids,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003817 .notify = hotkey_notify,
3818 .handle = &hkey_handle,
3819 .type = ACPI_DEVICE_NOTIFY,
3820};
3821
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003822static struct ibm_struct hotkey_driver_data = {
3823 .name = "hotkey",
3824 .read = hotkey_read,
3825 .write = hotkey_write,
3826 .exit = hotkey_exit,
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03003827 .resume = hotkey_resume,
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003828 .suspend = hotkey_suspend,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003829 .acpi = &ibm_hotkey_acpidriver,
3830};
3831
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003832/*************************************************************************
3833 * Bluetooth subdriver
3834 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003836enum {
3837 /* ACPI GBDC/SBDC bits */
3838 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
3839 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003840 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
3841 off / last state */
3842};
3843
3844enum {
3845 /* ACPI \BLTH commands */
3846 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
3847 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
3848 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
3849 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
3850 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003851};
3852
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003853#define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
3854
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003855static void bluetooth_suspend(pm_message_t state)
3856{
3857 /* Try to make sure radio will resume powered off */
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003858 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3859 TP_ACPI_BLTH_PWR_OFF_ON_RESUME))
3860 vdbg_printk(TPACPI_DBG_RFKILL,
3861 "bluetooth power down on resume request failed\n");
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003862}
3863
Johannes Berg19d337d2009-06-02 13:01:37 +02003864static int bluetooth_get_status(void)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003865{
3866 int status;
3867
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003868#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3869 if (dbg_bluetoothemul)
3870 return (tpacpi_bluetooth_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02003871 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003872#endif
3873
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003874 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
3875 return -EIO;
3876
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003877 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02003878 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003879}
3880
Johannes Berg19d337d2009-06-02 13:01:37 +02003881static int bluetooth_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003882{
3883 int status;
3884
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003885 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02003886 "will attempt to %s bluetooth\n",
3887 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003888
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003889#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3890 if (dbg_bluetoothemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02003891 tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003892 return 0;
3893 }
3894#endif
3895
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003896 /* We make sure to keep TP_ACPI_BLUETOOTH_RESUMECTRL off */
Johannes Berg19d337d2009-06-02 13:01:37 +02003897 if (state == TPACPI_RFK_RADIO_ON)
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003898 status = TP_ACPI_BLUETOOTH_RADIOSSW;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003899 else
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003900 status = 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02003901
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003902 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
3903 return -EIO;
3904
3905 return 0;
3906}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003907
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003908/* sysfs bluetooth enable ---------------------------------------------- */
3909static ssize_t bluetooth_enable_show(struct device *dev,
3910 struct device_attribute *attr,
3911 char *buf)
3912{
Johannes Berg19d337d2009-06-02 13:01:37 +02003913 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID,
3914 attr, buf);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003915}
3916
3917static ssize_t bluetooth_enable_store(struct device *dev,
3918 struct device_attribute *attr,
3919 const char *buf, size_t count)
3920{
Johannes Berg19d337d2009-06-02 13:01:37 +02003921 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID,
3922 attr, buf, count);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003923}
3924
3925static struct device_attribute dev_attr_bluetooth_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03003926 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003927 bluetooth_enable_show, bluetooth_enable_store);
3928
3929/* --------------------------------------------------------------------- */
3930
3931static struct attribute *bluetooth_attributes[] = {
3932 &dev_attr_bluetooth_enable.attr,
3933 NULL
3934};
3935
3936static const struct attribute_group bluetooth_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003937 .attrs = bluetooth_attributes,
3938};
3939
Johannes Berg19d337d2009-06-02 13:01:37 +02003940static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = {
3941 .get_status = bluetooth_get_status,
3942 .set_status = bluetooth_set_status,
3943};
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003944
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003945static void bluetooth_shutdown(void)
3946{
3947 /* Order firmware to save current state to NVRAM */
3948 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3949 TP_ACPI_BLTH_SAVE_STATE))
3950 printk(TPACPI_NOTICE
3951 "failed to save bluetooth state to NVRAM\n");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003952 else
3953 vdbg_printk(TPACPI_DBG_RFKILL,
3954 "bluestooth state saved to NVRAM\n");
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003955}
3956
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003957static void bluetooth_exit(void)
3958{
3959 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3960 &bluetooth_attr_group);
Johannes Berg19d337d2009-06-02 13:01:37 +02003961
3962 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
3963
3964 bluetooth_shutdown();
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003965}
3966
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003967static int __init bluetooth_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003969 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003970 int status = 0;
3971
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003972 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3973 "initializing bluetooth subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003974
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003975 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003976
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003977 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3978 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003979 tp_features.bluetooth = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003980 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003982 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3983 "bluetooth is %s, status 0x%02x\n",
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003984 str_supported(tp_features.bluetooth),
3985 status);
3986
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003987#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3988 if (dbg_bluetoothemul) {
3989 tp_features.bluetooth = 1;
3990 printk(TPACPI_INFO
3991 "bluetooth switch emulation enabled\n");
3992 } else
3993#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003994 if (tp_features.bluetooth &&
3995 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
3996 /* no bluetooth hardware present in system */
3997 tp_features.bluetooth = 0;
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003998 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003999 "bluetooth hardware not installed\n");
4000 }
4001
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004002 if (!tp_features.bluetooth)
4003 return 1;
4004
Johannes Berg19d337d2009-06-02 13:01:37 +02004005 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
4006 &bluetooth_tprfk_ops,
4007 RFKILL_TYPE_BLUETOOTH,
4008 TPACPI_RFK_BLUETOOTH_SW_NAME,
4009 true);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004010 if (res)
4011 return res;
4012
Johannes Berg19d337d2009-06-02 13:01:37 +02004013 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4014 &bluetooth_attr_group);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004015 if (res) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004016 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004017 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004018 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004019
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004020 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021}
4022
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004023/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004024static int bluetooth_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025{
Johannes Berg19d337d2009-06-02 13:01:37 +02004026 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027}
4028
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004029static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030{
Johannes Berg19d337d2009-06-02 13:01:37 +02004031 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032}
4033
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004034static struct ibm_struct bluetooth_driver_data = {
4035 .name = "bluetooth",
4036 .read = bluetooth_read,
4037 .write = bluetooth_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004038 .exit = bluetooth_exit,
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02004039 .suspend = bluetooth_suspend,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004040 .shutdown = bluetooth_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004041};
4042
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004043/*************************************************************************
4044 * Wan subdriver
4045 */
4046
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004047enum {
4048 /* ACPI GWAN/SWAN bits */
4049 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
4050 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02004051 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
4052 off / last state */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004053};
4054
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004055#define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
4056
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02004057static void wan_suspend(pm_message_t state)
4058{
4059 /* Try to make sure radio will resume powered off */
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004060 if (!acpi_evalf(NULL, NULL, "\\WGSV", "qvd",
4061 TP_ACPI_WGSV_PWR_OFF_ON_RESUME))
4062 vdbg_printk(TPACPI_DBG_RFKILL,
4063 "WWAN power down on resume request failed\n");
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02004064}
4065
Johannes Berg19d337d2009-06-02 13:01:37 +02004066static int wan_get_status(void)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004067{
4068 int status;
4069
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004070#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4071 if (dbg_wwanemul)
4072 return (tpacpi_wwan_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004073 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004074#endif
4075
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004076 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
4077 return -EIO;
4078
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004079 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004080 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004081}
4082
Johannes Berg19d337d2009-06-02 13:01:37 +02004083static int wan_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004084{
4085 int status;
4086
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004087 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02004088 "will attempt to %s wwan\n",
4089 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004090
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004091#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4092 if (dbg_wwanemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004093 tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004094 return 0;
4095 }
4096#endif
4097
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02004098 /* We make sure to keep TP_ACPI_WANCARD_RESUMECTRL off */
Johannes Berg19d337d2009-06-02 13:01:37 +02004099 if (state == TPACPI_RFK_RADIO_ON)
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02004100 status = TP_ACPI_WANCARD_RADIOSSW;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004101 else
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02004102 status = 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02004103
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004104 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
4105 return -EIO;
4106
4107 return 0;
4108}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004109
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004110/* sysfs wan enable ---------------------------------------------------- */
4111static ssize_t wan_enable_show(struct device *dev,
4112 struct device_attribute *attr,
4113 char *buf)
4114{
Johannes Berg19d337d2009-06-02 13:01:37 +02004115 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
4116 attr, buf);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004117}
4118
4119static ssize_t wan_enable_store(struct device *dev,
4120 struct device_attribute *attr,
4121 const char *buf, size_t count)
4122{
Johannes Berg19d337d2009-06-02 13:01:37 +02004123 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
4124 attr, buf, count);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004125}
4126
4127static struct device_attribute dev_attr_wan_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03004128 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004129 wan_enable_show, wan_enable_store);
4130
4131/* --------------------------------------------------------------------- */
4132
4133static struct attribute *wan_attributes[] = {
4134 &dev_attr_wan_enable.attr,
4135 NULL
4136};
4137
4138static const struct attribute_group wan_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004139 .attrs = wan_attributes,
4140};
4141
Johannes Berg19d337d2009-06-02 13:01:37 +02004142static const struct tpacpi_rfk_ops wan_tprfk_ops = {
4143 .get_status = wan_get_status,
4144 .set_status = wan_set_status,
4145};
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004146
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004147static void wan_shutdown(void)
4148{
4149 /* Order firmware to save current state to NVRAM */
4150 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
4151 TP_ACPI_WGSV_SAVE_STATE))
4152 printk(TPACPI_NOTICE
4153 "failed to save WWAN state to NVRAM\n");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004154 else
4155 vdbg_printk(TPACPI_DBG_RFKILL,
4156 "WWAN state saved to NVRAM\n");
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004157}
4158
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004159static void wan_exit(void)
4160{
4161 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4162 &wan_attr_group);
Johannes Berg19d337d2009-06-02 13:01:37 +02004163
4164 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4165
4166 wan_shutdown();
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004167}
4168
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004169static int __init wan_init(struct ibm_init_struct *iibm)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004170{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004171 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004172 int status = 0;
4173
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004174 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4175 "initializing wan subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004176
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004177 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004178
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004179 tp_features.wan = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004180 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004181
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004182 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4183 "wan is %s, status 0x%02x\n",
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004184 str_supported(tp_features.wan),
4185 status);
4186
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004187#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4188 if (dbg_wwanemul) {
4189 tp_features.wan = 1;
4190 printk(TPACPI_INFO
4191 "wwan switch emulation enabled\n");
4192 } else
4193#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004194 if (tp_features.wan &&
4195 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
4196 /* no wan hardware present in system */
4197 tp_features.wan = 0;
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004198 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004199 "wan hardware not installed\n");
4200 }
4201
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004202 if (!tp_features.wan)
4203 return 1;
4204
Johannes Berg19d337d2009-06-02 13:01:37 +02004205 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
4206 &wan_tprfk_ops,
4207 RFKILL_TYPE_WWAN,
4208 TPACPI_RFK_WWAN_SW_NAME,
4209 true);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004210 if (res)
4211 return res;
4212
Johannes Berg19d337d2009-06-02 13:01:37 +02004213 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4214 &wan_attr_group);
4215
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004216 if (res) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004217 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004218 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004219 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004220
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004221 return 0;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004222}
4223
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004224/* procfs -------------------------------------------------------------- */
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004225static int wan_read(char *p)
4226{
Johannes Berg19d337d2009-06-02 13:01:37 +02004227 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, p);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004228}
4229
4230static int wan_write(char *buf)
4231{
Johannes Berg19d337d2009-06-02 13:01:37 +02004232 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004233}
4234
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004235static struct ibm_struct wan_driver_data = {
4236 .name = "wan",
4237 .read = wan_read,
4238 .write = wan_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004239 .exit = wan_exit,
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02004240 .suspend = wan_suspend,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004241 .shutdown = wan_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004242};
4243
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004244/*************************************************************************
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004245 * UWB subdriver
4246 */
4247
4248enum {
4249 /* ACPI GUWB/SUWB bits */
4250 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4251 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4252};
4253
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004254#define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4255
Johannes Berg19d337d2009-06-02 13:01:37 +02004256static int uwb_get_status(void)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004257{
4258 int status;
4259
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004260#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4261 if (dbg_uwbemul)
4262 return (tpacpi_uwb_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004263 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004264#endif
4265
4266 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4267 return -EIO;
4268
4269 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004270 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004271}
4272
Johannes Berg19d337d2009-06-02 13:01:37 +02004273static int uwb_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004274{
4275 int status;
4276
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004277 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02004278 "will attempt to %s UWB\n",
4279 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004280
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004281#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4282 if (dbg_uwbemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004283 tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004284 return 0;
4285 }
4286#endif
4287
Johannes Berg19d337d2009-06-02 13:01:37 +02004288 if (state == TPACPI_RFK_RADIO_ON)
4289 status = TP_ACPI_UWB_RADIOSSW;
4290 else
4291 status = 0;
4292
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004293 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4294 return -EIO;
4295
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004296 return 0;
4297}
4298
4299/* --------------------------------------------------------------------- */
4300
Johannes Berg19d337d2009-06-02 13:01:37 +02004301static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
4302 .get_status = uwb_get_status,
4303 .set_status = uwb_set_status,
4304};
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004305
4306static void uwb_exit(void)
4307{
Johannes Berg19d337d2009-06-02 13:01:37 +02004308 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004309}
4310
4311static int __init uwb_init(struct ibm_init_struct *iibm)
4312{
4313 int res;
4314 int status = 0;
4315
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004316 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4317 "initializing uwb subdriver\n");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004318
4319 TPACPI_ACPIHANDLE_INIT(hkey);
4320
4321 tp_features.uwb = hkey_handle &&
4322 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4323
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004324 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4325 "uwb is %s, status 0x%02x\n",
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004326 str_supported(tp_features.uwb),
4327 status);
4328
4329#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4330 if (dbg_uwbemul) {
4331 tp_features.uwb = 1;
4332 printk(TPACPI_INFO
4333 "uwb switch emulation enabled\n");
4334 } else
4335#endif
4336 if (tp_features.uwb &&
4337 !(status & TP_ACPI_UWB_HWPRESENT)) {
4338 /* no uwb hardware present in system */
4339 tp_features.uwb = 0;
4340 dbg_printk(TPACPI_DBG_INIT,
4341 "uwb hardware not installed\n");
4342 }
4343
4344 if (!tp_features.uwb)
4345 return 1;
4346
4347 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
Johannes Berg19d337d2009-06-02 13:01:37 +02004348 &uwb_tprfk_ops,
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004349 RFKILL_TYPE_UWB,
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004350 TPACPI_RFK_UWB_SW_NAME,
Johannes Berg19d337d2009-06-02 13:01:37 +02004351 false);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004352 return res;
4353}
4354
4355static struct ibm_struct uwb_driver_data = {
4356 .name = "uwb",
4357 .exit = uwb_exit,
4358 .flags.experimental = 1,
4359};
4360
4361/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004362 * Video subdriver
4363 */
4364
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02004365#ifdef CONFIG_THINKPAD_ACPI_VIDEO
4366
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004367enum video_access_mode {
4368 TPACPI_VIDEO_NONE = 0,
4369 TPACPI_VIDEO_570, /* 570 */
4370 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4371 TPACPI_VIDEO_NEW, /* all others */
4372};
4373
4374enum { /* video status flags, based on VIDEO_570 */
4375 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4376 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4377 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4378};
4379
4380enum { /* TPACPI_VIDEO_570 constants */
4381 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4382 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4383 * video_status_flags */
4384 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4385 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4386};
4387
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02004388static enum video_access_mode video_supported;
4389static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004390
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004391static int video_autosw_get(void);
4392static int video_autosw_set(int enable);
4393
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004394TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004395
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004396static int __init video_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004398 int ivga;
4399
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004400 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4401
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004402 TPACPI_ACPIHANDLE_INIT(vid);
4403 TPACPI_ACPIHANDLE_INIT(vid2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004404
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004405 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4406 /* G41, assume IVGA doesn't change */
4407 vid_handle = vid2_handle;
4408
4409 if (!vid_handle)
4410 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004411 video_supported = TPACPI_VIDEO_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004412 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
4413 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004414 video_supported = TPACPI_VIDEO_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004415 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
4416 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004417 video_supported = TPACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004418 else
4419 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004420 video_supported = TPACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004422 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
4423 str_supported(video_supported != TPACPI_VIDEO_NONE),
4424 video_supported);
4425
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004426 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427}
4428
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004429static void video_exit(void)
4430{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004431 dbg_printk(TPACPI_DBG_EXIT,
4432 "restoring original video autoswitch mode\n");
4433 if (video_autosw_set(video_orig_autosw))
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004434 printk(TPACPI_ERR "error while trying to restore original "
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004435 "video autoswitch mode\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004436}
4437
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004438static int video_outputsw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439{
4440 int status = 0;
4441 int i;
4442
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004443 switch (video_supported) {
4444 case TPACPI_VIDEO_570:
4445 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
4446 TP_ACPI_VIDEO_570_PHSCMD))
4447 return -EIO;
4448 status = i & TP_ACPI_VIDEO_570_PHSMASK;
4449 break;
4450 case TPACPI_VIDEO_770:
4451 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4452 return -EIO;
4453 if (i)
4454 status |= TP_ACPI_VIDEO_S_LCD;
4455 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4456 return -EIO;
4457 if (i)
4458 status |= TP_ACPI_VIDEO_S_CRT;
4459 break;
4460 case TPACPI_VIDEO_NEW:
4461 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4462 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4463 return -EIO;
4464 if (i)
4465 status |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004467 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4468 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4469 return -EIO;
4470 if (i)
4471 status |= TP_ACPI_VIDEO_S_LCD;
4472 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4473 return -EIO;
4474 if (i)
4475 status |= TP_ACPI_VIDEO_S_DVI;
4476 break;
4477 default:
4478 return -ENOSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004479 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480
4481 return status;
4482}
4483
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004484static int video_outputsw_set(int status)
4485{
4486 int autosw;
4487 int res = 0;
4488
4489 switch (video_supported) {
4490 case TPACPI_VIDEO_570:
4491 res = acpi_evalf(NULL, NULL,
4492 "\\_SB.PHS2", "vdd",
4493 TP_ACPI_VIDEO_570_PHS2CMD,
4494 status | TP_ACPI_VIDEO_570_PHS2SET);
4495 break;
4496 case TPACPI_VIDEO_770:
4497 autosw = video_autosw_get();
4498 if (autosw < 0)
4499 return autosw;
4500
4501 res = video_autosw_set(1);
4502 if (res)
4503 return res;
4504 res = acpi_evalf(vid_handle, NULL,
4505 "ASWT", "vdd", status * 0x100, 0);
4506 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004507 printk(TPACPI_ERR
4508 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004509 return -EIO;
4510 }
4511 break;
4512 case TPACPI_VIDEO_NEW:
4513 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004514 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004515 break;
4516 default:
4517 return -ENOSYS;
4518 }
4519
4520 return (res)? 0 : -EIO;
4521}
4522
4523static int video_autosw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004525 int autosw = 0;
4526
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004527 switch (video_supported) {
4528 case TPACPI_VIDEO_570:
4529 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
4530 return -EIO;
4531 break;
4532 case TPACPI_VIDEO_770:
4533 case TPACPI_VIDEO_NEW:
4534 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4535 return -EIO;
4536 break;
4537 default:
4538 return -ENOSYS;
4539 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004540
4541 return autosw & 1;
4542}
4543
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004544static int video_autosw_set(int enable)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004545{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004546 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004547 return -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004548 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004549}
4550
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004551static int video_outputsw_cycle(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004552{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004553 int autosw = video_autosw_get();
4554 int res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004555
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004556 if (autosw < 0)
4557 return autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004558
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004559 switch (video_supported) {
4560 case TPACPI_VIDEO_570:
4561 res = video_autosw_set(1);
4562 if (res)
4563 return res;
4564 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4565 break;
4566 case TPACPI_VIDEO_770:
4567 case TPACPI_VIDEO_NEW:
4568 res = video_autosw_set(1);
4569 if (res)
4570 return res;
4571 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4572 break;
4573 default:
4574 return -ENOSYS;
4575 }
4576 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004577 printk(TPACPI_ERR
4578 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004579 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004580 }
4581
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004582 return (res)? 0 : -EIO;
4583}
4584
4585static int video_expand_toggle(void)
4586{
4587 switch (video_supported) {
4588 case TPACPI_VIDEO_570:
4589 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
4590 0 : -EIO;
4591 case TPACPI_VIDEO_770:
4592 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
4593 0 : -EIO;
4594 case TPACPI_VIDEO_NEW:
4595 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
4596 0 : -EIO;
4597 default:
4598 return -ENOSYS;
4599 }
4600 /* not reached */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004601}
4602
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004603static int video_read(char *p)
4604{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004605 int status, autosw;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004606 int len = 0;
4607
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004608 if (video_supported == TPACPI_VIDEO_NONE) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004609 len += sprintf(p + len, "status:\t\tnot supported\n");
4610 return len;
4611 }
4612
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004613 status = video_outputsw_get();
4614 if (status < 0)
4615 return status;
4616
4617 autosw = video_autosw_get();
4618 if (autosw < 0)
4619 return autosw;
4620
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004621 len += sprintf(p + len, "status:\t\tsupported\n");
4622 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
4623 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004624 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004625 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
4626 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
4627 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
4628 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004629 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004630 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
4631 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
4632 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
4633
4634 return len;
4635}
4636
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004637static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004638{
4639 char *cmd;
4640 int enable, disable, status;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004641 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004643 if (video_supported == TPACPI_VIDEO_NONE)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004644 return -ENODEV;
4645
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004646 enable = 0;
4647 disable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648
4649 while ((cmd = next_cmd(&buf))) {
4650 if (strlencmp(cmd, "lcd_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004651 enable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004652 } else if (strlencmp(cmd, "lcd_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004653 disable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654 } else if (strlencmp(cmd, "crt_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004655 enable |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656 } else if (strlencmp(cmd, "crt_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004657 disable |= TP_ACPI_VIDEO_S_CRT;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004658 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004659 strlencmp(cmd, "dvi_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004660 enable |= TP_ACPI_VIDEO_S_DVI;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004661 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004662 strlencmp(cmd, "dvi_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004663 disable |= TP_ACPI_VIDEO_S_DVI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664 } else if (strlencmp(cmd, "auto_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004665 res = video_autosw_set(1);
4666 if (res)
4667 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668 } else if (strlencmp(cmd, "auto_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004669 res = video_autosw_set(0);
4670 if (res)
4671 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004672 } else if (strlencmp(cmd, "video_switch") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004673 res = video_outputsw_cycle();
4674 if (res)
4675 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004677 res = video_expand_toggle();
4678 if (res)
4679 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680 } else
4681 return -EINVAL;
4682 }
4683
4684 if (enable || disable) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004685 status = video_outputsw_get();
4686 if (status < 0)
4687 return status;
4688 res = video_outputsw_set((status & ~disable) | enable);
4689 if (res)
4690 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004691 }
4692
4693 return 0;
4694}
4695
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004696static struct ibm_struct video_driver_data = {
4697 .name = "video",
4698 .read = video_read,
4699 .write = video_write,
4700 .exit = video_exit,
4701};
4702
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02004703#endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4704
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004705/*************************************************************************
4706 * Light (thinklight) subdriver
4707 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004709TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4710TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004711
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004712static int light_get_status(void)
4713{
4714 int status = 0;
4715
4716 if (tp_features.light_status) {
4717 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4718 return -EIO;
4719 return (!!status);
4720 }
4721
4722 return -ENXIO;
4723}
4724
4725static int light_set_status(int status)
4726{
4727 int rc;
4728
4729 if (tp_features.light) {
4730 if (cmos_handle) {
4731 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4732 (status)?
4733 TP_CMOS_THINKLIGHT_ON :
4734 TP_CMOS_THINKLIGHT_OFF);
4735 } else {
4736 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4737 (status)? 1 : 0);
4738 }
4739 return (rc)? 0 : -EIO;
4740 }
4741
4742 return -ENXIO;
4743}
4744
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004745static void light_set_status_worker(struct work_struct *work)
4746{
4747 struct tpacpi_led_classdev *data =
4748 container_of(work, struct tpacpi_led_classdev, work);
4749
4750 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00004751 light_set_status((data->new_state != TPACPI_LED_OFF));
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004752}
4753
4754static void light_sysfs_set(struct led_classdev *led_cdev,
4755 enum led_brightness brightness)
4756{
4757 struct tpacpi_led_classdev *data =
4758 container_of(led_cdev,
4759 struct tpacpi_led_classdev,
4760 led_classdev);
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00004761 data->new_state = (brightness != LED_OFF) ?
4762 TPACPI_LED_ON : TPACPI_LED_OFF;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004763 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004764}
4765
4766static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4767{
4768 return (light_get_status() == 1)? LED_FULL : LED_OFF;
4769}
4770
4771static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4772 .led_classdev = {
4773 .name = "tpacpi::thinklight",
4774 .brightness_set = &light_sysfs_set,
4775 .brightness_get = &light_sysfs_get,
4776 }
4777};
4778
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004779static int __init light_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004780{
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004781 int rc;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004782
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004783 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4784
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004785 TPACPI_ACPIHANDLE_INIT(ledb);
4786 TPACPI_ACPIHANDLE_INIT(lght);
4787 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004788 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004789
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004790 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004791 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004792
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004793 if (tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004794 /* light status not supported on
4795 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004796 tp_features.light_status =
4797 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004799 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4800 str_supported(tp_features.light),
4801 str_supported(tp_features.light_status));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004802
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004803 if (!tp_features.light)
4804 return 1;
4805
4806 rc = led_classdev_register(&tpacpi_pdev->dev,
4807 &tpacpi_led_thinklight.led_classdev);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004808
4809 if (rc < 0) {
4810 tp_features.light = 0;
4811 tp_features.light_status = 0;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004812 } else {
4813 rc = 0;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004814 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004815
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004816 return rc;
4817}
4818
4819static void light_exit(void)
4820{
4821 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
4822 if (work_pending(&tpacpi_led_thinklight.work))
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004823 flush_workqueue(tpacpi_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004824}
4825
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004826static int light_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004827{
4828 int len = 0;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004829 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004830
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004831 if (!tp_features.light) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004832 len += sprintf(p + len, "status:\t\tnot supported\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004833 } else if (!tp_features.light_status) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004834 len += sprintf(p + len, "status:\t\tunknown\n");
4835 len += sprintf(p + len, "commands:\ton, off\n");
4836 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004837 status = light_get_status();
4838 if (status < 0)
4839 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004841 len += sprintf(p + len, "commands:\ton, off\n");
4842 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843
4844 return len;
4845}
4846
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004847static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004848{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004849 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004850 int newstatus = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004851
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004852 if (!tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004853 return -ENODEV;
4854
Linus Torvalds1da177e2005-04-16 15:20:36 -07004855 while ((cmd = next_cmd(&buf))) {
4856 if (strlencmp(cmd, "on") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004857 newstatus = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004858 } else if (strlencmp(cmd, "off") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004859 newstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860 } else
4861 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004862 }
4863
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004864 return light_set_status(newstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865}
4866
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004867static struct ibm_struct light_driver_data = {
4868 .name = "light",
4869 .read = light_read,
4870 .write = light_write,
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004871 .exit = light_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004872};
4873
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004874/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004875 * CMOS subdriver
4876 */
4877
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004878/* sysfs cmos_command -------------------------------------------------- */
4879static ssize_t cmos_command_store(struct device *dev,
4880 struct device_attribute *attr,
4881 const char *buf, size_t count)
4882{
4883 unsigned long cmos_cmd;
4884 int res;
4885
4886 if (parse_strtoul(buf, 21, &cmos_cmd))
4887 return -EINVAL;
4888
4889 res = issue_thinkpad_cmos_command(cmos_cmd);
4890 return (res)? res : count;
4891}
4892
4893static struct device_attribute dev_attr_cmos_command =
4894 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4895
4896/* --------------------------------------------------------------------- */
4897
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004898static int __init cmos_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004899{
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004900 int res;
4901
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004902 vdbg_printk(TPACPI_DBG_INIT,
4903 "initializing cmos commands subdriver\n");
4904
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004905 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004906
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004907 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4908 str_supported(cmos_handle != NULL));
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004909
4910 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4911 if (res)
4912 return res;
4913
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004914 return (cmos_handle)? 0 : 1;
4915}
4916
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004917static void cmos_exit(void)
4918{
4919 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4920}
4921
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004922static int cmos_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923{
4924 int len = 0;
4925
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004926 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4927 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004928 if (!cmos_handle)
4929 len += sprintf(p + len, "status:\t\tnot supported\n");
4930 else {
4931 len += sprintf(p + len, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004932 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933 }
4934
4935 return len;
4936}
4937
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004938static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004939{
4940 char *cmd;
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004941 int cmos_cmd, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942
4943 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004944 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
4945 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946 /* cmos_cmd set */
4947 } else
4948 return -EINVAL;
4949
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004950 res = issue_thinkpad_cmos_command(cmos_cmd);
4951 if (res)
4952 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004953 }
4954
4955 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004956}
4957
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004958static struct ibm_struct cmos_driver_data = {
4959 .name = "cmos",
4960 .read = cmos_read,
4961 .write = cmos_write,
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004962 .exit = cmos_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004963};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004964
4965/*************************************************************************
4966 * LED subdriver
4967 */
4968
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004969enum led_access_mode {
4970 TPACPI_LED_NONE = 0,
4971 TPACPI_LED_570, /* 570 */
4972 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4973 TPACPI_LED_NEW, /* all others */
4974};
4975
4976enum { /* For TPACPI_LED_OLD */
4977 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
4978 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
4979 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
4980};
4981
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02004982static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004983
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004984TPACPI_HANDLE(led, ec, "SLED", /* 570 */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004985 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
4986 /* T20-22, X20-21 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004987 "LED", /* all others */
4988 ); /* R30, R31 */
4989
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03004990#define TPACPI_LED_NUMLEDS 16
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004991static struct tpacpi_led_classdev *tpacpi_leds;
4992static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
Harvey Harrisone3aa51f2008-05-29 17:51:57 -07004993static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004994 /* there's a limit of 19 chars + NULL before 2.6.26 */
4995 "tpacpi::power",
4996 "tpacpi:orange:batt",
4997 "tpacpi:green:batt",
4998 "tpacpi::dock_active",
4999 "tpacpi::bay_active",
5000 "tpacpi::dock_batt",
5001 "tpacpi::unknown_led",
5002 "tpacpi::standby",
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005003 "tpacpi::dock_status1",
5004 "tpacpi::dock_status2",
5005 "tpacpi::unknown_led2",
5006 "tpacpi::unknown_led3",
5007 "tpacpi::thinkvantage",
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005008};
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005009#define TPACPI_SAFE_LEDS 0x1081U
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005010
5011static inline bool tpacpi_is_led_restricted(const unsigned int led)
5012{
5013#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5014 return false;
5015#else
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005016 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005017#endif
5018}
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005019
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005020static int led_get_status(const unsigned int led)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005021{
5022 int status;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005023 enum led_status_t led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005024
5025 switch (led_supported) {
5026 case TPACPI_LED_570:
5027 if (!acpi_evalf(ec_handle,
5028 &status, "GLED", "dd", 1 << led))
5029 return -EIO;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005030 led_s = (status == 0)?
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005031 TPACPI_LED_OFF :
5032 ((status == 1)?
5033 TPACPI_LED_ON :
5034 TPACPI_LED_BLINK);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005035 tpacpi_led_state_cache[led] = led_s;
5036 return led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005037 default:
5038 return -ENXIO;
5039 }
5040
5041 /* not reached */
5042}
5043
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005044static int led_set_status(const unsigned int led,
5045 const enum led_status_t ledstatus)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005046{
5047 /* off, on, blink. Index is led_status_t */
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005048 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
5049 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005050
5051 int rc = 0;
5052
5053 switch (led_supported) {
5054 case TPACPI_LED_570:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005055 /* 570 */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005056 if (unlikely(led > 7))
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005057 return -EINVAL;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005058 if (unlikely(tpacpi_is_led_restricted(led)))
5059 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005060 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5061 (1 << led), led_sled_arg1[ledstatus]))
5062 rc = -EIO;
5063 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005064 case TPACPI_LED_OLD:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005065 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005066 if (unlikely(led > 7))
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005067 return -EINVAL;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005068 if (unlikely(tpacpi_is_led_restricted(led)))
5069 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005070 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
5071 if (rc >= 0)
5072 rc = ec_write(TPACPI_LED_EC_HLBL,
5073 (ledstatus == TPACPI_LED_BLINK) << led);
5074 if (rc >= 0)
5075 rc = ec_write(TPACPI_LED_EC_HLCL,
5076 (ledstatus != TPACPI_LED_OFF) << led);
5077 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005078 case TPACPI_LED_NEW:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005079 /* all others */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005080 if (unlikely(led >= TPACPI_LED_NUMLEDS))
5081 return -EINVAL;
5082 if (unlikely(tpacpi_is_led_restricted(led)))
5083 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005084 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5085 led, led_led_arg1[ledstatus]))
5086 rc = -EIO;
5087 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005088 default:
5089 rc = -ENXIO;
5090 }
5091
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005092 if (!rc)
5093 tpacpi_led_state_cache[led] = ledstatus;
5094
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005095 return rc;
5096}
5097
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005098static void led_set_status_worker(struct work_struct *work)
5099{
5100 struct tpacpi_led_classdev *data =
5101 container_of(work, struct tpacpi_led_classdev, work);
5102
5103 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005104 led_set_status(data->led, data->new_state);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005105}
5106
5107static void led_sysfs_set(struct led_classdev *led_cdev,
5108 enum led_brightness brightness)
5109{
5110 struct tpacpi_led_classdev *data = container_of(led_cdev,
5111 struct tpacpi_led_classdev, led_classdev);
5112
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005113 if (brightness == LED_OFF)
5114 data->new_state = TPACPI_LED_OFF;
5115 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
5116 data->new_state = TPACPI_LED_ON;
5117 else
5118 data->new_state = TPACPI_LED_BLINK;
5119
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03005120 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005121}
5122
5123static int led_sysfs_blink_set(struct led_classdev *led_cdev,
5124 unsigned long *delay_on, unsigned long *delay_off)
5125{
5126 struct tpacpi_led_classdev *data = container_of(led_cdev,
5127 struct tpacpi_led_classdev, led_classdev);
5128
5129 /* Can we choose the flash rate? */
5130 if (*delay_on == 0 && *delay_off == 0) {
5131 /* yes. set them to the hardware blink rate (1 Hz) */
5132 *delay_on = 500; /* ms */
5133 *delay_off = 500; /* ms */
5134 } else if ((*delay_on != 500) || (*delay_off != 500))
5135 return -EINVAL;
5136
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005137 data->new_state = TPACPI_LED_BLINK;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03005138 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005139
5140 return 0;
5141}
5142
5143static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
5144{
5145 int rc;
5146
5147 struct tpacpi_led_classdev *data = container_of(led_cdev,
5148 struct tpacpi_led_classdev, led_classdev);
5149
5150 rc = led_get_status(data->led);
5151
5152 if (rc == TPACPI_LED_OFF || rc < 0)
5153 rc = LED_OFF; /* no error handling in led class :( */
5154 else
5155 rc = LED_FULL;
5156
5157 return rc;
5158}
5159
5160static void led_exit(void)
5161{
5162 unsigned int i;
5163
5164 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5165 if (tpacpi_leds[i].led_classdev.name)
5166 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
5167 }
5168
5169 kfree(tpacpi_leds);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005170}
5171
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005172static int __init tpacpi_init_led(unsigned int led)
5173{
5174 int rc;
5175
5176 tpacpi_leds[led].led = led;
5177
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005178 /* LEDs with no name don't get registered */
5179 if (!tpacpi_led_names[led])
5180 return 0;
5181
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005182 tpacpi_leds[led].led_classdev.brightness_set = &led_sysfs_set;
5183 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5184 if (led_supported == TPACPI_LED_570)
5185 tpacpi_leds[led].led_classdev.brightness_get =
5186 &led_sysfs_get;
5187
5188 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5189
5190 INIT_WORK(&tpacpi_leds[led].work, led_set_status_worker);
5191
5192 rc = led_classdev_register(&tpacpi_pdev->dev,
5193 &tpacpi_leds[led].led_classdev);
5194 if (rc < 0)
5195 tpacpi_leds[led].led_classdev.name = NULL;
5196
5197 return rc;
5198}
5199
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005200static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
5201 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5202 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5203 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5204
5205 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5206 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5207 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5208 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5209 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5210 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5211 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5212 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5213
5214 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5215 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5216 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5217 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5218 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5219
5220 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5221 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5222 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5223 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5224
5225 /* (1) - may have excess leds enabled on MSB */
5226
5227 /* Defaults (order matters, keep last, don't reorder!) */
5228 { /* Lenovo */
5229 .vendor = PCI_VENDOR_ID_LENOVO,
5230 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5231 .quirks = 0x1fffU,
5232 },
5233 { /* IBM ThinkPads with no EC version string */
5234 .vendor = PCI_VENDOR_ID_IBM,
5235 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
5236 .quirks = 0x00ffU,
5237 },
5238 { /* IBM ThinkPads with EC version string */
5239 .vendor = PCI_VENDOR_ID_IBM,
5240 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5241 .quirks = 0x00bfU,
5242 },
5243};
5244
5245#undef TPACPI_LEDQ_IBM
5246#undef TPACPI_LEDQ_LNV
5247
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005248static int __init led_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005249{
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005250 unsigned int i;
5251 int rc;
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005252 unsigned long useful_leds;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005253
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005254 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
5255
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005256 TPACPI_ACPIHANDLE_INIT(led);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005257
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005258 if (!led_handle)
5259 /* led not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005260 led_supported = TPACPI_LED_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005261 else if (strlencmp(led_path, "SLED") == 0)
5262 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005263 led_supported = TPACPI_LED_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005264 else if (strlencmp(led_path, "SYSL") == 0)
5265 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005266 led_supported = TPACPI_LED_OLD;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005267 else
5268 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005269 led_supported = TPACPI_LED_NEW;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005270
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005271 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
5272 str_supported(led_supported), led_supported);
5273
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005274 if (led_supported == TPACPI_LED_NONE)
5275 return 1;
5276
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005277 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
5278 GFP_KERNEL);
5279 if (!tpacpi_leds) {
5280 printk(TPACPI_ERR "Out of memory for LED data\n");
5281 return -ENOMEM;
5282 }
5283
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005284 useful_leds = tpacpi_check_quirks(led_useful_qtable,
5285 ARRAY_SIZE(led_useful_qtable));
5286
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005287 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005288 if (!tpacpi_is_led_restricted(i) &&
5289 test_bit(i, &useful_leds)) {
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005290 rc = tpacpi_init_led(i);
5291 if (rc < 0) {
5292 led_exit();
5293 return rc;
5294 }
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005295 }
5296 }
5297
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005298#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005299 printk(TPACPI_NOTICE
5300 "warning: userspace override of important "
5301 "firmware LEDs is enabled\n");
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005302#endif
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005303 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005304}
5305
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005306#define str_led_status(s) \
5307 ((s) == TPACPI_LED_OFF ? "off" : \
5308 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005309
5310static int led_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005311{
5312 int len = 0;
5313
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005314 if (!led_supported) {
5315 len += sprintf(p + len, "status:\t\tnot supported\n");
5316 return len;
5317 }
5318 len += sprintf(p + len, "status:\t\tsupported\n");
5319
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005320 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005321 /* 570 */
5322 int i, status;
5323 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005324 status = led_get_status(i);
5325 if (status < 0)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005326 return -EIO;
5327 len += sprintf(p + len, "%d:\t\t%s\n",
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005328 i, str_led_status(status));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005329 }
5330 }
5331
Linus Torvalds1da177e2005-04-16 15:20:36 -07005332 len += sprintf(p + len, "commands:\t"
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005333 "<led> on, <led> off, <led> blink (<led> is 0-15)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005334
5335 return len;
5336}
5337
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005338static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005339{
5340 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005341 int led, rc;
5342 enum led_status_t s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005343
5344 if (!led_supported)
5345 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005346
5347 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005348 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 15)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005349 return -EINVAL;
5350
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005351 if (strstr(cmd, "off")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005352 s = TPACPI_LED_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005353 } else if (strstr(cmd, "on")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005354 s = TPACPI_LED_ON;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005355 } else if (strstr(cmd, "blink")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005356 s = TPACPI_LED_BLINK;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005357 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005358 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005359 }
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005360
5361 rc = led_set_status(led, s);
5362 if (rc < 0)
5363 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005364 }
5365
5366 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005367}
5368
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005369static struct ibm_struct led_driver_data = {
5370 .name = "led",
5371 .read = led_read,
5372 .write = led_write,
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005373 .exit = led_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005374};
5375
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005376/*************************************************************************
5377 * Beep subdriver
5378 */
5379
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005380TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005381
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005382#define TPACPI_BEEP_Q1 0x0001
5383
5384static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
5385 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
5386 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
5387};
5388
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005389static int __init beep_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005390{
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005391 unsigned long quirks;
5392
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005393 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
5394
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005395 TPACPI_ACPIHANDLE_INIT(beep);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005396
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005397 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
5398 str_supported(beep_handle != NULL));
5399
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005400 quirks = tpacpi_check_quirks(beep_quirk_table,
5401 ARRAY_SIZE(beep_quirk_table));
5402
5403 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
5404
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005405 return (beep_handle)? 0 : 1;
5406}
5407
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005408static int beep_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005409{
5410 int len = 0;
5411
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005412 if (!beep_handle)
5413 len += sprintf(p + len, "status:\t\tnot supported\n");
5414 else {
5415 len += sprintf(p + len, "status:\t\tsupported\n");
5416 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
5417 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005418
5419 return len;
5420}
5421
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005422static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005423{
5424 char *cmd;
5425 int beep_cmd;
5426
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005427 if (!beep_handle)
5428 return -ENODEV;
5429
Linus Torvalds1da177e2005-04-16 15:20:36 -07005430 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005431 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
5432 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005433 /* beep_cmd set */
5434 } else
5435 return -EINVAL;
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005436 if (tp_features.beep_needs_two_args) {
5437 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
5438 beep_cmd, 0))
5439 return -EIO;
5440 } else {
5441 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
5442 beep_cmd))
5443 return -EIO;
5444 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005445 }
5446
5447 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005448}
5449
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005450static struct ibm_struct beep_driver_data = {
5451 .name = "beep",
5452 .read = beep_read,
5453 .write = beep_write,
5454};
5455
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005456/*************************************************************************
5457 * Thermal subdriver
5458 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005459
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005460enum thermal_access_mode {
5461 TPACPI_THERMAL_NONE = 0, /* No thermal support */
5462 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
5463 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
5464 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
5465 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
5466};
5467
5468enum { /* TPACPI_THERMAL_TPEC_* */
5469 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
5470 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
5471 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
5472};
5473
5474#define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5475struct ibm_thermal_sensors_struct {
5476 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
5477};
5478
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005479static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005480
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005481/* idx is zero-based */
5482static int thermal_get_sensor(int idx, s32 *value)
5483{
5484 int t;
5485 s8 tmp;
5486 char tmpi[5];
5487
5488 t = TP_EC_THERMAL_TMP0;
5489
5490 switch (thermal_read_mode) {
5491#if TPACPI_MAX_THERMAL_SENSORS >= 16
5492 case TPACPI_THERMAL_TPEC_16:
5493 if (idx >= 8 && idx <= 15) {
5494 t = TP_EC_THERMAL_TMP8;
5495 idx -= 8;
5496 }
5497 /* fallthrough */
5498#endif
5499 case TPACPI_THERMAL_TPEC_8:
5500 if (idx <= 7) {
5501 if (!acpi_ec_read(t + idx, &tmp))
5502 return -EIO;
5503 *value = tmp * 1000;
5504 return 0;
5505 }
5506 break;
5507
5508 case TPACPI_THERMAL_ACPI_UPDT:
5509 if (idx <= 7) {
5510 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5511 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
5512 return -EIO;
5513 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5514 return -EIO;
5515 *value = (t - 2732) * 100;
5516 return 0;
5517 }
5518 break;
5519
5520 case TPACPI_THERMAL_ACPI_TMP07:
5521 if (idx <= 7) {
5522 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5523 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5524 return -EIO;
5525 if (t > 127 || t < -127)
5526 t = TP_EC_THERMAL_TMP_NA;
5527 *value = t * 1000;
5528 return 0;
5529 }
5530 break;
5531
5532 case TPACPI_THERMAL_NONE:
5533 default:
5534 return -ENOSYS;
5535 }
5536
5537 return -EINVAL;
5538}
5539
5540static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5541{
5542 int res, i;
5543 int n;
5544
5545 n = 8;
5546 i = 0;
5547
5548 if (!s)
5549 return -EINVAL;
5550
5551 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5552 n = 16;
5553
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005554 for (i = 0 ; i < n; i++) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005555 res = thermal_get_sensor(i, &s->temp[i]);
5556 if (res)
5557 return res;
5558 }
5559
5560 return n;
5561}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005562
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005563/* sysfs temp##_input -------------------------------------------------- */
5564
5565static ssize_t thermal_temp_input_show(struct device *dev,
5566 struct device_attribute *attr,
5567 char *buf)
5568{
5569 struct sensor_device_attribute *sensor_attr =
5570 to_sensor_dev_attr(attr);
5571 int idx = sensor_attr->index;
5572 s32 value;
5573 int res;
5574
5575 res = thermal_get_sensor(idx, &value);
5576 if (res)
5577 return res;
5578 if (value == TP_EC_THERMAL_TMP_NA * 1000)
5579 return -ENXIO;
5580
5581 return snprintf(buf, PAGE_SIZE, "%d\n", value);
5582}
5583
5584#define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005585 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5586 thermal_temp_input_show, NULL, _idxB)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005587
5588static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5589 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5590 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5591 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5592 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5593 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5594 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5595 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5596 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5597 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5598 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5599 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5600 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5601 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5602 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5603 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5604 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5605};
5606
5607#define THERMAL_ATTRS(X) \
5608 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5609
5610static struct attribute *thermal_temp_input_attr[] = {
5611 THERMAL_ATTRS(8),
5612 THERMAL_ATTRS(9),
5613 THERMAL_ATTRS(10),
5614 THERMAL_ATTRS(11),
5615 THERMAL_ATTRS(12),
5616 THERMAL_ATTRS(13),
5617 THERMAL_ATTRS(14),
5618 THERMAL_ATTRS(15),
5619 THERMAL_ATTRS(0),
5620 THERMAL_ATTRS(1),
5621 THERMAL_ATTRS(2),
5622 THERMAL_ATTRS(3),
5623 THERMAL_ATTRS(4),
5624 THERMAL_ATTRS(5),
5625 THERMAL_ATTRS(6),
5626 THERMAL_ATTRS(7),
5627 NULL
5628};
5629
5630static const struct attribute_group thermal_temp_input16_group = {
5631 .attrs = thermal_temp_input_attr
5632};
5633
5634static const struct attribute_group thermal_temp_input8_group = {
5635 .attrs = &thermal_temp_input_attr[8]
5636};
5637
5638#undef THERMAL_SENSOR_ATTR_TEMP
5639#undef THERMAL_ATTRS
5640
5641/* --------------------------------------------------------------------- */
5642
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005643static int __init thermal_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005644{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005645 u8 t, ta1, ta2;
5646 int i;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005647 int acpi_tmp7;
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005648 int res;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005649
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005650 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5651
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005652 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005653
Henrique de Moraes Holschuh3d6f99c2007-07-18 23:45:46 -03005654 if (thinkpad_id.ec_model) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005655 /*
5656 * Direct EC access mode: sensors at registers
5657 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5658 * non-implemented, thermal sensors return 0x80 when
5659 * not available
5660 */
5661
5662 ta1 = ta2 = 0;
5663 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03005664 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005665 ta1 |= t;
5666 } else {
5667 ta1 = 0;
5668 break;
5669 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03005670 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005671 ta2 |= t;
5672 } else {
5673 ta1 = 0;
5674 break;
5675 }
5676 }
5677 if (ta1 == 0) {
5678 /* This is sheer paranoia, but we handle it anyway */
5679 if (acpi_tmp7) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005680 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005681 "ThinkPad ACPI EC access misbehaving, "
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005682 "falling back to ACPI TMPx access "
5683 "mode\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005684 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005685 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005686 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005687 "ThinkPad ACPI EC access misbehaving, "
5688 "disabling thermal sensors access\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005689 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005690 }
5691 } else {
5692 thermal_read_mode =
5693 (ta2 != 0) ?
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005694 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005695 }
5696 } else if (acpi_tmp7) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005697 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
5698 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005699 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005700 } else {
5701 /* Standard ACPI TMPx access, max 8 sensors */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005702 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005703 }
5704 } else {
5705 /* temperatures not supported on 570, G4x, R30, R31, R32 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005706 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005707 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005708
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005709 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5710 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5711 thermal_read_mode);
5712
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005713 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005714 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005715 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005716 &thermal_temp_input16_group);
5717 if (res)
5718 return res;
5719 break;
5720 case TPACPI_THERMAL_TPEC_8:
5721 case TPACPI_THERMAL_ACPI_TMP07:
5722 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005723 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005724 &thermal_temp_input8_group);
5725 if (res)
5726 return res;
5727 break;
5728 case TPACPI_THERMAL_NONE:
5729 default:
5730 return 1;
5731 }
5732
5733 return 0;
5734}
5735
5736static void thermal_exit(void)
5737{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005738 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005739 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005740 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005741 &thermal_temp_input16_group);
5742 break;
5743 case TPACPI_THERMAL_TPEC_8:
5744 case TPACPI_THERMAL_ACPI_TMP07:
5745 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005746 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005747 &thermal_temp_input16_group);
5748 break;
5749 case TPACPI_THERMAL_NONE:
5750 default:
5751 break;
5752 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005753}
5754
5755static int thermal_read(char *p)
5756{
5757 int len = 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005758 int n, i;
5759 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005760
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005761 n = thermal_get_sensors(&t);
5762 if (unlikely(n < 0))
5763 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005764
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005765 len += sprintf(p + len, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005766
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005767 if (n > 0) {
5768 for (i = 0; i < (n - 1); i++)
5769 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
5770 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
5771 } else
5772 len += sprintf(p + len, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005773
5774 return len;
5775}
5776
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005777static struct ibm_struct thermal_driver_data = {
5778 .name = "thermal",
5779 .read = thermal_read,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005780 .exit = thermal_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005781};
5782
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005783/*************************************************************************
5784 * EC Dump subdriver
5785 */
5786
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005787static u8 ecdump_regs[256];
5788
5789static int ecdump_read(char *p)
5790{
5791 int len = 0;
5792 int i, j;
5793 u8 v;
5794
5795 len += sprintf(p + len, "EC "
5796 " +00 +01 +02 +03 +04 +05 +06 +07"
5797 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
5798 for (i = 0; i < 256; i += 16) {
5799 len += sprintf(p + len, "EC 0x%02x:", i);
5800 for (j = 0; j < 16; j++) {
5801 if (!acpi_ec_read(i + j, &v))
5802 break;
5803 if (v != ecdump_regs[i + j])
5804 len += sprintf(p + len, " *%02x", v);
5805 else
5806 len += sprintf(p + len, " %02x", v);
5807 ecdump_regs[i + j] = v;
5808 }
5809 len += sprintf(p + len, "\n");
5810 if (j != 16)
5811 break;
5812 }
5813
5814 /* These are way too dangerous to advertise openly... */
5815#if 0
5816 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
5817 " (<offset> is 00-ff, <value> is 00-ff)\n");
5818 len += sprintf(p + len, "commands:\t0x<offset> <value> "
5819 " (<offset> is 00-ff, <value> is 0-255)\n");
5820#endif
5821 return len;
5822}
5823
5824static int ecdump_write(char *buf)
5825{
5826 char *cmd;
5827 int i, v;
5828
5829 while ((cmd = next_cmd(&buf))) {
5830 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
5831 /* i and v set */
5832 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
5833 /* i and v set */
5834 } else
5835 return -EINVAL;
5836 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
5837 if (!acpi_ec_write(i, v))
5838 return -EIO;
5839 } else
5840 return -EINVAL;
5841 }
5842
5843 return 0;
5844}
5845
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005846static struct ibm_struct ecdump_driver_data = {
5847 .name = "ecdump",
5848 .read = ecdump_read,
5849 .write = ecdump_write,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03005850 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005851};
5852
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005853/*************************************************************************
5854 * Backlight/brightness subdriver
5855 */
Holger Macht8acb0252006-10-20 14:30:28 -07005856
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005857#define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5858
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005859/*
5860 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
5861 * CMOS NVRAM byte 0x5E, bits 0-3.
5862 *
5863 * EC HBRV (0x31) has the following layout
5864 * Bit 7: unknown function
5865 * Bit 6: unknown function
5866 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5867 * Bit 4: must be set to zero to avoid problems
5868 * Bit 3-0: backlight brightness level
5869 *
5870 * brightness_get_raw returns status data in the HBRV layout
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03005871 *
5872 * WARNING: The X61 has been verified to use HBRV for something else, so
5873 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
5874 * testing on the very early *60 Lenovo models...
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005875 */
5876
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005877enum {
5878 TP_EC_BACKLIGHT = 0x31,
5879
5880 /* TP_EC_BACKLIGHT bitmasks */
5881 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5882 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5883 TP_EC_BACKLIGHT_MAPSW = 0x20,
5884};
5885
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005886enum tpacpi_brightness_access_mode {
5887 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
5888 TPACPI_BRGHT_MODE_EC, /* EC control */
5889 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
5890 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
5891 TPACPI_BRGHT_MODE_MAX
5892};
5893
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03005894static struct backlight_device *ibm_backlight_device;
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005895
5896static enum tpacpi_brightness_access_mode brightness_mode =
5897 TPACPI_BRGHT_MODE_MAX;
5898
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005899static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5900
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005901static struct mutex brightness_mutex;
5902
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005903/* NVRAM brightness access,
5904 * call with brightness_mutex held! */
5905static unsigned int tpacpi_brightness_nvram_get(void)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005906{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005907 u8 lnvram;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005908
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005909 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5910 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5911 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
5912 lnvram &= (tp_features.bright_16levels) ? 0x0f : 0x07;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005913
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005914 return lnvram;
5915}
5916
5917static void tpacpi_brightness_checkpoint_nvram(void)
5918{
5919 u8 lec = 0;
5920 u8 b_nvram;
5921
5922 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
5923 return;
5924
5925 vdbg_printk(TPACPI_DBG_BRGHT,
5926 "trying to checkpoint backlight level to NVRAM...\n");
5927
5928 if (mutex_lock_killable(&brightness_mutex) < 0)
5929 return;
5930
5931 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5932 goto unlock;
5933 lec &= TP_EC_BACKLIGHT_LVLMSK;
5934 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
5935
5936 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5937 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
5938 /* NVRAM needs update */
5939 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
5940 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
5941 b_nvram |= lec;
5942 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
5943 dbg_printk(TPACPI_DBG_BRGHT,
5944 "updated NVRAM backlight level to %u (0x%02x)\n",
5945 (unsigned int) lec, (unsigned int) b_nvram);
5946 } else
5947 vdbg_printk(TPACPI_DBG_BRGHT,
5948 "NVRAM backlight level already is %u (0x%02x)\n",
5949 (unsigned int) lec, (unsigned int) b_nvram);
5950
5951unlock:
5952 mutex_unlock(&brightness_mutex);
5953}
5954
5955
5956/* call with brightness_mutex held! */
5957static int tpacpi_brightness_get_raw(int *status)
5958{
5959 u8 lec = 0;
5960
5961 switch (brightness_mode) {
5962 case TPACPI_BRGHT_MODE_UCMS_STEP:
5963 *status = tpacpi_brightness_nvram_get();
5964 return 0;
5965 case TPACPI_BRGHT_MODE_EC:
5966 case TPACPI_BRGHT_MODE_ECNVRAM:
5967 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
Henrique de Moraes Holschuh2d5e94d2008-04-26 01:02:20 -03005968 return -EIO;
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005969 *status = lec;
5970 return 0;
5971 default:
5972 return -ENXIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005973 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005974}
5975
5976/* call with brightness_mutex held! */
5977/* do NOT call with illegal backlight level value */
5978static int tpacpi_brightness_set_ec(unsigned int value)
5979{
5980 u8 lec = 0;
5981
5982 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5983 return -EIO;
5984
5985 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
5986 (lec & TP_EC_BACKLIGHT_CMDMSK) |
5987 (value & TP_EC_BACKLIGHT_LVLMSK))))
5988 return -EIO;
5989
5990 return 0;
5991}
5992
5993/* call with brightness_mutex held! */
5994static int tpacpi_brightness_set_ucmsstep(unsigned int value)
5995{
5996 int cmos_cmd, inc;
5997 unsigned int current_value, i;
5998
5999 current_value = tpacpi_brightness_nvram_get();
6000
6001 if (value == current_value)
6002 return 0;
6003
6004 cmos_cmd = (value > current_value) ?
6005 TP_CMOS_BRIGHTNESS_UP :
6006 TP_CMOS_BRIGHTNESS_DOWN;
6007 inc = (value > current_value) ? 1 : -1;
6008
6009 for (i = current_value; i != value; i += inc)
6010 if (issue_thinkpad_cmos_command(cmos_cmd))
6011 return -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006012
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006013 return 0;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006014}
6015
6016/* May return EINTR which can always be mapped to ERESTARTSYS */
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006017static int brightness_set(unsigned int value)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006018{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006019 int res;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006020
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006021 if (value > ((tp_features.bright_16levels)? 15 : 7) ||
6022 value < 0)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006023 return -EINVAL;
6024
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006025 vdbg_printk(TPACPI_DBG_BRGHT,
6026 "set backlight level to %d\n", value);
6027
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006028 res = mutex_lock_killable(&brightness_mutex);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006029 if (res < 0)
6030 return res;
6031
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006032 switch (brightness_mode) {
6033 case TPACPI_BRGHT_MODE_EC:
6034 case TPACPI_BRGHT_MODE_ECNVRAM:
6035 res = tpacpi_brightness_set_ec(value);
6036 break;
6037 case TPACPI_BRGHT_MODE_UCMS_STEP:
6038 res = tpacpi_brightness_set_ucmsstep(value);
6039 break;
6040 default:
6041 res = -ENXIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006042 }
6043
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006044 mutex_unlock(&brightness_mutex);
6045 return res;
6046}
6047
6048/* sysfs backlight class ----------------------------------------------- */
6049
6050static int brightness_update_status(struct backlight_device *bd)
6051{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006052 unsigned int level =
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006053 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
6054 bd->props.power == FB_BLANK_UNBLANK) ?
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006055 bd->props.brightness : 0;
6056
6057 dbg_printk(TPACPI_DBG_BRGHT,
6058 "backlight: attempt to set level to %d\n",
6059 level);
6060
6061 /* it is the backlight class's job (caller) to handle
6062 * EINTR and other errors properly */
6063 return brightness_set(level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006064}
Holger Macht8acb0252006-10-20 14:30:28 -07006065
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006066static int brightness_get(struct backlight_device *bd)
6067{
6068 int status, res;
6069
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006070 res = mutex_lock_killable(&brightness_mutex);
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006071 if (res < 0)
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006072 return 0;
6073
6074 res = tpacpi_brightness_get_raw(&status);
6075
6076 mutex_unlock(&brightness_mutex);
6077
6078 if (res < 0)
6079 return 0;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006080
6081 return status & TP_EC_BACKLIGHT_LVLMSK;
6082}
6083
Richard Purdie599a52d2007-02-10 23:07:48 +00006084static struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006085 .get_brightness = brightness_get,
6086 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006087};
6088
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006089/* --------------------------------------------------------------------- */
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03006090
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006091/*
6092 * These are only useful for models that have only one possibility
6093 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6094 * these quirks.
6095 */
6096#define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6097#define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6098#define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6099
6100static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
6101 /* Models with ATI GPUs known to require ECNVRAM mode */
6102 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
6103
Henrique de Moraes Holschuh6da25bf2009-09-12 15:22:11 -03006104 /* Models with ATI GPUs that can use ECNVRAM */
6105 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC),
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006106 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6107 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6108 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6109
Henrique de Moraes Holschuh6da25bf2009-09-12 15:22:11 -03006110 /* Models with Intel Extreme Graphics 2 */
6111 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC),
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006112 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_NOEC),
6113 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_NOEC),
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006114
6115 /* Models with Intel GMA900 */
6116 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
6117 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
6118 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
6119};
6120
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006121static int __init brightness_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006122{
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006123 int b;
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006124 unsigned long quirks;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006125
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006126 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
6127
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03006128 mutex_init(&brightness_mutex);
6129
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006130 quirks = tpacpi_check_quirks(brightness_quirk_table,
6131 ARRAY_SIZE(brightness_quirk_table));
6132
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03006133 /*
6134 * We always attempt to detect acpi support, so as to switch
6135 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6136 * going to publish a backlight interface
6137 */
6138 b = tpacpi_check_std_acpi_brightness_support();
6139 if (b > 0) {
Thomas Renninger2dba1b52008-08-01 17:38:03 +02006140
6141 if (acpi_video_backlight_support()) {
6142 if (brightness_enable > 1) {
6143 printk(TPACPI_NOTICE
6144 "Standard ACPI backlight interface "
6145 "available, not loading native one.\n");
6146 return 1;
6147 } else if (brightness_enable == 1) {
6148 printk(TPACPI_NOTICE
6149 "Backlight control force enabled, even if standard "
6150 "ACPI backlight interface is available\n");
6151 }
6152 } else {
6153 if (brightness_enable > 1) {
6154 printk(TPACPI_NOTICE
6155 "Standard ACPI backlight interface not "
6156 "available, thinkpad_acpi native "
6157 "brightness control enabled\n");
6158 }
Henrique de Moraes Holschuhe11e2112007-10-30 17:46:22 -02006159 }
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02006160 }
6161
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03006162 if (!brightness_enable) {
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006163 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03006164 "brightness support disabled by "
6165 "module parameter\n");
6166 return 1;
6167 }
6168
6169 if (b > 16) {
6170 printk(TPACPI_ERR
6171 "Unsupported brightness interface, "
6172 "please contact %s\n", TPACPI_MAIL);
6173 return 1;
6174 }
6175 if (b == 16)
6176 tp_features.bright_16levels = 1;
6177
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006178 /*
6179 * Check for module parameter bogosity, note that we
6180 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
6181 * able to detect "unspecified"
6182 */
6183 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03006184 return -EINVAL;
6185
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006186 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
6187 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
6188 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006189 if (quirks & TPACPI_BRGHT_Q_EC)
6190 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
6191 else
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006192 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
6193
6194 dbg_printk(TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006195 "driver auto-selected brightness_mode=%d\n",
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006196 brightness_mode);
6197 }
6198
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03006199 /* Safety */
6200 if (thinkpad_id.vendor != PCI_VENDOR_ID_IBM &&
6201 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
6202 brightness_mode == TPACPI_BRGHT_MODE_EC))
6203 return -EINVAL;
6204
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006205 if (tpacpi_brightness_get_raw(&b) < 0)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03006206 return 1;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006207
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02006208 if (tp_features.bright_16levels)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006209 printk(TPACPI_INFO
6210 "detected a 16-level brightness capable ThinkPad\n");
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02006211
Henrique de Moraes Holschuh7d5a0152007-04-24 11:48:20 -03006212 ibm_backlight_device = backlight_device_register(
6213 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
6214 &ibm_backlight_data);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006215 if (IS_ERR(ibm_backlight_device)) {
Henrique de Moraes Holschuh435c47e2009-09-20 14:09:22 -03006216 int rc = PTR_ERR(ibm_backlight_device);
6217 ibm_backlight_device = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006218 printk(TPACPI_ERR "Could not register backlight device\n");
Henrique de Moraes Holschuh435c47e2009-09-20 14:09:22 -03006219 return rc;
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006220 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006221 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6222 "brightness is supported\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006223
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006224 if (quirks & TPACPI_BRGHT_Q_ASK) {
6225 printk(TPACPI_NOTICE
6226 "brightness: will use unverified default: "
6227 "brightness_mode=%d\n", brightness_mode);
6228 printk(TPACPI_NOTICE
6229 "brightness: please report to %s whether it works well "
6230 "or not on your ThinkPad\n", TPACPI_MAIL);
6231 }
6232
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02006233 ibm_backlight_device->props.max_brightness =
6234 (tp_features.bright_16levels)? 15 : 7;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006235 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006236 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00006237
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006238 return 0;
6239}
6240
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006241static void brightness_suspend(pm_message_t state)
6242{
6243 tpacpi_brightness_checkpoint_nvram();
6244}
6245
6246static void brightness_shutdown(void)
6247{
6248 tpacpi_brightness_checkpoint_nvram();
6249}
6250
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006251static void brightness_exit(void)
6252{
6253 if (ibm_backlight_device) {
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006254 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006255 "calling backlight_device_unregister()\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006256 backlight_device_unregister(ibm_backlight_device);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006257 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006258
6259 tpacpi_brightness_checkpoint_nvram();
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006260}
6261
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006262static int brightness_read(char *p)
6263{
6264 int len = 0;
6265 int level;
6266
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006267 level = brightness_get(NULL);
6268 if (level < 0) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006269 len += sprintf(p + len, "level:\t\tunreadable\n");
6270 } else {
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02006271 len += sprintf(p + len, "level:\t\t%d\n", level);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006272 len += sprintf(p + len, "commands:\tup, down\n");
6273 len += sprintf(p + len, "commands:\tlevel <level>"
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02006274 " (<level> is 0-%d)\n",
6275 (tp_features.bright_16levels) ? 15 : 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006276 }
6277
6278 return len;
6279}
6280
6281static int brightness_write(char *buf)
6282{
6283 int level;
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006284 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006285 char *cmd;
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02006286 int max_level = (tp_features.bright_16levels) ? 15 : 7;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006287
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006288 level = brightness_get(NULL);
6289 if (level < 0)
6290 return level;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006291
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006292 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006293 if (strlencmp(cmd, "up") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006294 if (level < max_level)
6295 level++;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006296 } else if (strlencmp(cmd, "down") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006297 if (level > 0)
6298 level--;
6299 } else if (sscanf(cmd, "level %d", &level) == 1 &&
6300 level >= 0 && level <= max_level) {
6301 /* new level set */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006302 } else
6303 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006304 }
6305
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006306 tpacpi_disclose_usertask("procfs brightness",
6307 "set level to %d\n", level);
6308
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006309 /*
6310 * Now we know what the final level should be, so we try to set it.
6311 * Doing it this way makes the syscall restartable in case of EINTR
6312 */
6313 rc = brightness_set(level);
6314 return (rc == -EINTR)? ERESTARTSYS : rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006315}
6316
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006317static struct ibm_struct brightness_driver_data = {
6318 .name = "brightness",
6319 .read = brightness_read,
6320 .write = brightness_write,
6321 .exit = brightness_exit,
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006322 .suspend = brightness_suspend,
6323 .shutdown = brightness_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006324};
6325
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006326/*************************************************************************
6327 * Volume subdriver
6328 */
6329
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006330static int volume_offset = 0x30;
6331
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006332static int volume_read(char *p)
6333{
6334 int len = 0;
6335 u8 level;
6336
6337 if (!acpi_ec_read(volume_offset, &level)) {
6338 len += sprintf(p + len, "level:\t\tunreadable\n");
6339 } else {
6340 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
6341 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
6342 len += sprintf(p + len, "commands:\tup, down, mute\n");
6343 len += sprintf(p + len, "commands:\tlevel <level>"
6344 " (<level> is 0-15)\n");
6345 }
6346
6347 return len;
6348}
6349
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006350static int volume_write(char *buf)
6351{
6352 int cmos_cmd, inc, i;
6353 u8 level, mute;
6354 int new_level, new_mute;
6355 char *cmd;
6356
6357 while ((cmd = next_cmd(&buf))) {
6358 if (!acpi_ec_read(volume_offset, &level))
6359 return -EIO;
6360 new_mute = mute = level & 0x40;
6361 new_level = level = level & 0xf;
6362
6363 if (strlencmp(cmd, "up") == 0) {
6364 if (mute)
6365 new_mute = 0;
6366 else
6367 new_level = level == 15 ? 15 : level + 1;
6368 } else if (strlencmp(cmd, "down") == 0) {
6369 if (mute)
6370 new_mute = 0;
6371 else
6372 new_level = level == 0 ? 0 : level - 1;
6373 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
6374 new_level >= 0 && new_level <= 15) {
6375 /* new_level set */
6376 } else if (strlencmp(cmd, "mute") == 0) {
6377 new_mute = 0x40;
6378 } else
6379 return -EINVAL;
6380
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006381 if (new_level != level) {
6382 /* mute doesn't change */
6383
6384 cmos_cmd = (new_level > level) ?
6385 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006386 inc = new_level > level ? 1 : -1;
6387
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03006388 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006389 !acpi_ec_write(volume_offset, level)))
6390 return -EIO;
6391
6392 for (i = level; i != new_level; i += inc)
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03006393 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006394 !acpi_ec_write(volume_offset, i + inc))
6395 return -EIO;
6396
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006397 if (mute &&
6398 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
6399 !acpi_ec_write(volume_offset, new_level + mute))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006400 return -EIO;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006401 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006402 }
6403
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006404 if (new_mute != mute) {
6405 /* level doesn't change */
6406
6407 cmos_cmd = (new_mute) ?
6408 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006409
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03006410 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006411 !acpi_ec_write(volume_offset, level + new_mute))
6412 return -EIO;
6413 }
6414 }
6415
6416 return 0;
6417}
6418
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006419static struct ibm_struct volume_driver_data = {
6420 .name = "volume",
6421 .read = volume_read,
6422 .write = volume_write,
6423};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006424
6425/*************************************************************************
6426 * Fan subdriver
6427 */
6428
6429/*
6430 * FAN ACCESS MODES
6431 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006432 * TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006433 * ACPI GFAN method: returns fan level
6434 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006435 * see TPACPI_FAN_WR_ACPI_SFAN
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03006436 * EC 0x2f (HFSP) not available if GFAN exists
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006437 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006438 * TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006439 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
6440 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03006441 * EC 0x2f (HFSP) might be available *for reading*, but do not use
6442 * it for writing.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006443 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006444 * TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03006445 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
6446 * Supported on almost all ThinkPads
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006447 *
6448 * Fan speed changes of any sort (including those caused by the
6449 * disengaged mode) are usually done slowly by the firmware as the
6450 * maximum ammount of fan duty cycle change per second seems to be
6451 * limited.
6452 *
6453 * Reading is not available if GFAN exists.
6454 * Writing is not available if SFAN exists.
6455 *
6456 * Bits
6457 * 7 automatic mode engaged;
6458 * (default operation mode of the ThinkPad)
6459 * fan level is ignored in this mode.
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03006460 * 6 full speed mode (takes precedence over bit 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006461 * not available on all thinkpads. May disable
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03006462 * the tachometer while the fan controller ramps up
6463 * the speed (which can take up to a few *minutes*).
6464 * Speeds up fan to 100% duty-cycle, which is far above
6465 * the standard RPM levels. It is not impossible that
6466 * it could cause hardware damage.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006467 * 5-3 unused in some models. Extra bits for fan level
6468 * in others, but still useless as all values above
6469 * 7 map to the same speed as level 7 in these models.
6470 * 2-0 fan level (0..7 usually)
6471 * 0x00 = stop
6472 * 0x07 = max (set when temperatures critical)
6473 * Some ThinkPads may have other levels, see
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006474 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006475 *
6476 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
6477 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
6478 * does so, its initial value is meaningless (0x07).
6479 *
6480 * For firmware bugs, refer to:
6481 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
6482 *
6483 * ----
6484 *
6485 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
6486 * Main fan tachometer reading (in RPM)
6487 *
6488 * This register is present on all ThinkPads with a new-style EC, and
6489 * it is known not to be present on the A21m/e, and T22, as there is
6490 * something else in offset 0x84 according to the ACPI DSDT. Other
6491 * ThinkPads from this same time period (and earlier) probably lack the
6492 * tachometer as well.
6493 *
Nick Andrew877d0312009-01-26 11:06:57 +01006494 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006495 * was never fixed by IBM to report the EC firmware version string
6496 * probably support the tachometer (like the early X models), so
6497 * detecting it is quite hard. We need more data to know for sure.
6498 *
6499 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
6500 * might result.
6501 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03006502 * FIRMWARE BUG: may go stale while the EC is switching to full speed
6503 * mode.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006504 *
6505 * For firmware bugs, refer to:
6506 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
6507 *
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03006508 * ----
6509 *
6510 * ThinkPad EC register 0x31 bit 0 (only on select models)
6511 *
6512 * When bit 0 of EC register 0x31 is zero, the tachometer registers
6513 * show the speed of the main fan. When bit 0 of EC register 0x31
6514 * is one, the tachometer registers show the speed of the auxiliary
6515 * fan.
6516 *
6517 * Fan control seems to affect both fans, regardless of the state
6518 * of this bit.
6519 *
6520 * So far, only the firmware for the X60/X61 non-tablet versions
6521 * seem to support this (firmware TP-7M).
6522 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006523 * TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006524 * ThinkPad X31, X40, X41. Not available in the X60.
6525 *
6526 * FANS ACPI handle: takes three arguments: low speed, medium speed,
6527 * high speed. ACPI DSDT seems to map these three speeds to levels
6528 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
6529 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
6530 *
6531 * The speeds are stored on handles
6532 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
6533 *
6534 * There are three default speed sets, acessible as handles:
6535 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
6536 *
6537 * ACPI DSDT switches which set is in use depending on various
6538 * factors.
6539 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006540 * TPACPI_FAN_WR_TPEC is also available and should be used to
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006541 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
6542 * but the ACPI tables just mention level 7.
6543 */
6544
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006545enum { /* Fan control constants */
6546 fan_status_offset = 0x2f, /* EC register 0x2f */
6547 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
6548 * 0x84 must be read before 0x85 */
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03006549 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
6550 bit 0 selects which fan is active */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006551
6552 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
6553 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
6554
6555 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
6556};
6557
6558enum fan_status_access_mode {
6559 TPACPI_FAN_NONE = 0, /* No fan status or control */
6560 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
6561 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
6562};
6563
6564enum fan_control_access_mode {
6565 TPACPI_FAN_WR_NONE = 0, /* No fan control */
6566 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
6567 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
6568 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
6569};
6570
6571enum fan_control_commands {
6572 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
6573 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
6574 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
6575 * and also watchdog cmd */
6576};
6577
6578static int fan_control_allowed;
6579
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006580static enum fan_status_access_mode fan_status_access_mode;
6581static enum fan_control_access_mode fan_control_access_mode;
6582static enum fan_control_commands fan_control_commands;
6583
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02006584static u8 fan_control_initial_status;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006585static u8 fan_control_desired_level;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006586static u8 fan_control_resume_level;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02006587static int fan_watchdog_maxinterval;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006588
6589static struct mutex fan_mutex;
6590
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006591static void fan_watchdog_fire(struct work_struct *ignored);
Len Brown25c68a32006-12-08 04:43:41 -05006592static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02006593
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006594TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
6595TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006596 "\\FSPD", /* 600e/x, 770e, 770x */
6597 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006598TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006599 "JFNS", /* 770x-JL */
6600 ); /* all others */
6601
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006602/*
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006603 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
6604 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
6605 * be in auto mode (0x80).
6606 *
6607 * This is corrected by any write to HFSP either by the driver, or
6608 * by the firmware.
6609 *
6610 * We assume 0x07 really means auto mode while this quirk is active,
6611 * as this is far more likely than the ThinkPad being in level 7,
6612 * which is only used by the firmware during thermal emergencies.
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03006613 *
6614 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
6615 * TP-70 (T43, R52), which are known to be buggy.
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006616 */
6617
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03006618static void fan_quirk1_setup(void)
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006619{
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006620 if (fan_control_initial_status == 0x07) {
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03006621 printk(TPACPI_NOTICE
6622 "fan_init: initial fan status is unknown, "
6623 "assuming it is in auto mode\n");
6624 tp_features.fan_ctrl_status_undef = 1;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006625 }
6626}
6627
6628static void fan_quirk1_handle(u8 *fan_status)
6629{
6630 if (unlikely(tp_features.fan_ctrl_status_undef)) {
6631 if (*fan_status != fan_control_initial_status) {
6632 /* something changed the HFSP regisnter since
6633 * driver init time, so it is not undefined
6634 * anymore */
6635 tp_features.fan_ctrl_status_undef = 0;
6636 } else {
6637 /* Return most likely status. In fact, it
6638 * might be the only possible status */
6639 *fan_status = TP_EC_FAN_AUTO;
6640 }
6641 }
6642}
6643
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03006644/* Select main fan on X60/X61, NOOP on others */
6645static bool fan_select_fan1(void)
6646{
6647 if (tp_features.second_fan) {
6648 u8 val;
6649
6650 if (ec_read(fan_select_offset, &val) < 0)
6651 return false;
6652 val &= 0xFEU;
6653 if (ec_write(fan_select_offset, val) < 0)
6654 return false;
6655 }
6656 return true;
6657}
6658
6659/* Select secondary fan on X60/X61 */
6660static bool fan_select_fan2(void)
6661{
6662 u8 val;
6663
6664 if (!tp_features.second_fan)
6665 return false;
6666
6667 if (ec_read(fan_select_offset, &val) < 0)
6668 return false;
6669 val |= 0x01U;
6670 if (ec_write(fan_select_offset, val) < 0)
6671 return false;
6672
6673 return true;
6674}
6675
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006676/*
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006677 * Call with fan_mutex held
6678 */
6679static void fan_update_desired_level(u8 status)
6680{
6681 if ((status &
6682 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
6683 if (status > 7)
6684 fan_control_desired_level = 7;
6685 else
6686 fan_control_desired_level = status;
6687 }
6688}
6689
6690static int fan_get_status(u8 *status)
6691{
6692 u8 s;
6693
6694 /* TODO:
6695 * Add TPACPI_FAN_RD_ACPI_FANS ? */
6696
6697 switch (fan_status_access_mode) {
6698 case TPACPI_FAN_RD_ACPI_GFAN:
6699 /* 570, 600e/x, 770e, 770x */
6700
6701 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
6702 return -EIO;
6703
6704 if (likely(status))
6705 *status = s & 0x07;
6706
6707 break;
6708
6709 case TPACPI_FAN_RD_TPEC:
6710 /* all except 570, 600e/x, 770e, 770x */
6711 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
6712 return -EIO;
6713
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006714 if (likely(status)) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006715 *status = s;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006716 fan_quirk1_handle(status);
6717 }
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006718
6719 break;
6720
6721 default:
6722 return -ENXIO;
6723 }
6724
6725 return 0;
6726}
6727
6728static int fan_get_status_safe(u8 *status)
6729{
6730 int rc;
6731 u8 s;
6732
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006733 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006734 return -ERESTARTSYS;
6735 rc = fan_get_status(&s);
6736 if (!rc)
6737 fan_update_desired_level(s);
6738 mutex_unlock(&fan_mutex);
6739
6740 if (status)
6741 *status = s;
6742
6743 return rc;
6744}
6745
6746static int fan_get_speed(unsigned int *speed)
6747{
6748 u8 hi, lo;
6749
6750 switch (fan_status_access_mode) {
6751 case TPACPI_FAN_RD_TPEC:
6752 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03006753 if (unlikely(!fan_select_fan1()))
6754 return -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006755 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
6756 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
6757 return -EIO;
6758
6759 if (likely(speed))
6760 *speed = (hi << 8) | lo;
6761
6762 break;
6763
6764 default:
6765 return -ENXIO;
6766 }
6767
6768 return 0;
6769}
6770
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03006771static int fan2_get_speed(unsigned int *speed)
6772{
6773 u8 hi, lo;
6774 bool rc;
6775
6776 switch (fan_status_access_mode) {
6777 case TPACPI_FAN_RD_TPEC:
6778 /* all except 570, 600e/x, 770e, 770x */
6779 if (unlikely(!fan_select_fan2()))
6780 return -EIO;
6781 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
6782 !acpi_ec_read(fan_rpm_offset + 1, &hi);
6783 fan_select_fan1(); /* play it safe */
6784 if (rc)
6785 return -EIO;
6786
6787 if (likely(speed))
6788 *speed = (hi << 8) | lo;
6789
6790 break;
6791
6792 default:
6793 return -ENXIO;
6794 }
6795
6796 return 0;
6797}
6798
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006799static int fan_set_level(int level)
6800{
6801 if (!fan_control_allowed)
6802 return -EPERM;
6803
6804 switch (fan_control_access_mode) {
6805 case TPACPI_FAN_WR_ACPI_SFAN:
6806 if (level >= 0 && level <= 7) {
6807 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
6808 return -EIO;
6809 } else
6810 return -EINVAL;
6811 break;
6812
6813 case TPACPI_FAN_WR_ACPI_FANS:
6814 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006815 if (!(level & TP_EC_FAN_AUTO) &&
6816 !(level & TP_EC_FAN_FULLSPEED) &&
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006817 ((level < 0) || (level > 7)))
6818 return -EINVAL;
6819
6820 /* safety net should the EC not support AUTO
6821 * or FULLSPEED mode bits and just ignore them */
6822 if (level & TP_EC_FAN_FULLSPEED)
6823 level |= 7; /* safety min speed 7 */
Roel Kluin547266e2008-02-05 00:24:56 +01006824 else if (level & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006825 level |= 4; /* safety min speed 4 */
6826
6827 if (!acpi_ec_write(fan_status_offset, level))
6828 return -EIO;
6829 else
6830 tp_features.fan_ctrl_status_undef = 0;
6831 break;
6832
6833 default:
6834 return -ENXIO;
6835 }
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00006836
6837 vdbg_printk(TPACPI_DBG_FAN,
6838 "fan control: set fan control register to 0x%02x\n", level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006839 return 0;
6840}
6841
6842static int fan_set_level_safe(int level)
6843{
6844 int rc;
6845
6846 if (!fan_control_allowed)
6847 return -EPERM;
6848
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006849 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006850 return -ERESTARTSYS;
6851
6852 if (level == TPACPI_FAN_LAST_LEVEL)
6853 level = fan_control_desired_level;
6854
6855 rc = fan_set_level(level);
6856 if (!rc)
6857 fan_update_desired_level(level);
6858
6859 mutex_unlock(&fan_mutex);
6860 return rc;
6861}
6862
6863static int fan_set_enable(void)
6864{
6865 u8 s;
6866 int rc;
6867
6868 if (!fan_control_allowed)
6869 return -EPERM;
6870
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006871 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006872 return -ERESTARTSYS;
6873
6874 switch (fan_control_access_mode) {
6875 case TPACPI_FAN_WR_ACPI_FANS:
6876 case TPACPI_FAN_WR_TPEC:
6877 rc = fan_get_status(&s);
6878 if (rc < 0)
6879 break;
6880
6881 /* Don't go out of emergency fan mode */
6882 if (s != 7) {
6883 s &= 0x07;
6884 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
6885 }
6886
6887 if (!acpi_ec_write(fan_status_offset, s))
6888 rc = -EIO;
6889 else {
6890 tp_features.fan_ctrl_status_undef = 0;
6891 rc = 0;
6892 }
6893 break;
6894
6895 case TPACPI_FAN_WR_ACPI_SFAN:
6896 rc = fan_get_status(&s);
6897 if (rc < 0)
6898 break;
6899
6900 s &= 0x07;
6901
6902 /* Set fan to at least level 4 */
6903 s |= 4;
6904
6905 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006906 rc = -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006907 else
6908 rc = 0;
6909 break;
6910
6911 default:
6912 rc = -ENXIO;
6913 }
6914
6915 mutex_unlock(&fan_mutex);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00006916
6917 if (!rc)
6918 vdbg_printk(TPACPI_DBG_FAN,
6919 "fan control: set fan control register to 0x%02x\n",
6920 s);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006921 return rc;
6922}
6923
6924static int fan_set_disable(void)
6925{
6926 int rc;
6927
6928 if (!fan_control_allowed)
6929 return -EPERM;
6930
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006931 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006932 return -ERESTARTSYS;
6933
6934 rc = 0;
6935 switch (fan_control_access_mode) {
6936 case TPACPI_FAN_WR_ACPI_FANS:
6937 case TPACPI_FAN_WR_TPEC:
6938 if (!acpi_ec_write(fan_status_offset, 0x00))
6939 rc = -EIO;
6940 else {
6941 fan_control_desired_level = 0;
6942 tp_features.fan_ctrl_status_undef = 0;
6943 }
6944 break;
6945
6946 case TPACPI_FAN_WR_ACPI_SFAN:
6947 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
6948 rc = -EIO;
6949 else
6950 fan_control_desired_level = 0;
6951 break;
6952
6953 default:
6954 rc = -ENXIO;
6955 }
6956
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00006957 if (!rc)
6958 vdbg_printk(TPACPI_DBG_FAN,
6959 "fan control: set fan control register to 0\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006960
6961 mutex_unlock(&fan_mutex);
6962 return rc;
6963}
6964
6965static int fan_set_speed(int speed)
6966{
6967 int rc;
6968
6969 if (!fan_control_allowed)
6970 return -EPERM;
6971
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006972 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006973 return -ERESTARTSYS;
6974
6975 rc = 0;
6976 switch (fan_control_access_mode) {
6977 case TPACPI_FAN_WR_ACPI_FANS:
6978 if (speed >= 0 && speed <= 65535) {
6979 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
6980 speed, speed, speed))
6981 rc = -EIO;
6982 } else
6983 rc = -EINVAL;
6984 break;
6985
6986 default:
6987 rc = -ENXIO;
6988 }
6989
6990 mutex_unlock(&fan_mutex);
6991 return rc;
6992}
6993
6994static void fan_watchdog_reset(void)
6995{
6996 static int fan_watchdog_active;
6997
6998 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
6999 return;
7000
7001 if (fan_watchdog_active)
7002 cancel_delayed_work(&fan_watchdog_task);
7003
7004 if (fan_watchdog_maxinterval > 0 &&
7005 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
7006 fan_watchdog_active = 1;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03007007 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007008 msecs_to_jiffies(fan_watchdog_maxinterval
7009 * 1000))) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007010 printk(TPACPI_ERR
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03007011 "failed to queue the fan watchdog, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007012 "watchdog will not trigger\n");
7013 }
7014 } else
7015 fan_watchdog_active = 0;
7016}
7017
7018static void fan_watchdog_fire(struct work_struct *ignored)
7019{
7020 int rc;
7021
7022 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
7023 return;
7024
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007025 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007026 rc = fan_set_enable();
7027 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007028 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007029 "will try again later...\n", -rc);
7030 /* reschedule for later */
7031 fan_watchdog_reset();
7032 }
7033}
7034
7035/*
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007036 * SYSFS fan layout: hwmon compatible (device)
7037 *
7038 * pwm*_enable:
7039 * 0: "disengaged" mode
7040 * 1: manual mode
7041 * 2: native EC "auto" mode (recommended, hardware default)
7042 *
7043 * pwm*: set speed in manual mode, ignored otherwise.
7044 * 0 is level 0; 255 is level 7. Intermediate points done with linear
7045 * interpolation.
7046 *
7047 * fan*_input: tachometer reading, RPM
7048 *
7049 *
7050 * SYSFS fan layout: extensions
7051 *
7052 * fan_watchdog (driver):
7053 * fan watchdog interval in seconds, 0 disables (default), max 120
7054 */
7055
7056/* sysfs fan pwm1_enable ----------------------------------------------- */
7057static ssize_t fan_pwm1_enable_show(struct device *dev,
7058 struct device_attribute *attr,
7059 char *buf)
7060{
7061 int res, mode;
7062 u8 status;
7063
7064 res = fan_get_status_safe(&status);
7065 if (res)
7066 return res;
7067
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007068 if (status & TP_EC_FAN_FULLSPEED) {
7069 mode = 0;
7070 } else if (status & TP_EC_FAN_AUTO) {
7071 mode = 2;
7072 } else
7073 mode = 1;
7074
7075 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
7076}
7077
7078static ssize_t fan_pwm1_enable_store(struct device *dev,
7079 struct device_attribute *attr,
7080 const char *buf, size_t count)
7081{
7082 unsigned long t;
7083 int res, level;
7084
7085 if (parse_strtoul(buf, 2, &t))
7086 return -EINVAL;
7087
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007088 tpacpi_disclose_usertask("hwmon pwm1_enable",
7089 "set fan mode to %lu\n", t);
7090
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007091 switch (t) {
7092 case 0:
7093 level = TP_EC_FAN_FULLSPEED;
7094 break;
7095 case 1:
7096 level = TPACPI_FAN_LAST_LEVEL;
7097 break;
7098 case 2:
7099 level = TP_EC_FAN_AUTO;
7100 break;
7101 case 3:
7102 /* reserved for software-controlled auto mode */
7103 return -ENOSYS;
7104 default:
7105 return -EINVAL;
7106 }
7107
7108 res = fan_set_level_safe(level);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03007109 if (res == -ENXIO)
7110 return -EINVAL;
7111 else if (res < 0)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007112 return res;
7113
7114 fan_watchdog_reset();
7115
7116 return count;
7117}
7118
7119static struct device_attribute dev_attr_fan_pwm1_enable =
7120 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
7121 fan_pwm1_enable_show, fan_pwm1_enable_store);
7122
7123/* sysfs fan pwm1 ------------------------------------------------------ */
7124static ssize_t fan_pwm1_show(struct device *dev,
7125 struct device_attribute *attr,
7126 char *buf)
7127{
7128 int res;
7129 u8 status;
7130
7131 res = fan_get_status_safe(&status);
7132 if (res)
7133 return res;
7134
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007135 if ((status &
7136 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
7137 status = fan_control_desired_level;
7138
7139 if (status > 7)
7140 status = 7;
7141
7142 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
7143}
7144
7145static ssize_t fan_pwm1_store(struct device *dev,
7146 struct device_attribute *attr,
7147 const char *buf, size_t count)
7148{
7149 unsigned long s;
7150 int rc;
7151 u8 status, newlevel;
7152
7153 if (parse_strtoul(buf, 255, &s))
7154 return -EINVAL;
7155
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007156 tpacpi_disclose_usertask("hwmon pwm1",
7157 "set fan speed to %lu\n", s);
7158
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007159 /* scale down from 0-255 to 0-7 */
7160 newlevel = (s >> 5) & 0x07;
7161
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007162 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02007163 return -ERESTARTSYS;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007164
7165 rc = fan_get_status(&status);
7166 if (!rc && (status &
7167 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7168 rc = fan_set_level(newlevel);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03007169 if (rc == -ENXIO)
7170 rc = -EINVAL;
7171 else if (!rc) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007172 fan_update_desired_level(newlevel);
Henrique de Moraes Holschuhca4ac2f2007-04-27 22:00:11 -03007173 fan_watchdog_reset();
7174 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007175 }
7176
7177 mutex_unlock(&fan_mutex);
7178 return (rc)? rc : count;
7179}
7180
7181static struct device_attribute dev_attr_fan_pwm1 =
7182 __ATTR(pwm1, S_IWUSR | S_IRUGO,
7183 fan_pwm1_show, fan_pwm1_store);
7184
7185/* sysfs fan fan1_input ------------------------------------------------ */
7186static ssize_t fan_fan1_input_show(struct device *dev,
7187 struct device_attribute *attr,
7188 char *buf)
7189{
7190 int res;
7191 unsigned int speed;
7192
7193 res = fan_get_speed(&speed);
7194 if (res < 0)
7195 return res;
7196
7197 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7198}
7199
7200static struct device_attribute dev_attr_fan_fan1_input =
7201 __ATTR(fan1_input, S_IRUGO,
7202 fan_fan1_input_show, NULL);
7203
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007204/* sysfs fan fan2_input ------------------------------------------------ */
7205static ssize_t fan_fan2_input_show(struct device *dev,
7206 struct device_attribute *attr,
7207 char *buf)
7208{
7209 int res;
7210 unsigned int speed;
7211
7212 res = fan2_get_speed(&speed);
7213 if (res < 0)
7214 return res;
7215
7216 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7217}
7218
7219static struct device_attribute dev_attr_fan_fan2_input =
7220 __ATTR(fan2_input, S_IRUGO,
7221 fan_fan2_input_show, NULL);
7222
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007223/* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007224static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
7225 char *buf)
7226{
7227 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
7228}
7229
7230static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
7231 const char *buf, size_t count)
7232{
7233 unsigned long t;
7234
7235 if (parse_strtoul(buf, 120, &t))
7236 return -EINVAL;
7237
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03007238 if (!fan_control_allowed)
7239 return -EPERM;
7240
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007241 fan_watchdog_maxinterval = t;
7242 fan_watchdog_reset();
7243
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007244 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
7245
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007246 return count;
7247}
7248
7249static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
7250 fan_fan_watchdog_show, fan_fan_watchdog_store);
7251
7252/* --------------------------------------------------------------------- */
7253static struct attribute *fan_attributes[] = {
7254 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
7255 &dev_attr_fan_fan1_input.attr,
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007256 NULL, /* for fan2_input */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007257 NULL
7258};
7259
7260static const struct attribute_group fan_attr_group = {
7261 .attrs = fan_attributes,
7262};
7263
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007264#define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
7265#define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007266
7267#define TPACPI_FAN_QI(__id1, __id2, __quirks) \
7268 { .vendor = PCI_VENDOR_ID_IBM, \
7269 .bios = TPACPI_MATCH_ANY, \
7270 .ec = TPID(__id1, __id2), \
7271 .quirks = __quirks }
7272
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007273#define TPACPI_FAN_QL(__id1, __id2, __quirks) \
7274 { .vendor = PCI_VENDOR_ID_LENOVO, \
7275 .bios = TPACPI_MATCH_ANY, \
7276 .ec = TPID(__id1, __id2), \
7277 .quirks = __quirks }
7278
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007279static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
7280 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
7281 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
7282 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
7283 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007284 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007285};
7286
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007287#undef TPACPI_FAN_QL
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007288#undef TPACPI_FAN_QI
7289
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007290static int __init fan_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007291{
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007292 int rc;
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007293 unsigned long quirks;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007294
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007295 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7296 "initializing fan subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007297
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03007298 mutex_init(&fan_mutex);
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007299 fan_status_access_mode = TPACPI_FAN_NONE;
7300 fan_control_access_mode = TPACPI_FAN_WR_NONE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007301 fan_control_commands = 0;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007302 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03007303 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007304 tp_features.second_fan = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007305 fan_control_desired_level = 7;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007306
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007307 TPACPI_ACPIHANDLE_INIT(fans);
7308 TPACPI_ACPIHANDLE_INIT(gfan);
7309 TPACPI_ACPIHANDLE_INIT(sfan);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007310
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007311 quirks = tpacpi_check_quirks(fan_quirk_table,
7312 ARRAY_SIZE(fan_quirk_table));
7313
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007314 if (gfan_handle) {
7315 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007316 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007317 } else {
7318 /* all other ThinkPads: note that even old-style
7319 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02007320 if (likely(acpi_ec_read(fan_status_offset,
7321 &fan_control_initial_status))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007322 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007323 if (quirks & TPACPI_FAN_Q1)
7324 fan_quirk1_setup();
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007325 if (quirks & TPACPI_FAN_2FAN) {
7326 tp_features.second_fan = 1;
7327 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7328 "secondary fan support enabled\n");
7329 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007330 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007331 printk(TPACPI_ERR
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007332 "ThinkPad ACPI EC access misbehaving, "
7333 "fan status and control unavailable\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007334 return 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007335 }
7336 }
7337
7338 if (sfan_handle) {
7339 /* 570, 770x-JL */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007340 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02007341 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007342 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007343 } else {
7344 if (!gfan_handle) {
7345 /* gfan without sfan means no fan control */
7346 /* all other models implement TP EC 0x2f control */
7347
7348 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02007349 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007350 fan_control_access_mode =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007351 TPACPI_FAN_WR_ACPI_FANS;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007352 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007353 TPACPI_FAN_CMD_SPEED |
7354 TPACPI_FAN_CMD_LEVEL |
7355 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007356 } else {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007357 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02007358 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007359 TPACPI_FAN_CMD_LEVEL |
7360 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007361 }
7362 }
7363 }
7364
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007365 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7366 "fan is %s, modes %d, %d\n",
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007367 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
7368 fan_control_access_mode != TPACPI_FAN_WR_NONE),
7369 fan_status_access_mode, fan_control_access_mode);
7370
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03007371 /* fan control master switch */
7372 if (!fan_control_allowed) {
7373 fan_control_access_mode = TPACPI_FAN_WR_NONE;
7374 fan_control_commands = 0;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007375 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03007376 "fan control features disabled by parameter\n");
7377 }
7378
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007379 /* update fan_control_desired_level */
7380 if (fan_status_access_mode != TPACPI_FAN_NONE)
7381 fan_get_status_safe(NULL);
7382
7383 if (fan_status_access_mode != TPACPI_FAN_NONE ||
7384 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007385 if (tp_features.second_fan) {
7386 /* attach second fan tachometer */
7387 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
7388 &dev_attr_fan_fan2_input.attr;
7389 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007390 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007391 &fan_attr_group);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007392 if (rc < 0)
7393 return rc;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03007394
7395 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
7396 &driver_attr_fan_watchdog);
7397 if (rc < 0) {
7398 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
7399 &fan_attr_group);
7400 return rc;
7401 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007402 return 0;
7403 } else
7404 return 1;
7405}
7406
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007407static void fan_exit(void)
7408{
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007409 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007410 "cancelling any pending fan watchdog tasks\n");
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007411
7412 /* FIXME: can we really do this unconditionally? */
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007413 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007414 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
7415 &driver_attr_fan_watchdog);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007416
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007417 cancel_delayed_work(&fan_watchdog_task);
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03007418 flush_workqueue(tpacpi_wq);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007419}
7420
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007421static void fan_suspend(pm_message_t state)
7422{
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007423 int rc;
7424
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007425 if (!fan_control_allowed)
7426 return;
7427
7428 /* Store fan status in cache */
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007429 fan_control_resume_level = 0;
7430 rc = fan_get_status_safe(&fan_control_resume_level);
7431 if (rc < 0)
7432 printk(TPACPI_NOTICE
7433 "failed to read fan level for later "
7434 "restore during resume: %d\n", rc);
7435
7436 /* if it is undefined, don't attempt to restore it.
7437 * KEEP THIS LAST */
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007438 if (tp_features.fan_ctrl_status_undef)
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007439 fan_control_resume_level = 0;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007440}
7441
7442static void fan_resume(void)
7443{
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007444 u8 current_level = 7;
7445 bool do_set = false;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007446 int rc;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007447
7448 /* DSDT *always* updates status on resume */
7449 tp_features.fan_ctrl_status_undef = 0;
7450
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007451 if (!fan_control_allowed ||
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007452 !fan_control_resume_level ||
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007453 (fan_get_status_safe(&current_level) < 0))
7454 return;
7455
7456 switch (fan_control_access_mode) {
7457 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007458 /* never decrease fan level */
7459 do_set = (fan_control_resume_level > current_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007460 break;
7461 case TPACPI_FAN_WR_ACPI_FANS:
7462 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007463 /* never decrease fan level, scale is:
7464 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
7465 *
7466 * We expect the firmware to set either 7 or AUTO, but we
7467 * handle FULLSPEED out of paranoia.
7468 *
7469 * So, we can safely only restore FULLSPEED or 7, anything
7470 * else could slow the fan. Restoring AUTO is useless, at
7471 * best that's exactly what the DSDT already set (it is the
7472 * slower it uses).
7473 *
7474 * Always keep in mind that the DSDT *will* have set the
7475 * fans to what the vendor supposes is the best level. We
7476 * muck with it only to speed the fan up.
7477 */
7478 if (fan_control_resume_level != 7 &&
7479 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
7480 return;
7481 else
7482 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
7483 (current_level != fan_control_resume_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007484 break;
7485 default:
7486 return;
7487 }
7488 if (do_set) {
7489 printk(TPACPI_NOTICE
7490 "restoring fan level to 0x%02x\n",
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007491 fan_control_resume_level);
7492 rc = fan_set_level_safe(fan_control_resume_level);
7493 if (rc < 0)
7494 printk(TPACPI_NOTICE
7495 "failed to restore fan level: %d\n", rc);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007496 }
7497}
7498
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007499static int fan_read(char *p)
7500{
7501 int len = 0;
7502 int rc;
7503 u8 status;
7504 unsigned int speed = 0;
7505
7506 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007507 case TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007508 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007509 rc = fan_get_status_safe(&status);
7510 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007511 return rc;
7512
7513 len += sprintf(p + len, "status:\t\t%s\n"
7514 "level:\t\t%d\n",
7515 (status != 0) ? "enabled" : "disabled", status);
7516 break;
7517
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007518 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007519 /* all except 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
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007524 len += sprintf(p + len, "status:\t\t%s\n",
7525 (status != 0) ? "enabled" : "disabled");
7526
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007527 rc = fan_get_speed(&speed);
7528 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007529 return rc;
7530
7531 len += sprintf(p + len, "speed:\t\t%d\n", speed);
7532
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007533 if (status & TP_EC_FAN_FULLSPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007534 /* Disengaged mode takes precedence */
7535 len += sprintf(p + len, "level:\t\tdisengaged\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007536 else if (status & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007537 len += sprintf(p + len, "level:\t\tauto\n");
7538 else
7539 len += sprintf(p + len, "level:\t\t%d\n", status);
7540 break;
7541
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007542 case TPACPI_FAN_NONE:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007543 default:
7544 len += sprintf(p + len, "status:\t\tnot supported\n");
7545 }
7546
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007547 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007548 len += sprintf(p + len, "commands:\tlevel <level>");
7549
7550 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007551 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007552 len += sprintf(p + len, " (<level> is 0-7)\n");
7553 break;
7554
7555 default:
7556 len += sprintf(p + len, " (<level> is 0-7, "
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007557 "auto, disengaged, full-speed)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007558 break;
7559 }
7560 }
7561
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007562 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007563 len += sprintf(p + len, "commands:\tenable, disable\n"
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007564 "commands:\twatchdog <timeout> (<timeout> "
7565 "is 0 (off), 1-120 (seconds))\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007566
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007567 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007568 len += sprintf(p + len, "commands:\tspeed <speed>"
7569 " (<speed> is 0-65535)\n");
7570
7571 return len;
7572}
7573
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007574static int fan_write_cmd_level(const char *cmd, int *rc)
7575{
7576 int level;
7577
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02007578 if (strlencmp(cmd, "level auto") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007579 level = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007580 else if ((strlencmp(cmd, "level disengaged") == 0) |
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007581 (strlencmp(cmd, "level full-speed") == 0))
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007582 level = TP_EC_FAN_FULLSPEED;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02007583 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007584 return 0;
7585
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007586 *rc = fan_set_level_safe(level);
7587 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007588 printk(TPACPI_ERR "level command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007589 "access mode %d", fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007590 else if (!*rc)
7591 tpacpi_disclose_usertask("procfs fan",
7592 "set level to %d\n", level);
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007593
7594 return 1;
7595}
7596
7597static int fan_write_cmd_enable(const char *cmd, int *rc)
7598{
7599 if (strlencmp(cmd, "enable") != 0)
7600 return 0;
7601
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007602 *rc = fan_set_enable();
7603 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007604 printk(TPACPI_ERR "enable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007605 "access mode %d", fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007606 else if (!*rc)
7607 tpacpi_disclose_usertask("procfs fan", "enable\n");
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007608
7609 return 1;
7610}
7611
7612static int fan_write_cmd_disable(const char *cmd, int *rc)
7613{
7614 if (strlencmp(cmd, "disable") != 0)
7615 return 0;
7616
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007617 *rc = fan_set_disable();
7618 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007619 printk(TPACPI_ERR "disable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007620 "access mode %d", fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007621 else if (!*rc)
7622 tpacpi_disclose_usertask("procfs fan", "disable\n");
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007623
7624 return 1;
7625}
7626
7627static int fan_write_cmd_speed(const char *cmd, int *rc)
7628{
7629 int speed;
7630
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02007631 /* TODO:
7632 * Support speed <low> <medium> <high> ? */
7633
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007634 if (sscanf(cmd, "speed %d", &speed) != 1)
7635 return 0;
7636
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007637 *rc = fan_set_speed(speed);
7638 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007639 printk(TPACPI_ERR "speed command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007640 "access mode %d", fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007641 else if (!*rc)
7642 tpacpi_disclose_usertask("procfs fan",
7643 "set speed to %d\n", speed);
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007644
7645 return 1;
7646}
7647
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007648static int fan_write_cmd_watchdog(const char *cmd, int *rc)
7649{
7650 int interval;
7651
7652 if (sscanf(cmd, "watchdog %d", &interval) != 1)
7653 return 0;
7654
7655 if (interval < 0 || interval > 120)
7656 *rc = -EINVAL;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007657 else {
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007658 fan_watchdog_maxinterval = interval;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007659 tpacpi_disclose_usertask("procfs fan",
7660 "set watchdog timer to %d\n",
7661 interval);
7662 }
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007663
7664 return 1;
7665}
7666
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007667static int fan_write(char *buf)
7668{
7669 char *cmd;
7670 int rc = 0;
7671
7672 while (!rc && (cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007673 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007674 fan_write_cmd_level(cmd, &rc)) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007675 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007676 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007677 fan_write_cmd_disable(cmd, &rc) ||
7678 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007679 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007680 fan_write_cmd_speed(cmd, &rc))
7681 )
7682 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007683 else if (!rc)
7684 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007685 }
7686
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007687 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007688}
7689
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007690static struct ibm_struct fan_driver_data = {
7691 .name = "fan",
7692 .read = fan_read,
7693 .write = fan_write,
7694 .exit = fan_exit,
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007695 .suspend = fan_suspend,
7696 .resume = fan_resume,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007697};
7698
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007699/****************************************************************************
7700 ****************************************************************************
7701 *
7702 * Infrastructure
7703 *
7704 ****************************************************************************
7705 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07007706
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03007707/*
7708 * HKEY event callout for other subdrivers go here
7709 * (yes, it is ugly, but it is quick, safe, and gets the job done
7710 */
7711static void tpacpi_driver_event(const unsigned int hkey_event)
7712{
7713}
7714
7715
7716
7717static void hotkey_driver_event(const unsigned int scancode)
7718{
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03007719 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode);
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03007720}
7721
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007722/* sysfs name ---------------------------------------------------------- */
7723static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
7724 struct device_attribute *attr,
7725 char *buf)
7726{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007727 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007728}
7729
7730static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
7731 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
7732
7733/* --------------------------------------------------------------------- */
7734
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007735/* /proc support */
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03007736static struct proc_dir_entry *proc_dir;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007737
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007738/*
7739 * Module and infrastructure proble, init and exit handling
7740 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007741
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007742static int force_load;
7743
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007744#ifdef CONFIG_THINKPAD_ACPI_DEBUG
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007745static const char * __init str_supported(int is_supported)
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007746{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007747 static char text_unsupported[] __initdata = "not supported";
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007748
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007749 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007750}
7751#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
7752
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007753static void ibm_exit(struct ibm_struct *ibm)
7754{
7755 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
7756
7757 list_del_init(&ibm->all_drivers);
7758
7759 if (ibm->flags.acpi_notify_installed) {
7760 dbg_printk(TPACPI_DBG_EXIT,
7761 "%s: acpi_remove_notify_handler\n", ibm->name);
7762 BUG_ON(!ibm->acpi);
7763 acpi_remove_notify_handler(*ibm->acpi->handle,
7764 ibm->acpi->type,
7765 dispatch_acpi_notify);
7766 ibm->flags.acpi_notify_installed = 0;
7767 ibm->flags.acpi_notify_installed = 0;
7768 }
7769
7770 if (ibm->flags.proc_created) {
7771 dbg_printk(TPACPI_DBG_EXIT,
7772 "%s: remove_proc_entry\n", ibm->name);
7773 remove_proc_entry(ibm->name, proc_dir);
7774 ibm->flags.proc_created = 0;
7775 }
7776
7777 if (ibm->flags.acpi_driver_registered) {
7778 dbg_printk(TPACPI_DBG_EXIT,
7779 "%s: acpi_bus_unregister_driver\n", ibm->name);
7780 BUG_ON(!ibm->acpi);
7781 acpi_bus_unregister_driver(ibm->acpi->driver);
7782 kfree(ibm->acpi->driver);
7783 ibm->acpi->driver = NULL;
7784 ibm->flags.acpi_driver_registered = 0;
7785 }
7786
7787 if (ibm->flags.init_called && ibm->exit) {
7788 ibm->exit();
7789 ibm->flags.init_called = 0;
7790 }
7791
7792 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
7793}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007794
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007795static int __init ibm_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007796{
7797 int ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007798 struct ibm_struct *ibm = iibm->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007799 struct proc_dir_entry *entry;
7800
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007801 BUG_ON(ibm == NULL);
7802
7803 INIT_LIST_HEAD(&ibm->all_drivers);
7804
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03007805 if (ibm->flags.experimental && !experimental)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007806 return 0;
7807
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007808 dbg_printk(TPACPI_DBG_INIT,
7809 "probing for %s\n", ibm->name);
7810
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007811 if (iibm->init) {
7812 ret = iibm->init(iibm);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007813 if (ret > 0)
7814 return 0; /* probe failed */
7815 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007816 return ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007817
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03007818 ibm->flags.init_called = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007819 }
7820
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03007821 if (ibm->acpi) {
7822 if (ibm->acpi->hid) {
7823 ret = register_tpacpi_subdriver(ibm);
7824 if (ret)
7825 goto err_out;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007826 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03007827
7828 if (ibm->acpi->notify) {
7829 ret = setup_acpi_notify(ibm);
7830 if (ret == -ENODEV) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007831 printk(TPACPI_NOTICE "disabling subdriver %s\n",
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03007832 ibm->name);
7833 ret = 0;
7834 goto err_out;
7835 }
7836 if (ret < 0)
7837 goto err_out;
7838 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007839 }
7840
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007841 dbg_printk(TPACPI_DBG_INIT,
7842 "%s installed\n", ibm->name);
7843
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007844 if (ibm->read) {
7845 entry = create_proc_entry(ibm->name,
7846 S_IFREG | S_IRUGO | S_IWUSR,
7847 proc_dir);
7848 if (!entry) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007849 printk(TPACPI_ERR "unable to create proc entry %s\n",
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007850 ibm->name);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007851 ret = -ENODEV;
7852 goto err_out;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007853 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007854 entry->data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03007855 entry->read_proc = &dispatch_procfs_read;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007856 if (ibm->write)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03007857 entry->write_proc = &dispatch_procfs_write;
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03007858 ibm->flags.proc_created = 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007859 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007860
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007861 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
7862
Linus Torvalds1da177e2005-04-16 15:20:36 -07007863 return 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007864
7865err_out:
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007866 dbg_printk(TPACPI_DBG_INIT,
7867 "%s: at error exit path with result %d\n",
7868 ibm->name, ret);
7869
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007870 ibm_exit(ibm);
7871 return (ret < 0)? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007872}
7873
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007874/* Probing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007875
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03007876static bool __pure __init tpacpi_is_fw_digit(const char c)
7877{
7878 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
7879}
7880
7881/* Most models: xxyTkkWW (#.##c); Ancient 570/600 and -SL lacks (#.##c) */
7882static bool __pure __init tpacpi_is_valid_fw_id(const char* const s,
7883 const char t)
7884{
7885 return s && strlen(s) >= 8 &&
7886 tpacpi_is_fw_digit(s[0]) &&
7887 tpacpi_is_fw_digit(s[1]) &&
7888 s[2] == t && s[3] == 'T' &&
7889 tpacpi_is_fw_digit(s[4]) &&
7890 tpacpi_is_fw_digit(s[5]) &&
7891 s[6] == 'W' && s[7] == 'W';
7892}
7893
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007894/* returns 0 - probe ok, or < 0 - probe error.
7895 * Probe ok doesn't mean thinkpad found.
7896 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
7897static int __must_check __init get_thinkpad_model_data(
7898 struct thinkpad_id_data *tp)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02007899{
Jeff Garzik18552562007-10-03 15:15:40 -04007900 const struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02007901 char ec_fw_string[18];
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007902 char const *s;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02007903
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007904 if (!tp)
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007905 return -EINVAL;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007906
7907 memset(tp, 0, sizeof(*tp));
7908
7909 if (dmi_name_in_vendors("IBM"))
7910 tp->vendor = PCI_VENDOR_ID_IBM;
7911 else if (dmi_name_in_vendors("LENOVO"))
7912 tp->vendor = PCI_VENDOR_ID_LENOVO;
7913 else
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007914 return 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007915
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007916 s = dmi_get_system_info(DMI_BIOS_VERSION);
7917 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
7918 if (s && !tp->bios_version_str)
7919 return -ENOMEM;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03007920
7921 /* Really ancient ThinkPad 240X will fail this, which is fine */
7922 if (!tpacpi_is_valid_fw_id(tp->bios_version_str, 'E'))
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007923 return 0;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03007924
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007925 tp->bios_model = tp->bios_version_str[0]
7926 | (tp->bios_version_str[1] << 8);
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03007927 tp->bios_release = (tp->bios_version_str[4] << 8)
7928 | tp->bios_version_str[5];
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007929
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02007930 /*
7931 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
7932 * X32 or newer, all Z series; Some models must have an
7933 * up-to-date BIOS or they will not be detected.
7934 *
7935 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
7936 */
7937 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02007938 if (sscanf(dev->name,
7939 "IBM ThinkPad Embedded Controller -[%17c",
7940 ec_fw_string) == 1) {
7941 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
7942 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007943
7944 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007945 if (!tp->ec_version_str)
7946 return -ENOMEM;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03007947
7948 if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) {
7949 tp->ec_model = ec_fw_string[0]
7950 | (ec_fw_string[1] << 8);
7951 tp->ec_release = (ec_fw_string[4] << 8)
7952 | ec_fw_string[5];
7953 } else {
7954 printk(TPACPI_NOTICE
7955 "ThinkPad firmware release %s "
7956 "doesn't match the known patterns\n",
7957 ec_fw_string);
7958 printk(TPACPI_NOTICE
7959 "please report this to %s\n",
7960 TPACPI_MAIL);
7961 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007962 break;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02007963 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02007964 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007965
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007966 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
7967 if (s && !strnicmp(s, "ThinkPad", 8)) {
7968 tp->model_str = kstrdup(s, GFP_KERNEL);
7969 if (!tp->model_str)
7970 return -ENOMEM;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007971 }
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03007972
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007973 s = dmi_get_system_info(DMI_PRODUCT_NAME);
7974 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
7975 if (s && !tp->nummodel_str)
7976 return -ENOMEM;
7977
7978 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007979}
7980
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007981static int __init probe_for_thinkpad(void)
7982{
7983 int is_thinkpad;
7984
7985 if (acpi_disabled)
7986 return -ENODEV;
7987
7988 /*
7989 * Non-ancient models have better DMI tagging, but very old models
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03007990 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007991 */
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03007992 is_thinkpad = (thinkpad_id.model_str != NULL) ||
7993 (thinkpad_id.ec_model != 0) ||
7994 tpacpi_is_fw_known();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007995
7996 /* ec is required because many other handles are relative to it */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007997 TPACPI_ACPIHANDLE_INIT(ec);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007998 if (!ec_handle) {
7999 if (is_thinkpad)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008000 printk(TPACPI_ERR
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008001 "Not yet supported ThinkPad detected!\n");
8002 return -ENODEV;
8003 }
8004
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03008005 if (!is_thinkpad && !force_load)
8006 return -ENODEV;
8007
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008008 return 0;
8009}
8010
8011
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008012/* Module init, exit, parameters */
8013
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008014static struct ibm_init_struct ibms_init[] __initdata = {
8015 {
8016 .init = thinkpad_acpi_driver_init,
8017 .data = &thinkpad_acpi_driver_data,
8018 },
8019 {
8020 .init = hotkey_init,
8021 .data = &hotkey_driver_data,
8022 },
8023 {
8024 .init = bluetooth_init,
8025 .data = &bluetooth_driver_data,
8026 },
8027 {
8028 .init = wan_init,
8029 .data = &wan_driver_data,
8030 },
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02008031 {
8032 .init = uwb_init,
8033 .data = &uwb_driver_data,
8034 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02008035#ifdef CONFIG_THINKPAD_ACPI_VIDEO
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008036 {
8037 .init = video_init,
8038 .data = &video_driver_data,
8039 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02008040#endif
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008041 {
8042 .init = light_init,
8043 .data = &light_driver_data,
8044 },
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008045 {
8046 .init = cmos_init,
8047 .data = &cmos_driver_data,
8048 },
8049 {
8050 .init = led_init,
8051 .data = &led_driver_data,
8052 },
8053 {
8054 .init = beep_init,
8055 .data = &beep_driver_data,
8056 },
8057 {
8058 .init = thermal_init,
8059 .data = &thermal_driver_data,
8060 },
8061 {
8062 .data = &ecdump_driver_data,
8063 },
8064 {
8065 .init = brightness_init,
8066 .data = &brightness_driver_data,
8067 },
8068 {
8069 .data = &volume_driver_data,
8070 },
8071 {
8072 .init = fan_init,
8073 .data = &fan_driver_data,
8074 },
8075};
8076
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008077static int __init set_ibm_param(const char *val, struct kernel_param *kp)
8078{
8079 unsigned int i;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008080 struct ibm_struct *ibm;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008081
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02008082 if (!kp || !kp->name || !val)
8083 return -EINVAL;
8084
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008085 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
8086 ibm = ibms_init[i].data;
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02008087 WARN_ON(ibm == NULL);
8088
8089 if (!ibm || !ibm->name)
8090 continue;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008091
8092 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
8093 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008094 return -ENOSPC;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008095 strcpy(ibms_init[i].param, val);
8096 strcat(ibms_init[i].param, ",");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008097 return 0;
8098 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008099 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008100
8101 return -EINVAL;
8102}
8103
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008104module_param(experimental, int, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008105MODULE_PARM_DESC(experimental,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008106 "Enables experimental features when non-zero");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008107
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03008108module_param_named(debug, dbg_level, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008109MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03008110
Henrique de Moraes Holschuh86cc9442007-07-18 23:45:41 -03008111module_param(force_load, bool, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008112MODULE_PARM_DESC(force_load,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008113 "Attempts to load the driver even on a "
8114 "mis-identified ThinkPad when true");
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03008115
Henrique de Moraes Holschuh86cc9442007-07-18 23:45:41 -03008116module_param_named(fan_control, fan_control_allowed, bool, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008117MODULE_PARM_DESC(fan_control,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008118 "Enables setting fan parameters features when true");
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008119
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00008120module_param_named(brightness_mode, brightness_mode, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008121MODULE_PARM_DESC(brightness_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008122 "Selects brightness control strategy: "
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00008123 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03008124
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02008125module_param(brightness_enable, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008126MODULE_PARM_DESC(brightness_enable,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008127 "Enables backlight control when 1, disables when 0");
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02008128
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03008129module_param(hotkey_report_mode, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008130MODULE_PARM_DESC(hotkey_report_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008131 "used for backwards compatibility with userspace, "
8132 "see documentation");
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03008133
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008134#define TPACPI_PARAM(feature) \
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008135 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
Henrique de Moraes Holschuhcbb14842008-02-16 02:17:50 -02008136 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008137 "at module load, see documentation")
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008138
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008139TPACPI_PARAM(hotkey);
8140TPACPI_PARAM(bluetooth);
8141TPACPI_PARAM(video);
8142TPACPI_PARAM(light);
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008143TPACPI_PARAM(cmos);
8144TPACPI_PARAM(led);
8145TPACPI_PARAM(beep);
8146TPACPI_PARAM(ecdump);
8147TPACPI_PARAM(brightness);
8148TPACPI_PARAM(volume);
8149TPACPI_PARAM(fan);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008150
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02008151#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
8152module_param(dbg_wlswemul, uint, 0);
8153MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
8154module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
8155MODULE_PARM_DESC(wlsw_state,
8156 "Initial state of the emulated WLSW switch");
8157
8158module_param(dbg_bluetoothemul, uint, 0);
8159MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
8160module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
8161MODULE_PARM_DESC(bluetooth_state,
8162 "Initial state of the emulated bluetooth switch");
8163
8164module_param(dbg_wwanemul, uint, 0);
8165MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
8166module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
8167MODULE_PARM_DESC(wwan_state,
8168 "Initial state of the emulated WWAN switch");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02008169
8170module_param(dbg_uwbemul, uint, 0);
8171MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
8172module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
8173MODULE_PARM_DESC(uwb_state,
8174 "Initial state of the emulated UWB switch");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02008175#endif
8176
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008177static void thinkpad_acpi_module_exit(void)
8178{
8179 struct ibm_struct *ibm, *itmp;
8180
8181 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
8182
8183 list_for_each_entry_safe_reverse(ibm, itmp,
8184 &tpacpi_all_drivers,
8185 all_drivers) {
8186 ibm_exit(ibm);
8187 }
8188
8189 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
8190
8191 if (tpacpi_inputdev) {
8192 if (tp_features.input_device_registered)
8193 input_unregister_device(tpacpi_inputdev);
8194 else
8195 input_free_device(tpacpi_inputdev);
8196 }
8197
8198 if (tpacpi_hwmon)
8199 hwmon_device_unregister(tpacpi_hwmon);
8200
8201 if (tp_features.sensors_pdev_attrs_registered)
8202 device_remove_file(&tpacpi_sensors_pdev->dev,
8203 &dev_attr_thinkpad_acpi_pdev_name);
8204 if (tpacpi_sensors_pdev)
8205 platform_device_unregister(tpacpi_sensors_pdev);
8206 if (tpacpi_pdev)
8207 platform_device_unregister(tpacpi_pdev);
8208
8209 if (tp_features.sensors_pdrv_attrs_registered)
8210 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
8211 if (tp_features.platform_drv_attrs_registered)
8212 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
8213
8214 if (tp_features.sensors_pdrv_registered)
8215 platform_driver_unregister(&tpacpi_hwmon_pdriver);
8216
8217 if (tp_features.platform_drv_registered)
8218 platform_driver_unregister(&tpacpi_pdriver);
8219
8220 if (proc_dir)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008221 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008222
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03008223 if (tpacpi_wq)
8224 destroy_workqueue(tpacpi_wq);
8225
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008226 kfree(thinkpad_id.bios_version_str);
8227 kfree(thinkpad_id.ec_version_str);
8228 kfree(thinkpad_id.model_str);
8229}
8230
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02008231
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03008232static int __init thinkpad_acpi_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008233{
8234 int ret, i;
8235
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03008236 tpacpi_lifecycle = TPACPI_LIFE_INIT;
8237
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03008238 /* Parameter checking */
8239 if (hotkey_report_mode > 2)
8240 return -EINVAL;
8241
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008242 /* Driver-level probe */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008243
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008244 ret = get_thinkpad_model_data(&thinkpad_id);
8245 if (ret) {
8246 printk(TPACPI_ERR
8247 "unable to get DMI data: %d\n", ret);
8248 thinkpad_acpi_module_exit();
8249 return ret;
8250 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008251 ret = probe_for_thinkpad();
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008252 if (ret) {
8253 thinkpad_acpi_module_exit();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008254 return ret;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008255 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008256
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008257 /* Driver initialization */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008258
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008259 TPACPI_ACPIHANDLE_INIT(ecrd);
8260 TPACPI_ACPIHANDLE_INIT(ecwr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008261
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03008262 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
8263 if (!tpacpi_wq) {
8264 thinkpad_acpi_module_exit();
8265 return -ENOMEM;
8266 }
8267
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008268 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008269 if (!proc_dir) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008270 printk(TPACPI_ERR
8271 "unable to create proc dir " TPACPI_PROC_DIR);
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03008272 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008273 return -ENODEV;
8274 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008275
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008276 ret = platform_driver_register(&tpacpi_pdriver);
8277 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008278 printk(TPACPI_ERR
8279 "unable to register main platform driver\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008280 thinkpad_acpi_module_exit();
8281 return ret;
8282 }
Henrique de Moraes Holschuhac363932007-07-27 17:04:40 -03008283 tp_features.platform_drv_registered = 1;
8284
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008285 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
8286 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008287 printk(TPACPI_ERR
8288 "unable to register hwmon platform driver\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008289 thinkpad_acpi_module_exit();
8290 return ret;
8291 }
8292 tp_features.sensors_pdrv_registered = 1;
8293
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03008294 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03008295 if (!ret) {
8296 tp_features.platform_drv_attrs_registered = 1;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008297 ret = tpacpi_create_driver_attributes(
8298 &tpacpi_hwmon_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03008299 }
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03008300 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008301 printk(TPACPI_ERR
8302 "unable to create sysfs driver attributes\n");
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03008303 thinkpad_acpi_module_exit();
8304 return ret;
8305 }
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03008306 tp_features.sensors_pdrv_attrs_registered = 1;
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03008307
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008308
8309 /* Device initialization */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008310 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008311 NULL, 0);
8312 if (IS_ERR(tpacpi_pdev)) {
8313 ret = PTR_ERR(tpacpi_pdev);
8314 tpacpi_pdev = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008315 printk(TPACPI_ERR "unable to register platform device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008316 thinkpad_acpi_module_exit();
8317 return ret;
8318 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008319 tpacpi_sensors_pdev = platform_device_register_simple(
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008320 TPACPI_HWMON_DRVR_NAME,
8321 -1, NULL, 0);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008322 if (IS_ERR(tpacpi_sensors_pdev)) {
8323 ret = PTR_ERR(tpacpi_sensors_pdev);
8324 tpacpi_sensors_pdev = NULL;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008325 printk(TPACPI_ERR
8326 "unable to register hwmon platform device\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008327 thinkpad_acpi_module_exit();
8328 return ret;
8329 }
8330 ret = device_create_file(&tpacpi_sensors_pdev->dev,
8331 &dev_attr_thinkpad_acpi_pdev_name);
8332 if (ret) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008333 printk(TPACPI_ERR
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008334 "unable to create sysfs hwmon device attributes\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008335 thinkpad_acpi_module_exit();
8336 return ret;
8337 }
8338 tp_features.sensors_pdev_attrs_registered = 1;
8339 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008340 if (IS_ERR(tpacpi_hwmon)) {
8341 ret = PTR_ERR(tpacpi_hwmon);
8342 tpacpi_hwmon = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008343 printk(TPACPI_ERR "unable to register hwmon device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008344 thinkpad_acpi_module_exit();
8345 return ret;
8346 }
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -03008347 mutex_init(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03008348 tpacpi_inputdev = input_allocate_device();
8349 if (!tpacpi_inputdev) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008350 printk(TPACPI_ERR "unable to allocate input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03008351 thinkpad_acpi_module_exit();
8352 return -ENOMEM;
8353 } else {
8354 /* Prepare input device, but don't register */
8355 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008356 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03008357 tpacpi_inputdev->id.bustype = BUS_HOST;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03008358 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
8359 thinkpad_id.vendor :
8360 PCI_VENDOR_ID_IBM;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03008361 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
8362 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
8363 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008364 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
8365 ret = ibm_init(&ibms_init[i]);
8366 if (ret >= 0 && *ibms_init[i].param)
8367 ret = ibms_init[i].data->write(ibms_init[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008368 if (ret < 0) {
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03008369 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008370 return ret;
8371 }
8372 }
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03008373 ret = input_register_device(tpacpi_inputdev);
8374 if (ret < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008375 printk(TPACPI_ERR "unable to register input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03008376 thinkpad_acpi_module_exit();
8377 return ret;
8378 } else {
8379 tp_features.input_device_registered = 1;
8380 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008381
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03008382 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008383 return 0;
8384}
8385
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03008386MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
8387
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008388/*
Henrique de Moraes Holschuh922fe092009-04-14 02:44:13 +00008389 * This will autoload the driver in almost every ThinkPad
8390 * in widespread use.
8391 *
8392 * Only _VERY_ old models, like the 240, 240x and 570 lack
8393 * the HKEY event interface.
8394 */
8395MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
8396
8397/*
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008398 * DMI matching for module autoloading
8399 *
8400 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
8401 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
8402 *
8403 * Only models listed in thinkwiki will be supported, so add yours
8404 * if it is not there yet.
8405 */
8406#define IBM_BIOS_MODULE_ALIAS(__type) \
Mathieu Chouquet-Stringerb36a50f2009-03-14 16:35:26 +01008407 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008408
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008409/* Ancient thinkpad BIOSes have to be identified by
8410 * BIOS type or model number, and there are far less
8411 * BIOS types than model numbers... */
Henrique de Moraes Holschuh922fe092009-04-14 02:44:13 +00008412IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008413
Henrique de Moraes Holschuhf68f53a2009-04-14 02:44:12 +00008414MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
8415MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008416MODULE_DESCRIPTION(TPACPI_DESC);
8417MODULE_VERSION(TPACPI_VERSION);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008418MODULE_LICENSE("GPL");
8419
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03008420module_init(thinkpad_acpi_module_init);
8421module_exit(thinkpad_acpi_module_exit);