blob: e85600852502e0a0c6b99b1681ed0213806e8420 [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 Holschuhf21179a2009-05-30 13:25:08 -030025#define TPACPI_SYSFS_VERSION 0x020400
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
148/****************************************************************************
149 * Main driver
150 */
151
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200152#define TPACPI_NAME "thinkpad"
153#define TPACPI_DESC "ThinkPad ACPI Extras"
154#define TPACPI_FILE TPACPI_NAME "_acpi"
155#define TPACPI_URL "http://ibm-acpi.sf.net/"
156#define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200157
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200158#define TPACPI_PROC_DIR "ibm"
159#define TPACPI_ACPI_EVENT_PREFIX "ibm"
160#define TPACPI_DRVR_NAME TPACPI_FILE
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300161#define TPACPI_DRVR_SHORTNAME "tpacpi"
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200162#define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200163
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300164#define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -0300165#define TPACPI_WORKQUEUE_NAME "ktpacpid"
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300166
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200167#define TPACPI_MAX_ACPI_ARGS 3
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200168
Henrique de Moraes Holschuh7ff8d622009-04-04 04:25:46 +0000169/* printk headers */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200170#define TPACPI_LOG TPACPI_FILE ": "
Henrique de Moraes Holschuh7ff8d622009-04-04 04:25:46 +0000171#define TPACPI_EMERG KERN_EMERG TPACPI_LOG
172#define TPACPI_ALERT KERN_ALERT TPACPI_LOG
173#define TPACPI_CRIT KERN_CRIT TPACPI_LOG
174#define TPACPI_ERR KERN_ERR TPACPI_LOG
175#define TPACPI_WARN KERN_WARNING TPACPI_LOG
176#define TPACPI_NOTICE KERN_NOTICE TPACPI_LOG
177#define TPACPI_INFO KERN_INFO TPACPI_LOG
178#define TPACPI_DEBUG KERN_DEBUG TPACPI_LOG
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200179
Henrique de Moraes Holschuh7ff8d622009-04-04 04:25:46 +0000180/* Debugging printk groups */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200181#define TPACPI_DBG_ALL 0xffff
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +0000182#define TPACPI_DBG_DISCLOSETASK 0x8000
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200183#define TPACPI_DBG_INIT 0x0001
184#define TPACPI_DBG_EXIT 0x0002
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +0000185#define TPACPI_DBG_RFKILL 0x0004
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +0000186#define TPACPI_DBG_HKEY 0x0008
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +0000187#define TPACPI_DBG_FAN 0x0010
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +0000188#define TPACPI_DBG_BRGHT 0x0020
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200189
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200190#define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
191#define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
192#define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200193
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200194
195/****************************************************************************
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200196 * Driver-wide structs and misc. variables
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200197 */
198
199struct ibm_struct;
200
201struct tp_acpi_drv_struct {
202 const struct acpi_device_id *hid;
203 struct acpi_driver *driver;
204
205 void (*notify) (struct ibm_struct *, u32);
206 acpi_handle *handle;
207 u32 type;
208 struct acpi_device *device;
209};
210
211struct ibm_struct {
212 char *name;
213
214 int (*read) (char *);
215 int (*write) (char *);
216 void (*exit) (void);
217 void (*resume) (void);
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200218 void (*suspend) (pm_message_t state);
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200219 void (*shutdown) (void);
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200220
221 struct list_head all_drivers;
222
223 struct tp_acpi_drv_struct *acpi;
224
225 struct {
226 u8 acpi_driver_registered:1;
227 u8 acpi_notify_installed:1;
228 u8 proc_created:1;
229 u8 init_called:1;
230 u8 experimental:1;
231 } flags;
232};
233
234struct ibm_init_struct {
235 char param[32];
236
237 int (*init) (struct ibm_init_struct *);
238 struct ibm_struct *data;
239};
240
241static struct {
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200242 u32 bluetooth:1;
243 u32 hotkey:1;
244 u32 hotkey_mask:1;
245 u32 hotkey_wlsw:1;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -0200246 u32 hotkey_tablet:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200247 u32 light:1;
248 u32 light_status:1;
249 u32 bright_16levels:1;
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -0300250 u32 bright_acpimode:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200251 u32 wan:1;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -0200252 u32 uwb:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200253 u32 fan_ctrl_status_undef:1;
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -0300254 u32 second_fan:1;
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -0300255 u32 beep_needs_two_args:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200256 u32 input_device_registered:1;
257 u32 platform_drv_registered:1;
258 u32 platform_drv_attrs_registered:1;
259 u32 sensors_pdrv_registered:1;
260 u32 sensors_pdrv_attrs_registered:1;
261 u32 sensors_pdev_attrs_registered:1;
262 u32 hotkey_poll_active:1;
263} tp_features;
264
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -0300265static struct {
266 u16 hotkey_mask_ff:1;
267} tp_warned;
268
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200269struct thinkpad_id_data {
270 unsigned int vendor; /* ThinkPad vendor:
271 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
272
273 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
274 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
275
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -0300276 u16 bios_model; /* 1Y = 0x5931, 0 = unknown */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200277 u16 ec_model;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -0300278 u16 bios_release; /* 1ZETK1WW = 0x314b, 0 = unknown */
279 u16 ec_release;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200280
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -0300281 char *model_str; /* ThinkPad T43 */
282 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200283};
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200284static struct thinkpad_id_data thinkpad_id;
285
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300286static enum {
287 TPACPI_LIFE_INIT = 0,
288 TPACPI_LIFE_RUNNING,
289 TPACPI_LIFE_EXITING,
290} tpacpi_lifecycle;
291
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200292static int experimental;
293static u32 dbg_level;
294
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -0300295static struct workqueue_struct *tpacpi_wq;
296
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +0000297enum led_status_t {
298 TPACPI_LED_OFF = 0,
299 TPACPI_LED_ON,
300 TPACPI_LED_BLINK,
301};
302
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -0300303/* Special LED class that can defer work */
304struct tpacpi_led_classdev {
305 struct led_classdev led_classdev;
306 struct work_struct work;
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +0000307 enum led_status_t new_state;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -0300308 unsigned int led;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -0300309};
310
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200311#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
312static int dbg_wlswemul;
313static int tpacpi_wlsw_emulstate;
314static int dbg_bluetoothemul;
315static int tpacpi_bluetooth_emulstate;
316static int dbg_wwanemul;
317static int tpacpi_wwan_emulstate;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -0200318static int dbg_uwbemul;
319static int tpacpi_uwb_emulstate;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200320#endif
321
322
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000323/*************************************************************************
324 * Debugging helpers
325 */
326
327#define dbg_printk(a_dbg_level, format, arg...) \
328 do { if (dbg_level & (a_dbg_level)) \
329 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
330 } while (0)
331
332#ifdef CONFIG_THINKPAD_ACPI_DEBUG
333#define vdbg_printk dbg_printk
334static const char *str_supported(int is_supported);
335#else
336#define vdbg_printk(a_dbg_level, format, arg...) \
337 do { } while (0)
338#endif
339
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +0000340static void tpacpi_log_usertask(const char * const what)
341{
342 printk(TPACPI_DEBUG "%s: access by process with PID %d\n",
343 what, task_tgid_vnr(current));
344}
345
346#define tpacpi_disclose_usertask(what, format, arg...) \
347 do { \
348 if (unlikely( \
349 (dbg_level & TPACPI_DBG_DISCLOSETASK) && \
350 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
351 printk(TPACPI_DEBUG "%s: PID %d: " format, \
352 what, task_tgid_vnr(current), ## arg); \
353 } \
354 } while (0)
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000355
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -0300356/*
357 * Quirk handling helpers
358 *
359 * ThinkPad IDs and versions seen in the field so far
360 * are two-characters from the set [0-9A-Z], i.e. base 36.
361 *
362 * We use values well outside that range as specials.
363 */
364
365#define TPACPI_MATCH_ANY 0xffffU
366#define TPACPI_MATCH_UNKNOWN 0U
367
368/* TPID('1', 'Y') == 0x5931 */
369#define TPID(__c1, __c2) (((__c2) << 8) | (__c1))
370
371#define TPACPI_Q_IBM(__id1, __id2, __quirk) \
372 { .vendor = PCI_VENDOR_ID_IBM, \
373 .bios = TPID(__id1, __id2), \
374 .ec = TPACPI_MATCH_ANY, \
375 .quirks = (__quirk) }
376
377#define TPACPI_Q_LNV(__id1, __id2, __quirk) \
378 { .vendor = PCI_VENDOR_ID_LENOVO, \
379 .bios = TPID(__id1, __id2), \
380 .ec = TPACPI_MATCH_ANY, \
381 .quirks = (__quirk) }
382
383struct tpacpi_quirk {
384 unsigned int vendor;
385 u16 bios;
386 u16 ec;
387 unsigned long quirks;
388};
389
390/**
391 * tpacpi_check_quirks() - search BIOS/EC version on a list
392 * @qlist: array of &struct tpacpi_quirk
393 * @qlist_size: number of elements in @qlist
394 *
395 * Iterates over a quirks list until one is found that matches the
396 * ThinkPad's vendor, BIOS and EC model.
397 *
398 * Returns 0 if nothing matches, otherwise returns the quirks field of
399 * the matching &struct tpacpi_quirk entry.
400 *
401 * The match criteria is: vendor, ec and bios much match.
402 */
403static unsigned long __init tpacpi_check_quirks(
404 const struct tpacpi_quirk *qlist,
405 unsigned int qlist_size)
406{
407 while (qlist_size) {
408 if ((qlist->vendor == thinkpad_id.vendor ||
409 qlist->vendor == TPACPI_MATCH_ANY) &&
410 (qlist->bios == thinkpad_id.bios_model ||
411 qlist->bios == TPACPI_MATCH_ANY) &&
412 (qlist->ec == thinkpad_id.ec_model ||
413 qlist->ec == TPACPI_MATCH_ANY))
414 return qlist->quirks;
415
416 qlist_size--;
417 qlist++;
418 }
419 return 0;
420}
421
422
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300423/****************************************************************************
424 ****************************************************************************
425 *
426 * ACPI Helpers and device model
427 *
428 ****************************************************************************
429 ****************************************************************************/
430
431/*************************************************************************
432 * ACPI basic handles
433 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -0300435static acpi_handle root_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200437#define TPACPI_HANDLE(object, parent, paths...) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 static acpi_handle object##_handle; \
439 static acpi_handle *object##_parent = &parent##_handle; \
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400440 static char *object##_path; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 static char *object##_paths[] = { paths }
442
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200443TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400444 "\\_SB.PCI.ISA.EC", /* 570 */
445 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
446 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
447 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
448 "\\_SB.PCI0.ICH3.EC0", /* R31 */
449 "\\_SB.PCI0.LPC.EC", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300450 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200452TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
453TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200455TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
456 /* T4x, X31, X40 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400457 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
458 "\\CMS", /* R40, R40e */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300459 ); /* all others */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400460
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200461TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400462 "^HKEY", /* R30, R31 */
463 "HKEY", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300464 ); /* 570 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400465
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -0200466TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
467 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
468 "\\_SB.PCI0.VID0", /* 770e */
469 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
470 "\\_SB.PCI0.AGP.VID", /* all others */
471 ); /* R30, R31 */
472
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400473
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300474/*************************************************************************
475 * ACPI helpers
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -0200476 */
477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478static int acpi_evalf(acpi_handle handle,
479 void *res, char *method, char *fmt, ...)
480{
481 char *fmt0 = fmt;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400482 struct acpi_object_list params;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200483 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400484 struct acpi_buffer result, *resultp;
485 union acpi_object out_obj;
486 acpi_status status;
487 va_list ap;
488 char res_type;
489 int success;
490 int quiet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
492 if (!*fmt) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200493 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 return 0;
495 }
496
497 if (*fmt == 'q') {
498 quiet = 1;
499 fmt++;
500 } else
501 quiet = 0;
502
503 res_type = *(fmt++);
504
505 params.count = 0;
506 params.pointer = &in_objs[0];
507
508 va_start(ap, fmt);
509 while (*fmt) {
510 char c = *(fmt++);
511 switch (c) {
512 case 'd': /* int */
513 in_objs[params.count].integer.value = va_arg(ap, int);
514 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
515 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400516 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 default:
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200518 printk(TPACPI_ERR "acpi_evalf() called "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 "with invalid format character '%c'\n", c);
520 return 0;
521 }
522 }
523 va_end(ap);
524
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400525 if (res_type != 'v') {
526 result.length = sizeof(out_obj);
527 result.pointer = &out_obj;
528 resultp = &result;
529 } else
530 resultp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400532 status = acpi_evaluate_object(handle, method, &params, resultp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
534 switch (res_type) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400535 case 'd': /* int */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 if (res)
537 *(int *)res = out_obj.integer.value;
538 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
539 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400540 case 'v': /* void */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 success = status == AE_OK;
542 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400543 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 default:
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200545 printk(TPACPI_ERR "acpi_evalf() called "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 "with invalid format character '%c'\n", res_type);
547 return 0;
548 }
549
550 if (!success && !quiet)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200551 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 method, fmt0, status);
553
554 return success;
555}
556
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200557static int acpi_ec_read(int i, u8 *p)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300558{
559 int v;
560
561 if (ecrd_handle) {
562 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
563 return 0;
564 *p = v;
565 } else {
566 if (ec_read(i, p) < 0)
567 return 0;
568 }
569
570 return 1;
571}
572
573static int acpi_ec_write(int i, u8 v)
574{
575 if (ecwr_handle) {
576 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
577 return 0;
578 } else {
579 if (ec_write(i, v) < 0)
580 return 0;
581 }
582
583 return 1;
584}
585
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -0300586static int issue_thinkpad_cmos_command(int cmos_cmd)
587{
588 if (!cmos_handle)
589 return -ENXIO;
590
591 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
592 return -EIO;
593
594 return 0;
595}
596
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300597/*************************************************************************
598 * ACPI device model
599 */
600
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200601#define TPACPI_ACPIHANDLE_INIT(object) \
602 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200603 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
604
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300605static void drv_acpi_handle_init(char *name,
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300606 acpi_handle *handle, acpi_handle parent,
607 char **paths, int num_paths, char **path)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300608{
609 int i;
610 acpi_status status;
611
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300612 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
613 name);
614
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300615 for (i = 0; i < num_paths; i++) {
616 status = acpi_get_handle(parent, paths[i], handle);
617 if (ACPI_SUCCESS(status)) {
618 *path = paths[i];
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300619 dbg_printk(TPACPI_DBG_INIT,
620 "Found ACPI handle %s for %s\n",
621 *path, name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300622 return;
623 }
624 }
625
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300626 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
627 name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300628 *handle = NULL;
629}
630
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300631static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300632{
633 struct ibm_struct *ibm = data;
634
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300635 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
636 return;
637
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300638 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300639 return;
640
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300641 ibm->acpi->notify(ibm, event);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300642}
643
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300644static int __init setup_acpi_notify(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300645{
646 acpi_status status;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300647 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300648
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300649 BUG_ON(!ibm->acpi);
650
651 if (!*ibm->acpi->handle)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300652 return 0;
653
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300654 vdbg_printk(TPACPI_DBG_INIT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300655 "setting up ACPI notify for %s\n", ibm->name);
656
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300657 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
658 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200659 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300660 ibm->name, rc);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300661 return -ENODEV;
662 }
663
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700664 ibm->acpi->device->driver_data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300665 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200666 TPACPI_ACPI_EVENT_PREFIX,
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -0300667 ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300668
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300669 status = acpi_install_notify_handler(*ibm->acpi->handle,
670 ibm->acpi->type, dispatch_acpi_notify, ibm);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300671 if (ACPI_FAILURE(status)) {
672 if (status == AE_ALREADY_EXISTS) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200673 printk(TPACPI_NOTICE
674 "another device driver is already "
675 "handling %s events\n", ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300676 } else {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200677 printk(TPACPI_ERR
678 "acpi_install_notify_handler(%s) failed: %d\n",
679 ibm->name, status);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300680 }
681 return -ENODEV;
682 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300683 ibm->flags.acpi_notify_installed = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300684 return 0;
685}
686
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300687static int __init tpacpi_device_add(struct acpi_device *device)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300688{
689 return 0;
690}
691
Henrique de Moraes Holschuh67001212007-04-21 11:08:25 -0300692static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300693{
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300694 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300695
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300696 dbg_printk(TPACPI_DBG_INIT,
697 "registering %s as an ACPI driver\n", ibm->name);
698
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300699 BUG_ON(!ibm->acpi);
700
701 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
702 if (!ibm->acpi->driver) {
Mariusz Kozlowski4f778b92008-10-18 14:23:53 -0300703 printk(TPACPI_ERR
704 "failed to allocate memory for ibm->acpi->driver\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300705 return -ENOMEM;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300706 }
707
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200708 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300709 ibm->acpi->driver->ids = ibm->acpi->hid;
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200710
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300711 ibm->acpi->driver->ops.add = &tpacpi_device_add;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300712
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300713 rc = acpi_bus_register_driver(ibm->acpi->driver);
714 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200715 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200716 ibm->name, rc);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300717 kfree(ibm->acpi->driver);
718 ibm->acpi->driver = NULL;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300719 } else if (!rc)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300720 ibm->flags.acpi_driver_registered = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300721
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300722 return rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300723}
724
725
726/****************************************************************************
727 ****************************************************************************
728 *
729 * Procfs Helpers
730 *
731 ****************************************************************************
732 ****************************************************************************/
733
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300734static int dispatch_procfs_read(char *page, char **start, off_t off,
735 int count, int *eof, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300736{
737 struct ibm_struct *ibm = data;
738 int len;
739
740 if (!ibm || !ibm->read)
741 return -EINVAL;
742
743 len = ibm->read(page);
744 if (len < 0)
745 return len;
746
747 if (len <= off + count)
748 *eof = 1;
749 *start = page + off;
750 len -= off;
751 if (len > count)
752 len = count;
753 if (len < 0)
754 len = 0;
755
756 return len;
757}
758
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300759static int dispatch_procfs_write(struct file *file,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200760 const char __user *userbuf,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300761 unsigned long count, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300762{
763 struct ibm_struct *ibm = data;
764 char *kernbuf;
765 int ret;
766
767 if (!ibm || !ibm->write)
768 return -EINVAL;
Michael Buesch5b05d462009-08-01 12:04:19 -0300769 if (count > PAGE_SIZE - 2)
770 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300771
772 kernbuf = kmalloc(count + 2, GFP_KERNEL);
773 if (!kernbuf)
774 return -ENOMEM;
775
776 if (copy_from_user(kernbuf, userbuf, count)) {
777 kfree(kernbuf);
778 return -EFAULT;
779 }
780
781 kernbuf[count] = 0;
782 strcat(kernbuf, ",");
783 ret = ibm->write(kernbuf);
784 if (ret == 0)
785 ret = count;
786
787 kfree(kernbuf);
788
789 return ret;
790}
791
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792static char *next_cmd(char **cmds)
793{
794 char *start = *cmds;
795 char *end;
796
797 while ((end = strchr(start, ',')) && end == start)
798 start = end + 1;
799
800 if (!end)
801 return NULL;
802
803 *end = 0;
804 *cmds = end + 1;
805 return start;
806}
807
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300808
809/****************************************************************************
810 ****************************************************************************
811 *
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300812 * Device model: input, hwmon and platform
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300813 *
814 ****************************************************************************
815 ****************************************************************************/
816
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -0300817static struct platform_device *tpacpi_pdev;
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300818static struct platform_device *tpacpi_sensors_pdev;
Tony Jones1beeffe2007-08-20 13:46:20 -0700819static struct device *tpacpi_hwmon;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300820static struct input_dev *tpacpi_inputdev;
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -0300821static struct mutex tpacpi_inputdev_send_mutex;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200822static LIST_HEAD(tpacpi_all_drivers);
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300823
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200824static int tpacpi_suspend_handler(struct platform_device *pdev,
825 pm_message_t state)
826{
827 struct ibm_struct *ibm, *itmp;
828
829 list_for_each_entry_safe(ibm, itmp,
830 &tpacpi_all_drivers,
831 all_drivers) {
832 if (ibm->suspend)
833 (ibm->suspend)(state);
834 }
835
836 return 0;
837}
838
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300839static int tpacpi_resume_handler(struct platform_device *pdev)
840{
841 struct ibm_struct *ibm, *itmp;
842
843 list_for_each_entry_safe(ibm, itmp,
844 &tpacpi_all_drivers,
845 all_drivers) {
846 if (ibm->resume)
847 (ibm->resume)();
848 }
849
850 return 0;
851}
852
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200853static void tpacpi_shutdown_handler(struct platform_device *pdev)
854{
855 struct ibm_struct *ibm, *itmp;
856
857 list_for_each_entry_safe(ibm, itmp,
858 &tpacpi_all_drivers,
859 all_drivers) {
860 if (ibm->shutdown)
861 (ibm->shutdown)();
862 }
863}
864
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300865static struct platform_driver tpacpi_pdriver = {
866 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200867 .name = TPACPI_DRVR_NAME,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300868 .owner = THIS_MODULE,
869 },
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200870 .suspend = tpacpi_suspend_handler,
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300871 .resume = tpacpi_resume_handler,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200872 .shutdown = tpacpi_shutdown_handler,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300873};
874
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300875static struct platform_driver tpacpi_hwmon_pdriver = {
876 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200877 .name = TPACPI_HWMON_DRVR_NAME,
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300878 .owner = THIS_MODULE,
879 },
880};
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300881
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -0300882/*************************************************************************
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300883 * sysfs support helpers
884 */
885
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200886struct attribute_set {
887 unsigned int members, max_members;
888 struct attribute_group group;
889};
890
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300891struct attribute_set_obj {
892 struct attribute_set s;
893 struct attribute *a;
894} __attribute__((packed));
895
896static struct attribute_set *create_attr_set(unsigned int max_members,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200897 const char *name)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300898{
899 struct attribute_set_obj *sobj;
900
901 if (max_members == 0)
902 return NULL;
903
904 /* Allocates space for implicit NULL at the end too */
905 sobj = kzalloc(sizeof(struct attribute_set_obj) +
906 max_members * sizeof(struct attribute *),
907 GFP_KERNEL);
908 if (!sobj)
909 return NULL;
910 sobj->s.max_members = max_members;
911 sobj->s.group.attrs = &sobj->a;
912 sobj->s.group.name = name;
913
914 return &sobj->s;
915}
916
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200917#define destroy_attr_set(_set) \
918 kfree(_set);
919
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300920/* not multi-threaded safe, use it in a single thread per set */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200921static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300922{
923 if (!s || !attr)
924 return -EINVAL;
925
926 if (s->members >= s->max_members)
927 return -ENOMEM;
928
929 s->group.attrs[s->members] = attr;
930 s->members++;
931
932 return 0;
933}
934
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200935static int add_many_to_attr_set(struct attribute_set *s,
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300936 struct attribute **attr,
937 unsigned int count)
938{
939 int i, res;
940
941 for (i = 0; i < count; i++) {
942 res = add_to_attr_set(s, attr[i]);
943 if (res)
944 return res;
945 }
946
947 return 0;
948}
949
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200950static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300951{
952 sysfs_remove_group(kobj, &s->group);
953 destroy_attr_set(s);
954}
955
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200956#define register_attr_set_with_sysfs(_attr_set, _kobj) \
957 sysfs_create_group(_kobj, &_attr_set->group)
958
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300959static int parse_strtoul(const char *buf,
960 unsigned long max, unsigned long *value)
961{
962 char *endp;
963
Henrique de Moraes Holschuh32afbf02007-10-08 10:12:56 -0300964 while (*buf && isspace(*buf))
965 buf++;
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300966 *value = simple_strtoul(buf, &endp, 0);
967 while (*endp && isspace(*endp))
968 endp++;
969 if (*endp || *value > max)
970 return -EINVAL;
971
972 return 0;
973}
974
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -0300975static void tpacpi_disable_brightness_delay(void)
976{
977 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
978 printk(TPACPI_NOTICE
979 "ACPI backlight control delay disabled\n");
980}
981
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -0300982static int __init tpacpi_query_bcl_levels(acpi_handle handle)
983{
984 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
985 union acpi_object *obj;
986 int rc;
987
988 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
989 obj = (union acpi_object *)buffer.pointer;
990 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
991 printk(TPACPI_ERR "Unknown _BCL data, "
992 "please report this to %s\n", TPACPI_MAIL);
993 rc = 0;
994 } else {
995 rc = obj->package.count;
996 }
997 } else {
998 return 0;
999 }
1000
1001 kfree(buffer.pointer);
1002 return rc;
1003}
1004
1005static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
1006 u32 lvl, void *context, void **rv)
1007{
1008 char name[ACPI_PATH_SEGMENT_LENGTH];
1009 struct acpi_buffer buffer = { sizeof(name), &name };
1010
1011 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
1012 !strncmp("_BCL", name, sizeof(name) - 1)) {
1013 BUG_ON(!rv || !*rv);
1014 **(int **)rv = tpacpi_query_bcl_levels(handle);
1015 return AE_CTRL_TERMINATE;
1016 } else {
1017 return AE_OK;
1018 }
1019}
1020
1021/*
1022 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
1023 */
1024static int __init tpacpi_check_std_acpi_brightness_support(void)
1025{
1026 int status;
1027 int bcl_levels = 0;
1028 void *bcl_ptr = &bcl_levels;
1029
1030 if (!vid_handle) {
1031 TPACPI_ACPIHANDLE_INIT(vid);
1032 }
1033 if (!vid_handle)
1034 return 0;
1035
1036 /*
1037 * Search for a _BCL method, and execute it. This is safe on all
1038 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
1039 * BIOS in ACPI backlight control mode. We do NOT have to care
1040 * about calling the _BCL method in an enabled video device, any
1041 * will do for our purposes.
1042 */
1043
1044 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
1045 tpacpi_acpi_walk_find_bcl, NULL,
1046 &bcl_ptr);
1047
1048 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
1049 tp_features.bright_acpimode = 1;
1050 return (bcl_levels - 2);
1051 }
1052
1053 return 0;
1054}
1055
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00001056static void printk_deprecated_attribute(const char * const what,
1057 const char * const details)
1058{
1059 tpacpi_log_usertask("deprecated sysfs attribute");
1060 printk(TPACPI_WARN "WARNING: sysfs attribute %s is deprecated and "
1061 "will be removed. %s\n",
1062 what, details);
1063}
1064
Johannes Berg19d337d2009-06-02 13:01:37 +02001065/*************************************************************************
1066 * rfkill and radio control support helpers
1067 */
1068
1069/*
1070 * ThinkPad-ACPI firmware handling model:
1071 *
1072 * WLSW (master wireless switch) is event-driven, and is common to all
1073 * firmware-controlled radios. It cannot be controlled, just monitored,
1074 * as expected. It overrides all radio state in firmware
1075 *
1076 * The kernel, a masked-off hotkey, and WLSW can change the radio state
1077 * (TODO: verify how WLSW interacts with the returned radio state).
1078 *
1079 * The only time there are shadow radio state changes, is when
1080 * masked-off hotkeys are used.
1081 */
1082
1083/*
1084 * Internal driver API for radio state:
1085 *
1086 * int: < 0 = error, otherwise enum tpacpi_rfkill_state
1087 * bool: true means radio blocked (off)
1088 */
1089enum tpacpi_rfkill_state {
1090 TPACPI_RFK_RADIO_OFF = 0,
1091 TPACPI_RFK_RADIO_ON
1092};
1093
1094/* rfkill switches */
1095enum tpacpi_rfk_id {
1096 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
1097 TPACPI_RFK_WWAN_SW_ID,
1098 TPACPI_RFK_UWB_SW_ID,
1099 TPACPI_RFK_SW_MAX
1100};
1101
1102static const char *tpacpi_rfkill_names[] = {
1103 [TPACPI_RFK_BLUETOOTH_SW_ID] = "bluetooth",
1104 [TPACPI_RFK_WWAN_SW_ID] = "wwan",
1105 [TPACPI_RFK_UWB_SW_ID] = "uwb",
1106 [TPACPI_RFK_SW_MAX] = NULL
1107};
1108
1109/* ThinkPad-ACPI rfkill subdriver */
1110struct tpacpi_rfk {
1111 struct rfkill *rfkill;
1112 enum tpacpi_rfk_id id;
1113 const struct tpacpi_rfk_ops *ops;
1114};
1115
1116struct tpacpi_rfk_ops {
1117 /* firmware interface */
1118 int (*get_status)(void);
1119 int (*set_status)(const enum tpacpi_rfkill_state);
1120};
1121
1122static struct tpacpi_rfk *tpacpi_rfkill_switches[TPACPI_RFK_SW_MAX];
1123
1124/* Query FW and update rfkill sw state for a given rfkill switch */
1125static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk *tp_rfk)
1126{
1127 int status;
1128
1129 if (!tp_rfk)
1130 return -ENODEV;
1131
1132 status = (tp_rfk->ops->get_status)();
1133 if (status < 0)
1134 return status;
1135
1136 rfkill_set_sw_state(tp_rfk->rfkill,
1137 (status == TPACPI_RFK_RADIO_OFF));
1138
1139 return status;
1140}
1141
1142/* Query FW and update rfkill sw state for all rfkill switches */
1143static void tpacpi_rfk_update_swstate_all(void)
1144{
1145 unsigned int i;
1146
1147 for (i = 0; i < TPACPI_RFK_SW_MAX; i++)
1148 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[i]);
1149}
1150
1151/*
1152 * Sync the HW-blocking state of all rfkill switches,
1153 * do notice it causes the rfkill core to schedule uevents
1154 */
1155static void tpacpi_rfk_update_hwblock_state(bool blocked)
1156{
1157 unsigned int i;
1158 struct tpacpi_rfk *tp_rfk;
1159
1160 for (i = 0; i < TPACPI_RFK_SW_MAX; i++) {
1161 tp_rfk = tpacpi_rfkill_switches[i];
1162 if (tp_rfk) {
1163 if (rfkill_set_hw_state(tp_rfk->rfkill,
1164 blocked)) {
1165 /* ignore -- we track sw block */
1166 }
1167 }
1168 }
1169}
1170
1171/* Call to get the WLSW state from the firmware */
1172static int hotkey_get_wlsw(void);
1173
1174/* Call to query WLSW state and update all rfkill switches */
1175static bool tpacpi_rfk_check_hwblock_state(void)
1176{
1177 int res = hotkey_get_wlsw();
1178 int hw_blocked;
1179
1180 /* When unknown or unsupported, we have to assume it is unblocked */
1181 if (res < 0)
1182 return false;
1183
1184 hw_blocked = (res == TPACPI_RFK_RADIO_OFF);
1185 tpacpi_rfk_update_hwblock_state(hw_blocked);
1186
1187 return hw_blocked;
1188}
1189
1190static int tpacpi_rfk_hook_set_block(void *data, bool blocked)
1191{
1192 struct tpacpi_rfk *tp_rfk = data;
1193 int res;
1194
1195 dbg_printk(TPACPI_DBG_RFKILL,
1196 "request to change radio state to %s\n",
1197 blocked ? "blocked" : "unblocked");
1198
1199 /* try to set radio state */
1200 res = (tp_rfk->ops->set_status)(blocked ?
1201 TPACPI_RFK_RADIO_OFF : TPACPI_RFK_RADIO_ON);
1202
1203 /* and update the rfkill core with whatever the FW really did */
1204 tpacpi_rfk_update_swstate(tp_rfk);
1205
1206 return (res < 0) ? res : 0;
1207}
1208
1209static const struct rfkill_ops tpacpi_rfk_rfkill_ops = {
1210 .set_block = tpacpi_rfk_hook_set_block,
1211};
1212
1213static int __init tpacpi_new_rfkill(const enum tpacpi_rfk_id id,
1214 const struct tpacpi_rfk_ops *tp_rfkops,
1215 const enum rfkill_type rfktype,
1216 const char *name,
1217 const bool set_default)
1218{
1219 struct tpacpi_rfk *atp_rfk;
1220 int res;
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001221 bool sw_state = false;
1222 int sw_status;
Johannes Berg19d337d2009-06-02 13:01:37 +02001223
1224 BUG_ON(id >= TPACPI_RFK_SW_MAX || tpacpi_rfkill_switches[id]);
1225
Johannes Berg19d337d2009-06-02 13:01:37 +02001226 atp_rfk = kzalloc(sizeof(struct tpacpi_rfk), GFP_KERNEL);
1227 if (atp_rfk)
1228 atp_rfk->rfkill = rfkill_alloc(name,
1229 &tpacpi_pdev->dev,
1230 rfktype,
1231 &tpacpi_rfk_rfkill_ops,
1232 atp_rfk);
1233 if (!atp_rfk || !atp_rfk->rfkill) {
1234 printk(TPACPI_ERR
1235 "failed to allocate memory for rfkill class\n");
1236 kfree(atp_rfk);
1237 return -ENOMEM;
1238 }
1239
1240 atp_rfk->id = id;
1241 atp_rfk->ops = tp_rfkops;
1242
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001243 sw_status = (tp_rfkops->get_status)();
1244 if (sw_status < 0) {
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001245 printk(TPACPI_ERR
1246 "failed to read initial state for %s, error %d\n",
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001247 name, sw_status);
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001248 } else {
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001249 sw_state = (sw_status == TPACPI_RFK_RADIO_OFF);
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001250 if (set_default) {
1251 /* try to keep the initial state, since we ask the
1252 * firmware to preserve it across S5 in NVRAM */
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001253 rfkill_init_sw_state(atp_rfk->rfkill, sw_state);
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001254 }
1255 }
1256 rfkill_set_hw_state(atp_rfk->rfkill, tpacpi_rfk_check_hwblock_state());
Johannes Berg19d337d2009-06-02 13:01:37 +02001257
1258 res = rfkill_register(atp_rfk->rfkill);
1259 if (res < 0) {
1260 printk(TPACPI_ERR
1261 "failed to register %s rfkill switch: %d\n",
1262 name, res);
1263 rfkill_destroy(atp_rfk->rfkill);
1264 kfree(atp_rfk);
1265 return res;
1266 }
1267
1268 tpacpi_rfkill_switches[id] = atp_rfk;
1269 return 0;
1270}
1271
1272static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id)
1273{
1274 struct tpacpi_rfk *tp_rfk;
1275
1276 BUG_ON(id >= TPACPI_RFK_SW_MAX);
1277
1278 tp_rfk = tpacpi_rfkill_switches[id];
1279 if (tp_rfk) {
1280 rfkill_unregister(tp_rfk->rfkill);
1281 tpacpi_rfkill_switches[id] = NULL;
1282 kfree(tp_rfk);
1283 }
1284}
1285
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00001286static void printk_deprecated_rfkill_attribute(const char * const what)
1287{
1288 printk_deprecated_attribute(what,
1289 "Please switch to generic rfkill before year 2010");
1290}
1291
Johannes Berg19d337d2009-06-02 13:01:37 +02001292/* sysfs <radio> enable ------------------------------------------------ */
1293static ssize_t tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id,
1294 struct device_attribute *attr,
1295 char *buf)
1296{
1297 int status;
1298
1299 printk_deprecated_rfkill_attribute(attr->attr.name);
1300
1301 /* This is in the ABI... */
1302 if (tpacpi_rfk_check_hwblock_state()) {
1303 status = TPACPI_RFK_RADIO_OFF;
1304 } else {
1305 status = tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1306 if (status < 0)
1307 return status;
1308 }
1309
1310 return snprintf(buf, PAGE_SIZE, "%d\n",
1311 (status == TPACPI_RFK_RADIO_ON) ? 1 : 0);
1312}
1313
1314static ssize_t tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id,
1315 struct device_attribute *attr,
1316 const char *buf, size_t count)
1317{
1318 unsigned long t;
1319 int res;
1320
1321 printk_deprecated_rfkill_attribute(attr->attr.name);
1322
1323 if (parse_strtoul(buf, 1, &t))
1324 return -EINVAL;
1325
1326 tpacpi_disclose_usertask(attr->attr.name, "set to %ld\n", t);
1327
1328 /* This is in the ABI... */
1329 if (tpacpi_rfk_check_hwblock_state() && !!t)
1330 return -EPERM;
1331
1332 res = tpacpi_rfkill_switches[id]->ops->set_status((!!t) ?
1333 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF);
1334 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1335
1336 return (res < 0) ? res : count;
1337}
1338
1339/* procfs -------------------------------------------------------------- */
1340static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id, char *p)
1341{
1342 int len = 0;
1343
1344 if (id >= TPACPI_RFK_SW_MAX)
1345 len += sprintf(p + len, "status:\t\tnot supported\n");
1346 else {
1347 int status;
1348
1349 /* This is in the ABI... */
1350 if (tpacpi_rfk_check_hwblock_state()) {
1351 status = TPACPI_RFK_RADIO_OFF;
1352 } else {
1353 status = tpacpi_rfk_update_swstate(
1354 tpacpi_rfkill_switches[id]);
1355 if (status < 0)
1356 return status;
1357 }
1358
1359 len += sprintf(p + len, "status:\t\t%s\n",
1360 (status == TPACPI_RFK_RADIO_ON) ?
1361 "enabled" : "disabled");
1362 len += sprintf(p + len, "commands:\tenable, disable\n");
1363 }
1364
1365 return len;
1366}
1367
1368static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id, char *buf)
1369{
1370 char *cmd;
1371 int status = -1;
1372 int res = 0;
1373
1374 if (id >= TPACPI_RFK_SW_MAX)
1375 return -ENODEV;
1376
1377 while ((cmd = next_cmd(&buf))) {
1378 if (strlencmp(cmd, "enable") == 0)
1379 status = TPACPI_RFK_RADIO_ON;
1380 else if (strlencmp(cmd, "disable") == 0)
1381 status = TPACPI_RFK_RADIO_OFF;
1382 else
1383 return -EINVAL;
1384 }
1385
1386 if (status != -1) {
1387 tpacpi_disclose_usertask("procfs", "attempt to %s %s\n",
1388 (status == TPACPI_RFK_RADIO_ON) ?
1389 "enable" : "disable",
1390 tpacpi_rfkill_names[id]);
1391 res = (tpacpi_rfkill_switches[id]->ops->set_status)(status);
1392 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1393 }
1394
1395 return res;
1396}
1397
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001398/*************************************************************************
1399 * thinkpad-acpi driver attributes
1400 */
1401
1402/* interface_version --------------------------------------------------- */
1403static ssize_t tpacpi_driver_interface_version_show(
1404 struct device_driver *drv,
1405 char *buf)
1406{
1407 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1408}
1409
1410static DRIVER_ATTR(interface_version, S_IRUGO,
1411 tpacpi_driver_interface_version_show, NULL);
1412
1413/* debug_level --------------------------------------------------------- */
1414static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1415 char *buf)
1416{
1417 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1418}
1419
1420static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1421 const char *buf, size_t count)
1422{
1423 unsigned long t;
1424
1425 if (parse_strtoul(buf, 0xffff, &t))
1426 return -EINVAL;
1427
1428 dbg_level = t;
1429
1430 return count;
1431}
1432
1433static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1434 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1435
1436/* version ------------------------------------------------------------- */
1437static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1438 char *buf)
1439{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001440 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1441 TPACPI_DESC, TPACPI_VERSION);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001442}
1443
1444static DRIVER_ATTR(version, S_IRUGO,
1445 tpacpi_driver_version_show, NULL);
1446
1447/* --------------------------------------------------------------------- */
1448
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001449#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1450
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001451/* wlsw_emulstate ------------------------------------------------------ */
1452static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1453 char *buf)
1454{
1455 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1456}
1457
1458static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1459 const char *buf, size_t count)
1460{
1461 unsigned long t;
1462
1463 if (parse_strtoul(buf, 1, &t))
1464 return -EINVAL;
1465
Johannes Berg19d337d2009-06-02 13:01:37 +02001466 if (tpacpi_wlsw_emulstate != !!t) {
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001467 tpacpi_wlsw_emulstate = !!t;
Johannes Berg19d337d2009-06-02 13:01:37 +02001468 tpacpi_rfk_update_hwblock_state(!t); /* negative logic */
1469 }
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001470
1471 return count;
1472}
1473
1474static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1475 tpacpi_driver_wlsw_emulstate_show,
1476 tpacpi_driver_wlsw_emulstate_store);
1477
1478/* bluetooth_emulstate ------------------------------------------------- */
1479static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1480 struct device_driver *drv,
1481 char *buf)
1482{
1483 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1484}
1485
1486static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1487 struct device_driver *drv,
1488 const char *buf, size_t count)
1489{
1490 unsigned long t;
1491
1492 if (parse_strtoul(buf, 1, &t))
1493 return -EINVAL;
1494
1495 tpacpi_bluetooth_emulstate = !!t;
1496
1497 return count;
1498}
1499
1500static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1501 tpacpi_driver_bluetooth_emulstate_show,
1502 tpacpi_driver_bluetooth_emulstate_store);
1503
1504/* wwan_emulstate ------------------------------------------------- */
1505static ssize_t tpacpi_driver_wwan_emulstate_show(
1506 struct device_driver *drv,
1507 char *buf)
1508{
1509 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1510}
1511
1512static ssize_t tpacpi_driver_wwan_emulstate_store(
1513 struct device_driver *drv,
1514 const char *buf, size_t count)
1515{
1516 unsigned long t;
1517
1518 if (parse_strtoul(buf, 1, &t))
1519 return -EINVAL;
1520
1521 tpacpi_wwan_emulstate = !!t;
1522
1523 return count;
1524}
1525
1526static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1527 tpacpi_driver_wwan_emulstate_show,
1528 tpacpi_driver_wwan_emulstate_store);
1529
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001530/* uwb_emulstate ------------------------------------------------- */
1531static ssize_t tpacpi_driver_uwb_emulstate_show(
1532 struct device_driver *drv,
1533 char *buf)
1534{
1535 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1536}
1537
1538static ssize_t tpacpi_driver_uwb_emulstate_store(
1539 struct device_driver *drv,
1540 const char *buf, size_t count)
1541{
1542 unsigned long t;
1543
1544 if (parse_strtoul(buf, 1, &t))
1545 return -EINVAL;
1546
1547 tpacpi_uwb_emulstate = !!t;
1548
1549 return count;
1550}
1551
1552static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
1553 tpacpi_driver_uwb_emulstate_show,
1554 tpacpi_driver_uwb_emulstate_store);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001555#endif
1556
1557/* --------------------------------------------------------------------- */
1558
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001559static struct driver_attribute *tpacpi_driver_attributes[] = {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001560 &driver_attr_debug_level, &driver_attr_version,
1561 &driver_attr_interface_version,
1562};
1563
1564static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1565{
1566 int i, res;
1567
1568 i = 0;
1569 res = 0;
1570 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1571 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1572 i++;
1573 }
1574
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001575#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1576 if (!res && dbg_wlswemul)
1577 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1578 if (!res && dbg_bluetoothemul)
1579 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1580 if (!res && dbg_wwanemul)
1581 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001582 if (!res && dbg_uwbemul)
1583 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001584#endif
1585
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001586 return res;
1587}
1588
1589static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1590{
1591 int i;
1592
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001593 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001594 driver_remove_file(drv, tpacpi_driver_attributes[i]);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001595
1596#ifdef THINKPAD_ACPI_DEBUGFACILITIES
1597 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1598 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1599 driver_remove_file(drv, &driver_attr_wwan_emulstate);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001600 driver_remove_file(drv, &driver_attr_uwb_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001601#endif
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001602}
1603
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03001604/****************************************************************************
1605 ****************************************************************************
1606 *
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001607 * Subdrivers
1608 *
1609 ****************************************************************************
1610 ****************************************************************************/
1611
1612/*************************************************************************
Henrique de Moraes Holschuh142cfc92007-04-21 11:08:26 -03001613 * thinkpad-acpi init subdriver
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001614 */
1615
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001616static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001618 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
1619 printk(TPACPI_INFO "%s\n", TPACPI_URL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001621 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03001622 (thinkpad_id.bios_version_str) ?
1623 thinkpad_id.bios_version_str : "unknown",
1624 (thinkpad_id.ec_version_str) ?
1625 thinkpad_id.ec_version_str : "unknown");
1626
1627 if (thinkpad_id.vendor && thinkpad_id.model_str)
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03001628 printk(TPACPI_INFO "%s %s, model %s\n",
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03001629 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
1630 "IBM" : ((thinkpad_id.vendor ==
1631 PCI_VENDOR_ID_LENOVO) ?
1632 "Lenovo" : "Unknown vendor"),
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03001633 thinkpad_id.model_str,
1634 (thinkpad_id.nummodel_str) ?
1635 thinkpad_id.nummodel_str : "unknown");
Henrique de Moraes Holschuh3945ac32007-02-06 19:13:44 -02001636
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 return 0;
1638}
1639
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -03001640static int thinkpad_acpi_driver_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641{
1642 int len = 0;
1643
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001644 len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
1645 len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646
1647 return len;
1648}
1649
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001650static struct ibm_struct thinkpad_acpi_driver_data = {
1651 .name = "driver",
1652 .read = thinkpad_acpi_driver_read,
1653};
1654
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001655/*************************************************************************
1656 * Hotkey subdriver
1657 */
1658
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001659enum { /* hot key scan codes (derived from ACPI DSDT) */
1660 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1661 TP_ACPI_HOTKEYSCAN_FNF2,
1662 TP_ACPI_HOTKEYSCAN_FNF3,
1663 TP_ACPI_HOTKEYSCAN_FNF4,
1664 TP_ACPI_HOTKEYSCAN_FNF5,
1665 TP_ACPI_HOTKEYSCAN_FNF6,
1666 TP_ACPI_HOTKEYSCAN_FNF7,
1667 TP_ACPI_HOTKEYSCAN_FNF8,
1668 TP_ACPI_HOTKEYSCAN_FNF9,
1669 TP_ACPI_HOTKEYSCAN_FNF10,
1670 TP_ACPI_HOTKEYSCAN_FNF11,
1671 TP_ACPI_HOTKEYSCAN_FNF12,
1672 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1673 TP_ACPI_HOTKEYSCAN_FNINSERT,
1674 TP_ACPI_HOTKEYSCAN_FNDELETE,
1675 TP_ACPI_HOTKEYSCAN_FNHOME,
1676 TP_ACPI_HOTKEYSCAN_FNEND,
1677 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1678 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1679 TP_ACPI_HOTKEYSCAN_FNSPACE,
1680 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1681 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1682 TP_ACPI_HOTKEYSCAN_MUTE,
1683 TP_ACPI_HOTKEYSCAN_THINKPAD,
1684};
1685
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001686enum { /* Keys available through NVRAM polling */
1687 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1688 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1689};
1690
1691enum { /* Positions of some of the keys in hotkey masks */
1692 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1693 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1694 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1695 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1696 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1697 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1698 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1699 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1700 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1701 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1702 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1703};
1704
1705enum { /* NVRAM to ACPI HKEY group map */
1706 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1707 TP_ACPI_HKEY_ZOOM_MASK |
1708 TP_ACPI_HKEY_DISPSWTCH_MASK |
1709 TP_ACPI_HKEY_HIBERNATE_MASK,
1710 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1711 TP_ACPI_HKEY_BRGHTDWN_MASK,
1712 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1713 TP_ACPI_HKEY_VOLDWN_MASK |
1714 TP_ACPI_HKEY_MUTE_MASK,
1715};
1716
1717#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1718struct tp_nvram_state {
1719 u16 thinkpad_toggle:1;
1720 u16 zoom_toggle:1;
1721 u16 display_toggle:1;
1722 u16 thinklight_toggle:1;
1723 u16 hibernate_toggle:1;
1724 u16 displayexp_toggle:1;
1725 u16 display_state:1;
1726 u16 brightness_toggle:1;
1727 u16 volume_toggle:1;
1728 u16 mute:1;
1729
1730 u8 brightness_level;
1731 u8 volume_level;
1732};
1733
1734static struct task_struct *tpacpi_hotkey_task;
1735static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1736static int hotkey_poll_freq = 10; /* Hz */
1737static struct mutex hotkey_thread_mutex;
1738static struct mutex hotkey_thread_data_mutex;
1739static unsigned int hotkey_config_change;
1740
1741#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1742
1743#define hotkey_source_mask 0U
1744
1745#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1746
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001747static struct mutex hotkey_mutex;
1748
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02001749static enum { /* Reasons for waking up */
1750 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1751 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1752 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1753} hotkey_wakeup_reason;
1754
1755static int hotkey_autosleep_ack;
1756
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001757static u32 hotkey_orig_mask;
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001758static u32 hotkey_all_mask;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001759static u32 hotkey_reserved_mask;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001760static u32 hotkey_mask;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001761
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001762static unsigned int hotkey_report_mode;
1763
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001764static u16 *hotkey_keycode_map;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001765
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -03001766static struct attribute_set *hotkey_dev_attributes;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001767
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001768#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1769#define HOTKEY_CONFIG_CRITICAL_START \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001770 do { \
1771 mutex_lock(&hotkey_thread_data_mutex); \
1772 hotkey_config_change++; \
1773 } while (0);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001774#define HOTKEY_CONFIG_CRITICAL_END \
1775 mutex_unlock(&hotkey_thread_data_mutex);
1776#else
1777#define HOTKEY_CONFIG_CRITICAL_START
1778#define HOTKEY_CONFIG_CRITICAL_END
1779#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1780
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001781/* HKEY.MHKG() return bits */
1782#define TP_HOTKEY_TABLET_MASK (1 << 3)
1783
Johannes Berg19d337d2009-06-02 13:01:37 +02001784static int hotkey_get_wlsw(void)
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001785{
Johannes Berg19d337d2009-06-02 13:01:37 +02001786 int status;
1787
1788 if (!tp_features.hotkey_wlsw)
1789 return -ENODEV;
1790
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001791#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
Johannes Berg19d337d2009-06-02 13:01:37 +02001792 if (dbg_wlswemul)
1793 return (tpacpi_wlsw_emulstate) ?
1794 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001795#endif
Johannes Berg19d337d2009-06-02 13:01:37 +02001796
1797 if (!acpi_evalf(hkey_handle, &status, "WLSW", "d"))
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001798 return -EIO;
Johannes Berg19d337d2009-06-02 13:01:37 +02001799
1800 return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001801}
1802
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001803static int hotkey_get_tablet_mode(int *status)
1804{
1805 int s;
1806
1807 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
1808 return -EIO;
1809
Henrique de Moraes Holschuhcee47f52008-03-04 14:29:21 -08001810 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
1811 return 0;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001812}
1813
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001814/*
1815 * Call with hotkey_mutex held
1816 */
1817static int hotkey_mask_get(void)
1818{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001819 u32 m = 0;
1820
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001821 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001822 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001823 return -EIO;
1824 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001825 hotkey_mask = m | (hotkey_source_mask & hotkey_mask);
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001826
1827 return 0;
1828}
1829
1830/*
1831 * Call with hotkey_mutex held
1832 */
1833static int hotkey_mask_set(u32 mask)
1834{
1835 int i;
1836 int rc = 0;
1837
1838 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -03001839 if (!tp_warned.hotkey_mask_ff &&
1840 (mask == 0xffff || mask == 0xffffff ||
1841 mask == 0xffffffff)) {
1842 tp_warned.hotkey_mask_ff = 1;
1843 printk(TPACPI_NOTICE
1844 "setting the hotkey mask to 0x%08x is likely "
1845 "not the best way to go about it\n", mask);
1846 printk(TPACPI_NOTICE
1847 "please consider using the driver defaults, "
1848 "and refer to up-to-date thinkpad-acpi "
1849 "documentation\n");
1850 }
1851
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001852 HOTKEY_CONFIG_CRITICAL_START
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001853 for (i = 0; i < 32; i++) {
1854 u32 m = 1 << i;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001855 /* enable in firmware mask only keys not in NVRAM
1856 * mode, but enable the key in the cached hotkey_mask
1857 * regardless of mode, or the key will end up
1858 * disabled by hotkey_mask_get() */
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001859 if (!acpi_evalf(hkey_handle,
1860 NULL, "MHKM", "vdd", i + 1,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001861 !!((mask & ~hotkey_source_mask) & m))) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001862 rc = -EIO;
1863 break;
1864 } else {
1865 hotkey_mask = (hotkey_mask & ~m) | (mask & m);
1866 }
1867 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001868 HOTKEY_CONFIG_CRITICAL_END
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001869
1870 /* hotkey_mask_get must be called unconditionally below */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001871 if (!hotkey_mask_get() && !rc &&
1872 (hotkey_mask & ~hotkey_source_mask) !=
1873 (mask & ~hotkey_source_mask)) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001874 printk(TPACPI_NOTICE
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001875 "requested hot key mask 0x%08x, but "
1876 "firmware forced it to 0x%08x\n",
1877 mask, hotkey_mask);
1878 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001879 } else {
1880#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1881 HOTKEY_CONFIG_CRITICAL_START
1882 hotkey_mask = mask & hotkey_source_mask;
1883 HOTKEY_CONFIG_CRITICAL_END
1884 hotkey_mask_get();
1885 if (hotkey_mask != mask) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001886 printk(TPACPI_NOTICE
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001887 "requested hot key mask 0x%08x, "
1888 "forced to 0x%08x (NVRAM poll mask is "
1889 "0x%08x): no firmware mask support\n",
1890 mask, hotkey_mask, hotkey_source_mask);
1891 }
1892#else
1893 hotkey_mask_get();
1894 rc = -ENXIO;
1895#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001896 }
1897
1898 return rc;
1899}
1900
1901static int hotkey_status_get(int *status)
1902{
1903 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1904 return -EIO;
1905
1906 return 0;
1907}
1908
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00001909static int hotkey_status_set(bool enable)
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001910{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00001911 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001912 return -EIO;
1913
1914 return 0;
1915}
1916
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001917static void tpacpi_input_send_tabletsw(void)
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02001918{
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001919 int state;
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02001920
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001921 if (tp_features.hotkey_tablet &&
1922 !hotkey_get_tablet_mode(&state)) {
1923 mutex_lock(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02001924
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001925 input_report_switch(tpacpi_inputdev,
1926 SW_TABLET_MODE, !!state);
1927 input_sync(tpacpi_inputdev);
1928
1929 mutex_unlock(&tpacpi_inputdev_send_mutex);
1930 }
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02001931}
1932
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02001933static void tpacpi_input_send_key(unsigned int scancode)
1934{
1935 unsigned int keycode;
1936
1937 keycode = hotkey_keycode_map[scancode];
1938
1939 if (keycode != KEY_RESERVED) {
1940 mutex_lock(&tpacpi_inputdev_send_mutex);
1941
1942 input_report_key(tpacpi_inputdev, keycode, 1);
1943 if (keycode == KEY_UNKNOWN)
1944 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1945 scancode);
1946 input_sync(tpacpi_inputdev);
1947
1948 input_report_key(tpacpi_inputdev, keycode, 0);
1949 if (keycode == KEY_UNKNOWN)
1950 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1951 scancode);
1952 input_sync(tpacpi_inputdev);
1953
1954 mutex_unlock(&tpacpi_inputdev_send_mutex);
1955 }
1956}
1957
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001958#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1959static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
1960
1961static void tpacpi_hotkey_send_key(unsigned int scancode)
1962{
1963 tpacpi_input_send_key(scancode);
1964 if (hotkey_report_mode < 2) {
1965 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
1966 0x80, 0x1001 + scancode);
1967 }
1968}
1969
1970static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
1971{
1972 u8 d;
1973
1974 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
1975 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
1976 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
1977 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
1978 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
1979 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
1980 }
1981 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
1982 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
1983 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
1984 }
1985 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
1986 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
1987 n->displayexp_toggle =
1988 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
1989 }
1990 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
1991 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
1992 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
1993 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
1994 n->brightness_toggle =
1995 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
1996 }
1997 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
1998 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
1999 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
2000 >> TP_NVRAM_POS_LEVEL_VOLUME;
2001 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2002 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2003 }
2004}
2005
2006#define TPACPI_COMPARE_KEY(__scancode, __member) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002007 do { \
2008 if ((mask & (1 << __scancode)) && \
2009 oldn->__member != newn->__member) \
2010 tpacpi_hotkey_send_key(__scancode); \
2011 } while (0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002012
2013#define TPACPI_MAY_SEND_KEY(__scancode) \
2014 do { if (mask & (1 << __scancode)) \
2015 tpacpi_hotkey_send_key(__scancode); } while (0)
2016
2017static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002018 struct tp_nvram_state *newn,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002019 u32 mask)
2020{
2021 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2022 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2023 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2024 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2025
2026 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2027
2028 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2029
2030 /* handle volume */
2031 if (oldn->volume_toggle != newn->volume_toggle) {
2032 if (oldn->mute != newn->mute) {
2033 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2034 }
2035 if (oldn->volume_level > newn->volume_level) {
2036 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2037 } else if (oldn->volume_level < newn->volume_level) {
2038 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2039 } else if (oldn->mute == newn->mute) {
2040 /* repeated key presses that didn't change state */
2041 if (newn->mute) {
2042 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2043 } else if (newn->volume_level != 0) {
2044 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2045 } else {
2046 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2047 }
2048 }
2049 }
2050
2051 /* handle brightness */
2052 if (oldn->brightness_toggle != newn->brightness_toggle) {
2053 if (oldn->brightness_level < newn->brightness_level) {
2054 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2055 } else if (oldn->brightness_level > newn->brightness_level) {
2056 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2057 } else {
2058 /* repeated key presses that didn't change state */
2059 if (newn->brightness_level != 0) {
2060 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2061 } else {
2062 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2063 }
2064 }
2065 }
2066}
2067
2068#undef TPACPI_COMPARE_KEY
2069#undef TPACPI_MAY_SEND_KEY
2070
2071static int hotkey_kthread(void *data)
2072{
2073 struct tp_nvram_state s[2];
2074 u32 mask;
2075 unsigned int si, so;
2076 unsigned long t;
2077 unsigned int change_detector, must_reset;
2078
2079 mutex_lock(&hotkey_thread_mutex);
2080
2081 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2082 goto exit;
2083
2084 set_freezable();
2085
2086 so = 0;
2087 si = 1;
2088 t = 0;
2089
2090 /* Initial state for compares */
2091 mutex_lock(&hotkey_thread_data_mutex);
2092 change_detector = hotkey_config_change;
2093 mask = hotkey_source_mask & hotkey_mask;
2094 mutex_unlock(&hotkey_thread_data_mutex);
2095 hotkey_read_nvram(&s[so], mask);
2096
2097 while (!kthread_should_stop() && hotkey_poll_freq) {
2098 if (t == 0)
2099 t = 1000/hotkey_poll_freq;
2100 t = msleep_interruptible(t);
2101 if (unlikely(kthread_should_stop()))
2102 break;
2103 must_reset = try_to_freeze();
2104 if (t > 0 && !must_reset)
2105 continue;
2106
2107 mutex_lock(&hotkey_thread_data_mutex);
2108 if (must_reset || hotkey_config_change != change_detector) {
2109 /* forget old state on thaw or config change */
2110 si = so;
2111 t = 0;
2112 change_detector = hotkey_config_change;
2113 }
2114 mask = hotkey_source_mask & hotkey_mask;
2115 mutex_unlock(&hotkey_thread_data_mutex);
2116
2117 if (likely(mask)) {
2118 hotkey_read_nvram(&s[si], mask);
2119 if (likely(si != so)) {
2120 hotkey_compare_and_issue_event(&s[so], &s[si],
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002121 mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002122 }
2123 }
2124
2125 so = si;
2126 si ^= 1;
2127 }
2128
2129exit:
2130 mutex_unlock(&hotkey_thread_mutex);
2131 return 0;
2132}
2133
2134static void hotkey_poll_stop_sync(void)
2135{
2136 if (tpacpi_hotkey_task) {
2137 if (frozen(tpacpi_hotkey_task) ||
2138 freezing(tpacpi_hotkey_task))
2139 thaw_process(tpacpi_hotkey_task);
2140
2141 kthread_stop(tpacpi_hotkey_task);
2142 tpacpi_hotkey_task = NULL;
2143 mutex_lock(&hotkey_thread_mutex);
2144 /* at this point, the thread did exit */
2145 mutex_unlock(&hotkey_thread_mutex);
2146 }
2147}
2148
2149/* call with hotkey_mutex held */
2150static void hotkey_poll_setup(int may_warn)
2151{
2152 if ((hotkey_source_mask & hotkey_mask) != 0 &&
2153 hotkey_poll_freq > 0 &&
2154 (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
2155 if (!tpacpi_hotkey_task) {
2156 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03002157 NULL, TPACPI_NVRAM_KTHREAD_NAME);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002158 if (IS_ERR(tpacpi_hotkey_task)) {
2159 tpacpi_hotkey_task = NULL;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002160 printk(TPACPI_ERR
2161 "could not create kernel thread "
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002162 "for hotkey polling\n");
2163 }
2164 }
2165 } else {
2166 hotkey_poll_stop_sync();
2167 if (may_warn &&
2168 hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002169 printk(TPACPI_NOTICE
2170 "hot keys 0x%08x require polling, "
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002171 "which is currently disabled\n",
2172 hotkey_source_mask);
2173 }
2174 }
2175}
2176
2177static void hotkey_poll_setup_safe(int may_warn)
2178{
2179 mutex_lock(&hotkey_mutex);
2180 hotkey_poll_setup(may_warn);
2181 mutex_unlock(&hotkey_mutex);
2182}
2183
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02002184#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2185
2186static void hotkey_poll_setup_safe(int __unused)
2187{
2188}
2189
2190#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2191
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002192static int hotkey_inputdev_open(struct input_dev *dev)
2193{
2194 switch (tpacpi_lifecycle) {
2195 case TPACPI_LIFE_INIT:
2196 /*
2197 * hotkey_init will call hotkey_poll_setup_safe
2198 * at the appropriate moment
2199 */
2200 return 0;
2201 case TPACPI_LIFE_EXITING:
2202 return -EBUSY;
2203 case TPACPI_LIFE_RUNNING:
2204 hotkey_poll_setup_safe(0);
2205 return 0;
2206 }
2207
2208 /* Should only happen if tpacpi_lifecycle is corrupt */
2209 BUG();
2210 return -EBUSY;
2211}
2212
2213static void hotkey_inputdev_close(struct input_dev *dev)
2214{
2215 /* disable hotkey polling when possible */
2216 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
2217 hotkey_poll_setup_safe(0);
2218}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002219
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002220/* sysfs hotkey enable ------------------------------------------------- */
2221static ssize_t hotkey_enable_show(struct device *dev,
2222 struct device_attribute *attr,
2223 char *buf)
2224{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002225 int res, status;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002226
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002227 printk_deprecated_attribute("hotkey_enable",
2228 "Hotkey reporting is always enabled");
2229
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002230 res = hotkey_status_get(&status);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002231 if (res)
2232 return res;
2233
2234 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2235}
2236
2237static ssize_t hotkey_enable_store(struct device *dev,
2238 struct device_attribute *attr,
2239 const char *buf, size_t count)
2240{
2241 unsigned long t;
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002242
2243 printk_deprecated_attribute("hotkey_enable",
2244 "Hotkeys can be disabled through hotkey_mask");
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002245
2246 if (parse_strtoul(buf, 1, &t))
2247 return -EINVAL;
2248
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002249 if (t == 0)
2250 return -EPERM;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002251
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002252 return count;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002253}
2254
2255static struct device_attribute dev_attr_hotkey_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002256 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002257 hotkey_enable_show, hotkey_enable_store);
2258
2259/* sysfs hotkey mask --------------------------------------------------- */
2260static ssize_t hotkey_mask_show(struct device *dev,
2261 struct device_attribute *attr,
2262 char *buf)
2263{
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002264 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002265
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002266 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002267 return -ERESTARTSYS;
2268 res = hotkey_mask_get();
2269 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002270
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002271 return (res)?
2272 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002273}
2274
2275static ssize_t hotkey_mask_store(struct device *dev,
2276 struct device_attribute *attr,
2277 const char *buf, size_t count)
2278{
2279 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002280 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002281
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002282 if (parse_strtoul(buf, 0xffffffffUL, &t))
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002283 return -EINVAL;
2284
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002285 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002286 return -ERESTARTSYS;
2287
2288 res = hotkey_mask_set(t);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002289
2290#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2291 hotkey_poll_setup(1);
2292#endif
2293
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002294 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002295
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002296 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2297
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002298 return (res) ? res : count;
2299}
2300
2301static struct device_attribute dev_attr_hotkey_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002302 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002303 hotkey_mask_show, hotkey_mask_store);
2304
2305/* sysfs hotkey bios_enabled ------------------------------------------- */
2306static ssize_t hotkey_bios_enabled_show(struct device *dev,
2307 struct device_attribute *attr,
2308 char *buf)
2309{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002310 return sprintf(buf, "0\n");
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002311}
2312
2313static struct device_attribute dev_attr_hotkey_bios_enabled =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002314 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002315
2316/* sysfs hotkey bios_mask ---------------------------------------------- */
2317static ssize_t hotkey_bios_mask_show(struct device *dev,
2318 struct device_attribute *attr,
2319 char *buf)
2320{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002321 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002322}
2323
2324static struct device_attribute dev_attr_hotkey_bios_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002325 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002326
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002327/* sysfs hotkey all_mask ----------------------------------------------- */
2328static ssize_t hotkey_all_mask_show(struct device *dev,
2329 struct device_attribute *attr,
2330 char *buf)
2331{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002332 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2333 hotkey_all_mask | hotkey_source_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002334}
2335
2336static struct device_attribute dev_attr_hotkey_all_mask =
2337 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
2338
2339/* sysfs hotkey recommended_mask --------------------------------------- */
2340static ssize_t hotkey_recommended_mask_show(struct device *dev,
2341 struct device_attribute *attr,
2342 char *buf)
2343{
2344 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002345 (hotkey_all_mask | hotkey_source_mask)
2346 & ~hotkey_reserved_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002347}
2348
2349static struct device_attribute dev_attr_hotkey_recommended_mask =
2350 __ATTR(hotkey_recommended_mask, S_IRUGO,
2351 hotkey_recommended_mask_show, NULL);
2352
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002353#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2354
2355/* sysfs hotkey hotkey_source_mask ------------------------------------- */
2356static ssize_t hotkey_source_mask_show(struct device *dev,
2357 struct device_attribute *attr,
2358 char *buf)
2359{
2360 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2361}
2362
2363static ssize_t hotkey_source_mask_store(struct device *dev,
2364 struct device_attribute *attr,
2365 const char *buf, size_t count)
2366{
2367 unsigned long t;
2368
2369 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2370 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2371 return -EINVAL;
2372
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002373 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002374 return -ERESTARTSYS;
2375
2376 HOTKEY_CONFIG_CRITICAL_START
2377 hotkey_source_mask = t;
2378 HOTKEY_CONFIG_CRITICAL_END
2379
2380 hotkey_poll_setup(1);
2381
2382 mutex_unlock(&hotkey_mutex);
2383
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002384 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2385
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002386 return count;
2387}
2388
2389static struct device_attribute dev_attr_hotkey_source_mask =
2390 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2391 hotkey_source_mask_show, hotkey_source_mask_store);
2392
2393/* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2394static ssize_t hotkey_poll_freq_show(struct device *dev,
2395 struct device_attribute *attr,
2396 char *buf)
2397{
2398 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2399}
2400
2401static ssize_t hotkey_poll_freq_store(struct device *dev,
2402 struct device_attribute *attr,
2403 const char *buf, size_t count)
2404{
2405 unsigned long t;
2406
2407 if (parse_strtoul(buf, 25, &t))
2408 return -EINVAL;
2409
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002410 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002411 return -ERESTARTSYS;
2412
2413 hotkey_poll_freq = t;
2414
2415 hotkey_poll_setup(1);
2416 mutex_unlock(&hotkey_mutex);
2417
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002418 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2419
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002420 return count;
2421}
2422
2423static struct device_attribute dev_attr_hotkey_poll_freq =
2424 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2425 hotkey_poll_freq_show, hotkey_poll_freq_store);
2426
2427#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2428
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002429/* sysfs hotkey radio_sw (pollable) ------------------------------------ */
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002430static ssize_t hotkey_radio_sw_show(struct device *dev,
2431 struct device_attribute *attr,
2432 char *buf)
2433{
Johannes Berg19d337d2009-06-02 13:01:37 +02002434 int res;
2435 res = hotkey_get_wlsw();
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002436 if (res < 0)
2437 return res;
2438
Johannes Berg19d337d2009-06-02 13:01:37 +02002439 /* Opportunistic update */
2440 tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
2441
2442 return snprintf(buf, PAGE_SIZE, "%d\n",
2443 (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002444}
2445
2446static struct device_attribute dev_attr_hotkey_radio_sw =
2447 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2448
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002449static void hotkey_radio_sw_notify_change(void)
2450{
2451 if (tp_features.hotkey_wlsw)
2452 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2453 "hotkey_radio_sw");
2454}
2455
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002456/* sysfs hotkey tablet mode (pollable) --------------------------------- */
2457static ssize_t hotkey_tablet_mode_show(struct device *dev,
2458 struct device_attribute *attr,
2459 char *buf)
2460{
2461 int res, s;
2462 res = hotkey_get_tablet_mode(&s);
2463 if (res < 0)
2464 return res;
2465
2466 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2467}
2468
2469static struct device_attribute dev_attr_hotkey_tablet_mode =
2470 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2471
2472static void hotkey_tablet_mode_notify_change(void)
2473{
2474 if (tp_features.hotkey_tablet)
2475 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2476 "hotkey_tablet_mode");
2477}
2478
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002479/* sysfs hotkey report_mode -------------------------------------------- */
2480static ssize_t hotkey_report_mode_show(struct device *dev,
2481 struct device_attribute *attr,
2482 char *buf)
2483{
2484 return snprintf(buf, PAGE_SIZE, "%d\n",
2485 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2486}
2487
2488static struct device_attribute dev_attr_hotkey_report_mode =
2489 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2490
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002491/* sysfs wakeup reason (pollable) -------------------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002492static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2493 struct device_attribute *attr,
2494 char *buf)
2495{
2496 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2497}
2498
2499static struct device_attribute dev_attr_hotkey_wakeup_reason =
2500 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2501
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02002502static void hotkey_wakeup_reason_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002503{
2504 if (tp_features.hotkey_mask)
2505 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2506 "wakeup_reason");
2507}
2508
2509/* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002510static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2511 struct device_attribute *attr,
2512 char *buf)
2513{
2514 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2515}
2516
2517static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2518 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2519 hotkey_wakeup_hotunplug_complete_show, NULL);
2520
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02002521static void hotkey_wakeup_hotunplug_complete_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002522{
2523 if (tp_features.hotkey_mask)
2524 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2525 "wakeup_hotunplug_complete");
2526}
2527
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002528/* --------------------------------------------------------------------- */
2529
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002530static struct attribute *hotkey_attributes[] __initdata = {
2531 &dev_attr_hotkey_enable.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002532 &dev_attr_hotkey_bios_enabled.attr,
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002533 &dev_attr_hotkey_report_mode.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002534#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2535 &dev_attr_hotkey_mask.attr,
2536 &dev_attr_hotkey_all_mask.attr,
2537 &dev_attr_hotkey_recommended_mask.attr,
2538 &dev_attr_hotkey_source_mask.attr,
2539 &dev_attr_hotkey_poll_freq.attr,
2540#endif
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002541};
2542
2543static struct attribute *hotkey_mask_attributes[] __initdata = {
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002544 &dev_attr_hotkey_bios_mask.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002545#ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2546 &dev_attr_hotkey_mask.attr,
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002547 &dev_attr_hotkey_all_mask.attr,
2548 &dev_attr_hotkey_recommended_mask.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002549#endif
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002550 &dev_attr_hotkey_wakeup_reason.attr,
2551 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002552};
2553
Johannes Berg19d337d2009-06-02 13:01:37 +02002554/*
2555 * Sync both the hw and sw blocking state of all switches
2556 */
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002557static void tpacpi_send_radiosw_update(void)
2558{
2559 int wlsw;
2560
Johannes Berg19d337d2009-06-02 13:01:37 +02002561 /*
2562 * We must sync all rfkill controllers *before* issuing any
2563 * rfkill input events, or we will race the rfkill core input
2564 * handler.
2565 *
2566 * tpacpi_inputdev_send_mutex works as a syncronization point
2567 * for the above.
2568 *
2569 * We optimize to avoid numerous calls to hotkey_get_wlsw.
2570 */
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002571
Johannes Berg19d337d2009-06-02 13:01:37 +02002572 wlsw = hotkey_get_wlsw();
2573
2574 /* Sync hw blocking state first if it is hw-blocked */
2575 if (wlsw == TPACPI_RFK_RADIO_OFF)
2576 tpacpi_rfk_update_hwblock_state(true);
2577
2578 /* Sync sw blocking state */
2579 tpacpi_rfk_update_swstate_all();
2580
2581 /* Sync hw blocking state last if it is hw-unblocked */
2582 if (wlsw == TPACPI_RFK_RADIO_ON)
2583 tpacpi_rfk_update_hwblock_state(false);
2584
2585 /* Issue rfkill input event for WLSW switch */
2586 if (!(wlsw < 0)) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002587 mutex_lock(&tpacpi_inputdev_send_mutex);
2588
2589 input_report_switch(tpacpi_inputdev,
Johannes Berg19d337d2009-06-02 13:01:37 +02002590 SW_RFKILL_ALL, (wlsw > 0));
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002591 input_sync(tpacpi_inputdev);
2592
2593 mutex_unlock(&tpacpi_inputdev_send_mutex);
2594 }
Johannes Berg19d337d2009-06-02 13:01:37 +02002595
2596 /*
2597 * this can be unconditional, as we will poll state again
2598 * if userspace uses the notify to read data
2599 */
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002600 hotkey_radio_sw_notify_change();
2601}
2602
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002603static void hotkey_exit(void)
2604{
2605#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2606 hotkey_poll_stop_sync();
2607#endif
2608
2609 if (hotkey_dev_attributes)
2610 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2611
2612 kfree(hotkey_keycode_map);
2613
2614 if (tp_features.hotkey) {
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002615 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002616 "restoring original hot key mask\n");
2617 /* no short-circuit boolean operator below! */
2618 if ((hotkey_mask_set(hotkey_orig_mask) |
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002619 hotkey_status_set(false)) != 0)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002620 printk(TPACPI_ERR
2621 "failed to restore hot key mask "
2622 "to BIOS defaults\n");
2623 }
2624}
2625
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002626static int __init hotkey_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002627{
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002628 /* Requirements for changing the default keymaps:
2629 *
2630 * 1. Many of the keys are mapped to KEY_RESERVED for very
2631 * good reasons. Do not change them unless you have deep
2632 * knowledge on the IBM and Lenovo ThinkPad firmware for
2633 * the various ThinkPad models. The driver behaves
2634 * differently for KEY_RESERVED: such keys have their
2635 * hot key mask *unset* in mask_recommended, and also
2636 * in the initial hot key mask programmed into the
2637 * firmware at driver load time, which means the firm-
2638 * ware may react very differently if you change them to
2639 * something else;
2640 *
2641 * 2. You must be subscribed to the linux-thinkpad and
2642 * ibm-acpi-devel mailing lists, and you should read the
2643 * list archives since 2007 if you want to change the
2644 * keymaps. This requirement exists so that you will
2645 * know the past history of problems with the thinkpad-
2646 * acpi driver keymaps, and also that you will be
2647 * listening to any bug reports;
2648 *
2649 * 3. Do not send thinkpad-acpi specific patches directly to
2650 * for merging, *ever*. Send them to the linux-acpi
2651 * mailinglist for comments. Merging is to be done only
2652 * through acpi-test and the ACPI maintainer.
2653 *
2654 * If the above is too much to ask, don't change the keymap.
2655 * Ask the thinkpad-acpi maintainer to do it, instead.
2656 */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002657 static u16 ibm_keycode_map[] __initdata = {
2658 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2659 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
2660 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2661 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002662
2663 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002664 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2665 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2666 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002667
2668 /* brightness: firmware always reacts to them, unless
2669 * X.org did some tricks in the radeon BIOS scratch
2670 * registers of *some* models */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02002671 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02002672 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002673
2674 /* Thinklight: firmware always react to it */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002675 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002676
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002677 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2678 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002679
2680 /* Volume: firmware always react to it and reprograms
2681 * the built-in *extra* mixer. Never map it to control
2682 * another mixer by default. */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02002683 KEY_RESERVED, /* 0x14: VOLUME UP */
2684 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2685 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002686
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002687 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002688
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002689 /* (assignments unknown, please report if found) */
2690 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2691 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2692 };
2693 static u16 lenovo_keycode_map[] __initdata = {
2694 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2695 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
2696 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2697 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002698
2699 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002700 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2701 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2702 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002703
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03002704 /* These either have to go through ACPI video, or
2705 * act like in the IBM ThinkPads, so don't ever
2706 * enable them by default */
Henrique de Moraes Holschuh56a185b2007-12-13 12:14:09 -02002707 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuh56a185b2007-12-13 12:14:09 -02002708 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002709
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002710 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002711
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002712 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2713 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002714
2715 /* Volume: z60/z61, T60 (BIOS version?): firmware always
2716 * react to it and reprograms the built-in *extra* mixer.
2717 * Never map it to control another mixer by default.
2718 *
2719 * T60?, T61, R60?, R61: firmware and EC tries to send
2720 * these over the regular keyboard, so these are no-ops,
2721 * but there are still weird bugs re. MUTE, so do not
2722 * change unless you get test reports from all Lenovo
2723 * models. May cause the BIOS to interfere with the
2724 * HDA mixer.
2725 */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02002726 KEY_RESERVED, /* 0x14: VOLUME UP */
2727 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2728 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002729
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002730 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002731
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002732 /* (assignments unknown, please report if found) */
2733 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2734 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2735 };
2736
2737#define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
2738#define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
2739#define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
2740
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002741 int res, i;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002742 int status;
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03002743 int hkeyv;
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03002744
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002745 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2746 "initializing hotkey subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002747
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002748 BUG_ON(!tpacpi_inputdev);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002749 BUG_ON(tpacpi_inputdev->open != NULL ||
2750 tpacpi_inputdev->close != NULL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002751
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002752 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002753 mutex_init(&hotkey_mutex);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002754
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002755#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2756 mutex_init(&hotkey_thread_mutex);
2757 mutex_init(&hotkey_thread_data_mutex);
2758#endif
2759
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002760 /* hotkey not supported on 570 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002761 tp_features.hotkey = hkey_handle != NULL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002762
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002763 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2764 "hotkeys are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002765 str_supported(tp_features.hotkey));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002766
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002767 if (!tp_features.hotkey)
2768 return 1;
2769
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03002770 tpacpi_disable_brightness_delay();
2771
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002772 hotkey_dev_attributes = create_attr_set(13, NULL);
2773 if (!hotkey_dev_attributes)
2774 return -ENOMEM;
2775 res = add_many_to_attr_set(hotkey_dev_attributes,
2776 hotkey_attributes,
2777 ARRAY_SIZE(hotkey_attributes));
2778 if (res)
2779 goto err_exit;
2780
2781 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2782 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
2783 for HKEY interface version 0x100 */
2784 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
2785 if ((hkeyv >> 8) != 1) {
2786 printk(TPACPI_ERR "unknown version of the "
2787 "HKEY interface: 0x%x\n", hkeyv);
2788 printk(TPACPI_ERR "please report this to %s\n",
2789 TPACPI_MAIL);
2790 } else {
2791 /*
2792 * MHKV 0x100 in A31, R40, R40e,
2793 * T4x, X31, and later
2794 */
2795 tp_features.hotkey_mask = 1;
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002796 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2797 "firmware HKEY interface version: 0x%x\n",
2798 hkeyv);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002799 }
2800 }
2801
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002802 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2803 "hotkey masks are %s\n",
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002804 str_supported(tp_features.hotkey_mask));
2805
2806 if (tp_features.hotkey_mask) {
2807 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
2808 "MHKA", "qd")) {
2809 printk(TPACPI_ERR
2810 "missing MHKA handler, "
2811 "please report this to %s\n",
2812 TPACPI_MAIL);
2813 /* FN+F12, FN+F4, FN+F3 */
2814 hotkey_all_mask = 0x080cU;
2815 }
2816 }
2817
2818 /* hotkey_source_mask *must* be zero for
2819 * the first hotkey_mask_get */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002820 if (tp_features.hotkey_mask) {
2821 res = hotkey_mask_get();
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002822 if (res)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002823 goto err_exit;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002824
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002825 hotkey_orig_mask = hotkey_mask;
2826 res = add_many_to_attr_set(
2827 hotkey_dev_attributes,
2828 hotkey_mask_attributes,
2829 ARRAY_SIZE(hotkey_mask_attributes));
2830 if (res)
2831 goto err_exit;
2832 }
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002833
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002834#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002835 if (tp_features.hotkey_mask) {
2836 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
2837 & ~hotkey_all_mask;
2838 } else {
2839 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK;
2840 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002841
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002842 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002843 "hotkey source mask 0x%08x, polling freq %d\n",
2844 hotkey_source_mask, hotkey_poll_freq);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002845#endif
2846
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02002847#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2848 if (dbg_wlswemul) {
2849 tp_features.hotkey_wlsw = 1;
2850 printk(TPACPI_INFO
2851 "radio switch emulation enabled\n");
2852 } else
2853#endif
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002854 /* Not all thinkpads have a hardware radio switch */
2855 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
2856 tp_features.hotkey_wlsw = 1;
2857 printk(TPACPI_INFO
2858 "radio switch found; radios are %s\n",
2859 enabled(status, 0));
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03002860 }
2861 if (tp_features.hotkey_wlsw)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002862 res = add_to_attr_set(hotkey_dev_attributes,
2863 &dev_attr_hotkey_radio_sw.attr);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002864
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002865 /* For X41t, X60t, X61t Tablets... */
2866 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
2867 tp_features.hotkey_tablet = 1;
2868 printk(TPACPI_INFO
2869 "possible tablet mode switch found; "
2870 "ThinkPad in %s mode\n",
2871 (status & TP_HOTKEY_TABLET_MASK)?
2872 "tablet" : "laptop");
2873 res = add_to_attr_set(hotkey_dev_attributes,
2874 &dev_attr_hotkey_tablet_mode.attr);
2875 }
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002876
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002877 if (!res)
2878 res = register_attr_set_with_sysfs(
2879 hotkey_dev_attributes,
2880 &tpacpi_pdev->dev.kobj);
2881 if (res)
2882 goto err_exit;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002883
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002884 /* Set up key map */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002885
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002886 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
2887 GFP_KERNEL);
2888 if (!hotkey_keycode_map) {
2889 printk(TPACPI_ERR
2890 "failed to allocate memory for key map\n");
2891 res = -ENOMEM;
2892 goto err_exit;
2893 }
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002894
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002895 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002896 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002897 "using Lenovo default hot key map\n");
2898 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
2899 TPACPI_HOTKEY_MAP_SIZE);
2900 } else {
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002901 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002902 "using IBM default hot key map\n");
2903 memcpy(hotkey_keycode_map, &ibm_keycode_map,
2904 TPACPI_HOTKEY_MAP_SIZE);
2905 }
2906
2907 set_bit(EV_KEY, tpacpi_inputdev->evbit);
2908 set_bit(EV_MSC, tpacpi_inputdev->evbit);
2909 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
2910 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
2911 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
2912 tpacpi_inputdev->keycode = hotkey_keycode_map;
2913 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
2914 if (hotkey_keycode_map[i] != KEY_RESERVED) {
2915 set_bit(hotkey_keycode_map[i],
2916 tpacpi_inputdev->keybit);
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002917 } else {
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002918 if (i < sizeof(hotkey_reserved_mask)*8)
2919 hotkey_reserved_mask |= 1 << i;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002920 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002921 }
2922
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002923 if (tp_features.hotkey_wlsw) {
2924 set_bit(EV_SW, tpacpi_inputdev->evbit);
2925 set_bit(SW_RFKILL_ALL, tpacpi_inputdev->swbit);
2926 }
2927 if (tp_features.hotkey_tablet) {
2928 set_bit(EV_SW, tpacpi_inputdev->evbit);
2929 set_bit(SW_TABLET_MODE, tpacpi_inputdev->swbit);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002930 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002931
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002932 /* Do not issue duplicate brightness change events to
2933 * userspace */
2934 if (!tp_features.bright_acpimode)
2935 /* update bright_acpimode... */
2936 tpacpi_check_std_acpi_brightness_support();
2937
Henrique de Moraes Holschuh8bf3d4c2009-05-30 13:25:09 -03002938 if (tp_features.bright_acpimode && acpi_video_backlight_support()) {
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002939 printk(TPACPI_INFO
2940 "This ThinkPad has standard ACPI backlight "
2941 "brightness control, supported by the ACPI "
2942 "video driver\n");
2943 printk(TPACPI_NOTICE
2944 "Disabling thinkpad-acpi brightness events "
2945 "by default...\n");
2946
2947 /* The hotkey_reserved_mask change below is not
2948 * necessary while the keys are at KEY_RESERVED in the
2949 * default map, but better safe than sorry, leave it
2950 * here as a marker of what we have to do, especially
2951 * when we finally become able to set this at runtime
2952 * on response to X.org requests */
2953 hotkey_reserved_mask |=
2954 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
2955 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002956 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002957
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002958 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2959 "enabling firmware HKEY event interface...\n");
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002960 res = hotkey_status_set(true);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002961 if (res) {
2962 hotkey_exit();
2963 return res;
2964 }
2965 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
2966 & ~hotkey_reserved_mask)
2967 | hotkey_orig_mask);
2968 if (res < 0 && res != -ENXIO) {
2969 hotkey_exit();
2970 return res;
2971 }
2972
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002973 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2974 "legacy ibm/hotkey event reporting over procfs %s\n",
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002975 (hotkey_report_mode < 2) ?
2976 "enabled" : "disabled");
2977
2978 tpacpi_inputdev->open = &hotkey_inputdev_open;
2979 tpacpi_inputdev->close = &hotkey_inputdev_close;
2980
2981 hotkey_poll_setup_safe(1);
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002982 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002983 tpacpi_input_send_tabletsw();
2984
2985 return 0;
2986
2987err_exit:
2988 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2989 hotkey_dev_attributes = NULL;
2990
2991 return (res < 0)? res : 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002992}
2993
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002994static bool hotkey_notify_hotkey(const u32 hkey,
2995 bool *send_acpi_ev,
2996 bool *ignore_acpi_ev)
2997{
2998 /* 0x1000-0x1FFF: key presses */
2999 unsigned int scancode = hkey & 0xfff;
3000 *send_acpi_ev = true;
3001 *ignore_acpi_ev = false;
3002
3003 if (scancode > 0 && scancode < 0x21) {
3004 scancode--;
3005 if (!(hotkey_source_mask & (1 << scancode))) {
3006 tpacpi_input_send_key(scancode);
3007 *send_acpi_ev = false;
3008 } else {
3009 *ignore_acpi_ev = true;
3010 }
3011 return true;
3012 }
3013 return false;
3014}
3015
3016static bool hotkey_notify_wakeup(const u32 hkey,
3017 bool *send_acpi_ev,
3018 bool *ignore_acpi_ev)
3019{
3020 /* 0x2000-0x2FFF: Wakeup reason */
3021 *send_acpi_ev = true;
3022 *ignore_acpi_ev = false;
3023
3024 switch (hkey) {
3025 case 0x2304: /* suspend, undock */
3026 case 0x2404: /* hibernation, undock */
3027 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3028 *ignore_acpi_ev = true;
3029 break;
3030
3031 case 0x2305: /* suspend, bay eject */
3032 case 0x2405: /* hibernation, bay eject */
3033 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3034 *ignore_acpi_ev = true;
3035 break;
3036
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003037 case 0x2313: /* Battery on critical low level (S3) */
3038 case 0x2413: /* Battery on critical low level (S4) */
3039 printk(TPACPI_ALERT
3040 "EMERGENCY WAKEUP: battery almost empty\n");
3041 /* how to auto-heal: */
3042 /* 2313: woke up from S3, go to S4/S5 */
3043 /* 2413: woke up from S4, go to S5 */
3044 break;
3045
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003046 default:
3047 return false;
3048 }
3049
3050 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
3051 printk(TPACPI_INFO
3052 "woke up due to a hot-unplug "
3053 "request...\n");
3054 hotkey_wakeup_reason_notify_change();
3055 }
3056 return true;
3057}
3058
3059static bool hotkey_notify_usrevent(const u32 hkey,
3060 bool *send_acpi_ev,
3061 bool *ignore_acpi_ev)
3062{
3063 /* 0x5000-0x5FFF: human interface helpers */
3064 *send_acpi_ev = true;
3065 *ignore_acpi_ev = false;
3066
3067 switch (hkey) {
3068 case 0x5010: /* Lenovo new BIOS: brightness changed */
3069 case 0x500b: /* X61t: tablet pen inserted into bay */
3070 case 0x500c: /* X61t: tablet pen removed from bay */
3071 return true;
3072
3073 case 0x5009: /* X41t-X61t: swivel up (tablet mode) */
3074 case 0x500a: /* X41t-X61t: swivel down (normal mode) */
3075 tpacpi_input_send_tabletsw();
3076 hotkey_tablet_mode_notify_change();
3077 *send_acpi_ev = false;
3078 return true;
3079
3080 case 0x5001:
3081 case 0x5002:
3082 /* LID switch events. Do not propagate */
3083 *ignore_acpi_ev = true;
3084 return true;
3085
3086 default:
3087 return false;
3088 }
3089}
3090
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003091static bool hotkey_notify_thermal(const u32 hkey,
3092 bool *send_acpi_ev,
3093 bool *ignore_acpi_ev)
3094{
3095 /* 0x6000-0x6FFF: thermal alarms */
3096 *send_acpi_ev = true;
3097 *ignore_acpi_ev = false;
3098
3099 switch (hkey) {
3100 case 0x6011:
3101 printk(TPACPI_CRIT
3102 "THERMAL ALARM: battery is too hot!\n");
3103 /* recommended action: warn user through gui */
3104 return true;
3105 case 0x6012:
3106 printk(TPACPI_ALERT
3107 "THERMAL EMERGENCY: battery is extremely hot!\n");
3108 /* recommended action: immediate sleep/hibernate */
3109 return true;
3110 case 0x6021:
3111 printk(TPACPI_CRIT
3112 "THERMAL ALARM: "
3113 "a sensor reports something is too hot!\n");
3114 /* recommended action: warn user through gui, that */
3115 /* some internal component is too hot */
3116 return true;
3117 case 0x6022:
3118 printk(TPACPI_ALERT
3119 "THERMAL EMERGENCY: "
3120 "a sensor reports something is extremely hot!\n");
3121 /* recommended action: immediate sleep/hibernate */
3122 return true;
Henrique de Moraes Holschuh54926ce2009-01-11 03:01:09 -02003123 case 0x6030:
3124 printk(TPACPI_INFO
3125 "EC reports that Thermal Table has changed\n");
3126 /* recommended action: do nothing, we don't have
3127 * Lenovo ATM information */
3128 return true;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003129 default:
3130 printk(TPACPI_ALERT
3131 "THERMAL ALERT: unknown thermal alarm received\n");
3132 return false;
3133 }
3134}
3135
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003136static void hotkey_notify(struct ibm_struct *ibm, u32 event)
3137{
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003138 u32 hkey;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003139 bool send_acpi_ev;
3140 bool ignore_acpi_ev;
3141 bool known_ev;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003142
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003143 if (event != 0x80) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003144 printk(TPACPI_ERR
3145 "unknown HKEY notification event %d\n", event);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003146 /* forward it to userspace, maybe it knows how to handle it */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003147 acpi_bus_generate_netlink_event(
3148 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02003149 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003150 event, 0);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003151 return;
3152 }
3153
3154 while (1) {
3155 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003156 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003157 return;
3158 }
3159
3160 if (hkey == 0) {
3161 /* queue empty */
3162 return;
3163 }
3164
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003165 send_acpi_ev = true;
3166 ignore_acpi_ev = false;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003167
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003168 switch (hkey >> 12) {
3169 case 1:
3170 /* 0x1000-0x1FFF: key presses */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003171 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
3172 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003173 break;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003174 case 2:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003175 /* 0x2000-0x2FFF: Wakeup reason */
3176 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
3177 &ignore_acpi_ev);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003178 break;
3179 case 3:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003180 /* 0x3000-0x3FFF: bay-related wakeups */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003181 if (hkey == 0x3003) {
3182 hotkey_autosleep_ack = 1;
3183 printk(TPACPI_INFO
3184 "bay ejected\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003185 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003186 known_ev = true;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003187 } else {
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003188 known_ev = false;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003189 }
3190 break;
3191 case 4:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003192 /* 0x4000-0x4FFF: dock-related wakeups */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003193 if (hkey == 0x4003) {
3194 hotkey_autosleep_ack = 1;
3195 printk(TPACPI_INFO
3196 "undocked\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003197 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003198 known_ev = true;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003199 } else {
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003200 known_ev = false;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003201 }
3202 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003203 case 5:
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02003204 /* 0x5000-0x5FFF: human interface helpers */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003205 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
3206 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003207 break;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003208 case 6:
3209 /* 0x6000-0x6FFF: thermal alarms */
3210 known_ev = hotkey_notify_thermal(hkey, &send_acpi_ev,
3211 &ignore_acpi_ev);
3212 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003213 case 7:
3214 /* 0x7000-0x7FFF: misc */
3215 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003216 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02003217 send_acpi_ev = 0;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003218 known_ev = true;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003219 break;
3220 }
3221 /* fallthrough to default */
3222 default:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003223 known_ev = false;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02003224 }
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003225 if (!known_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003226 printk(TPACPI_NOTICE
3227 "unhandled HKEY event 0x%04x\n", hkey);
Henrique de Moraes Holschuhcb429352009-01-11 03:01:07 -02003228 printk(TPACPI_NOTICE
3229 "please report the conditions when this "
3230 "event happened to %s\n", TPACPI_MAIL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003231 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003232
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003233 /* Legacy events */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003234 if (!ignore_acpi_ev &&
3235 (send_acpi_ev || hotkey_report_mode < 2)) {
3236 acpi_bus_generate_proc_event(ibm->acpi->device,
3237 event, hkey);
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03003238 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003239
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003240 /* netlink events */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03003241 if (!ignore_acpi_ev && send_acpi_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003242 acpi_bus_generate_netlink_event(
3243 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02003244 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003245 event, hkey);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003246 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003247 }
3248}
3249
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003250static void hotkey_suspend(pm_message_t state)
3251{
3252 /* Do these on suspend, we get the events on early resume! */
3253 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
3254 hotkey_autosleep_ack = 0;
3255}
3256
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03003257static void hotkey_resume(void)
3258{
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03003259 tpacpi_disable_brightness_delay();
3260
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02003261 if (hotkey_mask_get())
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003262 printk(TPACPI_ERR
3263 "error while trying to read hot key mask "
3264 "from firmware\n");
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003265 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02003266 hotkey_tablet_mode_notify_change();
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003267 hotkey_wakeup_reason_notify_change();
3268 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003269 hotkey_poll_setup_safe(0);
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03003270}
3271
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003272/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003273static int hotkey_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03003275 int res, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 int len = 0;
3277
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003278 if (!tp_features.hotkey) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003279 len += sprintf(p + len, "status:\t\tnot supported\n");
3280 return len;
3281 }
3282
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02003283 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02003284 return -ERESTARTSYS;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02003285 res = hotkey_status_get(&status);
3286 if (!res)
3287 res = hotkey_mask_get();
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003288 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03003289 if (res)
3290 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291
3292 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003293 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02003294 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295 len += sprintf(p + len,
3296 "commands:\tenable, disable, reset, <mask>\n");
3297 } else {
3298 len += sprintf(p + len, "mask:\t\tnot supported\n");
3299 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
3300 }
3301
3302 return len;
3303}
3304
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003305static void hotkey_enabledisable_warn(bool enable)
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003306{
3307 tpacpi_log_usertask("procfs hotkey enable/disable");
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003308 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
3309 TPACPI_WARN
3310 "hotkey enable/disable functionality has been "
3311 "removed from the driver. Hotkeys are always "
3312 "enabled\n"))
3313 printk(TPACPI_ERR
3314 "Please remove the hotkey=enable module "
3315 "parameter, it is deprecated. Hotkeys are always "
3316 "enabled\n");
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003317}
3318
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003319static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003321 int res;
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03003322 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003325 if (!tp_features.hotkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326 return -ENODEV;
3327
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02003328 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02003329 return -ERESTARTSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003330
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02003331 mask = hotkey_mask;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003332
3333 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334 while ((cmd = next_cmd(&buf))) {
3335 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003336 hotkey_enabledisable_warn(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003338 hotkey_enabledisable_warn(0);
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003339 res = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 } else if (strlencmp(cmd, "reset") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003341 mask = hotkey_orig_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
3343 /* mask set */
3344 } else if (sscanf(cmd, "%x", &mask) == 1) {
3345 /* mask set */
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003346 } else {
3347 res = -EINVAL;
3348 goto errexit;
3349 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350 }
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003351
3352 if (!res)
3353 tpacpi_disclose_usertask("procfs hotkey",
3354 "set mask to 0x%08x\n", mask);
3355
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02003356 if (!res && mask != hotkey_mask)
3357 res = hotkey_mask_set(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003359errexit:
3360 mutex_unlock(&hotkey_mutex);
3361 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003362}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003364static const struct acpi_device_id ibm_htk_device_ids[] = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003365 {TPACPI_ACPI_HKEY_HID, 0},
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003366 {"", 0},
3367};
3368
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003369static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003370 .hid = ibm_htk_device_ids,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003371 .notify = hotkey_notify,
3372 .handle = &hkey_handle,
3373 .type = ACPI_DEVICE_NOTIFY,
3374};
3375
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003376static struct ibm_struct hotkey_driver_data = {
3377 .name = "hotkey",
3378 .read = hotkey_read,
3379 .write = hotkey_write,
3380 .exit = hotkey_exit,
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03003381 .resume = hotkey_resume,
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003382 .suspend = hotkey_suspend,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003383 .acpi = &ibm_hotkey_acpidriver,
3384};
3385
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003386/*************************************************************************
3387 * Bluetooth subdriver
3388 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003390enum {
3391 /* ACPI GBDC/SBDC bits */
3392 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
3393 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003394 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
3395 off / last state */
3396};
3397
3398enum {
3399 /* ACPI \BLTH commands */
3400 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
3401 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
3402 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
3403 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
3404 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003405};
3406
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003407#define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
3408
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003409static void bluetooth_suspend(pm_message_t state)
3410{
3411 /* Try to make sure radio will resume powered off */
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003412 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3413 TP_ACPI_BLTH_PWR_OFF_ON_RESUME))
3414 vdbg_printk(TPACPI_DBG_RFKILL,
3415 "bluetooth power down on resume request failed\n");
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003416}
3417
Johannes Berg19d337d2009-06-02 13:01:37 +02003418static int bluetooth_get_status(void)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003419{
3420 int status;
3421
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003422#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3423 if (dbg_bluetoothemul)
3424 return (tpacpi_bluetooth_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02003425 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003426#endif
3427
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003428 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
3429 return -EIO;
3430
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003431 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02003432 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003433}
3434
Johannes Berg19d337d2009-06-02 13:01:37 +02003435static int bluetooth_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003436{
3437 int status;
3438
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003439 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02003440 "will attempt to %s bluetooth\n",
3441 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003442
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003443#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3444 if (dbg_bluetoothemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02003445 tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003446 return 0;
3447 }
3448#endif
3449
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003450 /* We make sure to keep TP_ACPI_BLUETOOTH_RESUMECTRL off */
Johannes Berg19d337d2009-06-02 13:01:37 +02003451 if (state == TPACPI_RFK_RADIO_ON)
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003452 status = TP_ACPI_BLUETOOTH_RADIOSSW;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003453 else
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003454 status = 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02003455
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003456 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
3457 return -EIO;
3458
3459 return 0;
3460}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003461
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003462/* sysfs bluetooth enable ---------------------------------------------- */
3463static ssize_t bluetooth_enable_show(struct device *dev,
3464 struct device_attribute *attr,
3465 char *buf)
3466{
Johannes Berg19d337d2009-06-02 13:01:37 +02003467 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID,
3468 attr, buf);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003469}
3470
3471static ssize_t bluetooth_enable_store(struct device *dev,
3472 struct device_attribute *attr,
3473 const char *buf, size_t count)
3474{
Johannes Berg19d337d2009-06-02 13:01:37 +02003475 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID,
3476 attr, buf, count);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003477}
3478
3479static struct device_attribute dev_attr_bluetooth_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03003480 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003481 bluetooth_enable_show, bluetooth_enable_store);
3482
3483/* --------------------------------------------------------------------- */
3484
3485static struct attribute *bluetooth_attributes[] = {
3486 &dev_attr_bluetooth_enable.attr,
3487 NULL
3488};
3489
3490static const struct attribute_group bluetooth_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003491 .attrs = bluetooth_attributes,
3492};
3493
Johannes Berg19d337d2009-06-02 13:01:37 +02003494static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = {
3495 .get_status = bluetooth_get_status,
3496 .set_status = bluetooth_set_status,
3497};
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003498
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003499static void bluetooth_shutdown(void)
3500{
3501 /* Order firmware to save current state to NVRAM */
3502 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3503 TP_ACPI_BLTH_SAVE_STATE))
3504 printk(TPACPI_NOTICE
3505 "failed to save bluetooth state to NVRAM\n");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003506 else
3507 vdbg_printk(TPACPI_DBG_RFKILL,
3508 "bluestooth state saved to NVRAM\n");
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003509}
3510
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003511static void bluetooth_exit(void)
3512{
3513 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3514 &bluetooth_attr_group);
Johannes Berg19d337d2009-06-02 13:01:37 +02003515
3516 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
3517
3518 bluetooth_shutdown();
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003519}
3520
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003521static int __init bluetooth_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003523 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003524 int status = 0;
3525
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003526 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3527 "initializing bluetooth subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003528
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003529 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003530
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003531 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3532 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003533 tp_features.bluetooth = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003534 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003536 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3537 "bluetooth is %s, status 0x%02x\n",
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003538 str_supported(tp_features.bluetooth),
3539 status);
3540
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003541#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3542 if (dbg_bluetoothemul) {
3543 tp_features.bluetooth = 1;
3544 printk(TPACPI_INFO
3545 "bluetooth switch emulation enabled\n");
3546 } else
3547#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003548 if (tp_features.bluetooth &&
3549 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
3550 /* no bluetooth hardware present in system */
3551 tp_features.bluetooth = 0;
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003552 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003553 "bluetooth hardware not installed\n");
3554 }
3555
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003556 if (!tp_features.bluetooth)
3557 return 1;
3558
Johannes Berg19d337d2009-06-02 13:01:37 +02003559 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
3560 &bluetooth_tprfk_ops,
3561 RFKILL_TYPE_BLUETOOTH,
3562 TPACPI_RFK_BLUETOOTH_SW_NAME,
3563 true);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003564 if (res)
3565 return res;
3566
Johannes Berg19d337d2009-06-02 13:01:37 +02003567 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3568 &bluetooth_attr_group);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003569 if (res) {
Johannes Berg19d337d2009-06-02 13:01:37 +02003570 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003571 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003572 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003573
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003574 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575}
3576
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003577/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003578static int bluetooth_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579{
Johannes Berg19d337d2009-06-02 13:01:37 +02003580 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581}
3582
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003583static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584{
Johannes Berg19d337d2009-06-02 13:01:37 +02003585 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586}
3587
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003588static struct ibm_struct bluetooth_driver_data = {
3589 .name = "bluetooth",
3590 .read = bluetooth_read,
3591 .write = bluetooth_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003592 .exit = bluetooth_exit,
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003593 .suspend = bluetooth_suspend,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003594 .shutdown = bluetooth_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003595};
3596
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003597/*************************************************************************
3598 * Wan subdriver
3599 */
3600
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003601enum {
3602 /* ACPI GWAN/SWAN bits */
3603 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
3604 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003605 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
3606 off / last state */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003607};
3608
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003609#define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
3610
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003611static void wan_suspend(pm_message_t state)
3612{
3613 /* Try to make sure radio will resume powered off */
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003614 if (!acpi_evalf(NULL, NULL, "\\WGSV", "qvd",
3615 TP_ACPI_WGSV_PWR_OFF_ON_RESUME))
3616 vdbg_printk(TPACPI_DBG_RFKILL,
3617 "WWAN power down on resume request failed\n");
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003618}
3619
Johannes Berg19d337d2009-06-02 13:01:37 +02003620static int wan_get_status(void)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003621{
3622 int status;
3623
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003624#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3625 if (dbg_wwanemul)
3626 return (tpacpi_wwan_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02003627 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003628#endif
3629
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003630 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
3631 return -EIO;
3632
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003633 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02003634 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003635}
3636
Johannes Berg19d337d2009-06-02 13:01:37 +02003637static int wan_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003638{
3639 int status;
3640
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003641 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02003642 "will attempt to %s wwan\n",
3643 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003644
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003645#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3646 if (dbg_wwanemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02003647 tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003648 return 0;
3649 }
3650#endif
3651
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003652 /* We make sure to keep TP_ACPI_WANCARD_RESUMECTRL off */
Johannes Berg19d337d2009-06-02 13:01:37 +02003653 if (state == TPACPI_RFK_RADIO_ON)
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003654 status = TP_ACPI_WANCARD_RADIOSSW;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003655 else
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003656 status = 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02003657
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003658 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
3659 return -EIO;
3660
3661 return 0;
3662}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003663
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003664/* sysfs wan enable ---------------------------------------------------- */
3665static ssize_t wan_enable_show(struct device *dev,
3666 struct device_attribute *attr,
3667 char *buf)
3668{
Johannes Berg19d337d2009-06-02 13:01:37 +02003669 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
3670 attr, buf);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003671}
3672
3673static ssize_t wan_enable_store(struct device *dev,
3674 struct device_attribute *attr,
3675 const char *buf, size_t count)
3676{
Johannes Berg19d337d2009-06-02 13:01:37 +02003677 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
3678 attr, buf, count);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003679}
3680
3681static struct device_attribute dev_attr_wan_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03003682 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003683 wan_enable_show, wan_enable_store);
3684
3685/* --------------------------------------------------------------------- */
3686
3687static struct attribute *wan_attributes[] = {
3688 &dev_attr_wan_enable.attr,
3689 NULL
3690};
3691
3692static const struct attribute_group wan_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003693 .attrs = wan_attributes,
3694};
3695
Johannes Berg19d337d2009-06-02 13:01:37 +02003696static const struct tpacpi_rfk_ops wan_tprfk_ops = {
3697 .get_status = wan_get_status,
3698 .set_status = wan_set_status,
3699};
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003700
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003701static void wan_shutdown(void)
3702{
3703 /* Order firmware to save current state to NVRAM */
3704 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
3705 TP_ACPI_WGSV_SAVE_STATE))
3706 printk(TPACPI_NOTICE
3707 "failed to save WWAN state to NVRAM\n");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003708 else
3709 vdbg_printk(TPACPI_DBG_RFKILL,
3710 "WWAN state saved to NVRAM\n");
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003711}
3712
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003713static void wan_exit(void)
3714{
3715 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3716 &wan_attr_group);
Johannes Berg19d337d2009-06-02 13:01:37 +02003717
3718 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
3719
3720 wan_shutdown();
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003721}
3722
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003723static int __init wan_init(struct ibm_init_struct *iibm)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003724{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003725 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003726 int status = 0;
3727
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003728 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3729 "initializing wan subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003730
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003731 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003732
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003733 tp_features.wan = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003734 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003735
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003736 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3737 "wan is %s, status 0x%02x\n",
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003738 str_supported(tp_features.wan),
3739 status);
3740
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003741#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3742 if (dbg_wwanemul) {
3743 tp_features.wan = 1;
3744 printk(TPACPI_INFO
3745 "wwan switch emulation enabled\n");
3746 } else
3747#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003748 if (tp_features.wan &&
3749 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
3750 /* no wan hardware present in system */
3751 tp_features.wan = 0;
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003752 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003753 "wan hardware not installed\n");
3754 }
3755
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003756 if (!tp_features.wan)
3757 return 1;
3758
Johannes Berg19d337d2009-06-02 13:01:37 +02003759 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
3760 &wan_tprfk_ops,
3761 RFKILL_TYPE_WWAN,
3762 TPACPI_RFK_WWAN_SW_NAME,
3763 true);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003764 if (res)
3765 return res;
3766
Johannes Berg19d337d2009-06-02 13:01:37 +02003767 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3768 &wan_attr_group);
3769
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003770 if (res) {
Johannes Berg19d337d2009-06-02 13:01:37 +02003771 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003772 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003773 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003774
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003775 return 0;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003776}
3777
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003778/* procfs -------------------------------------------------------------- */
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003779static int wan_read(char *p)
3780{
Johannes Berg19d337d2009-06-02 13:01:37 +02003781 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, p);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003782}
3783
3784static int wan_write(char *buf)
3785{
Johannes Berg19d337d2009-06-02 13:01:37 +02003786 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003787}
3788
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003789static struct ibm_struct wan_driver_data = {
3790 .name = "wan",
3791 .read = wan_read,
3792 .write = wan_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003793 .exit = wan_exit,
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003794 .suspend = wan_suspend,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003795 .shutdown = wan_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003796};
3797
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003798/*************************************************************************
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02003799 * UWB subdriver
3800 */
3801
3802enum {
3803 /* ACPI GUWB/SUWB bits */
3804 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
3805 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
3806};
3807
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003808#define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
3809
Johannes Berg19d337d2009-06-02 13:01:37 +02003810static int uwb_get_status(void)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02003811{
3812 int status;
3813
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02003814#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3815 if (dbg_uwbemul)
3816 return (tpacpi_uwb_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02003817 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02003818#endif
3819
3820 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
3821 return -EIO;
3822
3823 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02003824 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02003825}
3826
Johannes Berg19d337d2009-06-02 13:01:37 +02003827static int uwb_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02003828{
3829 int status;
3830
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003831 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02003832 "will attempt to %s UWB\n",
3833 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003834
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02003835#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3836 if (dbg_uwbemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02003837 tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02003838 return 0;
3839 }
3840#endif
3841
Johannes Berg19d337d2009-06-02 13:01:37 +02003842 if (state == TPACPI_RFK_RADIO_ON)
3843 status = TP_ACPI_UWB_RADIOSSW;
3844 else
3845 status = 0;
3846
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02003847 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
3848 return -EIO;
3849
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02003850 return 0;
3851}
3852
3853/* --------------------------------------------------------------------- */
3854
Johannes Berg19d337d2009-06-02 13:01:37 +02003855static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
3856 .get_status = uwb_get_status,
3857 .set_status = uwb_set_status,
3858};
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02003859
3860static void uwb_exit(void)
3861{
Johannes Berg19d337d2009-06-02 13:01:37 +02003862 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02003863}
3864
3865static int __init uwb_init(struct ibm_init_struct *iibm)
3866{
3867 int res;
3868 int status = 0;
3869
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003870 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3871 "initializing uwb subdriver\n");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02003872
3873 TPACPI_ACPIHANDLE_INIT(hkey);
3874
3875 tp_features.uwb = hkey_handle &&
3876 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
3877
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003878 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3879 "uwb is %s, status 0x%02x\n",
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02003880 str_supported(tp_features.uwb),
3881 status);
3882
3883#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3884 if (dbg_uwbemul) {
3885 tp_features.uwb = 1;
3886 printk(TPACPI_INFO
3887 "uwb switch emulation enabled\n");
3888 } else
3889#endif
3890 if (tp_features.uwb &&
3891 !(status & TP_ACPI_UWB_HWPRESENT)) {
3892 /* no uwb hardware present in system */
3893 tp_features.uwb = 0;
3894 dbg_printk(TPACPI_DBG_INIT,
3895 "uwb hardware not installed\n");
3896 }
3897
3898 if (!tp_features.uwb)
3899 return 1;
3900
3901 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
Johannes Berg19d337d2009-06-02 13:01:37 +02003902 &uwb_tprfk_ops,
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02003903 RFKILL_TYPE_UWB,
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003904 TPACPI_RFK_UWB_SW_NAME,
Johannes Berg19d337d2009-06-02 13:01:37 +02003905 false);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02003906 return res;
3907}
3908
3909static struct ibm_struct uwb_driver_data = {
3910 .name = "uwb",
3911 .exit = uwb_exit,
3912 .flags.experimental = 1,
3913};
3914
3915/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003916 * Video subdriver
3917 */
3918
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02003919#ifdef CONFIG_THINKPAD_ACPI_VIDEO
3920
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003921enum video_access_mode {
3922 TPACPI_VIDEO_NONE = 0,
3923 TPACPI_VIDEO_570, /* 570 */
3924 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
3925 TPACPI_VIDEO_NEW, /* all others */
3926};
3927
3928enum { /* video status flags, based on VIDEO_570 */
3929 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
3930 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
3931 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
3932};
3933
3934enum { /* TPACPI_VIDEO_570 constants */
3935 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
3936 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
3937 * video_status_flags */
3938 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
3939 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
3940};
3941
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02003942static enum video_access_mode video_supported;
3943static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003944
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003945static int video_autosw_get(void);
3946static int video_autosw_set(int enable);
3947
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003948TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003949
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003950static int __init video_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003952 int ivga;
3953
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003954 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
3955
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003956 TPACPI_ACPIHANDLE_INIT(vid);
3957 TPACPI_ACPIHANDLE_INIT(vid2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003958
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003959 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
3960 /* G41, assume IVGA doesn't change */
3961 vid_handle = vid2_handle;
3962
3963 if (!vid_handle)
3964 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003965 video_supported = TPACPI_VIDEO_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003966 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
3967 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003968 video_supported = TPACPI_VIDEO_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003969 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
3970 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003971 video_supported = TPACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003972 else
3973 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003974 video_supported = TPACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003976 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
3977 str_supported(video_supported != TPACPI_VIDEO_NONE),
3978 video_supported);
3979
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003980 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981}
3982
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003983static void video_exit(void)
3984{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003985 dbg_printk(TPACPI_DBG_EXIT,
3986 "restoring original video autoswitch mode\n");
3987 if (video_autosw_set(video_orig_autosw))
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003988 printk(TPACPI_ERR "error while trying to restore original "
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003989 "video autoswitch mode\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003990}
3991
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003992static int video_outputsw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993{
3994 int status = 0;
3995 int i;
3996
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003997 switch (video_supported) {
3998 case TPACPI_VIDEO_570:
3999 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
4000 TP_ACPI_VIDEO_570_PHSCMD))
4001 return -EIO;
4002 status = i & TP_ACPI_VIDEO_570_PHSMASK;
4003 break;
4004 case TPACPI_VIDEO_770:
4005 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4006 return -EIO;
4007 if (i)
4008 status |= TP_ACPI_VIDEO_S_LCD;
4009 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4010 return -EIO;
4011 if (i)
4012 status |= TP_ACPI_VIDEO_S_CRT;
4013 break;
4014 case TPACPI_VIDEO_NEW:
4015 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4016 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4017 return -EIO;
4018 if (i)
4019 status |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004021 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4022 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4023 return -EIO;
4024 if (i)
4025 status |= TP_ACPI_VIDEO_S_LCD;
4026 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4027 return -EIO;
4028 if (i)
4029 status |= TP_ACPI_VIDEO_S_DVI;
4030 break;
4031 default:
4032 return -ENOSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004033 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034
4035 return status;
4036}
4037
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004038static int video_outputsw_set(int status)
4039{
4040 int autosw;
4041 int res = 0;
4042
4043 switch (video_supported) {
4044 case TPACPI_VIDEO_570:
4045 res = acpi_evalf(NULL, NULL,
4046 "\\_SB.PHS2", "vdd",
4047 TP_ACPI_VIDEO_570_PHS2CMD,
4048 status | TP_ACPI_VIDEO_570_PHS2SET);
4049 break;
4050 case TPACPI_VIDEO_770:
4051 autosw = video_autosw_get();
4052 if (autosw < 0)
4053 return autosw;
4054
4055 res = video_autosw_set(1);
4056 if (res)
4057 return res;
4058 res = acpi_evalf(vid_handle, NULL,
4059 "ASWT", "vdd", status * 0x100, 0);
4060 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004061 printk(TPACPI_ERR
4062 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004063 return -EIO;
4064 }
4065 break;
4066 case TPACPI_VIDEO_NEW:
4067 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004068 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004069 break;
4070 default:
4071 return -ENOSYS;
4072 }
4073
4074 return (res)? 0 : -EIO;
4075}
4076
4077static int video_autosw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004079 int autosw = 0;
4080
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004081 switch (video_supported) {
4082 case TPACPI_VIDEO_570:
4083 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
4084 return -EIO;
4085 break;
4086 case TPACPI_VIDEO_770:
4087 case TPACPI_VIDEO_NEW:
4088 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4089 return -EIO;
4090 break;
4091 default:
4092 return -ENOSYS;
4093 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004094
4095 return autosw & 1;
4096}
4097
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004098static int video_autosw_set(int enable)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004099{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004100 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004101 return -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004102 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004103}
4104
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004105static int video_outputsw_cycle(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004106{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004107 int autosw = video_autosw_get();
4108 int res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004109
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004110 if (autosw < 0)
4111 return autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004112
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004113 switch (video_supported) {
4114 case TPACPI_VIDEO_570:
4115 res = video_autosw_set(1);
4116 if (res)
4117 return res;
4118 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4119 break;
4120 case TPACPI_VIDEO_770:
4121 case TPACPI_VIDEO_NEW:
4122 res = video_autosw_set(1);
4123 if (res)
4124 return res;
4125 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4126 break;
4127 default:
4128 return -ENOSYS;
4129 }
4130 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004131 printk(TPACPI_ERR
4132 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004133 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004134 }
4135
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004136 return (res)? 0 : -EIO;
4137}
4138
4139static int video_expand_toggle(void)
4140{
4141 switch (video_supported) {
4142 case TPACPI_VIDEO_570:
4143 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
4144 0 : -EIO;
4145 case TPACPI_VIDEO_770:
4146 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
4147 0 : -EIO;
4148 case TPACPI_VIDEO_NEW:
4149 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
4150 0 : -EIO;
4151 default:
4152 return -ENOSYS;
4153 }
4154 /* not reached */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004155}
4156
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004157static int video_read(char *p)
4158{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004159 int status, autosw;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004160 int len = 0;
4161
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004162 if (video_supported == TPACPI_VIDEO_NONE) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004163 len += sprintf(p + len, "status:\t\tnot supported\n");
4164 return len;
4165 }
4166
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004167 status = video_outputsw_get();
4168 if (status < 0)
4169 return status;
4170
4171 autosw = video_autosw_get();
4172 if (autosw < 0)
4173 return autosw;
4174
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004175 len += sprintf(p + len, "status:\t\tsupported\n");
4176 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
4177 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004178 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004179 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
4180 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
4181 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
4182 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004183 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004184 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
4185 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
4186 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
4187
4188 return len;
4189}
4190
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004191static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192{
4193 char *cmd;
4194 int enable, disable, status;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004195 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004197 if (video_supported == TPACPI_VIDEO_NONE)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004198 return -ENODEV;
4199
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004200 enable = 0;
4201 disable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004202
4203 while ((cmd = next_cmd(&buf))) {
4204 if (strlencmp(cmd, "lcd_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004205 enable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206 } else if (strlencmp(cmd, "lcd_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004207 disable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208 } else if (strlencmp(cmd, "crt_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004209 enable |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210 } else if (strlencmp(cmd, "crt_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004211 disable |= TP_ACPI_VIDEO_S_CRT;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004212 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004213 strlencmp(cmd, "dvi_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004214 enable |= TP_ACPI_VIDEO_S_DVI;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004215 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004216 strlencmp(cmd, "dvi_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004217 disable |= TP_ACPI_VIDEO_S_DVI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218 } else if (strlencmp(cmd, "auto_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004219 res = video_autosw_set(1);
4220 if (res)
4221 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004222 } else if (strlencmp(cmd, "auto_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004223 res = video_autosw_set(0);
4224 if (res)
4225 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226 } else if (strlencmp(cmd, "video_switch") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004227 res = video_outputsw_cycle();
4228 if (res)
4229 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004231 res = video_expand_toggle();
4232 if (res)
4233 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234 } else
4235 return -EINVAL;
4236 }
4237
4238 if (enable || disable) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004239 status = video_outputsw_get();
4240 if (status < 0)
4241 return status;
4242 res = video_outputsw_set((status & ~disable) | enable);
4243 if (res)
4244 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245 }
4246
4247 return 0;
4248}
4249
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004250static struct ibm_struct video_driver_data = {
4251 .name = "video",
4252 .read = video_read,
4253 .write = video_write,
4254 .exit = video_exit,
4255};
4256
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02004257#endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4258
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004259/*************************************************************************
4260 * Light (thinklight) subdriver
4261 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004263TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4264TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004265
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004266static int light_get_status(void)
4267{
4268 int status = 0;
4269
4270 if (tp_features.light_status) {
4271 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4272 return -EIO;
4273 return (!!status);
4274 }
4275
4276 return -ENXIO;
4277}
4278
4279static int light_set_status(int status)
4280{
4281 int rc;
4282
4283 if (tp_features.light) {
4284 if (cmos_handle) {
4285 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4286 (status)?
4287 TP_CMOS_THINKLIGHT_ON :
4288 TP_CMOS_THINKLIGHT_OFF);
4289 } else {
4290 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4291 (status)? 1 : 0);
4292 }
4293 return (rc)? 0 : -EIO;
4294 }
4295
4296 return -ENXIO;
4297}
4298
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004299static void light_set_status_worker(struct work_struct *work)
4300{
4301 struct tpacpi_led_classdev *data =
4302 container_of(work, struct tpacpi_led_classdev, work);
4303
4304 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00004305 light_set_status((data->new_state != TPACPI_LED_OFF));
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004306}
4307
4308static void light_sysfs_set(struct led_classdev *led_cdev,
4309 enum led_brightness brightness)
4310{
4311 struct tpacpi_led_classdev *data =
4312 container_of(led_cdev,
4313 struct tpacpi_led_classdev,
4314 led_classdev);
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00004315 data->new_state = (brightness != LED_OFF) ?
4316 TPACPI_LED_ON : TPACPI_LED_OFF;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004317 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004318}
4319
4320static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4321{
4322 return (light_get_status() == 1)? LED_FULL : LED_OFF;
4323}
4324
4325static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4326 .led_classdev = {
4327 .name = "tpacpi::thinklight",
4328 .brightness_set = &light_sysfs_set,
4329 .brightness_get = &light_sysfs_get,
4330 }
4331};
4332
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004333static int __init light_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334{
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004335 int rc;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004336
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004337 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4338
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004339 TPACPI_ACPIHANDLE_INIT(ledb);
4340 TPACPI_ACPIHANDLE_INIT(lght);
4341 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004342 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004343
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004344 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004345 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004346
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004347 if (tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004348 /* light status not supported on
4349 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004350 tp_features.light_status =
4351 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004353 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4354 str_supported(tp_features.light),
4355 str_supported(tp_features.light_status));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004356
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004357 if (!tp_features.light)
4358 return 1;
4359
4360 rc = led_classdev_register(&tpacpi_pdev->dev,
4361 &tpacpi_led_thinklight.led_classdev);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004362
4363 if (rc < 0) {
4364 tp_features.light = 0;
4365 tp_features.light_status = 0;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004366 } else {
4367 rc = 0;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004368 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004369
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004370 return rc;
4371}
4372
4373static void light_exit(void)
4374{
4375 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
4376 if (work_pending(&tpacpi_led_thinklight.work))
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004377 flush_workqueue(tpacpi_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378}
4379
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004380static int light_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381{
4382 int len = 0;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004383 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004385 if (!tp_features.light) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004386 len += sprintf(p + len, "status:\t\tnot supported\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004387 } else if (!tp_features.light_status) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004388 len += sprintf(p + len, "status:\t\tunknown\n");
4389 len += sprintf(p + len, "commands:\ton, off\n");
4390 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004391 status = light_get_status();
4392 if (status < 0)
4393 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004395 len += sprintf(p + len, "commands:\ton, off\n");
4396 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397
4398 return len;
4399}
4400
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004401static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004404 int newstatus = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004405
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004406 if (!tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004407 return -ENODEV;
4408
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409 while ((cmd = next_cmd(&buf))) {
4410 if (strlencmp(cmd, "on") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004411 newstatus = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004412 } else if (strlencmp(cmd, "off") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004413 newstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414 } else
4415 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416 }
4417
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004418 return light_set_status(newstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419}
4420
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004421static struct ibm_struct light_driver_data = {
4422 .name = "light",
4423 .read = light_read,
4424 .write = light_write,
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004425 .exit = light_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004426};
4427
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004428/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004429 * CMOS subdriver
4430 */
4431
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004432/* sysfs cmos_command -------------------------------------------------- */
4433static ssize_t cmos_command_store(struct device *dev,
4434 struct device_attribute *attr,
4435 const char *buf, size_t count)
4436{
4437 unsigned long cmos_cmd;
4438 int res;
4439
4440 if (parse_strtoul(buf, 21, &cmos_cmd))
4441 return -EINVAL;
4442
4443 res = issue_thinkpad_cmos_command(cmos_cmd);
4444 return (res)? res : count;
4445}
4446
4447static struct device_attribute dev_attr_cmos_command =
4448 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4449
4450/* --------------------------------------------------------------------- */
4451
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004452static int __init cmos_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004453{
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004454 int res;
4455
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004456 vdbg_printk(TPACPI_DBG_INIT,
4457 "initializing cmos commands subdriver\n");
4458
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004459 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004460
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004461 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4462 str_supported(cmos_handle != NULL));
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004463
4464 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4465 if (res)
4466 return res;
4467
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004468 return (cmos_handle)? 0 : 1;
4469}
4470
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004471static void cmos_exit(void)
4472{
4473 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4474}
4475
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004476static int cmos_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477{
4478 int len = 0;
4479
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004480 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4481 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482 if (!cmos_handle)
4483 len += sprintf(p + len, "status:\t\tnot supported\n");
4484 else {
4485 len += sprintf(p + len, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004486 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487 }
4488
4489 return len;
4490}
4491
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004492static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493{
4494 char *cmd;
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004495 int cmos_cmd, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496
4497 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004498 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
4499 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500 /* cmos_cmd set */
4501 } else
4502 return -EINVAL;
4503
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004504 res = issue_thinkpad_cmos_command(cmos_cmd);
4505 if (res)
4506 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507 }
4508
4509 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004510}
4511
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004512static struct ibm_struct cmos_driver_data = {
4513 .name = "cmos",
4514 .read = cmos_read,
4515 .write = cmos_write,
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004516 .exit = cmos_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004517};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004518
4519/*************************************************************************
4520 * LED subdriver
4521 */
4522
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004523enum led_access_mode {
4524 TPACPI_LED_NONE = 0,
4525 TPACPI_LED_570, /* 570 */
4526 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4527 TPACPI_LED_NEW, /* all others */
4528};
4529
4530enum { /* For TPACPI_LED_OLD */
4531 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
4532 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
4533 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
4534};
4535
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02004536static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004537
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004538TPACPI_HANDLE(led, ec, "SLED", /* 570 */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004539 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
4540 /* T20-22, X20-21 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004541 "LED", /* all others */
4542 ); /* R30, R31 */
4543
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03004544#define TPACPI_LED_NUMLEDS 16
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004545static struct tpacpi_led_classdev *tpacpi_leds;
4546static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
Harvey Harrisone3aa51f2008-05-29 17:51:57 -07004547static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004548 /* there's a limit of 19 chars + NULL before 2.6.26 */
4549 "tpacpi::power",
4550 "tpacpi:orange:batt",
4551 "tpacpi:green:batt",
4552 "tpacpi::dock_active",
4553 "tpacpi::bay_active",
4554 "tpacpi::dock_batt",
4555 "tpacpi::unknown_led",
4556 "tpacpi::standby",
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03004557 "tpacpi::dock_status1",
4558 "tpacpi::dock_status2",
4559 "tpacpi::unknown_led2",
4560 "tpacpi::unknown_led3",
4561 "tpacpi::thinkvantage",
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004562};
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03004563#define TPACPI_SAFE_LEDS 0x1081U
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004564
4565static inline bool tpacpi_is_led_restricted(const unsigned int led)
4566{
4567#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
4568 return false;
4569#else
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03004570 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004571#endif
4572}
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004573
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004574static int led_get_status(const unsigned int led)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004575{
4576 int status;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004577 enum led_status_t led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004578
4579 switch (led_supported) {
4580 case TPACPI_LED_570:
4581 if (!acpi_evalf(ec_handle,
4582 &status, "GLED", "dd", 1 << led))
4583 return -EIO;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004584 led_s = (status == 0)?
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004585 TPACPI_LED_OFF :
4586 ((status == 1)?
4587 TPACPI_LED_ON :
4588 TPACPI_LED_BLINK);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004589 tpacpi_led_state_cache[led] = led_s;
4590 return led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004591 default:
4592 return -ENXIO;
4593 }
4594
4595 /* not reached */
4596}
4597
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004598static int led_set_status(const unsigned int led,
4599 const enum led_status_t ledstatus)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004600{
4601 /* off, on, blink. Index is led_status_t */
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004602 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
4603 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004604
4605 int rc = 0;
4606
4607 switch (led_supported) {
4608 case TPACPI_LED_570:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004609 /* 570 */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004610 if (unlikely(led > 7))
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004611 return -EINVAL;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004612 if (unlikely(tpacpi_is_led_restricted(led)))
4613 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004614 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4615 (1 << led), led_sled_arg1[ledstatus]))
4616 rc = -EIO;
4617 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004618 case TPACPI_LED_OLD:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004619 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004620 if (unlikely(led > 7))
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004621 return -EINVAL;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004622 if (unlikely(tpacpi_is_led_restricted(led)))
4623 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004624 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
4625 if (rc >= 0)
4626 rc = ec_write(TPACPI_LED_EC_HLBL,
4627 (ledstatus == TPACPI_LED_BLINK) << led);
4628 if (rc >= 0)
4629 rc = ec_write(TPACPI_LED_EC_HLCL,
4630 (ledstatus != TPACPI_LED_OFF) << led);
4631 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004632 case TPACPI_LED_NEW:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004633 /* all others */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004634 if (unlikely(led >= TPACPI_LED_NUMLEDS))
4635 return -EINVAL;
4636 if (unlikely(tpacpi_is_led_restricted(led)))
4637 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004638 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4639 led, led_led_arg1[ledstatus]))
4640 rc = -EIO;
4641 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004642 default:
4643 rc = -ENXIO;
4644 }
4645
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004646 if (!rc)
4647 tpacpi_led_state_cache[led] = ledstatus;
4648
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004649 return rc;
4650}
4651
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004652static void led_set_status_worker(struct work_struct *work)
4653{
4654 struct tpacpi_led_classdev *data =
4655 container_of(work, struct tpacpi_led_classdev, work);
4656
4657 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00004658 led_set_status(data->led, data->new_state);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004659}
4660
4661static void led_sysfs_set(struct led_classdev *led_cdev,
4662 enum led_brightness brightness)
4663{
4664 struct tpacpi_led_classdev *data = container_of(led_cdev,
4665 struct tpacpi_led_classdev, led_classdev);
4666
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00004667 if (brightness == LED_OFF)
4668 data->new_state = TPACPI_LED_OFF;
4669 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
4670 data->new_state = TPACPI_LED_ON;
4671 else
4672 data->new_state = TPACPI_LED_BLINK;
4673
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004674 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004675}
4676
4677static int led_sysfs_blink_set(struct led_classdev *led_cdev,
4678 unsigned long *delay_on, unsigned long *delay_off)
4679{
4680 struct tpacpi_led_classdev *data = container_of(led_cdev,
4681 struct tpacpi_led_classdev, led_classdev);
4682
4683 /* Can we choose the flash rate? */
4684 if (*delay_on == 0 && *delay_off == 0) {
4685 /* yes. set them to the hardware blink rate (1 Hz) */
4686 *delay_on = 500; /* ms */
4687 *delay_off = 500; /* ms */
4688 } else if ((*delay_on != 500) || (*delay_off != 500))
4689 return -EINVAL;
4690
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00004691 data->new_state = TPACPI_LED_BLINK;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004692 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004693
4694 return 0;
4695}
4696
4697static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
4698{
4699 int rc;
4700
4701 struct tpacpi_led_classdev *data = container_of(led_cdev,
4702 struct tpacpi_led_classdev, led_classdev);
4703
4704 rc = led_get_status(data->led);
4705
4706 if (rc == TPACPI_LED_OFF || rc < 0)
4707 rc = LED_OFF; /* no error handling in led class :( */
4708 else
4709 rc = LED_FULL;
4710
4711 return rc;
4712}
4713
4714static void led_exit(void)
4715{
4716 unsigned int i;
4717
4718 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4719 if (tpacpi_leds[i].led_classdev.name)
4720 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
4721 }
4722
4723 kfree(tpacpi_leds);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004724}
4725
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004726static int __init tpacpi_init_led(unsigned int led)
4727{
4728 int rc;
4729
4730 tpacpi_leds[led].led = led;
4731
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03004732 /* LEDs with no name don't get registered */
4733 if (!tpacpi_led_names[led])
4734 return 0;
4735
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004736 tpacpi_leds[led].led_classdev.brightness_set = &led_sysfs_set;
4737 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
4738 if (led_supported == TPACPI_LED_570)
4739 tpacpi_leds[led].led_classdev.brightness_get =
4740 &led_sysfs_get;
4741
4742 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
4743
4744 INIT_WORK(&tpacpi_leds[led].work, led_set_status_worker);
4745
4746 rc = led_classdev_register(&tpacpi_pdev->dev,
4747 &tpacpi_leds[led].led_classdev);
4748 if (rc < 0)
4749 tpacpi_leds[led].led_classdev.name = NULL;
4750
4751 return rc;
4752}
4753
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03004754static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
4755 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
4756 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
4757 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
4758
4759 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
4760 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
4761 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
4762 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
4763 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
4764 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
4765 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
4766 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
4767
4768 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
4769 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
4770 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
4771 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
4772 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
4773
4774 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
4775 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
4776 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
4777 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
4778
4779 /* (1) - may have excess leds enabled on MSB */
4780
4781 /* Defaults (order matters, keep last, don't reorder!) */
4782 { /* Lenovo */
4783 .vendor = PCI_VENDOR_ID_LENOVO,
4784 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
4785 .quirks = 0x1fffU,
4786 },
4787 { /* IBM ThinkPads with no EC version string */
4788 .vendor = PCI_VENDOR_ID_IBM,
4789 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
4790 .quirks = 0x00ffU,
4791 },
4792 { /* IBM ThinkPads with EC version string */
4793 .vendor = PCI_VENDOR_ID_IBM,
4794 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
4795 .quirks = 0x00bfU,
4796 },
4797};
4798
4799#undef TPACPI_LEDQ_IBM
4800#undef TPACPI_LEDQ_LNV
4801
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004802static int __init led_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004803{
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004804 unsigned int i;
4805 int rc;
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03004806 unsigned long useful_leds;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004807
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004808 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
4809
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004810 TPACPI_ACPIHANDLE_INIT(led);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004811
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004812 if (!led_handle)
4813 /* led not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004814 led_supported = TPACPI_LED_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004815 else if (strlencmp(led_path, "SLED") == 0)
4816 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004817 led_supported = TPACPI_LED_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004818 else if (strlencmp(led_path, "SYSL") == 0)
4819 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004820 led_supported = TPACPI_LED_OLD;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004821 else
4822 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004823 led_supported = TPACPI_LED_NEW;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004824
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004825 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
4826 str_supported(led_supported), led_supported);
4827
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03004828 if (led_supported == TPACPI_LED_NONE)
4829 return 1;
4830
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004831 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
4832 GFP_KERNEL);
4833 if (!tpacpi_leds) {
4834 printk(TPACPI_ERR "Out of memory for LED data\n");
4835 return -ENOMEM;
4836 }
4837
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03004838 useful_leds = tpacpi_check_quirks(led_useful_qtable,
4839 ARRAY_SIZE(led_useful_qtable));
4840
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004841 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03004842 if (!tpacpi_is_led_restricted(i) &&
4843 test_bit(i, &useful_leds)) {
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004844 rc = tpacpi_init_led(i);
4845 if (rc < 0) {
4846 led_exit();
4847 return rc;
4848 }
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004849 }
4850 }
4851
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004852#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03004853 printk(TPACPI_NOTICE
4854 "warning: userspace override of important "
4855 "firmware LEDs is enabled\n");
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004856#endif
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03004857 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004858}
4859
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004860#define str_led_status(s) \
4861 ((s) == TPACPI_LED_OFF ? "off" : \
4862 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004863
4864static int led_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865{
4866 int len = 0;
4867
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004868 if (!led_supported) {
4869 len += sprintf(p + len, "status:\t\tnot supported\n");
4870 return len;
4871 }
4872 len += sprintf(p + len, "status:\t\tsupported\n");
4873
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004874 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004875 /* 570 */
4876 int i, status;
4877 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004878 status = led_get_status(i);
4879 if (status < 0)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004880 return -EIO;
4881 len += sprintf(p + len, "%d:\t\t%s\n",
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004882 i, str_led_status(status));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004883 }
4884 }
4885
Linus Torvalds1da177e2005-04-16 15:20:36 -07004886 len += sprintf(p + len, "commands:\t"
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03004887 "<led> on, <led> off, <led> blink (<led> is 0-15)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004888
4889 return len;
4890}
4891
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004892static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004893{
4894 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004895 int led, rc;
4896 enum led_status_t s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004897
4898 if (!led_supported)
4899 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004900
4901 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03004902 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 15)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004903 return -EINVAL;
4904
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004905 if (strstr(cmd, "off")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004906 s = TPACPI_LED_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907 } else if (strstr(cmd, "on")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004908 s = TPACPI_LED_ON;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004909 } else if (strstr(cmd, "blink")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004910 s = TPACPI_LED_BLINK;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004911 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004912 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004913 }
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004914
4915 rc = led_set_status(led, s);
4916 if (rc < 0)
4917 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004918 }
4919
4920 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004921}
4922
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004923static struct ibm_struct led_driver_data = {
4924 .name = "led",
4925 .read = led_read,
4926 .write = led_write,
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004927 .exit = led_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004928};
4929
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004930/*************************************************************************
4931 * Beep subdriver
4932 */
4933
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004934TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004935
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03004936#define TPACPI_BEEP_Q1 0x0001
4937
4938static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
4939 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
4940 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
4941};
4942
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004943static int __init beep_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004944{
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03004945 unsigned long quirks;
4946
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004947 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
4948
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004949 TPACPI_ACPIHANDLE_INIT(beep);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004950
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004951 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
4952 str_supported(beep_handle != NULL));
4953
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03004954 quirks = tpacpi_check_quirks(beep_quirk_table,
4955 ARRAY_SIZE(beep_quirk_table));
4956
4957 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
4958
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004959 return (beep_handle)? 0 : 1;
4960}
4961
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004962static int beep_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004963{
4964 int len = 0;
4965
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004966 if (!beep_handle)
4967 len += sprintf(p + len, "status:\t\tnot supported\n");
4968 else {
4969 len += sprintf(p + len, "status:\t\tsupported\n");
4970 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
4971 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004972
4973 return len;
4974}
4975
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004976static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977{
4978 char *cmd;
4979 int beep_cmd;
4980
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004981 if (!beep_handle)
4982 return -ENODEV;
4983
Linus Torvalds1da177e2005-04-16 15:20:36 -07004984 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004985 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
4986 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004987 /* beep_cmd set */
4988 } else
4989 return -EINVAL;
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03004990 if (tp_features.beep_needs_two_args) {
4991 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
4992 beep_cmd, 0))
4993 return -EIO;
4994 } else {
4995 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
4996 beep_cmd))
4997 return -EIO;
4998 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004999 }
5000
5001 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005002}
5003
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005004static struct ibm_struct beep_driver_data = {
5005 .name = "beep",
5006 .read = beep_read,
5007 .write = beep_write,
5008};
5009
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005010/*************************************************************************
5011 * Thermal subdriver
5012 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005013
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005014enum thermal_access_mode {
5015 TPACPI_THERMAL_NONE = 0, /* No thermal support */
5016 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
5017 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
5018 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
5019 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
5020};
5021
5022enum { /* TPACPI_THERMAL_TPEC_* */
5023 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
5024 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
5025 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
5026};
5027
5028#define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5029struct ibm_thermal_sensors_struct {
5030 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
5031};
5032
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005033static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005034
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005035/* idx is zero-based */
5036static int thermal_get_sensor(int idx, s32 *value)
5037{
5038 int t;
5039 s8 tmp;
5040 char tmpi[5];
5041
5042 t = TP_EC_THERMAL_TMP0;
5043
5044 switch (thermal_read_mode) {
5045#if TPACPI_MAX_THERMAL_SENSORS >= 16
5046 case TPACPI_THERMAL_TPEC_16:
5047 if (idx >= 8 && idx <= 15) {
5048 t = TP_EC_THERMAL_TMP8;
5049 idx -= 8;
5050 }
5051 /* fallthrough */
5052#endif
5053 case TPACPI_THERMAL_TPEC_8:
5054 if (idx <= 7) {
5055 if (!acpi_ec_read(t + idx, &tmp))
5056 return -EIO;
5057 *value = tmp * 1000;
5058 return 0;
5059 }
5060 break;
5061
5062 case TPACPI_THERMAL_ACPI_UPDT:
5063 if (idx <= 7) {
5064 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5065 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
5066 return -EIO;
5067 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5068 return -EIO;
5069 *value = (t - 2732) * 100;
5070 return 0;
5071 }
5072 break;
5073
5074 case TPACPI_THERMAL_ACPI_TMP07:
5075 if (idx <= 7) {
5076 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5077 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5078 return -EIO;
5079 if (t > 127 || t < -127)
5080 t = TP_EC_THERMAL_TMP_NA;
5081 *value = t * 1000;
5082 return 0;
5083 }
5084 break;
5085
5086 case TPACPI_THERMAL_NONE:
5087 default:
5088 return -ENOSYS;
5089 }
5090
5091 return -EINVAL;
5092}
5093
5094static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5095{
5096 int res, i;
5097 int n;
5098
5099 n = 8;
5100 i = 0;
5101
5102 if (!s)
5103 return -EINVAL;
5104
5105 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5106 n = 16;
5107
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005108 for (i = 0 ; i < n; i++) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005109 res = thermal_get_sensor(i, &s->temp[i]);
5110 if (res)
5111 return res;
5112 }
5113
5114 return n;
5115}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005116
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005117/* sysfs temp##_input -------------------------------------------------- */
5118
5119static ssize_t thermal_temp_input_show(struct device *dev,
5120 struct device_attribute *attr,
5121 char *buf)
5122{
5123 struct sensor_device_attribute *sensor_attr =
5124 to_sensor_dev_attr(attr);
5125 int idx = sensor_attr->index;
5126 s32 value;
5127 int res;
5128
5129 res = thermal_get_sensor(idx, &value);
5130 if (res)
5131 return res;
5132 if (value == TP_EC_THERMAL_TMP_NA * 1000)
5133 return -ENXIO;
5134
5135 return snprintf(buf, PAGE_SIZE, "%d\n", value);
5136}
5137
5138#define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005139 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5140 thermal_temp_input_show, NULL, _idxB)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005141
5142static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5143 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5144 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5145 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5146 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5147 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5148 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5149 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5150 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5151 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5152 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5153 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5154 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5155 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5156 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5157 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5158 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5159};
5160
5161#define THERMAL_ATTRS(X) \
5162 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5163
5164static struct attribute *thermal_temp_input_attr[] = {
5165 THERMAL_ATTRS(8),
5166 THERMAL_ATTRS(9),
5167 THERMAL_ATTRS(10),
5168 THERMAL_ATTRS(11),
5169 THERMAL_ATTRS(12),
5170 THERMAL_ATTRS(13),
5171 THERMAL_ATTRS(14),
5172 THERMAL_ATTRS(15),
5173 THERMAL_ATTRS(0),
5174 THERMAL_ATTRS(1),
5175 THERMAL_ATTRS(2),
5176 THERMAL_ATTRS(3),
5177 THERMAL_ATTRS(4),
5178 THERMAL_ATTRS(5),
5179 THERMAL_ATTRS(6),
5180 THERMAL_ATTRS(7),
5181 NULL
5182};
5183
5184static const struct attribute_group thermal_temp_input16_group = {
5185 .attrs = thermal_temp_input_attr
5186};
5187
5188static const struct attribute_group thermal_temp_input8_group = {
5189 .attrs = &thermal_temp_input_attr[8]
5190};
5191
5192#undef THERMAL_SENSOR_ATTR_TEMP
5193#undef THERMAL_ATTRS
5194
5195/* --------------------------------------------------------------------- */
5196
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005197static int __init thermal_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005198{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005199 u8 t, ta1, ta2;
5200 int i;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005201 int acpi_tmp7;
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005202 int res;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005203
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005204 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5205
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005206 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005207
Henrique de Moraes Holschuh3d6f99c2007-07-18 23:45:46 -03005208 if (thinkpad_id.ec_model) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005209 /*
5210 * Direct EC access mode: sensors at registers
5211 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5212 * non-implemented, thermal sensors return 0x80 when
5213 * not available
5214 */
5215
5216 ta1 = ta2 = 0;
5217 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03005218 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005219 ta1 |= t;
5220 } else {
5221 ta1 = 0;
5222 break;
5223 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03005224 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005225 ta2 |= t;
5226 } else {
5227 ta1 = 0;
5228 break;
5229 }
5230 }
5231 if (ta1 == 0) {
5232 /* This is sheer paranoia, but we handle it anyway */
5233 if (acpi_tmp7) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005234 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005235 "ThinkPad ACPI EC access misbehaving, "
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005236 "falling back to ACPI TMPx access "
5237 "mode\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005238 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005239 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005240 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005241 "ThinkPad ACPI EC access misbehaving, "
5242 "disabling thermal sensors access\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005243 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005244 }
5245 } else {
5246 thermal_read_mode =
5247 (ta2 != 0) ?
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005248 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005249 }
5250 } else if (acpi_tmp7) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005251 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
5252 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005253 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005254 } else {
5255 /* Standard ACPI TMPx access, max 8 sensors */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005256 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005257 }
5258 } else {
5259 /* temperatures not supported on 570, G4x, R30, R31, R32 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005260 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005261 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005262
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005263 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5264 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5265 thermal_read_mode);
5266
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005267 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005268 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005269 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005270 &thermal_temp_input16_group);
5271 if (res)
5272 return res;
5273 break;
5274 case TPACPI_THERMAL_TPEC_8:
5275 case TPACPI_THERMAL_ACPI_TMP07:
5276 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005277 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005278 &thermal_temp_input8_group);
5279 if (res)
5280 return res;
5281 break;
5282 case TPACPI_THERMAL_NONE:
5283 default:
5284 return 1;
5285 }
5286
5287 return 0;
5288}
5289
5290static void thermal_exit(void)
5291{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005292 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005293 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005294 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005295 &thermal_temp_input16_group);
5296 break;
5297 case TPACPI_THERMAL_TPEC_8:
5298 case TPACPI_THERMAL_ACPI_TMP07:
5299 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005300 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005301 &thermal_temp_input16_group);
5302 break;
5303 case TPACPI_THERMAL_NONE:
5304 default:
5305 break;
5306 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005307}
5308
5309static int thermal_read(char *p)
5310{
5311 int len = 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005312 int n, i;
5313 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005314
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005315 n = thermal_get_sensors(&t);
5316 if (unlikely(n < 0))
5317 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005318
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005319 len += sprintf(p + len, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005320
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005321 if (n > 0) {
5322 for (i = 0; i < (n - 1); i++)
5323 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
5324 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
5325 } else
5326 len += sprintf(p + len, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005327
5328 return len;
5329}
5330
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005331static struct ibm_struct thermal_driver_data = {
5332 .name = "thermal",
5333 .read = thermal_read,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005334 .exit = thermal_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005335};
5336
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005337/*************************************************************************
5338 * EC Dump subdriver
5339 */
5340
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005341static u8 ecdump_regs[256];
5342
5343static int ecdump_read(char *p)
5344{
5345 int len = 0;
5346 int i, j;
5347 u8 v;
5348
5349 len += sprintf(p + len, "EC "
5350 " +00 +01 +02 +03 +04 +05 +06 +07"
5351 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
5352 for (i = 0; i < 256; i += 16) {
5353 len += sprintf(p + len, "EC 0x%02x:", i);
5354 for (j = 0; j < 16; j++) {
5355 if (!acpi_ec_read(i + j, &v))
5356 break;
5357 if (v != ecdump_regs[i + j])
5358 len += sprintf(p + len, " *%02x", v);
5359 else
5360 len += sprintf(p + len, " %02x", v);
5361 ecdump_regs[i + j] = v;
5362 }
5363 len += sprintf(p + len, "\n");
5364 if (j != 16)
5365 break;
5366 }
5367
5368 /* These are way too dangerous to advertise openly... */
5369#if 0
5370 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
5371 " (<offset> is 00-ff, <value> is 00-ff)\n");
5372 len += sprintf(p + len, "commands:\t0x<offset> <value> "
5373 " (<offset> is 00-ff, <value> is 0-255)\n");
5374#endif
5375 return len;
5376}
5377
5378static int ecdump_write(char *buf)
5379{
5380 char *cmd;
5381 int i, v;
5382
5383 while ((cmd = next_cmd(&buf))) {
5384 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
5385 /* i and v set */
5386 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
5387 /* i and v set */
5388 } else
5389 return -EINVAL;
5390 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
5391 if (!acpi_ec_write(i, v))
5392 return -EIO;
5393 } else
5394 return -EINVAL;
5395 }
5396
5397 return 0;
5398}
5399
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005400static struct ibm_struct ecdump_driver_data = {
5401 .name = "ecdump",
5402 .read = ecdump_read,
5403 .write = ecdump_write,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03005404 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005405};
5406
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005407/*************************************************************************
5408 * Backlight/brightness subdriver
5409 */
Holger Macht8acb0252006-10-20 14:30:28 -07005410
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005411#define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5412
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005413/*
5414 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
5415 * CMOS NVRAM byte 0x5E, bits 0-3.
5416 *
5417 * EC HBRV (0x31) has the following layout
5418 * Bit 7: unknown function
5419 * Bit 6: unknown function
5420 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5421 * Bit 4: must be set to zero to avoid problems
5422 * Bit 3-0: backlight brightness level
5423 *
5424 * brightness_get_raw returns status data in the HBRV layout
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03005425 *
5426 * WARNING: The X61 has been verified to use HBRV for something else, so
5427 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
5428 * testing on the very early *60 Lenovo models...
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005429 */
5430
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005431enum {
5432 TP_EC_BACKLIGHT = 0x31,
5433
5434 /* TP_EC_BACKLIGHT bitmasks */
5435 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5436 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5437 TP_EC_BACKLIGHT_MAPSW = 0x20,
5438};
5439
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005440enum tpacpi_brightness_access_mode {
5441 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
5442 TPACPI_BRGHT_MODE_EC, /* EC control */
5443 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
5444 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
5445 TPACPI_BRGHT_MODE_MAX
5446};
5447
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -03005448static struct backlight_device *ibm_backlight_device;
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005449
5450static enum tpacpi_brightness_access_mode brightness_mode =
5451 TPACPI_BRGHT_MODE_MAX;
5452
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005453static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5454
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005455static struct mutex brightness_mutex;
5456
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005457/* NVRAM brightness access,
5458 * call with brightness_mutex held! */
5459static unsigned int tpacpi_brightness_nvram_get(void)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005460{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005461 u8 lnvram;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005462
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005463 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5464 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5465 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
5466 lnvram &= (tp_features.bright_16levels) ? 0x0f : 0x07;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005467
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005468 return lnvram;
5469}
5470
5471static void tpacpi_brightness_checkpoint_nvram(void)
5472{
5473 u8 lec = 0;
5474 u8 b_nvram;
5475
5476 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
5477 return;
5478
5479 vdbg_printk(TPACPI_DBG_BRGHT,
5480 "trying to checkpoint backlight level to NVRAM...\n");
5481
5482 if (mutex_lock_killable(&brightness_mutex) < 0)
5483 return;
5484
5485 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5486 goto unlock;
5487 lec &= TP_EC_BACKLIGHT_LVLMSK;
5488 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
5489
5490 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5491 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
5492 /* NVRAM needs update */
5493 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
5494 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
5495 b_nvram |= lec;
5496 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
5497 dbg_printk(TPACPI_DBG_BRGHT,
5498 "updated NVRAM backlight level to %u (0x%02x)\n",
5499 (unsigned int) lec, (unsigned int) b_nvram);
5500 } else
5501 vdbg_printk(TPACPI_DBG_BRGHT,
5502 "NVRAM backlight level already is %u (0x%02x)\n",
5503 (unsigned int) lec, (unsigned int) b_nvram);
5504
5505unlock:
5506 mutex_unlock(&brightness_mutex);
5507}
5508
5509
5510/* call with brightness_mutex held! */
5511static int tpacpi_brightness_get_raw(int *status)
5512{
5513 u8 lec = 0;
5514
5515 switch (brightness_mode) {
5516 case TPACPI_BRGHT_MODE_UCMS_STEP:
5517 *status = tpacpi_brightness_nvram_get();
5518 return 0;
5519 case TPACPI_BRGHT_MODE_EC:
5520 case TPACPI_BRGHT_MODE_ECNVRAM:
5521 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
Henrique de Moraes Holschuh2d5e94d2008-04-26 01:02:20 -03005522 return -EIO;
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005523 *status = lec;
5524 return 0;
5525 default:
5526 return -ENXIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005527 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005528}
5529
5530/* call with brightness_mutex held! */
5531/* do NOT call with illegal backlight level value */
5532static int tpacpi_brightness_set_ec(unsigned int value)
5533{
5534 u8 lec = 0;
5535
5536 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5537 return -EIO;
5538
5539 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
5540 (lec & TP_EC_BACKLIGHT_CMDMSK) |
5541 (value & TP_EC_BACKLIGHT_LVLMSK))))
5542 return -EIO;
5543
5544 return 0;
5545}
5546
5547/* call with brightness_mutex held! */
5548static int tpacpi_brightness_set_ucmsstep(unsigned int value)
5549{
5550 int cmos_cmd, inc;
5551 unsigned int current_value, i;
5552
5553 current_value = tpacpi_brightness_nvram_get();
5554
5555 if (value == current_value)
5556 return 0;
5557
5558 cmos_cmd = (value > current_value) ?
5559 TP_CMOS_BRIGHTNESS_UP :
5560 TP_CMOS_BRIGHTNESS_DOWN;
5561 inc = (value > current_value) ? 1 : -1;
5562
5563 for (i = current_value; i != value; i += inc)
5564 if (issue_thinkpad_cmos_command(cmos_cmd))
5565 return -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005566
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005567 return 0;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005568}
5569
5570/* May return EINTR which can always be mapped to ERESTARTSYS */
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005571static int brightness_set(unsigned int value)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005572{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005573 int res;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005574
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005575 if (value > ((tp_features.bright_16levels)? 15 : 7) ||
5576 value < 0)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005577 return -EINVAL;
5578
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005579 vdbg_printk(TPACPI_DBG_BRGHT,
5580 "set backlight level to %d\n", value);
5581
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02005582 res = mutex_lock_killable(&brightness_mutex);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005583 if (res < 0)
5584 return res;
5585
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005586 switch (brightness_mode) {
5587 case TPACPI_BRGHT_MODE_EC:
5588 case TPACPI_BRGHT_MODE_ECNVRAM:
5589 res = tpacpi_brightness_set_ec(value);
5590 break;
5591 case TPACPI_BRGHT_MODE_UCMS_STEP:
5592 res = tpacpi_brightness_set_ucmsstep(value);
5593 break;
5594 default:
5595 res = -ENXIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005596 }
5597
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005598 mutex_unlock(&brightness_mutex);
5599 return res;
5600}
5601
5602/* sysfs backlight class ----------------------------------------------- */
5603
5604static int brightness_update_status(struct backlight_device *bd)
5605{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005606 unsigned int level =
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005607 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
5608 bd->props.power == FB_BLANK_UNBLANK) ?
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005609 bd->props.brightness : 0;
5610
5611 dbg_printk(TPACPI_DBG_BRGHT,
5612 "backlight: attempt to set level to %d\n",
5613 level);
5614
5615 /* it is the backlight class's job (caller) to handle
5616 * EINTR and other errors properly */
5617 return brightness_set(level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005618}
Holger Macht8acb0252006-10-20 14:30:28 -07005619
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005620static int brightness_get(struct backlight_device *bd)
5621{
5622 int status, res;
5623
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005624 res = mutex_lock_killable(&brightness_mutex);
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005625 if (res < 0)
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005626 return 0;
5627
5628 res = tpacpi_brightness_get_raw(&status);
5629
5630 mutex_unlock(&brightness_mutex);
5631
5632 if (res < 0)
5633 return 0;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005634
5635 return status & TP_EC_BACKLIGHT_LVLMSK;
5636}
5637
Richard Purdie599a52d2007-02-10 23:07:48 +00005638static struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005639 .get_brightness = brightness_get,
5640 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005641};
5642
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005643/* --------------------------------------------------------------------- */
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03005644
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03005645/*
5646 * These are only useful for models that have only one possibility
5647 * of GPU. If the BIOS model handles both ATI and Intel, don't use
5648 * these quirks.
5649 */
5650#define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
5651#define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
5652#define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
5653
5654static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
5655 /* Models with ATI GPUs known to require ECNVRAM mode */
5656 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
5657
5658 /* Models with ATI GPUs (waiting confirmation) */
5659 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
5660 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
5661 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
5662 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
5663
5664 /* Models with Intel Extreme Graphics 2 (waiting confirmation) */
5665 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_NOEC),
5666 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_NOEC),
5667 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_NOEC),
5668
5669 /* Models with Intel GMA900 */
5670 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
5671 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
5672 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
5673};
5674
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005675static int __init brightness_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005676{
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02005677 int b;
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03005678 unsigned long quirks;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02005679
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005680 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
5681
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03005682 mutex_init(&brightness_mutex);
5683
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03005684 quirks = tpacpi_check_quirks(brightness_quirk_table,
5685 ARRAY_SIZE(brightness_quirk_table));
5686
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03005687 /*
5688 * We always attempt to detect acpi support, so as to switch
5689 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
5690 * going to publish a backlight interface
5691 */
5692 b = tpacpi_check_std_acpi_brightness_support();
5693 if (b > 0) {
Thomas Renninger2dba1b52008-08-01 17:38:03 +02005694
5695 if (acpi_video_backlight_support()) {
5696 if (brightness_enable > 1) {
5697 printk(TPACPI_NOTICE
5698 "Standard ACPI backlight interface "
5699 "available, not loading native one.\n");
5700 return 1;
5701 } else if (brightness_enable == 1) {
5702 printk(TPACPI_NOTICE
5703 "Backlight control force enabled, even if standard "
5704 "ACPI backlight interface is available\n");
5705 }
5706 } else {
5707 if (brightness_enable > 1) {
5708 printk(TPACPI_NOTICE
5709 "Standard ACPI backlight interface not "
5710 "available, thinkpad_acpi native "
5711 "brightness control enabled\n");
5712 }
Henrique de Moraes Holschuhe11e2112007-10-30 17:46:22 -02005713 }
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02005714 }
5715
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03005716 if (!brightness_enable) {
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005717 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03005718 "brightness support disabled by "
5719 "module parameter\n");
5720 return 1;
5721 }
5722
5723 if (b > 16) {
5724 printk(TPACPI_ERR
5725 "Unsupported brightness interface, "
5726 "please contact %s\n", TPACPI_MAIL);
5727 return 1;
5728 }
5729 if (b == 16)
5730 tp_features.bright_16levels = 1;
5731
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005732 /*
5733 * Check for module parameter bogosity, note that we
5734 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
5735 * able to detect "unspecified"
5736 */
5737 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03005738 return -EINVAL;
5739
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005740 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
5741 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
5742 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03005743 if (quirks & TPACPI_BRGHT_Q_EC)
5744 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
5745 else
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005746 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
5747
5748 dbg_printk(TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03005749 "driver auto-selected brightness_mode=%d\n",
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005750 brightness_mode);
5751 }
5752
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03005753 /* Safety */
5754 if (thinkpad_id.vendor != PCI_VENDOR_ID_IBM &&
5755 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
5756 brightness_mode == TPACPI_BRGHT_MODE_EC))
5757 return -EINVAL;
5758
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005759 if (tpacpi_brightness_get_raw(&b) < 0)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03005760 return 1;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02005761
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02005762 if (tp_features.bright_16levels)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005763 printk(TPACPI_INFO
5764 "detected a 16-level brightness capable ThinkPad\n");
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02005765
Henrique de Moraes Holschuh7d5a0152007-04-24 11:48:20 -03005766 ibm_backlight_device = backlight_device_register(
5767 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
5768 &ibm_backlight_data);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005769 if (IS_ERR(ibm_backlight_device)) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005770 printk(TPACPI_ERR "Could not register backlight device\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005771 return PTR_ERR(ibm_backlight_device);
5772 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005773 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
5774 "brightness is supported\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005775
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03005776 if (quirks & TPACPI_BRGHT_Q_ASK) {
5777 printk(TPACPI_NOTICE
5778 "brightness: will use unverified default: "
5779 "brightness_mode=%d\n", brightness_mode);
5780 printk(TPACPI_NOTICE
5781 "brightness: please report to %s whether it works well "
5782 "or not on your ThinkPad\n", TPACPI_MAIL);
5783 }
5784
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02005785 ibm_backlight_device->props.max_brightness =
5786 (tp_features.bright_16levels)? 15 : 7;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005787 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02005788 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00005789
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005790 return 0;
5791}
5792
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005793static void brightness_suspend(pm_message_t state)
5794{
5795 tpacpi_brightness_checkpoint_nvram();
5796}
5797
5798static void brightness_shutdown(void)
5799{
5800 tpacpi_brightness_checkpoint_nvram();
5801}
5802
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005803static void brightness_exit(void)
5804{
5805 if (ibm_backlight_device) {
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005806 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005807 "calling backlight_device_unregister()\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005808 backlight_device_unregister(ibm_backlight_device);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005809 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005810
5811 tpacpi_brightness_checkpoint_nvram();
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005812}
5813
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005814static int brightness_read(char *p)
5815{
5816 int len = 0;
5817 int level;
5818
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005819 level = brightness_get(NULL);
5820 if (level < 0) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005821 len += sprintf(p + len, "level:\t\tunreadable\n");
5822 } else {
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02005823 len += sprintf(p + len, "level:\t\t%d\n", level);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005824 len += sprintf(p + len, "commands:\tup, down\n");
5825 len += sprintf(p + len, "commands:\tlevel <level>"
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02005826 " (<level> is 0-%d)\n",
5827 (tp_features.bright_16levels) ? 15 : 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005828 }
5829
5830 return len;
5831}
5832
5833static int brightness_write(char *buf)
5834{
5835 int level;
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02005836 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005837 char *cmd;
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02005838 int max_level = (tp_features.bright_16levels) ? 15 : 7;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005839
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02005840 level = brightness_get(NULL);
5841 if (level < 0)
5842 return level;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005843
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02005844 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005845 if (strlencmp(cmd, "up") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02005846 if (level < max_level)
5847 level++;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005848 } else if (strlencmp(cmd, "down") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02005849 if (level > 0)
5850 level--;
5851 } else if (sscanf(cmd, "level %d", &level) == 1 &&
5852 level >= 0 && level <= max_level) {
5853 /* new level set */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005854 } else
5855 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005856 }
5857
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005858 tpacpi_disclose_usertask("procfs brightness",
5859 "set level to %d\n", level);
5860
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02005861 /*
5862 * Now we know what the final level should be, so we try to set it.
5863 * Doing it this way makes the syscall restartable in case of EINTR
5864 */
5865 rc = brightness_set(level);
5866 return (rc == -EINTR)? ERESTARTSYS : rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005867}
5868
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005869static struct ibm_struct brightness_driver_data = {
5870 .name = "brightness",
5871 .read = brightness_read,
5872 .write = brightness_write,
5873 .exit = brightness_exit,
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005874 .suspend = brightness_suspend,
5875 .shutdown = brightness_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005876};
5877
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005878/*************************************************************************
5879 * Volume subdriver
5880 */
5881
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005882static int volume_offset = 0x30;
5883
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005884static int volume_read(char *p)
5885{
5886 int len = 0;
5887 u8 level;
5888
5889 if (!acpi_ec_read(volume_offset, &level)) {
5890 len += sprintf(p + len, "level:\t\tunreadable\n");
5891 } else {
5892 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
5893 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
5894 len += sprintf(p + len, "commands:\tup, down, mute\n");
5895 len += sprintf(p + len, "commands:\tlevel <level>"
5896 " (<level> is 0-15)\n");
5897 }
5898
5899 return len;
5900}
5901
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005902static int volume_write(char *buf)
5903{
5904 int cmos_cmd, inc, i;
5905 u8 level, mute;
5906 int new_level, new_mute;
5907 char *cmd;
5908
5909 while ((cmd = next_cmd(&buf))) {
5910 if (!acpi_ec_read(volume_offset, &level))
5911 return -EIO;
5912 new_mute = mute = level & 0x40;
5913 new_level = level = level & 0xf;
5914
5915 if (strlencmp(cmd, "up") == 0) {
5916 if (mute)
5917 new_mute = 0;
5918 else
5919 new_level = level == 15 ? 15 : level + 1;
5920 } else if (strlencmp(cmd, "down") == 0) {
5921 if (mute)
5922 new_mute = 0;
5923 else
5924 new_level = level == 0 ? 0 : level - 1;
5925 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
5926 new_level >= 0 && new_level <= 15) {
5927 /* new_level set */
5928 } else if (strlencmp(cmd, "mute") == 0) {
5929 new_mute = 0x40;
5930 } else
5931 return -EINVAL;
5932
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005933 if (new_level != level) {
5934 /* mute doesn't change */
5935
5936 cmos_cmd = (new_level > level) ?
5937 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005938 inc = new_level > level ? 1 : -1;
5939
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03005940 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005941 !acpi_ec_write(volume_offset, level)))
5942 return -EIO;
5943
5944 for (i = level; i != new_level; i += inc)
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03005945 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005946 !acpi_ec_write(volume_offset, i + inc))
5947 return -EIO;
5948
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005949 if (mute &&
5950 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
5951 !acpi_ec_write(volume_offset, new_level + mute))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005952 return -EIO;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005953 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005954 }
5955
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005956 if (new_mute != mute) {
5957 /* level doesn't change */
5958
5959 cmos_cmd = (new_mute) ?
5960 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005961
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03005962 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005963 !acpi_ec_write(volume_offset, level + new_mute))
5964 return -EIO;
5965 }
5966 }
5967
5968 return 0;
5969}
5970
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005971static struct ibm_struct volume_driver_data = {
5972 .name = "volume",
5973 .read = volume_read,
5974 .write = volume_write,
5975};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005976
5977/*************************************************************************
5978 * Fan subdriver
5979 */
5980
5981/*
5982 * FAN ACCESS MODES
5983 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005984 * TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005985 * ACPI GFAN method: returns fan level
5986 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005987 * see TPACPI_FAN_WR_ACPI_SFAN
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03005988 * EC 0x2f (HFSP) not available if GFAN exists
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005989 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005990 * TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005991 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
5992 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03005993 * EC 0x2f (HFSP) might be available *for reading*, but do not use
5994 * it for writing.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005995 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005996 * TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03005997 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
5998 * Supported on almost all ThinkPads
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005999 *
6000 * Fan speed changes of any sort (including those caused by the
6001 * disengaged mode) are usually done slowly by the firmware as the
6002 * maximum ammount of fan duty cycle change per second seems to be
6003 * limited.
6004 *
6005 * Reading is not available if GFAN exists.
6006 * Writing is not available if SFAN exists.
6007 *
6008 * Bits
6009 * 7 automatic mode engaged;
6010 * (default operation mode of the ThinkPad)
6011 * fan level is ignored in this mode.
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03006012 * 6 full speed mode (takes precedence over bit 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006013 * not available on all thinkpads. May disable
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03006014 * the tachometer while the fan controller ramps up
6015 * the speed (which can take up to a few *minutes*).
6016 * Speeds up fan to 100% duty-cycle, which is far above
6017 * the standard RPM levels. It is not impossible that
6018 * it could cause hardware damage.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006019 * 5-3 unused in some models. Extra bits for fan level
6020 * in others, but still useless as all values above
6021 * 7 map to the same speed as level 7 in these models.
6022 * 2-0 fan level (0..7 usually)
6023 * 0x00 = stop
6024 * 0x07 = max (set when temperatures critical)
6025 * Some ThinkPads may have other levels, see
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006026 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006027 *
6028 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
6029 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
6030 * does so, its initial value is meaningless (0x07).
6031 *
6032 * For firmware bugs, refer to:
6033 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
6034 *
6035 * ----
6036 *
6037 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
6038 * Main fan tachometer reading (in RPM)
6039 *
6040 * This register is present on all ThinkPads with a new-style EC, and
6041 * it is known not to be present on the A21m/e, and T22, as there is
6042 * something else in offset 0x84 according to the ACPI DSDT. Other
6043 * ThinkPads from this same time period (and earlier) probably lack the
6044 * tachometer as well.
6045 *
Nick Andrew877d0312009-01-26 11:06:57 +01006046 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006047 * was never fixed by IBM to report the EC firmware version string
6048 * probably support the tachometer (like the early X models), so
6049 * detecting it is quite hard. We need more data to know for sure.
6050 *
6051 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
6052 * might result.
6053 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03006054 * FIRMWARE BUG: may go stale while the EC is switching to full speed
6055 * mode.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006056 *
6057 * For firmware bugs, refer to:
6058 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
6059 *
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03006060 * ----
6061 *
6062 * ThinkPad EC register 0x31 bit 0 (only on select models)
6063 *
6064 * When bit 0 of EC register 0x31 is zero, the tachometer registers
6065 * show the speed of the main fan. When bit 0 of EC register 0x31
6066 * is one, the tachometer registers show the speed of the auxiliary
6067 * fan.
6068 *
6069 * Fan control seems to affect both fans, regardless of the state
6070 * of this bit.
6071 *
6072 * So far, only the firmware for the X60/X61 non-tablet versions
6073 * seem to support this (firmware TP-7M).
6074 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006075 * TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006076 * ThinkPad X31, X40, X41. Not available in the X60.
6077 *
6078 * FANS ACPI handle: takes three arguments: low speed, medium speed,
6079 * high speed. ACPI DSDT seems to map these three speeds to levels
6080 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
6081 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
6082 *
6083 * The speeds are stored on handles
6084 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
6085 *
6086 * There are three default speed sets, acessible as handles:
6087 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
6088 *
6089 * ACPI DSDT switches which set is in use depending on various
6090 * factors.
6091 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006092 * TPACPI_FAN_WR_TPEC is also available and should be used to
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006093 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
6094 * but the ACPI tables just mention level 7.
6095 */
6096
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006097enum { /* Fan control constants */
6098 fan_status_offset = 0x2f, /* EC register 0x2f */
6099 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
6100 * 0x84 must be read before 0x85 */
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03006101 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
6102 bit 0 selects which fan is active */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006103
6104 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
6105 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
6106
6107 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
6108};
6109
6110enum fan_status_access_mode {
6111 TPACPI_FAN_NONE = 0, /* No fan status or control */
6112 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
6113 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
6114};
6115
6116enum fan_control_access_mode {
6117 TPACPI_FAN_WR_NONE = 0, /* No fan control */
6118 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
6119 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
6120 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
6121};
6122
6123enum fan_control_commands {
6124 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
6125 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
6126 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
6127 * and also watchdog cmd */
6128};
6129
6130static int fan_control_allowed;
6131
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006132static enum fan_status_access_mode fan_status_access_mode;
6133static enum fan_control_access_mode fan_control_access_mode;
6134static enum fan_control_commands fan_control_commands;
6135
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02006136static u8 fan_control_initial_status;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006137static u8 fan_control_desired_level;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006138static u8 fan_control_resume_level;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02006139static int fan_watchdog_maxinterval;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006140
6141static struct mutex fan_mutex;
6142
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006143static void fan_watchdog_fire(struct work_struct *ignored);
Len Brown25c68a32006-12-08 04:43:41 -05006144static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02006145
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006146TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
6147TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006148 "\\FSPD", /* 600e/x, 770e, 770x */
6149 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006150TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006151 "JFNS", /* 770x-JL */
6152 ); /* all others */
6153
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006154/*
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006155 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
6156 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
6157 * be in auto mode (0x80).
6158 *
6159 * This is corrected by any write to HFSP either by the driver, or
6160 * by the firmware.
6161 *
6162 * We assume 0x07 really means auto mode while this quirk is active,
6163 * as this is far more likely than the ThinkPad being in level 7,
6164 * which is only used by the firmware during thermal emergencies.
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03006165 *
6166 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
6167 * TP-70 (T43, R52), which are known to be buggy.
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006168 */
6169
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03006170static void fan_quirk1_setup(void)
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006171{
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006172 if (fan_control_initial_status == 0x07) {
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03006173 printk(TPACPI_NOTICE
6174 "fan_init: initial fan status is unknown, "
6175 "assuming it is in auto mode\n");
6176 tp_features.fan_ctrl_status_undef = 1;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006177 }
6178}
6179
6180static void fan_quirk1_handle(u8 *fan_status)
6181{
6182 if (unlikely(tp_features.fan_ctrl_status_undef)) {
6183 if (*fan_status != fan_control_initial_status) {
6184 /* something changed the HFSP regisnter since
6185 * driver init time, so it is not undefined
6186 * anymore */
6187 tp_features.fan_ctrl_status_undef = 0;
6188 } else {
6189 /* Return most likely status. In fact, it
6190 * might be the only possible status */
6191 *fan_status = TP_EC_FAN_AUTO;
6192 }
6193 }
6194}
6195
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03006196/* Select main fan on X60/X61, NOOP on others */
6197static bool fan_select_fan1(void)
6198{
6199 if (tp_features.second_fan) {
6200 u8 val;
6201
6202 if (ec_read(fan_select_offset, &val) < 0)
6203 return false;
6204 val &= 0xFEU;
6205 if (ec_write(fan_select_offset, val) < 0)
6206 return false;
6207 }
6208 return true;
6209}
6210
6211/* Select secondary fan on X60/X61 */
6212static bool fan_select_fan2(void)
6213{
6214 u8 val;
6215
6216 if (!tp_features.second_fan)
6217 return false;
6218
6219 if (ec_read(fan_select_offset, &val) < 0)
6220 return false;
6221 val |= 0x01U;
6222 if (ec_write(fan_select_offset, val) < 0)
6223 return false;
6224
6225 return true;
6226}
6227
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006228/*
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006229 * Call with fan_mutex held
6230 */
6231static void fan_update_desired_level(u8 status)
6232{
6233 if ((status &
6234 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
6235 if (status > 7)
6236 fan_control_desired_level = 7;
6237 else
6238 fan_control_desired_level = status;
6239 }
6240}
6241
6242static int fan_get_status(u8 *status)
6243{
6244 u8 s;
6245
6246 /* TODO:
6247 * Add TPACPI_FAN_RD_ACPI_FANS ? */
6248
6249 switch (fan_status_access_mode) {
6250 case TPACPI_FAN_RD_ACPI_GFAN:
6251 /* 570, 600e/x, 770e, 770x */
6252
6253 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
6254 return -EIO;
6255
6256 if (likely(status))
6257 *status = s & 0x07;
6258
6259 break;
6260
6261 case TPACPI_FAN_RD_TPEC:
6262 /* all except 570, 600e/x, 770e, 770x */
6263 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
6264 return -EIO;
6265
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006266 if (likely(status)) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006267 *status = s;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006268 fan_quirk1_handle(status);
6269 }
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006270
6271 break;
6272
6273 default:
6274 return -ENXIO;
6275 }
6276
6277 return 0;
6278}
6279
6280static int fan_get_status_safe(u8 *status)
6281{
6282 int rc;
6283 u8 s;
6284
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006285 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006286 return -ERESTARTSYS;
6287 rc = fan_get_status(&s);
6288 if (!rc)
6289 fan_update_desired_level(s);
6290 mutex_unlock(&fan_mutex);
6291
6292 if (status)
6293 *status = s;
6294
6295 return rc;
6296}
6297
6298static int fan_get_speed(unsigned int *speed)
6299{
6300 u8 hi, lo;
6301
6302 switch (fan_status_access_mode) {
6303 case TPACPI_FAN_RD_TPEC:
6304 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03006305 if (unlikely(!fan_select_fan1()))
6306 return -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006307 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
6308 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
6309 return -EIO;
6310
6311 if (likely(speed))
6312 *speed = (hi << 8) | lo;
6313
6314 break;
6315
6316 default:
6317 return -ENXIO;
6318 }
6319
6320 return 0;
6321}
6322
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03006323static int fan2_get_speed(unsigned int *speed)
6324{
6325 u8 hi, lo;
6326 bool rc;
6327
6328 switch (fan_status_access_mode) {
6329 case TPACPI_FAN_RD_TPEC:
6330 /* all except 570, 600e/x, 770e, 770x */
6331 if (unlikely(!fan_select_fan2()))
6332 return -EIO;
6333 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
6334 !acpi_ec_read(fan_rpm_offset + 1, &hi);
6335 fan_select_fan1(); /* play it safe */
6336 if (rc)
6337 return -EIO;
6338
6339 if (likely(speed))
6340 *speed = (hi << 8) | lo;
6341
6342 break;
6343
6344 default:
6345 return -ENXIO;
6346 }
6347
6348 return 0;
6349}
6350
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006351static int fan_set_level(int level)
6352{
6353 if (!fan_control_allowed)
6354 return -EPERM;
6355
6356 switch (fan_control_access_mode) {
6357 case TPACPI_FAN_WR_ACPI_SFAN:
6358 if (level >= 0 && level <= 7) {
6359 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
6360 return -EIO;
6361 } else
6362 return -EINVAL;
6363 break;
6364
6365 case TPACPI_FAN_WR_ACPI_FANS:
6366 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006367 if (!(level & TP_EC_FAN_AUTO) &&
6368 !(level & TP_EC_FAN_FULLSPEED) &&
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006369 ((level < 0) || (level > 7)))
6370 return -EINVAL;
6371
6372 /* safety net should the EC not support AUTO
6373 * or FULLSPEED mode bits and just ignore them */
6374 if (level & TP_EC_FAN_FULLSPEED)
6375 level |= 7; /* safety min speed 7 */
Roel Kluin547266e2008-02-05 00:24:56 +01006376 else if (level & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006377 level |= 4; /* safety min speed 4 */
6378
6379 if (!acpi_ec_write(fan_status_offset, level))
6380 return -EIO;
6381 else
6382 tp_features.fan_ctrl_status_undef = 0;
6383 break;
6384
6385 default:
6386 return -ENXIO;
6387 }
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00006388
6389 vdbg_printk(TPACPI_DBG_FAN,
6390 "fan control: set fan control register to 0x%02x\n", level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006391 return 0;
6392}
6393
6394static int fan_set_level_safe(int level)
6395{
6396 int rc;
6397
6398 if (!fan_control_allowed)
6399 return -EPERM;
6400
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006401 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006402 return -ERESTARTSYS;
6403
6404 if (level == TPACPI_FAN_LAST_LEVEL)
6405 level = fan_control_desired_level;
6406
6407 rc = fan_set_level(level);
6408 if (!rc)
6409 fan_update_desired_level(level);
6410
6411 mutex_unlock(&fan_mutex);
6412 return rc;
6413}
6414
6415static int fan_set_enable(void)
6416{
6417 u8 s;
6418 int rc;
6419
6420 if (!fan_control_allowed)
6421 return -EPERM;
6422
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006423 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006424 return -ERESTARTSYS;
6425
6426 switch (fan_control_access_mode) {
6427 case TPACPI_FAN_WR_ACPI_FANS:
6428 case TPACPI_FAN_WR_TPEC:
6429 rc = fan_get_status(&s);
6430 if (rc < 0)
6431 break;
6432
6433 /* Don't go out of emergency fan mode */
6434 if (s != 7) {
6435 s &= 0x07;
6436 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
6437 }
6438
6439 if (!acpi_ec_write(fan_status_offset, s))
6440 rc = -EIO;
6441 else {
6442 tp_features.fan_ctrl_status_undef = 0;
6443 rc = 0;
6444 }
6445 break;
6446
6447 case TPACPI_FAN_WR_ACPI_SFAN:
6448 rc = fan_get_status(&s);
6449 if (rc < 0)
6450 break;
6451
6452 s &= 0x07;
6453
6454 /* Set fan to at least level 4 */
6455 s |= 4;
6456
6457 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006458 rc = -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006459 else
6460 rc = 0;
6461 break;
6462
6463 default:
6464 rc = -ENXIO;
6465 }
6466
6467 mutex_unlock(&fan_mutex);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00006468
6469 if (!rc)
6470 vdbg_printk(TPACPI_DBG_FAN,
6471 "fan control: set fan control register to 0x%02x\n",
6472 s);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006473 return rc;
6474}
6475
6476static int fan_set_disable(void)
6477{
6478 int rc;
6479
6480 if (!fan_control_allowed)
6481 return -EPERM;
6482
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006483 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006484 return -ERESTARTSYS;
6485
6486 rc = 0;
6487 switch (fan_control_access_mode) {
6488 case TPACPI_FAN_WR_ACPI_FANS:
6489 case TPACPI_FAN_WR_TPEC:
6490 if (!acpi_ec_write(fan_status_offset, 0x00))
6491 rc = -EIO;
6492 else {
6493 fan_control_desired_level = 0;
6494 tp_features.fan_ctrl_status_undef = 0;
6495 }
6496 break;
6497
6498 case TPACPI_FAN_WR_ACPI_SFAN:
6499 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
6500 rc = -EIO;
6501 else
6502 fan_control_desired_level = 0;
6503 break;
6504
6505 default:
6506 rc = -ENXIO;
6507 }
6508
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00006509 if (!rc)
6510 vdbg_printk(TPACPI_DBG_FAN,
6511 "fan control: set fan control register to 0\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006512
6513 mutex_unlock(&fan_mutex);
6514 return rc;
6515}
6516
6517static int fan_set_speed(int speed)
6518{
6519 int rc;
6520
6521 if (!fan_control_allowed)
6522 return -EPERM;
6523
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006524 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006525 return -ERESTARTSYS;
6526
6527 rc = 0;
6528 switch (fan_control_access_mode) {
6529 case TPACPI_FAN_WR_ACPI_FANS:
6530 if (speed >= 0 && speed <= 65535) {
6531 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
6532 speed, speed, speed))
6533 rc = -EIO;
6534 } else
6535 rc = -EINVAL;
6536 break;
6537
6538 default:
6539 rc = -ENXIO;
6540 }
6541
6542 mutex_unlock(&fan_mutex);
6543 return rc;
6544}
6545
6546static void fan_watchdog_reset(void)
6547{
6548 static int fan_watchdog_active;
6549
6550 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
6551 return;
6552
6553 if (fan_watchdog_active)
6554 cancel_delayed_work(&fan_watchdog_task);
6555
6556 if (fan_watchdog_maxinterval > 0 &&
6557 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
6558 fan_watchdog_active = 1;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03006559 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006560 msecs_to_jiffies(fan_watchdog_maxinterval
6561 * 1000))) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006562 printk(TPACPI_ERR
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03006563 "failed to queue the fan watchdog, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006564 "watchdog will not trigger\n");
6565 }
6566 } else
6567 fan_watchdog_active = 0;
6568}
6569
6570static void fan_watchdog_fire(struct work_struct *ignored)
6571{
6572 int rc;
6573
6574 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
6575 return;
6576
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006577 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006578 rc = fan_set_enable();
6579 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006580 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006581 "will try again later...\n", -rc);
6582 /* reschedule for later */
6583 fan_watchdog_reset();
6584 }
6585}
6586
6587/*
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006588 * SYSFS fan layout: hwmon compatible (device)
6589 *
6590 * pwm*_enable:
6591 * 0: "disengaged" mode
6592 * 1: manual mode
6593 * 2: native EC "auto" mode (recommended, hardware default)
6594 *
6595 * pwm*: set speed in manual mode, ignored otherwise.
6596 * 0 is level 0; 255 is level 7. Intermediate points done with linear
6597 * interpolation.
6598 *
6599 * fan*_input: tachometer reading, RPM
6600 *
6601 *
6602 * SYSFS fan layout: extensions
6603 *
6604 * fan_watchdog (driver):
6605 * fan watchdog interval in seconds, 0 disables (default), max 120
6606 */
6607
6608/* sysfs fan pwm1_enable ----------------------------------------------- */
6609static ssize_t fan_pwm1_enable_show(struct device *dev,
6610 struct device_attribute *attr,
6611 char *buf)
6612{
6613 int res, mode;
6614 u8 status;
6615
6616 res = fan_get_status_safe(&status);
6617 if (res)
6618 return res;
6619
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006620 if (status & TP_EC_FAN_FULLSPEED) {
6621 mode = 0;
6622 } else if (status & TP_EC_FAN_AUTO) {
6623 mode = 2;
6624 } else
6625 mode = 1;
6626
6627 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
6628}
6629
6630static ssize_t fan_pwm1_enable_store(struct device *dev,
6631 struct device_attribute *attr,
6632 const char *buf, size_t count)
6633{
6634 unsigned long t;
6635 int res, level;
6636
6637 if (parse_strtoul(buf, 2, &t))
6638 return -EINVAL;
6639
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00006640 tpacpi_disclose_usertask("hwmon pwm1_enable",
6641 "set fan mode to %lu\n", t);
6642
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006643 switch (t) {
6644 case 0:
6645 level = TP_EC_FAN_FULLSPEED;
6646 break;
6647 case 1:
6648 level = TPACPI_FAN_LAST_LEVEL;
6649 break;
6650 case 2:
6651 level = TP_EC_FAN_AUTO;
6652 break;
6653 case 3:
6654 /* reserved for software-controlled auto mode */
6655 return -ENOSYS;
6656 default:
6657 return -EINVAL;
6658 }
6659
6660 res = fan_set_level_safe(level);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03006661 if (res == -ENXIO)
6662 return -EINVAL;
6663 else if (res < 0)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006664 return res;
6665
6666 fan_watchdog_reset();
6667
6668 return count;
6669}
6670
6671static struct device_attribute dev_attr_fan_pwm1_enable =
6672 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
6673 fan_pwm1_enable_show, fan_pwm1_enable_store);
6674
6675/* sysfs fan pwm1 ------------------------------------------------------ */
6676static ssize_t fan_pwm1_show(struct device *dev,
6677 struct device_attribute *attr,
6678 char *buf)
6679{
6680 int res;
6681 u8 status;
6682
6683 res = fan_get_status_safe(&status);
6684 if (res)
6685 return res;
6686
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006687 if ((status &
6688 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
6689 status = fan_control_desired_level;
6690
6691 if (status > 7)
6692 status = 7;
6693
6694 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
6695}
6696
6697static ssize_t fan_pwm1_store(struct device *dev,
6698 struct device_attribute *attr,
6699 const char *buf, size_t count)
6700{
6701 unsigned long s;
6702 int rc;
6703 u8 status, newlevel;
6704
6705 if (parse_strtoul(buf, 255, &s))
6706 return -EINVAL;
6707
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00006708 tpacpi_disclose_usertask("hwmon pwm1",
6709 "set fan speed to %lu\n", s);
6710
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006711 /* scale down from 0-255 to 0-7 */
6712 newlevel = (s >> 5) & 0x07;
6713
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006714 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02006715 return -ERESTARTSYS;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006716
6717 rc = fan_get_status(&status);
6718 if (!rc && (status &
6719 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
6720 rc = fan_set_level(newlevel);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03006721 if (rc == -ENXIO)
6722 rc = -EINVAL;
6723 else if (!rc) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006724 fan_update_desired_level(newlevel);
Henrique de Moraes Holschuhca4ac2f2007-04-27 22:00:11 -03006725 fan_watchdog_reset();
6726 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006727 }
6728
6729 mutex_unlock(&fan_mutex);
6730 return (rc)? rc : count;
6731}
6732
6733static struct device_attribute dev_attr_fan_pwm1 =
6734 __ATTR(pwm1, S_IWUSR | S_IRUGO,
6735 fan_pwm1_show, fan_pwm1_store);
6736
6737/* sysfs fan fan1_input ------------------------------------------------ */
6738static ssize_t fan_fan1_input_show(struct device *dev,
6739 struct device_attribute *attr,
6740 char *buf)
6741{
6742 int res;
6743 unsigned int speed;
6744
6745 res = fan_get_speed(&speed);
6746 if (res < 0)
6747 return res;
6748
6749 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
6750}
6751
6752static struct device_attribute dev_attr_fan_fan1_input =
6753 __ATTR(fan1_input, S_IRUGO,
6754 fan_fan1_input_show, NULL);
6755
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03006756/* sysfs fan fan2_input ------------------------------------------------ */
6757static ssize_t fan_fan2_input_show(struct device *dev,
6758 struct device_attribute *attr,
6759 char *buf)
6760{
6761 int res;
6762 unsigned int speed;
6763
6764 res = fan2_get_speed(&speed);
6765 if (res < 0)
6766 return res;
6767
6768 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
6769}
6770
6771static struct device_attribute dev_attr_fan_fan2_input =
6772 __ATTR(fan2_input, S_IRUGO,
6773 fan_fan2_input_show, NULL);
6774
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006775/* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006776static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
6777 char *buf)
6778{
6779 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
6780}
6781
6782static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
6783 const char *buf, size_t count)
6784{
6785 unsigned long t;
6786
6787 if (parse_strtoul(buf, 120, &t))
6788 return -EINVAL;
6789
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03006790 if (!fan_control_allowed)
6791 return -EPERM;
6792
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006793 fan_watchdog_maxinterval = t;
6794 fan_watchdog_reset();
6795
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00006796 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
6797
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006798 return count;
6799}
6800
6801static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
6802 fan_fan_watchdog_show, fan_fan_watchdog_store);
6803
6804/* --------------------------------------------------------------------- */
6805static struct attribute *fan_attributes[] = {
6806 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
6807 &dev_attr_fan_fan1_input.attr,
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03006808 NULL, /* for fan2_input */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006809 NULL
6810};
6811
6812static const struct attribute_group fan_attr_group = {
6813 .attrs = fan_attributes,
6814};
6815
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03006816#define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
6817#define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03006818
6819#define TPACPI_FAN_QI(__id1, __id2, __quirks) \
6820 { .vendor = PCI_VENDOR_ID_IBM, \
6821 .bios = TPACPI_MATCH_ANY, \
6822 .ec = TPID(__id1, __id2), \
6823 .quirks = __quirks }
6824
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03006825#define TPACPI_FAN_QL(__id1, __id2, __quirks) \
6826 { .vendor = PCI_VENDOR_ID_LENOVO, \
6827 .bios = TPACPI_MATCH_ANY, \
6828 .ec = TPID(__id1, __id2), \
6829 .quirks = __quirks }
6830
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03006831static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
6832 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
6833 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
6834 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
6835 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03006836 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03006837};
6838
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03006839#undef TPACPI_FAN_QL
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03006840#undef TPACPI_FAN_QI
6841
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006842static int __init fan_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006843{
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006844 int rc;
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03006845 unsigned long quirks;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006846
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00006847 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
6848 "initializing fan subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006849
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03006850 mutex_init(&fan_mutex);
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006851 fan_status_access_mode = TPACPI_FAN_NONE;
6852 fan_control_access_mode = TPACPI_FAN_WR_NONE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006853 fan_control_commands = 0;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02006854 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03006855 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03006856 tp_features.second_fan = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006857 fan_control_desired_level = 7;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006858
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006859 TPACPI_ACPIHANDLE_INIT(fans);
6860 TPACPI_ACPIHANDLE_INIT(gfan);
6861 TPACPI_ACPIHANDLE_INIT(sfan);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006862
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03006863 quirks = tpacpi_check_quirks(fan_quirk_table,
6864 ARRAY_SIZE(fan_quirk_table));
6865
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006866 if (gfan_handle) {
6867 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006868 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006869 } else {
6870 /* all other ThinkPads: note that even old-style
6871 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02006872 if (likely(acpi_ec_read(fan_status_offset,
6873 &fan_control_initial_status))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006874 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03006875 if (quirks & TPACPI_FAN_Q1)
6876 fan_quirk1_setup();
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03006877 if (quirks & TPACPI_FAN_2FAN) {
6878 tp_features.second_fan = 1;
6879 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
6880 "secondary fan support enabled\n");
6881 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006882 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006883 printk(TPACPI_ERR
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006884 "ThinkPad ACPI EC access misbehaving, "
6885 "fan status and control unavailable\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006886 return 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006887 }
6888 }
6889
6890 if (sfan_handle) {
6891 /* 570, 770x-JL */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006892 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02006893 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006894 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006895 } else {
6896 if (!gfan_handle) {
6897 /* gfan without sfan means no fan control */
6898 /* all other models implement TP EC 0x2f control */
6899
6900 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02006901 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006902 fan_control_access_mode =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006903 TPACPI_FAN_WR_ACPI_FANS;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006904 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006905 TPACPI_FAN_CMD_SPEED |
6906 TPACPI_FAN_CMD_LEVEL |
6907 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006908 } else {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006909 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02006910 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006911 TPACPI_FAN_CMD_LEVEL |
6912 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006913 }
6914 }
6915 }
6916
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00006917 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
6918 "fan is %s, modes %d, %d\n",
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006919 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
6920 fan_control_access_mode != TPACPI_FAN_WR_NONE),
6921 fan_status_access_mode, fan_control_access_mode);
6922
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03006923 /* fan control master switch */
6924 if (!fan_control_allowed) {
6925 fan_control_access_mode = TPACPI_FAN_WR_NONE;
6926 fan_control_commands = 0;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00006927 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03006928 "fan control features disabled by parameter\n");
6929 }
6930
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006931 /* update fan_control_desired_level */
6932 if (fan_status_access_mode != TPACPI_FAN_NONE)
6933 fan_get_status_safe(NULL);
6934
6935 if (fan_status_access_mode != TPACPI_FAN_NONE ||
6936 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03006937 if (tp_features.second_fan) {
6938 /* attach second fan tachometer */
6939 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
6940 &dev_attr_fan_fan2_input.attr;
6941 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006942 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006943 &fan_attr_group);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006944 if (rc < 0)
6945 return rc;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03006946
6947 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
6948 &driver_attr_fan_watchdog);
6949 if (rc < 0) {
6950 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
6951 &fan_attr_group);
6952 return rc;
6953 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006954 return 0;
6955 } else
6956 return 1;
6957}
6958
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006959static void fan_exit(void)
6960{
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00006961 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006962 "cancelling any pending fan watchdog tasks\n");
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006963
6964 /* FIXME: can we really do this unconditionally? */
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006965 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006966 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
6967 &driver_attr_fan_watchdog);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006968
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006969 cancel_delayed_work(&fan_watchdog_task);
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03006970 flush_workqueue(tpacpi_wq);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006971}
6972
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006973static void fan_suspend(pm_message_t state)
6974{
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006975 int rc;
6976
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006977 if (!fan_control_allowed)
6978 return;
6979
6980 /* Store fan status in cache */
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006981 fan_control_resume_level = 0;
6982 rc = fan_get_status_safe(&fan_control_resume_level);
6983 if (rc < 0)
6984 printk(TPACPI_NOTICE
6985 "failed to read fan level for later "
6986 "restore during resume: %d\n", rc);
6987
6988 /* if it is undefined, don't attempt to restore it.
6989 * KEEP THIS LAST */
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006990 if (tp_features.fan_ctrl_status_undef)
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006991 fan_control_resume_level = 0;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006992}
6993
6994static void fan_resume(void)
6995{
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006996 u8 current_level = 7;
6997 bool do_set = false;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006998 int rc;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006999
7000 /* DSDT *always* updates status on resume */
7001 tp_features.fan_ctrl_status_undef = 0;
7002
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007003 if (!fan_control_allowed ||
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007004 !fan_control_resume_level ||
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007005 (fan_get_status_safe(&current_level) < 0))
7006 return;
7007
7008 switch (fan_control_access_mode) {
7009 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007010 /* never decrease fan level */
7011 do_set = (fan_control_resume_level > current_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007012 break;
7013 case TPACPI_FAN_WR_ACPI_FANS:
7014 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007015 /* never decrease fan level, scale is:
7016 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
7017 *
7018 * We expect the firmware to set either 7 or AUTO, but we
7019 * handle FULLSPEED out of paranoia.
7020 *
7021 * So, we can safely only restore FULLSPEED or 7, anything
7022 * else could slow the fan. Restoring AUTO is useless, at
7023 * best that's exactly what the DSDT already set (it is the
7024 * slower it uses).
7025 *
7026 * Always keep in mind that the DSDT *will* have set the
7027 * fans to what the vendor supposes is the best level. We
7028 * muck with it only to speed the fan up.
7029 */
7030 if (fan_control_resume_level != 7 &&
7031 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
7032 return;
7033 else
7034 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
7035 (current_level != fan_control_resume_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007036 break;
7037 default:
7038 return;
7039 }
7040 if (do_set) {
7041 printk(TPACPI_NOTICE
7042 "restoring fan level to 0x%02x\n",
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007043 fan_control_resume_level);
7044 rc = fan_set_level_safe(fan_control_resume_level);
7045 if (rc < 0)
7046 printk(TPACPI_NOTICE
7047 "failed to restore fan level: %d\n", rc);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007048 }
7049}
7050
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007051static int fan_read(char *p)
7052{
7053 int len = 0;
7054 int rc;
7055 u8 status;
7056 unsigned int speed = 0;
7057
7058 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007059 case TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007060 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007061 rc = fan_get_status_safe(&status);
7062 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007063 return rc;
7064
7065 len += sprintf(p + len, "status:\t\t%s\n"
7066 "level:\t\t%d\n",
7067 (status != 0) ? "enabled" : "disabled", status);
7068 break;
7069
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007070 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007071 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007072 rc = fan_get_status_safe(&status);
7073 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007074 return rc;
7075
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007076 len += sprintf(p + len, "status:\t\t%s\n",
7077 (status != 0) ? "enabled" : "disabled");
7078
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007079 rc = fan_get_speed(&speed);
7080 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007081 return rc;
7082
7083 len += sprintf(p + len, "speed:\t\t%d\n", speed);
7084
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007085 if (status & TP_EC_FAN_FULLSPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007086 /* Disengaged mode takes precedence */
7087 len += sprintf(p + len, "level:\t\tdisengaged\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007088 else if (status & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007089 len += sprintf(p + len, "level:\t\tauto\n");
7090 else
7091 len += sprintf(p + len, "level:\t\t%d\n", status);
7092 break;
7093
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007094 case TPACPI_FAN_NONE:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007095 default:
7096 len += sprintf(p + len, "status:\t\tnot supported\n");
7097 }
7098
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007099 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007100 len += sprintf(p + len, "commands:\tlevel <level>");
7101
7102 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007103 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007104 len += sprintf(p + len, " (<level> is 0-7)\n");
7105 break;
7106
7107 default:
7108 len += sprintf(p + len, " (<level> is 0-7, "
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007109 "auto, disengaged, full-speed)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007110 break;
7111 }
7112 }
7113
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007114 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007115 len += sprintf(p + len, "commands:\tenable, disable\n"
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007116 "commands:\twatchdog <timeout> (<timeout> "
7117 "is 0 (off), 1-120 (seconds))\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007118
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007119 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007120 len += sprintf(p + len, "commands:\tspeed <speed>"
7121 " (<speed> is 0-65535)\n");
7122
7123 return len;
7124}
7125
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007126static int fan_write_cmd_level(const char *cmd, int *rc)
7127{
7128 int level;
7129
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02007130 if (strlencmp(cmd, "level auto") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007131 level = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007132 else if ((strlencmp(cmd, "level disengaged") == 0) |
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007133 (strlencmp(cmd, "level full-speed") == 0))
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007134 level = TP_EC_FAN_FULLSPEED;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02007135 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007136 return 0;
7137
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007138 *rc = fan_set_level_safe(level);
7139 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007140 printk(TPACPI_ERR "level command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007141 "access mode %d", fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007142 else if (!*rc)
7143 tpacpi_disclose_usertask("procfs fan",
7144 "set level to %d\n", level);
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007145
7146 return 1;
7147}
7148
7149static int fan_write_cmd_enable(const char *cmd, int *rc)
7150{
7151 if (strlencmp(cmd, "enable") != 0)
7152 return 0;
7153
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007154 *rc = fan_set_enable();
7155 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007156 printk(TPACPI_ERR "enable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007157 "access mode %d", fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007158 else if (!*rc)
7159 tpacpi_disclose_usertask("procfs fan", "enable\n");
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007160
7161 return 1;
7162}
7163
7164static int fan_write_cmd_disable(const char *cmd, int *rc)
7165{
7166 if (strlencmp(cmd, "disable") != 0)
7167 return 0;
7168
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007169 *rc = fan_set_disable();
7170 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007171 printk(TPACPI_ERR "disable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007172 "access mode %d", fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007173 else if (!*rc)
7174 tpacpi_disclose_usertask("procfs fan", "disable\n");
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007175
7176 return 1;
7177}
7178
7179static int fan_write_cmd_speed(const char *cmd, int *rc)
7180{
7181 int speed;
7182
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02007183 /* TODO:
7184 * Support speed <low> <medium> <high> ? */
7185
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007186 if (sscanf(cmd, "speed %d", &speed) != 1)
7187 return 0;
7188
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007189 *rc = fan_set_speed(speed);
7190 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007191 printk(TPACPI_ERR "speed command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007192 "access mode %d", fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007193 else if (!*rc)
7194 tpacpi_disclose_usertask("procfs fan",
7195 "set speed to %d\n", speed);
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007196
7197 return 1;
7198}
7199
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007200static int fan_write_cmd_watchdog(const char *cmd, int *rc)
7201{
7202 int interval;
7203
7204 if (sscanf(cmd, "watchdog %d", &interval) != 1)
7205 return 0;
7206
7207 if (interval < 0 || interval > 120)
7208 *rc = -EINVAL;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007209 else {
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007210 fan_watchdog_maxinterval = interval;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007211 tpacpi_disclose_usertask("procfs fan",
7212 "set watchdog timer to %d\n",
7213 interval);
7214 }
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007215
7216 return 1;
7217}
7218
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007219static int fan_write(char *buf)
7220{
7221 char *cmd;
7222 int rc = 0;
7223
7224 while (!rc && (cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007225 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007226 fan_write_cmd_level(cmd, &rc)) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007227 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007228 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007229 fan_write_cmd_disable(cmd, &rc) ||
7230 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007231 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007232 fan_write_cmd_speed(cmd, &rc))
7233 )
7234 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007235 else if (!rc)
7236 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007237 }
7238
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007239 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007240}
7241
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007242static struct ibm_struct fan_driver_data = {
7243 .name = "fan",
7244 .read = fan_read,
7245 .write = fan_write,
7246 .exit = fan_exit,
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007247 .suspend = fan_suspend,
7248 .resume = fan_resume,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007249};
7250
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007251/****************************************************************************
7252 ****************************************************************************
7253 *
7254 * Infrastructure
7255 *
7256 ****************************************************************************
7257 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07007258
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007259/* sysfs name ---------------------------------------------------------- */
7260static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
7261 struct device_attribute *attr,
7262 char *buf)
7263{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007264 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007265}
7266
7267static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
7268 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
7269
7270/* --------------------------------------------------------------------- */
7271
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007272/* /proc support */
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -03007273static struct proc_dir_entry *proc_dir;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007274
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007275/*
7276 * Module and infrastructure proble, init and exit handling
7277 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007278
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007279static int force_load;
7280
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007281#ifdef CONFIG_THINKPAD_ACPI_DEBUG
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007282static const char * __init str_supported(int is_supported)
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007283{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007284 static char text_unsupported[] __initdata = "not supported";
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007285
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007286 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007287}
7288#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
7289
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007290static void ibm_exit(struct ibm_struct *ibm)
7291{
7292 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
7293
7294 list_del_init(&ibm->all_drivers);
7295
7296 if (ibm->flags.acpi_notify_installed) {
7297 dbg_printk(TPACPI_DBG_EXIT,
7298 "%s: acpi_remove_notify_handler\n", ibm->name);
7299 BUG_ON(!ibm->acpi);
7300 acpi_remove_notify_handler(*ibm->acpi->handle,
7301 ibm->acpi->type,
7302 dispatch_acpi_notify);
7303 ibm->flags.acpi_notify_installed = 0;
7304 ibm->flags.acpi_notify_installed = 0;
7305 }
7306
7307 if (ibm->flags.proc_created) {
7308 dbg_printk(TPACPI_DBG_EXIT,
7309 "%s: remove_proc_entry\n", ibm->name);
7310 remove_proc_entry(ibm->name, proc_dir);
7311 ibm->flags.proc_created = 0;
7312 }
7313
7314 if (ibm->flags.acpi_driver_registered) {
7315 dbg_printk(TPACPI_DBG_EXIT,
7316 "%s: acpi_bus_unregister_driver\n", ibm->name);
7317 BUG_ON(!ibm->acpi);
7318 acpi_bus_unregister_driver(ibm->acpi->driver);
7319 kfree(ibm->acpi->driver);
7320 ibm->acpi->driver = NULL;
7321 ibm->flags.acpi_driver_registered = 0;
7322 }
7323
7324 if (ibm->flags.init_called && ibm->exit) {
7325 ibm->exit();
7326 ibm->flags.init_called = 0;
7327 }
7328
7329 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
7330}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007331
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007332static int __init ibm_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007333{
7334 int ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007335 struct ibm_struct *ibm = iibm->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007336 struct proc_dir_entry *entry;
7337
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007338 BUG_ON(ibm == NULL);
7339
7340 INIT_LIST_HEAD(&ibm->all_drivers);
7341
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03007342 if (ibm->flags.experimental && !experimental)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007343 return 0;
7344
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007345 dbg_printk(TPACPI_DBG_INIT,
7346 "probing for %s\n", ibm->name);
7347
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007348 if (iibm->init) {
7349 ret = iibm->init(iibm);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007350 if (ret > 0)
7351 return 0; /* probe failed */
7352 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007353 return ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007354
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03007355 ibm->flags.init_called = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007356 }
7357
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03007358 if (ibm->acpi) {
7359 if (ibm->acpi->hid) {
7360 ret = register_tpacpi_subdriver(ibm);
7361 if (ret)
7362 goto err_out;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007363 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03007364
7365 if (ibm->acpi->notify) {
7366 ret = setup_acpi_notify(ibm);
7367 if (ret == -ENODEV) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007368 printk(TPACPI_NOTICE "disabling subdriver %s\n",
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03007369 ibm->name);
7370 ret = 0;
7371 goto err_out;
7372 }
7373 if (ret < 0)
7374 goto err_out;
7375 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007376 }
7377
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007378 dbg_printk(TPACPI_DBG_INIT,
7379 "%s installed\n", ibm->name);
7380
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007381 if (ibm->read) {
7382 entry = create_proc_entry(ibm->name,
7383 S_IFREG | S_IRUGO | S_IWUSR,
7384 proc_dir);
7385 if (!entry) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007386 printk(TPACPI_ERR "unable to create proc entry %s\n",
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007387 ibm->name);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007388 ret = -ENODEV;
7389 goto err_out;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007390 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007391 entry->data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03007392 entry->read_proc = &dispatch_procfs_read;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007393 if (ibm->write)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03007394 entry->write_proc = &dispatch_procfs_write;
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03007395 ibm->flags.proc_created = 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007396 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007397
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007398 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
7399
Linus Torvalds1da177e2005-04-16 15:20:36 -07007400 return 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007401
7402err_out:
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007403 dbg_printk(TPACPI_DBG_INIT,
7404 "%s: at error exit path with result %d\n",
7405 ibm->name, ret);
7406
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007407 ibm_exit(ibm);
7408 return (ret < 0)? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007409}
7410
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007411/* Probing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007412
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03007413static bool __pure __init tpacpi_is_fw_digit(const char c)
7414{
7415 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
7416}
7417
7418/* Most models: xxyTkkWW (#.##c); Ancient 570/600 and -SL lacks (#.##c) */
7419static bool __pure __init tpacpi_is_valid_fw_id(const char* const s,
7420 const char t)
7421{
7422 return s && strlen(s) >= 8 &&
7423 tpacpi_is_fw_digit(s[0]) &&
7424 tpacpi_is_fw_digit(s[1]) &&
7425 s[2] == t && s[3] == 'T' &&
7426 tpacpi_is_fw_digit(s[4]) &&
7427 tpacpi_is_fw_digit(s[5]) &&
7428 s[6] == 'W' && s[7] == 'W';
7429}
7430
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007431/* returns 0 - probe ok, or < 0 - probe error.
7432 * Probe ok doesn't mean thinkpad found.
7433 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
7434static int __must_check __init get_thinkpad_model_data(
7435 struct thinkpad_id_data *tp)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02007436{
Jeff Garzik18552562007-10-03 15:15:40 -04007437 const struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02007438 char ec_fw_string[18];
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007439 char const *s;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02007440
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007441 if (!tp)
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007442 return -EINVAL;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007443
7444 memset(tp, 0, sizeof(*tp));
7445
7446 if (dmi_name_in_vendors("IBM"))
7447 tp->vendor = PCI_VENDOR_ID_IBM;
7448 else if (dmi_name_in_vendors("LENOVO"))
7449 tp->vendor = PCI_VENDOR_ID_LENOVO;
7450 else
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007451 return 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007452
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007453 s = dmi_get_system_info(DMI_BIOS_VERSION);
7454 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
7455 if (s && !tp->bios_version_str)
7456 return -ENOMEM;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03007457
7458 /* Really ancient ThinkPad 240X will fail this, which is fine */
7459 if (!tpacpi_is_valid_fw_id(tp->bios_version_str, 'E'))
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007460 return 0;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03007461
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007462 tp->bios_model = tp->bios_version_str[0]
7463 | (tp->bios_version_str[1] << 8);
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03007464 tp->bios_release = (tp->bios_version_str[4] << 8)
7465 | tp->bios_version_str[5];
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007466
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02007467 /*
7468 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
7469 * X32 or newer, all Z series; Some models must have an
7470 * up-to-date BIOS or they will not be detected.
7471 *
7472 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
7473 */
7474 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02007475 if (sscanf(dev->name,
7476 "IBM ThinkPad Embedded Controller -[%17c",
7477 ec_fw_string) == 1) {
7478 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
7479 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007480
7481 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007482 if (!tp->ec_version_str)
7483 return -ENOMEM;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03007484
7485 if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) {
7486 tp->ec_model = ec_fw_string[0]
7487 | (ec_fw_string[1] << 8);
7488 tp->ec_release = (ec_fw_string[4] << 8)
7489 | ec_fw_string[5];
7490 } else {
7491 printk(TPACPI_NOTICE
7492 "ThinkPad firmware release %s "
7493 "doesn't match the known patterns\n",
7494 ec_fw_string);
7495 printk(TPACPI_NOTICE
7496 "please report this to %s\n",
7497 TPACPI_MAIL);
7498 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007499 break;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02007500 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02007501 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007502
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007503 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
7504 if (s && !strnicmp(s, "ThinkPad", 8)) {
7505 tp->model_str = kstrdup(s, GFP_KERNEL);
7506 if (!tp->model_str)
7507 return -ENOMEM;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007508 }
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03007509
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007510 s = dmi_get_system_info(DMI_PRODUCT_NAME);
7511 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
7512 if (s && !tp->nummodel_str)
7513 return -ENOMEM;
7514
7515 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007516}
7517
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007518static int __init probe_for_thinkpad(void)
7519{
7520 int is_thinkpad;
7521
7522 if (acpi_disabled)
7523 return -ENODEV;
7524
7525 /*
7526 * Non-ancient models have better DMI tagging, but very old models
7527 * don't.
7528 */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007529 is_thinkpad = (thinkpad_id.model_str != NULL);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007530
7531 /* ec is required because many other handles are relative to it */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007532 TPACPI_ACPIHANDLE_INIT(ec);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007533 if (!ec_handle) {
7534 if (is_thinkpad)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007535 printk(TPACPI_ERR
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007536 "Not yet supported ThinkPad detected!\n");
7537 return -ENODEV;
7538 }
7539
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03007540 /*
7541 * Risks a regression on very old machines, but reduces potential
7542 * false positives a damn great deal
7543 */
7544 if (!is_thinkpad)
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007545 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03007546
7547 if (!is_thinkpad && !force_load)
7548 return -ENODEV;
7549
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007550 return 0;
7551}
7552
7553
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007554/* Module init, exit, parameters */
7555
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007556static struct ibm_init_struct ibms_init[] __initdata = {
7557 {
7558 .init = thinkpad_acpi_driver_init,
7559 .data = &thinkpad_acpi_driver_data,
7560 },
7561 {
7562 .init = hotkey_init,
7563 .data = &hotkey_driver_data,
7564 },
7565 {
7566 .init = bluetooth_init,
7567 .data = &bluetooth_driver_data,
7568 },
7569 {
7570 .init = wan_init,
7571 .data = &wan_driver_data,
7572 },
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02007573 {
7574 .init = uwb_init,
7575 .data = &uwb_driver_data,
7576 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02007577#ifdef CONFIG_THINKPAD_ACPI_VIDEO
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007578 {
7579 .init = video_init,
7580 .data = &video_driver_data,
7581 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02007582#endif
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007583 {
7584 .init = light_init,
7585 .data = &light_driver_data,
7586 },
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007587 {
7588 .init = cmos_init,
7589 .data = &cmos_driver_data,
7590 },
7591 {
7592 .init = led_init,
7593 .data = &led_driver_data,
7594 },
7595 {
7596 .init = beep_init,
7597 .data = &beep_driver_data,
7598 },
7599 {
7600 .init = thermal_init,
7601 .data = &thermal_driver_data,
7602 },
7603 {
7604 .data = &ecdump_driver_data,
7605 },
7606 {
7607 .init = brightness_init,
7608 .data = &brightness_driver_data,
7609 },
7610 {
7611 .data = &volume_driver_data,
7612 },
7613 {
7614 .init = fan_init,
7615 .data = &fan_driver_data,
7616 },
7617};
7618
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007619static int __init set_ibm_param(const char *val, struct kernel_param *kp)
7620{
7621 unsigned int i;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007622 struct ibm_struct *ibm;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007623
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02007624 if (!kp || !kp->name || !val)
7625 return -EINVAL;
7626
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007627 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
7628 ibm = ibms_init[i].data;
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02007629 WARN_ON(ibm == NULL);
7630
7631 if (!ibm || !ibm->name)
7632 continue;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007633
7634 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
7635 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007636 return -ENOSPC;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007637 strcpy(ibms_init[i].param, val);
7638 strcat(ibms_init[i].param, ",");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007639 return 0;
7640 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007641 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007642
7643 return -EINVAL;
7644}
7645
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007646module_param(experimental, int, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007647MODULE_PARM_DESC(experimental,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007648 "Enables experimental features when non-zero");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007649
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03007650module_param_named(debug, dbg_level, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007651MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03007652
Henrique de Moraes Holschuh86cc9442007-07-18 23:45:41 -03007653module_param(force_load, bool, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007654MODULE_PARM_DESC(force_load,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007655 "Attempts to load the driver even on a "
7656 "mis-identified ThinkPad when true");
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03007657
Henrique de Moraes Holschuh86cc9442007-07-18 23:45:41 -03007658module_param_named(fan_control, fan_control_allowed, bool, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007659MODULE_PARM_DESC(fan_control,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007660 "Enables setting fan parameters features when true");
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03007661
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00007662module_param_named(brightness_mode, brightness_mode, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007663MODULE_PARM_DESC(brightness_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007664 "Selects brightness control strategy: "
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00007665 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03007666
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02007667module_param(brightness_enable, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007668MODULE_PARM_DESC(brightness_enable,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007669 "Enables backlight control when 1, disables when 0");
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02007670
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03007671module_param(hotkey_report_mode, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007672MODULE_PARM_DESC(hotkey_report_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007673 "used for backwards compatibility with userspace, "
7674 "see documentation");
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03007675
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007676#define TPACPI_PARAM(feature) \
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007677 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
Henrique de Moraes Holschuhcbb14842008-02-16 02:17:50 -02007678 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007679 "at module load, see documentation")
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007680
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007681TPACPI_PARAM(hotkey);
7682TPACPI_PARAM(bluetooth);
7683TPACPI_PARAM(video);
7684TPACPI_PARAM(light);
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007685TPACPI_PARAM(cmos);
7686TPACPI_PARAM(led);
7687TPACPI_PARAM(beep);
7688TPACPI_PARAM(ecdump);
7689TPACPI_PARAM(brightness);
7690TPACPI_PARAM(volume);
7691TPACPI_PARAM(fan);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007692
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02007693#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
7694module_param(dbg_wlswemul, uint, 0);
7695MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
7696module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
7697MODULE_PARM_DESC(wlsw_state,
7698 "Initial state of the emulated WLSW switch");
7699
7700module_param(dbg_bluetoothemul, uint, 0);
7701MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
7702module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
7703MODULE_PARM_DESC(bluetooth_state,
7704 "Initial state of the emulated bluetooth switch");
7705
7706module_param(dbg_wwanemul, uint, 0);
7707MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
7708module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
7709MODULE_PARM_DESC(wwan_state,
7710 "Initial state of the emulated WWAN switch");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02007711
7712module_param(dbg_uwbemul, uint, 0);
7713MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
7714module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
7715MODULE_PARM_DESC(uwb_state,
7716 "Initial state of the emulated UWB switch");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02007717#endif
7718
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007719static void thinkpad_acpi_module_exit(void)
7720{
7721 struct ibm_struct *ibm, *itmp;
7722
7723 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
7724
7725 list_for_each_entry_safe_reverse(ibm, itmp,
7726 &tpacpi_all_drivers,
7727 all_drivers) {
7728 ibm_exit(ibm);
7729 }
7730
7731 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
7732
7733 if (tpacpi_inputdev) {
7734 if (tp_features.input_device_registered)
7735 input_unregister_device(tpacpi_inputdev);
7736 else
7737 input_free_device(tpacpi_inputdev);
7738 }
7739
7740 if (tpacpi_hwmon)
7741 hwmon_device_unregister(tpacpi_hwmon);
7742
7743 if (tp_features.sensors_pdev_attrs_registered)
7744 device_remove_file(&tpacpi_sensors_pdev->dev,
7745 &dev_attr_thinkpad_acpi_pdev_name);
7746 if (tpacpi_sensors_pdev)
7747 platform_device_unregister(tpacpi_sensors_pdev);
7748 if (tpacpi_pdev)
7749 platform_device_unregister(tpacpi_pdev);
7750
7751 if (tp_features.sensors_pdrv_attrs_registered)
7752 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
7753 if (tp_features.platform_drv_attrs_registered)
7754 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
7755
7756 if (tp_features.sensors_pdrv_registered)
7757 platform_driver_unregister(&tpacpi_hwmon_pdriver);
7758
7759 if (tp_features.platform_drv_registered)
7760 platform_driver_unregister(&tpacpi_pdriver);
7761
7762 if (proc_dir)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007763 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007764
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03007765 if (tpacpi_wq)
7766 destroy_workqueue(tpacpi_wq);
7767
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007768 kfree(thinkpad_id.bios_version_str);
7769 kfree(thinkpad_id.ec_version_str);
7770 kfree(thinkpad_id.model_str);
7771}
7772
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007773
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03007774static int __init thinkpad_acpi_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007775{
7776 int ret, i;
7777
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03007778 tpacpi_lifecycle = TPACPI_LIFE_INIT;
7779
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03007780 /* Parameter checking */
7781 if (hotkey_report_mode > 2)
7782 return -EINVAL;
7783
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007784 /* Driver-level probe */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007785
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007786 ret = get_thinkpad_model_data(&thinkpad_id);
7787 if (ret) {
7788 printk(TPACPI_ERR
7789 "unable to get DMI data: %d\n", ret);
7790 thinkpad_acpi_module_exit();
7791 return ret;
7792 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007793 ret = probe_for_thinkpad();
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007794 if (ret) {
7795 thinkpad_acpi_module_exit();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007796 return ret;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007797 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007798
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007799 /* Driver initialization */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007800
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007801 TPACPI_ACPIHANDLE_INIT(ecrd);
7802 TPACPI_ACPIHANDLE_INIT(ecwr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007803
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03007804 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
7805 if (!tpacpi_wq) {
7806 thinkpad_acpi_module_exit();
7807 return -ENOMEM;
7808 }
7809
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007810 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007811 if (!proc_dir) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007812 printk(TPACPI_ERR
7813 "unable to create proc dir " TPACPI_PROC_DIR);
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03007814 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007815 return -ENODEV;
7816 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007817
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007818 ret = platform_driver_register(&tpacpi_pdriver);
7819 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007820 printk(TPACPI_ERR
7821 "unable to register main platform driver\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007822 thinkpad_acpi_module_exit();
7823 return ret;
7824 }
Henrique de Moraes Holschuhac363932007-07-27 17:04:40 -03007825 tp_features.platform_drv_registered = 1;
7826
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007827 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
7828 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007829 printk(TPACPI_ERR
7830 "unable to register hwmon platform driver\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007831 thinkpad_acpi_module_exit();
7832 return ret;
7833 }
7834 tp_features.sensors_pdrv_registered = 1;
7835
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03007836 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03007837 if (!ret) {
7838 tp_features.platform_drv_attrs_registered = 1;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007839 ret = tpacpi_create_driver_attributes(
7840 &tpacpi_hwmon_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03007841 }
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03007842 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007843 printk(TPACPI_ERR
7844 "unable to create sysfs driver attributes\n");
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03007845 thinkpad_acpi_module_exit();
7846 return ret;
7847 }
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03007848 tp_features.sensors_pdrv_attrs_registered = 1;
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03007849
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007850
7851 /* Device initialization */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007852 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007853 NULL, 0);
7854 if (IS_ERR(tpacpi_pdev)) {
7855 ret = PTR_ERR(tpacpi_pdev);
7856 tpacpi_pdev = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007857 printk(TPACPI_ERR "unable to register platform device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007858 thinkpad_acpi_module_exit();
7859 return ret;
7860 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007861 tpacpi_sensors_pdev = platform_device_register_simple(
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007862 TPACPI_HWMON_DRVR_NAME,
7863 -1, NULL, 0);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007864 if (IS_ERR(tpacpi_sensors_pdev)) {
7865 ret = PTR_ERR(tpacpi_sensors_pdev);
7866 tpacpi_sensors_pdev = NULL;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007867 printk(TPACPI_ERR
7868 "unable to register hwmon platform device\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007869 thinkpad_acpi_module_exit();
7870 return ret;
7871 }
7872 ret = device_create_file(&tpacpi_sensors_pdev->dev,
7873 &dev_attr_thinkpad_acpi_pdev_name);
7874 if (ret) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007875 printk(TPACPI_ERR
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007876 "unable to create sysfs hwmon device attributes\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007877 thinkpad_acpi_module_exit();
7878 return ret;
7879 }
7880 tp_features.sensors_pdev_attrs_registered = 1;
7881 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007882 if (IS_ERR(tpacpi_hwmon)) {
7883 ret = PTR_ERR(tpacpi_hwmon);
7884 tpacpi_hwmon = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007885 printk(TPACPI_ERR "unable to register hwmon device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007886 thinkpad_acpi_module_exit();
7887 return ret;
7888 }
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -03007889 mutex_init(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03007890 tpacpi_inputdev = input_allocate_device();
7891 if (!tpacpi_inputdev) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007892 printk(TPACPI_ERR "unable to allocate input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03007893 thinkpad_acpi_module_exit();
7894 return -ENOMEM;
7895 } else {
7896 /* Prepare input device, but don't register */
7897 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007898 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03007899 tpacpi_inputdev->id.bustype = BUS_HOST;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03007900 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
7901 thinkpad_id.vendor :
7902 PCI_VENDOR_ID_IBM;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03007903 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
7904 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
7905 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007906 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
7907 ret = ibm_init(&ibms_init[i]);
7908 if (ret >= 0 && *ibms_init[i].param)
7909 ret = ibms_init[i].data->write(ibms_init[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007910 if (ret < 0) {
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03007911 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007912 return ret;
7913 }
7914 }
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03007915 ret = input_register_device(tpacpi_inputdev);
7916 if (ret < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007917 printk(TPACPI_ERR "unable to register input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03007918 thinkpad_acpi_module_exit();
7919 return ret;
7920 } else {
7921 tp_features.input_device_registered = 1;
7922 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007923
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03007924 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007925 return 0;
7926}
7927
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03007928MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
7929
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007930/*
Henrique de Moraes Holschuh922fe092009-04-14 02:44:13 +00007931 * This will autoload the driver in almost every ThinkPad
7932 * in widespread use.
7933 *
7934 * Only _VERY_ old models, like the 240, 240x and 570 lack
7935 * the HKEY event interface.
7936 */
7937MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
7938
7939/*
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007940 * DMI matching for module autoloading
7941 *
7942 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
7943 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
7944 *
7945 * Only models listed in thinkwiki will be supported, so add yours
7946 * if it is not there yet.
7947 */
7948#define IBM_BIOS_MODULE_ALIAS(__type) \
Mathieu Chouquet-Stringerb36a50f2009-03-14 16:35:26 +01007949 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007950
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007951/* Ancient thinkpad BIOSes have to be identified by
7952 * BIOS type or model number, and there are far less
7953 * BIOS types than model numbers... */
Henrique de Moraes Holschuh922fe092009-04-14 02:44:13 +00007954IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007955
Henrique de Moraes Holschuhf68f53a2009-04-14 02:44:12 +00007956MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
7957MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007958MODULE_DESCRIPTION(TPACPI_DESC);
7959MODULE_VERSION(TPACPI_VERSION);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007960MODULE_LICENSE("GPL");
7961
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03007962module_init(thinkpad_acpi_module_init);
7963module_exit(thinkpad_acpi_module_exit);