blob: 50aa4c112b287a2710af78e6051851dddb3aac1d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -03002 * thinkpad_acpi.c - ThinkPad ACPI Extras
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 *
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005 * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
Henrique de Moraes Holschuh1c762ca2009-04-04 04:25:42 +00006 * Copyright (C) 2006-2009 Henrique de Moraes Holschuh <hmh@hmh.eng.br>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
Henrique de Moraes Holschuha62bc912007-03-23 17:33:58 -030020 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301, USA.
Borislav Deianov78f81cc2005-08-17 00:00:00 -040022 */
23
Henrique de Moraes Holschuhb57f7e72009-04-14 02:44:14 +000024#define TPACPI_VERSION "0.23"
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -030025#define TPACPI_SYSFS_VERSION 0x020500
Borislav Deianov78f81cc2005-08-17 00:00:00 -040026
27/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 * Changelog:
Henrique de Moraes Holschuh4b45cc02008-01-08 13:02:46 -020029 * 2007-10-20 changelog trimmed down
30 *
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -030031 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
32 * drivers/misc.
Henrique de Moraes Holschuhf9ff43a2006-11-25 16:37:38 -020033 *
34 * 2006-11-22 0.13 new maintainer
35 * changelog now lives in git commit history, and will
36 * not be updated further in-file.
Henrique de Moraes Holschuh837ca6d2007-03-23 17:33:54 -030037 *
Borislav Deianov78f81cc2005-08-17 00:00:00 -040038 * 2005-03-17 0.11 support for 600e, 770x
39 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
Henrique de Moraes Holschuh4b45cc02008-01-08 13:02:46 -020040 *
41 * 2005-01-16 0.9 use MODULE_VERSION
Borislav Deianov78f81cc2005-08-17 00:00:00 -040042 * thanks to Henrik Brix Andersen <brix@gentoo.org>
43 * fix parameter passing on module loading
44 * thanks to Rusty Russell <rusty@rustcorp.com.au>
45 * thanks to Jim Radford <radford@blackbean.org>
46 * 2004-11-08 0.8 fix init error case, don't return from a macro
47 * thanks to Chris Wright <chrisw@osdl.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 */
49
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020050#include <linux/kernel.h>
51#include <linux/module.h>
52#include <linux/init.h>
53#include <linux/types.h>
54#include <linux/string.h>
55#include <linux/list.h>
56#include <linux/mutex.h>
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +000057#include <linux/sched.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020058#include <linux/kthread.h>
59#include <linux/freezer.h>
60#include <linux/delay.h>
61
62#include <linux/nvram.h>
63#include <linux/proc_fs.h>
64#include <linux/sysfs.h>
65#include <linux/backlight.h>
66#include <linux/fb.h>
67#include <linux/platform_device.h>
68#include <linux/hwmon.h>
69#include <linux/hwmon-sysfs.h>
70#include <linux/input.h>
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -030071#include <linux/leds.h>
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -030072#include <linux/rfkill.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020073#include <asm/uaccess.h>
74
75#include <linux/dmi.h>
76#include <linux/jiffies.h>
77#include <linux/workqueue.h>
78
79#include <acpi/acpi_drivers.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020080
81#include <linux/pci_ids.h>
82
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020083
84/* ThinkPad CMOS commands */
85#define TP_CMOS_VOLUME_DOWN 0
86#define TP_CMOS_VOLUME_UP 1
87#define TP_CMOS_VOLUME_MUTE 2
88#define TP_CMOS_BRIGHTNESS_UP 4
89#define TP_CMOS_BRIGHTNESS_DOWN 5
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -030090#define TP_CMOS_THINKLIGHT_ON 12
91#define TP_CMOS_THINKLIGHT_OFF 13
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020092
93/* NVRAM Addresses */
94enum tp_nvram_addr {
95 TP_NVRAM_ADDR_HK2 = 0x57,
96 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
97 TP_NVRAM_ADDR_VIDEO = 0x59,
98 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
99 TP_NVRAM_ADDR_MIXER = 0x60,
100};
101
102/* NVRAM bit masks */
103enum {
104 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
105 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
106 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
107 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
108 TP_NVRAM_MASK_THINKLIGHT = 0x10,
109 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
110 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
111 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
112 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
113 TP_NVRAM_MASK_MUTE = 0x40,
114 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
115 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
116 TP_NVRAM_POS_LEVEL_VOLUME = 0,
117};
118
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200119/* ACPI HIDs */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200120#define TPACPI_ACPI_HKEY_HID "IBM0068"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200121
122/* Input IDs */
123#define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
124#define TPACPI_HKEY_INPUT_VERSION 0x4101
125
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -0200126/* ACPI \WGSV commands */
127enum {
128 TP_ACPI_WGSV_GET_STATE = 0x01, /* Get state information */
129 TP_ACPI_WGSV_PWR_ON_ON_RESUME = 0x02, /* Resume WWAN powered on */
130 TP_ACPI_WGSV_PWR_OFF_ON_RESUME = 0x03, /* Resume WWAN powered off */
131 TP_ACPI_WGSV_SAVE_STATE = 0x04, /* Save state for S4/S5 */
132};
133
134/* TP_ACPI_WGSV_GET_STATE bits */
135enum {
136 TP_ACPI_WGSV_STATE_WWANEXIST = 0x0001, /* WWAN hw available */
137 TP_ACPI_WGSV_STATE_WWANPWR = 0x0002, /* WWAN radio enabled */
138 TP_ACPI_WGSV_STATE_WWANPWRRES = 0x0004, /* WWAN state at resume */
139 TP_ACPI_WGSV_STATE_WWANBIOSOFF = 0x0008, /* WWAN disabled in BIOS */
140 TP_ACPI_WGSV_STATE_BLTHEXIST = 0x0001, /* BLTH hw available */
141 TP_ACPI_WGSV_STATE_BLTHPWR = 0x0002, /* BLTH radio enabled */
142 TP_ACPI_WGSV_STATE_BLTHPWRRES = 0x0004, /* BLTH state at resume */
143 TP_ACPI_WGSV_STATE_BLTHBIOSOFF = 0x0008, /* BLTH disabled in BIOS */
144 TP_ACPI_WGSV_STATE_UWBEXIST = 0x0010, /* UWB hw available */
145 TP_ACPI_WGSV_STATE_UWBPWR = 0x0020, /* UWB radio enabled */
146};
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200147
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);
Corentin Chary5f0dadb2009-09-14 12:43:52 +02001281 rfkill_destroy(tp_rfk->rfkill);
Johannes Berg19d337d2009-06-02 13:01:37 +02001282 tpacpi_rfkill_switches[id] = NULL;
1283 kfree(tp_rfk);
1284 }
1285}
1286
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00001287static void printk_deprecated_rfkill_attribute(const char * const what)
1288{
1289 printk_deprecated_attribute(what,
1290 "Please switch to generic rfkill before year 2010");
1291}
1292
Johannes Berg19d337d2009-06-02 13:01:37 +02001293/* sysfs <radio> enable ------------------------------------------------ */
1294static ssize_t tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id,
1295 struct device_attribute *attr,
1296 char *buf)
1297{
1298 int status;
1299
1300 printk_deprecated_rfkill_attribute(attr->attr.name);
1301
1302 /* This is in the ABI... */
1303 if (tpacpi_rfk_check_hwblock_state()) {
1304 status = TPACPI_RFK_RADIO_OFF;
1305 } else {
1306 status = tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1307 if (status < 0)
1308 return status;
1309 }
1310
1311 return snprintf(buf, PAGE_SIZE, "%d\n",
1312 (status == TPACPI_RFK_RADIO_ON) ? 1 : 0);
1313}
1314
1315static ssize_t tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id,
1316 struct device_attribute *attr,
1317 const char *buf, size_t count)
1318{
1319 unsigned long t;
1320 int res;
1321
1322 printk_deprecated_rfkill_attribute(attr->attr.name);
1323
1324 if (parse_strtoul(buf, 1, &t))
1325 return -EINVAL;
1326
1327 tpacpi_disclose_usertask(attr->attr.name, "set to %ld\n", t);
1328
1329 /* This is in the ABI... */
1330 if (tpacpi_rfk_check_hwblock_state() && !!t)
1331 return -EPERM;
1332
1333 res = tpacpi_rfkill_switches[id]->ops->set_status((!!t) ?
1334 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF);
1335 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1336
1337 return (res < 0) ? res : count;
1338}
1339
1340/* procfs -------------------------------------------------------------- */
1341static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id, char *p)
1342{
1343 int len = 0;
1344
1345 if (id >= TPACPI_RFK_SW_MAX)
1346 len += sprintf(p + len, "status:\t\tnot supported\n");
1347 else {
1348 int status;
1349
1350 /* This is in the ABI... */
1351 if (tpacpi_rfk_check_hwblock_state()) {
1352 status = TPACPI_RFK_RADIO_OFF;
1353 } else {
1354 status = tpacpi_rfk_update_swstate(
1355 tpacpi_rfkill_switches[id]);
1356 if (status < 0)
1357 return status;
1358 }
1359
1360 len += sprintf(p + len, "status:\t\t%s\n",
1361 (status == TPACPI_RFK_RADIO_ON) ?
1362 "enabled" : "disabled");
1363 len += sprintf(p + len, "commands:\tenable, disable\n");
1364 }
1365
1366 return len;
1367}
1368
1369static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id, char *buf)
1370{
1371 char *cmd;
1372 int status = -1;
1373 int res = 0;
1374
1375 if (id >= TPACPI_RFK_SW_MAX)
1376 return -ENODEV;
1377
1378 while ((cmd = next_cmd(&buf))) {
1379 if (strlencmp(cmd, "enable") == 0)
1380 status = TPACPI_RFK_RADIO_ON;
1381 else if (strlencmp(cmd, "disable") == 0)
1382 status = TPACPI_RFK_RADIO_OFF;
1383 else
1384 return -EINVAL;
1385 }
1386
1387 if (status != -1) {
1388 tpacpi_disclose_usertask("procfs", "attempt to %s %s\n",
1389 (status == TPACPI_RFK_RADIO_ON) ?
1390 "enable" : "disable",
1391 tpacpi_rfkill_names[id]);
1392 res = (tpacpi_rfkill_switches[id]->ops->set_status)(status);
1393 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1394 }
1395
1396 return res;
1397}
1398
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001399/*************************************************************************
1400 * thinkpad-acpi driver attributes
1401 */
1402
1403/* interface_version --------------------------------------------------- */
1404static ssize_t tpacpi_driver_interface_version_show(
1405 struct device_driver *drv,
1406 char *buf)
1407{
1408 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1409}
1410
1411static DRIVER_ATTR(interface_version, S_IRUGO,
1412 tpacpi_driver_interface_version_show, NULL);
1413
1414/* debug_level --------------------------------------------------------- */
1415static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1416 char *buf)
1417{
1418 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1419}
1420
1421static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1422 const char *buf, size_t count)
1423{
1424 unsigned long t;
1425
1426 if (parse_strtoul(buf, 0xffff, &t))
1427 return -EINVAL;
1428
1429 dbg_level = t;
1430
1431 return count;
1432}
1433
1434static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1435 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1436
1437/* version ------------------------------------------------------------- */
1438static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1439 char *buf)
1440{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001441 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1442 TPACPI_DESC, TPACPI_VERSION);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001443}
1444
1445static DRIVER_ATTR(version, S_IRUGO,
1446 tpacpi_driver_version_show, NULL);
1447
1448/* --------------------------------------------------------------------- */
1449
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001450#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1451
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001452/* wlsw_emulstate ------------------------------------------------------ */
1453static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1454 char *buf)
1455{
1456 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1457}
1458
1459static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1460 const char *buf, size_t count)
1461{
1462 unsigned long t;
1463
1464 if (parse_strtoul(buf, 1, &t))
1465 return -EINVAL;
1466
Johannes Berg19d337d2009-06-02 13:01:37 +02001467 if (tpacpi_wlsw_emulstate != !!t) {
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001468 tpacpi_wlsw_emulstate = !!t;
Johannes Berg19d337d2009-06-02 13:01:37 +02001469 tpacpi_rfk_update_hwblock_state(!t); /* negative logic */
1470 }
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001471
1472 return count;
1473}
1474
1475static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1476 tpacpi_driver_wlsw_emulstate_show,
1477 tpacpi_driver_wlsw_emulstate_store);
1478
1479/* bluetooth_emulstate ------------------------------------------------- */
1480static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1481 struct device_driver *drv,
1482 char *buf)
1483{
1484 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1485}
1486
1487static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1488 struct device_driver *drv,
1489 const char *buf, size_t count)
1490{
1491 unsigned long t;
1492
1493 if (parse_strtoul(buf, 1, &t))
1494 return -EINVAL;
1495
1496 tpacpi_bluetooth_emulstate = !!t;
1497
1498 return count;
1499}
1500
1501static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1502 tpacpi_driver_bluetooth_emulstate_show,
1503 tpacpi_driver_bluetooth_emulstate_store);
1504
1505/* wwan_emulstate ------------------------------------------------- */
1506static ssize_t tpacpi_driver_wwan_emulstate_show(
1507 struct device_driver *drv,
1508 char *buf)
1509{
1510 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1511}
1512
1513static ssize_t tpacpi_driver_wwan_emulstate_store(
1514 struct device_driver *drv,
1515 const char *buf, size_t count)
1516{
1517 unsigned long t;
1518
1519 if (parse_strtoul(buf, 1, &t))
1520 return -EINVAL;
1521
1522 tpacpi_wwan_emulstate = !!t;
1523
1524 return count;
1525}
1526
1527static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1528 tpacpi_driver_wwan_emulstate_show,
1529 tpacpi_driver_wwan_emulstate_store);
1530
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001531/* uwb_emulstate ------------------------------------------------- */
1532static ssize_t tpacpi_driver_uwb_emulstate_show(
1533 struct device_driver *drv,
1534 char *buf)
1535{
1536 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1537}
1538
1539static ssize_t tpacpi_driver_uwb_emulstate_store(
1540 struct device_driver *drv,
1541 const char *buf, size_t count)
1542{
1543 unsigned long t;
1544
1545 if (parse_strtoul(buf, 1, &t))
1546 return -EINVAL;
1547
1548 tpacpi_uwb_emulstate = !!t;
1549
1550 return count;
1551}
1552
1553static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
1554 tpacpi_driver_uwb_emulstate_show,
1555 tpacpi_driver_uwb_emulstate_store);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001556#endif
1557
1558/* --------------------------------------------------------------------- */
1559
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001560static struct driver_attribute *tpacpi_driver_attributes[] = {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001561 &driver_attr_debug_level, &driver_attr_version,
1562 &driver_attr_interface_version,
1563};
1564
1565static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1566{
1567 int i, res;
1568
1569 i = 0;
1570 res = 0;
1571 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1572 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1573 i++;
1574 }
1575
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001576#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1577 if (!res && dbg_wlswemul)
1578 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1579 if (!res && dbg_bluetoothemul)
1580 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1581 if (!res && dbg_wwanemul)
1582 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001583 if (!res && dbg_uwbemul)
1584 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001585#endif
1586
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001587 return res;
1588}
1589
1590static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1591{
1592 int i;
1593
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001594 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001595 driver_remove_file(drv, tpacpi_driver_attributes[i]);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001596
1597#ifdef THINKPAD_ACPI_DEBUGFACILITIES
1598 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1599 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1600 driver_remove_file(drv, &driver_attr_wwan_emulstate);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001601 driver_remove_file(drv, &driver_attr_uwb_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001602#endif
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001603}
1604
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001605/*************************************************************************
1606 * Firmware Data
1607 */
1608
1609/*
1610 * Table of recommended minimum BIOS versions
1611 *
1612 * Reasons for listing:
1613 * 1. Stable BIOS, listed because the unknown ammount of
1614 * bugs and bad ACPI behaviour on older versions
1615 *
1616 * 2. BIOS or EC fw with known bugs that trigger on Linux
1617 *
1618 * 3. BIOS with known reduced functionality in older versions
1619 *
1620 * We recommend the latest BIOS and EC version.
1621 * We only support the latest BIOS and EC fw version as a rule.
1622 *
1623 * Sources: IBM ThinkPad Public Web Documents (update changelogs),
1624 * Information from users in ThinkWiki
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03001625 *
1626 * WARNING: we use this table also to detect that the machine is
1627 * a ThinkPad in some cases, so don't remove entries lightly.
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001628 */
1629
1630#define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1631 { .vendor = (__v), \
1632 .bios = TPID(__id1, __id2), \
1633 .ec = TPACPI_MATCH_ANY, \
1634 .quirks = TPACPI_MATCH_ANY << 16 \
1635 | (__bv1) << 8 | (__bv2) }
1636
1637#define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
1638 __eid1, __eid2, __ev1, __ev2) \
1639 { .vendor = (__v), \
1640 .bios = TPID(__bid1, __bid2), \
1641 .ec = TPID(__eid1, __eid2), \
1642 .quirks = (__ev1) << 24 | (__ev2) << 16 \
1643 | (__bv1) << 8 | (__bv2) }
1644
1645#define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1646 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1647
1648#define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1649 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1650 __bv1, __bv2, __id1, __id2, __ev1, __ev2)
1651
1652#define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1653 __eid1, __eid2, __ev1, __ev2) \
1654 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1655 __bv1, __bv2, __eid1, __eid2, __ev1, __ev2)
1656
1657#define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1658 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1659
1660#define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1661 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
1662 __bv1, __bv2, __id1, __id2, __ev1, __ev2)
1663
1664#define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1665 __eid1, __eid2, __ev1, __ev2) \
1666 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
1667 __bv1, __bv2, __eid1, __eid2, __ev1, __ev2)
1668
1669static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
1670 /* Numeric models ------------------ */
1671 /* FW MODEL BIOS VERS */
1672 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1673 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1674 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1675 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1676 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1677 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1678 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1679 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1680 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1681
1682 /* A-series ------------------------- */
1683 /* FW MODEL BIOS VERS EC VERS */
1684 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1685 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1686 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1687 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1688 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1689 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1690 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1691 TPV_QI0('1', '3', '2', '0'), /* A22m */
1692 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1693 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1694 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1695
1696 /* G-series ------------------------- */
1697 /* FW MODEL BIOS VERS */
1698 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1699 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1700
1701 /* R-series, T-series --------------- */
1702 /* FW MODEL BIOS VERS EC VERS */
1703 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1704 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1705 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1706 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1707 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1708 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1709 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1710 T40/p, T41/p, T42/p (1) */
1711 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1712 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1713 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1714 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1715
1716 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1717 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1718 TPV_QI0('1', '6', '3', '2'), /* T22 */
1719 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1720 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1721 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1722
1723 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1724 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
1725 TPV_QL0('7', 'E', 'D', '0'), /* R60e, R60i */
1726
1727 /* BIOS FW BIOS VERS EC FW EC VERS */
1728 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1729 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1730
1731 /* X-series ------------------------- */
1732 /* FW MODEL BIOS VERS EC VERS */
1733 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1734 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1735 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1736 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1737 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1738 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1739 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1740
1741 TPV_QL0('7', 'B', 'D', '7'), /* X60/s */
1742 TPV_QL0('7', 'J', '3', '0'), /* X60t */
1743
1744 /* (0) - older versions lack DMI EC fw string and functionality */
1745 /* (1) - older versions known to lack functionality */
1746};
1747
1748#undef TPV_QL1
1749#undef TPV_QL0
1750#undef TPV_QI2
1751#undef TPV_QI1
1752#undef TPV_QI0
1753#undef TPV_Q_X
1754#undef TPV_Q
1755
1756static void __init tpacpi_check_outdated_fw(void)
1757{
1758 unsigned long fwvers;
1759 u16 ec_version, bios_version;
1760
1761 fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
1762 ARRAY_SIZE(tpacpi_bios_version_qtable));
1763
1764 if (!fwvers)
1765 return;
1766
1767 bios_version = fwvers & 0xffffU;
1768 ec_version = (fwvers >> 16) & 0xffffU;
1769
1770 /* note that unknown versions are set to 0x0000 and we use that */
1771 if ((bios_version > thinkpad_id.bios_release) ||
1772 (ec_version > thinkpad_id.ec_release &&
1773 ec_version != TPACPI_MATCH_ANY)) {
1774 /*
1775 * The changelogs would let us track down the exact
1776 * reason, but it is just too much of a pain to track
1777 * it. We only list BIOSes that are either really
1778 * broken, or really stable to begin with, so it is
1779 * best if the user upgrades the firmware anyway.
1780 */
1781 printk(TPACPI_WARN
1782 "WARNING: Outdated ThinkPad BIOS/EC firmware\n");
1783 printk(TPACPI_WARN
1784 "WARNING: This firmware may be missing critical bug "
1785 "fixes and/or important features\n");
1786 }
1787}
1788
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03001789static bool __init tpacpi_is_fw_known(void)
1790{
1791 return tpacpi_check_quirks(tpacpi_bios_version_qtable,
1792 ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
1793}
1794
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03001795/****************************************************************************
1796 ****************************************************************************
1797 *
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001798 * Subdrivers
1799 *
1800 ****************************************************************************
1801 ****************************************************************************/
1802
1803/*************************************************************************
Henrique de Moraes Holschuh142cfc92007-04-21 11:08:26 -03001804 * thinkpad-acpi init subdriver
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001805 */
1806
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001807static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001809 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
1810 printk(TPACPI_INFO "%s\n", TPACPI_URL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001812 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03001813 (thinkpad_id.bios_version_str) ?
1814 thinkpad_id.bios_version_str : "unknown",
1815 (thinkpad_id.ec_version_str) ?
1816 thinkpad_id.ec_version_str : "unknown");
1817
1818 if (thinkpad_id.vendor && thinkpad_id.model_str)
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03001819 printk(TPACPI_INFO "%s %s, model %s\n",
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03001820 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
1821 "IBM" : ((thinkpad_id.vendor ==
1822 PCI_VENDOR_ID_LENOVO) ?
1823 "Lenovo" : "Unknown vendor"),
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03001824 thinkpad_id.model_str,
1825 (thinkpad_id.nummodel_str) ?
1826 thinkpad_id.nummodel_str : "unknown");
Henrique de Moraes Holschuh3945ac32007-02-06 19:13:44 -02001827
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001828 tpacpi_check_outdated_fw();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 return 0;
1830}
1831
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -03001832static int thinkpad_acpi_driver_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833{
1834 int len = 0;
1835
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001836 len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
1837 len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838
1839 return len;
1840}
1841
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001842static struct ibm_struct thinkpad_acpi_driver_data = {
1843 .name = "driver",
1844 .read = thinkpad_acpi_driver_read,
1845};
1846
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001847/*************************************************************************
1848 * Hotkey subdriver
1849 */
1850
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001851/*
1852 * ThinkPad firmware event model
1853 *
1854 * The ThinkPad firmware has two main event interfaces: normal ACPI
1855 * notifications (which follow the ACPI standard), and a private event
1856 * interface.
1857 *
1858 * The private event interface also issues events for the hotkeys. As
1859 * the driver gained features, the event handling code ended up being
1860 * built around the hotkey subdriver. This will need to be refactored
1861 * to a more formal event API eventually.
1862 *
1863 * Some "hotkeys" are actually supposed to be used as event reports,
1864 * such as "brightness has changed", "volume has changed", depending on
1865 * the ThinkPad model and how the firmware is operating.
1866 *
1867 * Unlike other classes, hotkey-class events have mask/unmask control on
1868 * non-ancient firmware. However, how it behaves changes a lot with the
1869 * firmware model and version.
1870 */
1871
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001872enum { /* hot key scan codes (derived from ACPI DSDT) */
1873 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1874 TP_ACPI_HOTKEYSCAN_FNF2,
1875 TP_ACPI_HOTKEYSCAN_FNF3,
1876 TP_ACPI_HOTKEYSCAN_FNF4,
1877 TP_ACPI_HOTKEYSCAN_FNF5,
1878 TP_ACPI_HOTKEYSCAN_FNF6,
1879 TP_ACPI_HOTKEYSCAN_FNF7,
1880 TP_ACPI_HOTKEYSCAN_FNF8,
1881 TP_ACPI_HOTKEYSCAN_FNF9,
1882 TP_ACPI_HOTKEYSCAN_FNF10,
1883 TP_ACPI_HOTKEYSCAN_FNF11,
1884 TP_ACPI_HOTKEYSCAN_FNF12,
1885 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1886 TP_ACPI_HOTKEYSCAN_FNINSERT,
1887 TP_ACPI_HOTKEYSCAN_FNDELETE,
1888 TP_ACPI_HOTKEYSCAN_FNHOME,
1889 TP_ACPI_HOTKEYSCAN_FNEND,
1890 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1891 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1892 TP_ACPI_HOTKEYSCAN_FNSPACE,
1893 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1894 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1895 TP_ACPI_HOTKEYSCAN_MUTE,
1896 TP_ACPI_HOTKEYSCAN_THINKPAD,
1897};
1898
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001899enum { /* Keys/events available through NVRAM polling */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001900 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1901 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1902};
1903
1904enum { /* Positions of some of the keys in hotkey masks */
1905 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1906 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1907 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1908 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1909 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1910 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1911 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1912 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1913 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1914 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1915 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1916};
1917
1918enum { /* NVRAM to ACPI HKEY group map */
1919 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1920 TP_ACPI_HKEY_ZOOM_MASK |
1921 TP_ACPI_HKEY_DISPSWTCH_MASK |
1922 TP_ACPI_HKEY_HIBERNATE_MASK,
1923 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1924 TP_ACPI_HKEY_BRGHTDWN_MASK,
1925 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1926 TP_ACPI_HKEY_VOLDWN_MASK |
1927 TP_ACPI_HKEY_MUTE_MASK,
1928};
1929
1930#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1931struct tp_nvram_state {
1932 u16 thinkpad_toggle:1;
1933 u16 zoom_toggle:1;
1934 u16 display_toggle:1;
1935 u16 thinklight_toggle:1;
1936 u16 hibernate_toggle:1;
1937 u16 displayexp_toggle:1;
1938 u16 display_state:1;
1939 u16 brightness_toggle:1;
1940 u16 volume_toggle:1;
1941 u16 mute:1;
1942
1943 u8 brightness_level;
1944 u8 volume_level;
1945};
1946
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001947/* kthread for the hotkey poller */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001948static struct task_struct *tpacpi_hotkey_task;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001949
1950/* Acquired while the poller kthread is running, use to sync start/stop */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001951static struct mutex hotkey_thread_mutex;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001952
1953/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001954 * Acquire mutex to write poller control variables as an
1955 * atomic block.
1956 *
1957 * Increment hotkey_config_change when changing them if you
1958 * want the kthread to forget old state.
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001959 *
1960 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1961 */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001962static struct mutex hotkey_thread_data_mutex;
1963static unsigned int hotkey_config_change;
1964
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001965/*
1966 * hotkey poller control variables
1967 *
1968 * Must be atomic or readers will also need to acquire mutex
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001969 *
1970 * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1971 * should be used only when the changes need to be taken as
1972 * a block, OR when one needs to force the kthread to forget
1973 * old state.
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001974 */
1975static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1976static unsigned int hotkey_poll_freq = 10; /* Hz */
1977
1978#define HOTKEY_CONFIG_CRITICAL_START \
1979 do { \
1980 mutex_lock(&hotkey_thread_data_mutex); \
1981 hotkey_config_change++; \
1982 } while (0);
1983#define HOTKEY_CONFIG_CRITICAL_END \
1984 mutex_unlock(&hotkey_thread_data_mutex);
1985
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001986#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1987
1988#define hotkey_source_mask 0U
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001989#define HOTKEY_CONFIG_CRITICAL_START
1990#define HOTKEY_CONFIG_CRITICAL_END
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001991
1992#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1993
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001994static struct mutex hotkey_mutex;
1995
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02001996static enum { /* Reasons for waking up */
1997 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1998 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1999 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
2000} hotkey_wakeup_reason;
2001
2002static int hotkey_autosleep_ack;
2003
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002004static u32 hotkey_orig_mask; /* events the BIOS had enabled */
2005static u32 hotkey_all_mask; /* all events supported in fw */
2006static u32 hotkey_reserved_mask; /* events better left disabled */
2007static u32 hotkey_driver_mask; /* events needed by the driver */
2008static u32 hotkey_user_mask; /* events visible to userspace */
2009static u32 hotkey_acpi_mask; /* events enabled in firmware */
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002010
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02002011static unsigned int hotkey_report_mode;
2012
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002013static u16 *hotkey_keycode_map;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002014
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -03002015static struct attribute_set *hotkey_dev_attributes;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002016
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002017/* HKEY.MHKG() return bits */
2018#define TP_HOTKEY_TABLET_MASK (1 << 3)
2019
Johannes Berg19d337d2009-06-02 13:01:37 +02002020static int hotkey_get_wlsw(void)
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002021{
Johannes Berg19d337d2009-06-02 13:01:37 +02002022 int status;
2023
2024 if (!tp_features.hotkey_wlsw)
2025 return -ENODEV;
2026
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02002027#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
Johannes Berg19d337d2009-06-02 13:01:37 +02002028 if (dbg_wlswemul)
2029 return (tpacpi_wlsw_emulstate) ?
2030 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02002031#endif
Johannes Berg19d337d2009-06-02 13:01:37 +02002032
2033 if (!acpi_evalf(hkey_handle, &status, "WLSW", "d"))
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002034 return -EIO;
Johannes Berg19d337d2009-06-02 13:01:37 +02002035
2036 return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002037}
2038
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002039static int hotkey_get_tablet_mode(int *status)
2040{
2041 int s;
2042
2043 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
2044 return -EIO;
2045
Henrique de Moraes Holschuhcee47f52008-03-04 14:29:21 -08002046 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
2047 return 0;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002048}
2049
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002050/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002051 * Reads current event mask from firmware, and updates
2052 * hotkey_acpi_mask accordingly. Also resets any bits
2053 * from hotkey_user_mask that are unavailable to be
2054 * delivered (shadow requirement of the userspace ABI).
2055 *
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002056 * Call with hotkey_mutex held
2057 */
2058static int hotkey_mask_get(void)
2059{
2060 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002061 u32 m = 0;
2062
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002063 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002064 return -EIO;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002065
2066 hotkey_acpi_mask = m;
2067 } else {
2068 /* no mask support doesn't mean no event support... */
2069 hotkey_acpi_mask = hotkey_all_mask;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002070 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002071
2072 /* sync userspace-visible mask */
2073 hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002074
2075 return 0;
2076}
2077
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002078void static hotkey_mask_warn_incomplete_mask(void)
2079{
2080 /* log only what the user can fix... */
2081 const u32 wantedmask = hotkey_driver_mask &
2082 ~(hotkey_acpi_mask | hotkey_source_mask) &
2083 (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
2084
2085 if (wantedmask)
2086 printk(TPACPI_NOTICE
2087 "required events 0x%08x not enabled!\n",
2088 wantedmask);
2089}
2090
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002091/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002092 * Set the firmware mask when supported
2093 *
2094 * Also calls hotkey_mask_get to update hotkey_acpi_mask.
2095 *
2096 * NOTE: does not set bits in hotkey_user_mask, but may reset them.
2097 *
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002098 * Call with hotkey_mutex held
2099 */
2100static int hotkey_mask_set(u32 mask)
2101{
2102 int i;
2103 int rc = 0;
2104
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002105 const u32 fwmask = mask & ~hotkey_source_mask;
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -03002106
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002107 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002108 for (i = 0; i < 32; i++) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002109 if (!acpi_evalf(hkey_handle,
2110 NULL, "MHKM", "vdd", i + 1,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002111 !!(mask & (1 << i)))) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002112 rc = -EIO;
2113 break;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002114 }
2115 }
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002116 }
2117
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002118 /*
2119 * We *must* make an inconditional call to hotkey_mask_get to
2120 * refresh hotkey_acpi_mask and update hotkey_user_mask
2121 *
2122 * Take the opportunity to also log when we cannot _enable_
2123 * a given event.
2124 */
2125 if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
2126 printk(TPACPI_NOTICE
2127 "asked for hotkey mask 0x%08x, but "
2128 "firmware forced it to 0x%08x\n",
2129 fwmask, hotkey_acpi_mask);
2130 }
2131
2132 hotkey_mask_warn_incomplete_mask();
2133
2134 return rc;
2135}
2136
2137/*
2138 * Sets hotkey_user_mask and tries to set the firmware mask
2139 *
2140 * Call with hotkey_mutex held
2141 */
2142static int hotkey_user_mask_set(const u32 mask)
2143{
2144 int rc;
2145
2146 /* Give people a chance to notice they are doing something that
2147 * is bound to go boom on their users sooner or later */
2148 if (!tp_warned.hotkey_mask_ff &&
2149 (mask == 0xffff || mask == 0xffffff ||
2150 mask == 0xffffffff)) {
2151 tp_warned.hotkey_mask_ff = 1;
2152 printk(TPACPI_NOTICE
2153 "setting the hotkey mask to 0x%08x is likely "
2154 "not the best way to go about it\n", mask);
2155 printk(TPACPI_NOTICE
2156 "please consider using the driver defaults, "
2157 "and refer to up-to-date thinkpad-acpi "
2158 "documentation\n");
2159 }
2160
2161 /* Try to enable what the user asked for, plus whatever we need.
2162 * this syncs everything but won't enable bits in hotkey_user_mask */
2163 rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
2164
2165 /* Enable the available bits in hotkey_user_mask */
2166 hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
2167
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002168 return rc;
2169}
2170
2171static int hotkey_status_get(int *status)
2172{
2173 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
2174 return -EIO;
2175
2176 return 0;
2177}
2178
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002179static int hotkey_status_set(bool enable)
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002180{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002181 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002182 return -EIO;
2183
2184 return 0;
2185}
2186
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002187static void tpacpi_input_send_tabletsw(void)
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002188{
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002189 int state;
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002190
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002191 if (tp_features.hotkey_tablet &&
2192 !hotkey_get_tablet_mode(&state)) {
2193 mutex_lock(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002194
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002195 input_report_switch(tpacpi_inputdev,
2196 SW_TABLET_MODE, !!state);
2197 input_sync(tpacpi_inputdev);
2198
2199 mutex_unlock(&tpacpi_inputdev_send_mutex);
2200 }
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002201}
2202
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002203/* Do NOT call without validating scancode first */
2204static void tpacpi_input_send_key(const unsigned int scancode)
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002205{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002206 const unsigned int keycode = hotkey_keycode_map[scancode];
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002207
2208 if (keycode != KEY_RESERVED) {
2209 mutex_lock(&tpacpi_inputdev_send_mutex);
2210
2211 input_report_key(tpacpi_inputdev, keycode, 1);
2212 if (keycode == KEY_UNKNOWN)
2213 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
2214 scancode);
2215 input_sync(tpacpi_inputdev);
2216
2217 input_report_key(tpacpi_inputdev, keycode, 0);
2218 if (keycode == KEY_UNKNOWN)
2219 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
2220 scancode);
2221 input_sync(tpacpi_inputdev);
2222
2223 mutex_unlock(&tpacpi_inputdev_send_mutex);
2224 }
2225}
2226
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002227/* Do NOT call without validating scancode first */
2228static void tpacpi_input_send_key_masked(const unsigned int scancode)
2229{
2230 if (hotkey_user_mask & (1 << scancode))
2231 tpacpi_input_send_key(scancode);
2232}
2233
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002234#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2235static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
2236
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002237/* Do NOT call without validating scancode first */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002238static void tpacpi_hotkey_send_key(unsigned int scancode)
2239{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002240 tpacpi_input_send_key_masked(scancode);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002241 if (hotkey_report_mode < 2) {
2242 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
2243 0x80, 0x1001 + scancode);
2244 }
2245}
2246
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002247static void hotkey_read_nvram(struct tp_nvram_state *n, const u32 m)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002248{
2249 u8 d;
2250
2251 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
2252 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
2253 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
2254 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
2255 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
2256 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
2257 }
2258 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
2259 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
2260 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
2261 }
2262 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
2263 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
2264 n->displayexp_toggle =
2265 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
2266 }
2267 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
2268 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
2269 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
2270 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
2271 n->brightness_toggle =
2272 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
2273 }
2274 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
2275 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
2276 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
2277 >> TP_NVRAM_POS_LEVEL_VOLUME;
2278 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2279 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2280 }
2281}
2282
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002283static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
2284 struct tp_nvram_state *newn,
2285 const u32 event_mask)
2286{
2287
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002288#define TPACPI_COMPARE_KEY(__scancode, __member) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002289 do { \
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002290 if ((event_mask & (1 << __scancode)) && \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002291 oldn->__member != newn->__member) \
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002292 tpacpi_hotkey_send_key(__scancode); \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002293 } while (0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002294
2295#define TPACPI_MAY_SEND_KEY(__scancode) \
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002296 do { \
2297 if (event_mask & (1 << __scancode)) \
2298 tpacpi_hotkey_send_key(__scancode); \
2299 } while (0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002300
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002301 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2302 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2303 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2304 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2305
2306 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2307
2308 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2309
2310 /* handle volume */
2311 if (oldn->volume_toggle != newn->volume_toggle) {
2312 if (oldn->mute != newn->mute) {
2313 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2314 }
2315 if (oldn->volume_level > newn->volume_level) {
2316 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2317 } else if (oldn->volume_level < newn->volume_level) {
2318 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2319 } else if (oldn->mute == newn->mute) {
2320 /* repeated key presses that didn't change state */
2321 if (newn->mute) {
2322 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2323 } else if (newn->volume_level != 0) {
2324 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2325 } else {
2326 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2327 }
2328 }
2329 }
2330
2331 /* handle brightness */
2332 if (oldn->brightness_toggle != newn->brightness_toggle) {
2333 if (oldn->brightness_level < newn->brightness_level) {
2334 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2335 } else if (oldn->brightness_level > newn->brightness_level) {
2336 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2337 } else {
2338 /* repeated key presses that didn't change state */
2339 if (newn->brightness_level != 0) {
2340 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2341 } else {
2342 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2343 }
2344 }
2345 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002346
2347#undef TPACPI_COMPARE_KEY
2348#undef TPACPI_MAY_SEND_KEY
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002349}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002350
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002351/*
2352 * Polling driver
2353 *
2354 * We track all events in hotkey_source_mask all the time, since
2355 * most of them are edge-based. We only issue those requested by
2356 * hotkey_user_mask or hotkey_driver_mask, though.
2357 */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002358static int hotkey_kthread(void *data)
2359{
2360 struct tp_nvram_state s[2];
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002361 u32 poll_mask, event_mask;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002362 unsigned int si, so;
2363 unsigned long t;
2364 unsigned int change_detector, must_reset;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002365 unsigned int poll_freq;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002366
2367 mutex_lock(&hotkey_thread_mutex);
2368
2369 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2370 goto exit;
2371
2372 set_freezable();
2373
2374 so = 0;
2375 si = 1;
2376 t = 0;
2377
2378 /* Initial state for compares */
2379 mutex_lock(&hotkey_thread_data_mutex);
2380 change_detector = hotkey_config_change;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002381 poll_mask = hotkey_source_mask;
2382 event_mask = hotkey_source_mask &
2383 (hotkey_driver_mask | hotkey_user_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002384 poll_freq = hotkey_poll_freq;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002385 mutex_unlock(&hotkey_thread_data_mutex);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002386 hotkey_read_nvram(&s[so], poll_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002387
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002388 while (!kthread_should_stop()) {
2389 if (t == 0) {
2390 if (likely(poll_freq))
2391 t = 1000/poll_freq;
2392 else
2393 t = 100; /* should never happen... */
2394 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002395 t = msleep_interruptible(t);
2396 if (unlikely(kthread_should_stop()))
2397 break;
2398 must_reset = try_to_freeze();
2399 if (t > 0 && !must_reset)
2400 continue;
2401
2402 mutex_lock(&hotkey_thread_data_mutex);
2403 if (must_reset || hotkey_config_change != change_detector) {
2404 /* forget old state on thaw or config change */
2405 si = so;
2406 t = 0;
2407 change_detector = hotkey_config_change;
2408 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002409 poll_mask = hotkey_source_mask;
2410 event_mask = hotkey_source_mask &
2411 (hotkey_driver_mask | hotkey_user_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002412 poll_freq = hotkey_poll_freq;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002413 mutex_unlock(&hotkey_thread_data_mutex);
2414
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002415 if (likely(poll_mask)) {
2416 hotkey_read_nvram(&s[si], poll_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002417 if (likely(si != so)) {
2418 hotkey_compare_and_issue_event(&s[so], &s[si],
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002419 event_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002420 }
2421 }
2422
2423 so = si;
2424 si ^= 1;
2425 }
2426
2427exit:
2428 mutex_unlock(&hotkey_thread_mutex);
2429 return 0;
2430}
2431
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002432/* call with hotkey_mutex held */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002433static void hotkey_poll_stop_sync(void)
2434{
2435 if (tpacpi_hotkey_task) {
2436 if (frozen(tpacpi_hotkey_task) ||
2437 freezing(tpacpi_hotkey_task))
2438 thaw_process(tpacpi_hotkey_task);
2439
2440 kthread_stop(tpacpi_hotkey_task);
2441 tpacpi_hotkey_task = NULL;
2442 mutex_lock(&hotkey_thread_mutex);
2443 /* at this point, the thread did exit */
2444 mutex_unlock(&hotkey_thread_mutex);
2445 }
2446}
2447
2448/* call with hotkey_mutex held */
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002449static void hotkey_poll_setup(bool may_warn)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002450{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002451 const u32 poll_driver_mask = hotkey_driver_mask & hotkey_source_mask;
2452 const u32 poll_user_mask = hotkey_user_mask & hotkey_source_mask;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002453
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002454 if (hotkey_poll_freq > 0 &&
2455 (poll_driver_mask ||
2456 (poll_user_mask && tpacpi_inputdev->users > 0))) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002457 if (!tpacpi_hotkey_task) {
2458 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03002459 NULL, TPACPI_NVRAM_KTHREAD_NAME);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002460 if (IS_ERR(tpacpi_hotkey_task)) {
2461 tpacpi_hotkey_task = NULL;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002462 printk(TPACPI_ERR
2463 "could not create kernel thread "
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002464 "for hotkey polling\n");
2465 }
2466 }
2467 } else {
2468 hotkey_poll_stop_sync();
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002469 if (may_warn && (poll_driver_mask || poll_user_mask) &&
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002470 hotkey_poll_freq == 0) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002471 printk(TPACPI_NOTICE
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002472 "hot keys 0x%08x and/or events 0x%08x "
2473 "require polling, which is currently "
2474 "disabled\n",
2475 poll_user_mask, poll_driver_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002476 }
2477 }
2478}
2479
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002480static void hotkey_poll_setup_safe(bool may_warn)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002481{
2482 mutex_lock(&hotkey_mutex);
2483 hotkey_poll_setup(may_warn);
2484 mutex_unlock(&hotkey_mutex);
2485}
2486
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002487/* call with hotkey_mutex held */
2488static void hotkey_poll_set_freq(unsigned int freq)
2489{
2490 if (!freq)
2491 hotkey_poll_stop_sync();
2492
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002493 hotkey_poll_freq = freq;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002494}
2495
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02002496#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2497
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002498static void hotkey_poll_setup_safe(bool __unused)
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02002499{
2500}
2501
2502#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2503
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002504static int hotkey_inputdev_open(struct input_dev *dev)
2505{
2506 switch (tpacpi_lifecycle) {
2507 case TPACPI_LIFE_INIT:
2508 /*
2509 * hotkey_init will call hotkey_poll_setup_safe
2510 * at the appropriate moment
2511 */
2512 return 0;
2513 case TPACPI_LIFE_EXITING:
2514 return -EBUSY;
2515 case TPACPI_LIFE_RUNNING:
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002516 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002517 return 0;
2518 }
2519
2520 /* Should only happen if tpacpi_lifecycle is corrupt */
2521 BUG();
2522 return -EBUSY;
2523}
2524
2525static void hotkey_inputdev_close(struct input_dev *dev)
2526{
2527 /* disable hotkey polling when possible */
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002528 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING &&
2529 !(hotkey_source_mask & hotkey_driver_mask))
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002530 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002531}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002532
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002533/* sysfs hotkey enable ------------------------------------------------- */
2534static ssize_t hotkey_enable_show(struct device *dev,
2535 struct device_attribute *attr,
2536 char *buf)
2537{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002538 int res, status;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002539
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002540 printk_deprecated_attribute("hotkey_enable",
2541 "Hotkey reporting is always enabled");
2542
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002543 res = hotkey_status_get(&status);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002544 if (res)
2545 return res;
2546
2547 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2548}
2549
2550static ssize_t hotkey_enable_store(struct device *dev,
2551 struct device_attribute *attr,
2552 const char *buf, size_t count)
2553{
2554 unsigned long t;
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002555
2556 printk_deprecated_attribute("hotkey_enable",
2557 "Hotkeys can be disabled through hotkey_mask");
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002558
2559 if (parse_strtoul(buf, 1, &t))
2560 return -EINVAL;
2561
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002562 if (t == 0)
2563 return -EPERM;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002564
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002565 return count;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002566}
2567
2568static struct device_attribute dev_attr_hotkey_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002569 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002570 hotkey_enable_show, hotkey_enable_store);
2571
2572/* sysfs hotkey mask --------------------------------------------------- */
2573static ssize_t hotkey_mask_show(struct device *dev,
2574 struct device_attribute *attr,
2575 char *buf)
2576{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002577 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_user_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002578}
2579
2580static ssize_t hotkey_mask_store(struct device *dev,
2581 struct device_attribute *attr,
2582 const char *buf, size_t count)
2583{
2584 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002585 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002586
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002587 if (parse_strtoul(buf, 0xffffffffUL, &t))
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002588 return -EINVAL;
2589
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002590 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002591 return -ERESTARTSYS;
2592
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002593 res = hotkey_user_mask_set(t);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002594
2595#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002596 hotkey_poll_setup(true);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002597#endif
2598
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002599 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002600
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002601 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2602
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002603 return (res) ? res : count;
2604}
2605
2606static struct device_attribute dev_attr_hotkey_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002607 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002608 hotkey_mask_show, hotkey_mask_store);
2609
2610/* sysfs hotkey bios_enabled ------------------------------------------- */
2611static ssize_t hotkey_bios_enabled_show(struct device *dev,
2612 struct device_attribute *attr,
2613 char *buf)
2614{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002615 return sprintf(buf, "0\n");
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002616}
2617
2618static struct device_attribute dev_attr_hotkey_bios_enabled =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002619 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002620
2621/* sysfs hotkey bios_mask ---------------------------------------------- */
2622static ssize_t hotkey_bios_mask_show(struct device *dev,
2623 struct device_attribute *attr,
2624 char *buf)
2625{
Henrique de Moraes Holschuh06777be2009-09-12 15:22:15 -03002626 printk_deprecated_attribute("hotkey_bios_mask",
2627 "This attribute is useless.");
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002628 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002629}
2630
2631static struct device_attribute dev_attr_hotkey_bios_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002632 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002633
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002634/* sysfs hotkey all_mask ----------------------------------------------- */
2635static ssize_t hotkey_all_mask_show(struct device *dev,
2636 struct device_attribute *attr,
2637 char *buf)
2638{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002639 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2640 hotkey_all_mask | hotkey_source_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002641}
2642
2643static struct device_attribute dev_attr_hotkey_all_mask =
2644 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
2645
2646/* sysfs hotkey recommended_mask --------------------------------------- */
2647static ssize_t hotkey_recommended_mask_show(struct device *dev,
2648 struct device_attribute *attr,
2649 char *buf)
2650{
2651 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002652 (hotkey_all_mask | hotkey_source_mask)
2653 & ~hotkey_reserved_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002654}
2655
2656static struct device_attribute dev_attr_hotkey_recommended_mask =
2657 __ATTR(hotkey_recommended_mask, S_IRUGO,
2658 hotkey_recommended_mask_show, NULL);
2659
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002660#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2661
2662/* sysfs hotkey hotkey_source_mask ------------------------------------- */
2663static ssize_t hotkey_source_mask_show(struct device *dev,
2664 struct device_attribute *attr,
2665 char *buf)
2666{
2667 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2668}
2669
2670static ssize_t hotkey_source_mask_store(struct device *dev,
2671 struct device_attribute *attr,
2672 const char *buf, size_t count)
2673{
2674 unsigned long t;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002675 u32 r_ev;
2676 int rc;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002677
2678 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2679 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2680 return -EINVAL;
2681
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002682 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002683 return -ERESTARTSYS;
2684
2685 HOTKEY_CONFIG_CRITICAL_START
2686 hotkey_source_mask = t;
2687 HOTKEY_CONFIG_CRITICAL_END
2688
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002689 rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
2690 ~hotkey_source_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002691 hotkey_poll_setup(true);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002692
2693 /* check if events needed by the driver got disabled */
2694 r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
2695 & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002696
2697 mutex_unlock(&hotkey_mutex);
2698
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002699 if (rc < 0)
2700 printk(TPACPI_ERR "hotkey_source_mask: failed to update the"
2701 "firmware event mask!\n");
2702
2703 if (r_ev)
2704 printk(TPACPI_NOTICE "hotkey_source_mask: "
2705 "some important events were disabled: "
2706 "0x%04x\n", r_ev);
2707
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002708 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2709
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002710 return (rc < 0) ? rc : count;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002711}
2712
2713static struct device_attribute dev_attr_hotkey_source_mask =
2714 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2715 hotkey_source_mask_show, hotkey_source_mask_store);
2716
2717/* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2718static ssize_t hotkey_poll_freq_show(struct device *dev,
2719 struct device_attribute *attr,
2720 char *buf)
2721{
2722 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2723}
2724
2725static ssize_t hotkey_poll_freq_store(struct device *dev,
2726 struct device_attribute *attr,
2727 const char *buf, size_t count)
2728{
2729 unsigned long t;
2730
2731 if (parse_strtoul(buf, 25, &t))
2732 return -EINVAL;
2733
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002734 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002735 return -ERESTARTSYS;
2736
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002737 hotkey_poll_set_freq(t);
2738 hotkey_poll_setup(true);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002739
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002740 mutex_unlock(&hotkey_mutex);
2741
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002742 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2743
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002744 return count;
2745}
2746
2747static struct device_attribute dev_attr_hotkey_poll_freq =
2748 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2749 hotkey_poll_freq_show, hotkey_poll_freq_store);
2750
2751#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2752
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002753/* sysfs hotkey radio_sw (pollable) ------------------------------------ */
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002754static ssize_t hotkey_radio_sw_show(struct device *dev,
2755 struct device_attribute *attr,
2756 char *buf)
2757{
Johannes Berg19d337d2009-06-02 13:01:37 +02002758 int res;
2759 res = hotkey_get_wlsw();
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002760 if (res < 0)
2761 return res;
2762
Johannes Berg19d337d2009-06-02 13:01:37 +02002763 /* Opportunistic update */
2764 tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
2765
2766 return snprintf(buf, PAGE_SIZE, "%d\n",
2767 (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002768}
2769
2770static struct device_attribute dev_attr_hotkey_radio_sw =
2771 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2772
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002773static void hotkey_radio_sw_notify_change(void)
2774{
2775 if (tp_features.hotkey_wlsw)
2776 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2777 "hotkey_radio_sw");
2778}
2779
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002780/* sysfs hotkey tablet mode (pollable) --------------------------------- */
2781static ssize_t hotkey_tablet_mode_show(struct device *dev,
2782 struct device_attribute *attr,
2783 char *buf)
2784{
2785 int res, s;
2786 res = hotkey_get_tablet_mode(&s);
2787 if (res < 0)
2788 return res;
2789
2790 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2791}
2792
2793static struct device_attribute dev_attr_hotkey_tablet_mode =
2794 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2795
2796static void hotkey_tablet_mode_notify_change(void)
2797{
2798 if (tp_features.hotkey_tablet)
2799 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2800 "hotkey_tablet_mode");
2801}
2802
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002803/* sysfs hotkey report_mode -------------------------------------------- */
2804static ssize_t hotkey_report_mode_show(struct device *dev,
2805 struct device_attribute *attr,
2806 char *buf)
2807{
2808 return snprintf(buf, PAGE_SIZE, "%d\n",
2809 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2810}
2811
2812static struct device_attribute dev_attr_hotkey_report_mode =
2813 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2814
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002815/* sysfs wakeup reason (pollable) -------------------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002816static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2817 struct device_attribute *attr,
2818 char *buf)
2819{
2820 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2821}
2822
2823static struct device_attribute dev_attr_hotkey_wakeup_reason =
2824 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2825
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02002826static void hotkey_wakeup_reason_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002827{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002828 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2829 "wakeup_reason");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002830}
2831
2832/* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002833static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2834 struct device_attribute *attr,
2835 char *buf)
2836{
2837 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2838}
2839
2840static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2841 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2842 hotkey_wakeup_hotunplug_complete_show, NULL);
2843
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02002844static void hotkey_wakeup_hotunplug_complete_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002845{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002846 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2847 "wakeup_hotunplug_complete");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002848}
2849
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002850/* --------------------------------------------------------------------- */
2851
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002852static struct attribute *hotkey_attributes[] __initdata = {
2853 &dev_attr_hotkey_enable.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002854 &dev_attr_hotkey_bios_enabled.attr,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002855 &dev_attr_hotkey_bios_mask.attr,
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002856 &dev_attr_hotkey_report_mode.attr,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002857 &dev_attr_hotkey_wakeup_reason.attr,
2858 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002859 &dev_attr_hotkey_mask.attr,
2860 &dev_attr_hotkey_all_mask.attr,
2861 &dev_attr_hotkey_recommended_mask.attr,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002862#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002863 &dev_attr_hotkey_source_mask.attr,
2864 &dev_attr_hotkey_poll_freq.attr,
2865#endif
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002866};
2867
Johannes Berg19d337d2009-06-02 13:01:37 +02002868/*
2869 * Sync both the hw and sw blocking state of all switches
2870 */
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002871static void tpacpi_send_radiosw_update(void)
2872{
2873 int wlsw;
2874
Johannes Berg19d337d2009-06-02 13:01:37 +02002875 /*
2876 * We must sync all rfkill controllers *before* issuing any
2877 * rfkill input events, or we will race the rfkill core input
2878 * handler.
2879 *
2880 * tpacpi_inputdev_send_mutex works as a syncronization point
2881 * for the above.
2882 *
2883 * We optimize to avoid numerous calls to hotkey_get_wlsw.
2884 */
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002885
Johannes Berg19d337d2009-06-02 13:01:37 +02002886 wlsw = hotkey_get_wlsw();
2887
2888 /* Sync hw blocking state first if it is hw-blocked */
2889 if (wlsw == TPACPI_RFK_RADIO_OFF)
2890 tpacpi_rfk_update_hwblock_state(true);
2891
2892 /* Sync sw blocking state */
2893 tpacpi_rfk_update_swstate_all();
2894
2895 /* Sync hw blocking state last if it is hw-unblocked */
2896 if (wlsw == TPACPI_RFK_RADIO_ON)
2897 tpacpi_rfk_update_hwblock_state(false);
2898
2899 /* Issue rfkill input event for WLSW switch */
2900 if (!(wlsw < 0)) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002901 mutex_lock(&tpacpi_inputdev_send_mutex);
2902
2903 input_report_switch(tpacpi_inputdev,
Johannes Berg19d337d2009-06-02 13:01:37 +02002904 SW_RFKILL_ALL, (wlsw > 0));
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002905 input_sync(tpacpi_inputdev);
2906
2907 mutex_unlock(&tpacpi_inputdev_send_mutex);
2908 }
Johannes Berg19d337d2009-06-02 13:01:37 +02002909
2910 /*
2911 * this can be unconditional, as we will poll state again
2912 * if userspace uses the notify to read data
2913 */
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002914 hotkey_radio_sw_notify_change();
2915}
2916
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002917static void hotkey_exit(void)
2918{
2919#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002920 mutex_lock(&hotkey_mutex);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002921 hotkey_poll_stop_sync();
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002922 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002923#endif
2924
2925 if (hotkey_dev_attributes)
2926 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2927
2928 kfree(hotkey_keycode_map);
2929
Henrique de Moraes Holschuh4be73002009-09-20 14:09:23 -03002930 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002931 "restoring original HKEY status and mask\n");
2932 /* yes, there is a bitwise or below, we want the
2933 * functions to be called even if one of them fail */
2934 if (((tp_features.hotkey_mask &&
2935 hotkey_mask_set(hotkey_orig_mask)) |
2936 hotkey_status_set(false)) != 0)
Henrique de Moraes Holschuh4be73002009-09-20 14:09:23 -03002937 printk(TPACPI_ERR
2938 "failed to restore hot key mask "
2939 "to BIOS defaults\n");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002940}
2941
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03002942static void __init hotkey_unmap(const unsigned int scancode)
2943{
2944 if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
2945 clear_bit(hotkey_keycode_map[scancode],
2946 tpacpi_inputdev->keybit);
2947 hotkey_keycode_map[scancode] = KEY_RESERVED;
2948 }
2949}
2950
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002951/*
2952 * HKEY quirks:
2953 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
2954 */
2955
2956#define TPACPI_HK_Q_INIMASK 0x0001
2957
2958static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
2959 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
2960 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
2961 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
2962 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
2963 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
2964 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
2965 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
2966 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
2967 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
2968 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
2969 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
2970 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
2971 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
2972 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
2973 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
2974 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
2975 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
2976 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
2977 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
2978};
2979
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002980static int __init hotkey_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002981{
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002982 /* Requirements for changing the default keymaps:
2983 *
2984 * 1. Many of the keys are mapped to KEY_RESERVED for very
2985 * good reasons. Do not change them unless you have deep
2986 * knowledge on the IBM and Lenovo ThinkPad firmware for
2987 * the various ThinkPad models. The driver behaves
2988 * differently for KEY_RESERVED: such keys have their
2989 * hot key mask *unset* in mask_recommended, and also
2990 * in the initial hot key mask programmed into the
2991 * firmware at driver load time, which means the firm-
2992 * ware may react very differently if you change them to
2993 * something else;
2994 *
2995 * 2. You must be subscribed to the linux-thinkpad and
2996 * ibm-acpi-devel mailing lists, and you should read the
2997 * list archives since 2007 if you want to change the
2998 * keymaps. This requirement exists so that you will
2999 * know the past history of problems with the thinkpad-
3000 * acpi driver keymaps, and also that you will be
3001 * listening to any bug reports;
3002 *
3003 * 3. Do not send thinkpad-acpi specific patches directly to
3004 * for merging, *ever*. Send them to the linux-acpi
3005 * mailinglist for comments. Merging is to be done only
3006 * through acpi-test and the ACPI maintainer.
3007 *
3008 * If the above is too much to ask, don't change the keymap.
3009 * Ask the thinkpad-acpi maintainer to do it, instead.
3010 */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003011 static u16 ibm_keycode_map[] __initdata = {
3012 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3013 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
3014 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3015 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003016
3017 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003018 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3019 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3020 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003021
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003022 /* brightness: firmware always reacts to them */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003023 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003024 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003025
3026 /* Thinklight: firmware always react to it */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003027 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003028
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003029 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3030 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003031
3032 /* Volume: firmware always react to it and reprograms
3033 * the built-in *extra* mixer. Never map it to control
3034 * another mixer by default. */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003035 KEY_RESERVED, /* 0x14: VOLUME UP */
3036 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3037 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003038
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003039 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003040
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003041 /* (assignments unknown, please report if found) */
3042 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3043 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3044 };
3045 static u16 lenovo_keycode_map[] __initdata = {
3046 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3047 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
3048 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3049 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003050
3051 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003052 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3053 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3054 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003055
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003056 /* These should be enabled --only-- when ACPI video
3057 * is disabled (i.e. in "vendor" mode), and are handled
3058 * in a special way by the init code */
3059 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
3060 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003061
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003062 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003063
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003064 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3065 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003066
3067 /* Volume: z60/z61, T60 (BIOS version?): firmware always
3068 * react to it and reprograms the built-in *extra* mixer.
3069 * Never map it to control another mixer by default.
3070 *
3071 * T60?, T61, R60?, R61: firmware and EC tries to send
3072 * these over the regular keyboard, so these are no-ops,
3073 * but there are still weird bugs re. MUTE, so do not
3074 * change unless you get test reports from all Lenovo
3075 * models. May cause the BIOS to interfere with the
3076 * HDA mixer.
3077 */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003078 KEY_RESERVED, /* 0x14: VOLUME UP */
3079 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3080 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003081
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003082 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003083
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003084 /* (assignments unknown, please report if found) */
3085 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3086 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3087 };
3088
3089#define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
3090#define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
3091#define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
3092
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003093 int res, i;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003094 int status;
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03003095 int hkeyv;
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03003096
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003097 unsigned long quirks;
3098
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003099 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3100 "initializing hotkey subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003101
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003102 BUG_ON(!tpacpi_inputdev);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003103 BUG_ON(tpacpi_inputdev->open != NULL ||
3104 tpacpi_inputdev->close != NULL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003105
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003106 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003107 mutex_init(&hotkey_mutex);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003108
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003109#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3110 mutex_init(&hotkey_thread_mutex);
3111 mutex_init(&hotkey_thread_data_mutex);
3112#endif
3113
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003114 /* hotkey not supported on 570 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003115 tp_features.hotkey = hkey_handle != NULL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003116
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003117 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3118 "hotkeys are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003119 str_supported(tp_features.hotkey));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003120
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003121 if (!tp_features.hotkey)
3122 return 1;
3123
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003124 quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
3125 ARRAY_SIZE(tpacpi_hotkey_qtable));
3126
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03003127 tpacpi_disable_brightness_delay();
3128
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003129 /* MUST have enough space for all attributes to be added to
3130 * hotkey_dev_attributes */
3131 hotkey_dev_attributes = create_attr_set(
3132 ARRAY_SIZE(hotkey_attributes) + 2,
3133 NULL);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003134 if (!hotkey_dev_attributes)
3135 return -ENOMEM;
3136 res = add_many_to_attr_set(hotkey_dev_attributes,
3137 hotkey_attributes,
3138 ARRAY_SIZE(hotkey_attributes));
3139 if (res)
3140 goto err_exit;
3141
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003142 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003143 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
3144 for HKEY interface version 0x100 */
3145 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
3146 if ((hkeyv >> 8) != 1) {
3147 printk(TPACPI_ERR "unknown version of the "
3148 "HKEY interface: 0x%x\n", hkeyv);
3149 printk(TPACPI_ERR "please report this to %s\n",
3150 TPACPI_MAIL);
3151 } else {
3152 /*
3153 * MHKV 0x100 in A31, R40, R40e,
3154 * T4x, X31, and later
3155 */
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003156 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3157 "firmware HKEY interface version: 0x%x\n",
3158 hkeyv);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003159
3160 /* Paranoia check AND init hotkey_all_mask */
3161 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3162 "MHKA", "qd")) {
3163 printk(TPACPI_ERR
3164 "missing MHKA handler, "
3165 "please report this to %s\n",
3166 TPACPI_MAIL);
3167 /* Fallback: pre-init for FN+F3,F4,F12 */
3168 hotkey_all_mask = 0x080cU;
3169 } else {
3170 tp_features.hotkey_mask = 1;
3171 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003172 }
3173 }
3174
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003175 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3176 "hotkey masks are %s\n",
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003177 str_supported(tp_features.hotkey_mask));
3178
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003179 /* Init hotkey_all_mask if not initialized yet */
3180 if (!tp_features.hotkey_mask && !hotkey_all_mask &&
3181 (quirks & TPACPI_HK_Q_INIMASK))
3182 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003183
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003184 /* Init hotkey_acpi_mask and hotkey_orig_mask */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003185 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003186 /* hotkey_source_mask *must* be zero for
3187 * the first hotkey_mask_get to return hotkey_orig_mask */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003188 res = hotkey_mask_get();
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003189 if (res)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003190 goto err_exit;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003191
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003192 hotkey_orig_mask = hotkey_acpi_mask;
3193 } else {
3194 hotkey_orig_mask = hotkey_all_mask;
3195 hotkey_acpi_mask = hotkey_all_mask;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003196 }
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003197
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003198#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3199 if (dbg_wlswemul) {
3200 tp_features.hotkey_wlsw = 1;
3201 printk(TPACPI_INFO
3202 "radio switch emulation enabled\n");
3203 } else
3204#endif
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003205 /* Not all thinkpads have a hardware radio switch */
3206 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
3207 tp_features.hotkey_wlsw = 1;
3208 printk(TPACPI_INFO
3209 "radio switch found; radios are %s\n",
3210 enabled(status, 0));
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003211 }
3212 if (tp_features.hotkey_wlsw)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003213 res = add_to_attr_set(hotkey_dev_attributes,
3214 &dev_attr_hotkey_radio_sw.attr);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003215
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003216 /* For X41t, X60t, X61t Tablets... */
3217 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
3218 tp_features.hotkey_tablet = 1;
3219 printk(TPACPI_INFO
3220 "possible tablet mode switch found; "
3221 "ThinkPad in %s mode\n",
3222 (status & TP_HOTKEY_TABLET_MASK)?
3223 "tablet" : "laptop");
3224 res = add_to_attr_set(hotkey_dev_attributes,
3225 &dev_attr_hotkey_tablet_mode.attr);
3226 }
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02003227
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003228 if (!res)
3229 res = register_attr_set_with_sysfs(
3230 hotkey_dev_attributes,
3231 &tpacpi_pdev->dev.kobj);
3232 if (res)
3233 goto err_exit;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003234
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003235 /* Set up key map */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003236
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003237 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
3238 GFP_KERNEL);
3239 if (!hotkey_keycode_map) {
3240 printk(TPACPI_ERR
3241 "failed to allocate memory for key map\n");
3242 res = -ENOMEM;
3243 goto err_exit;
3244 }
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003245
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003246 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003247 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003248 "using Lenovo default hot key map\n");
3249 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
3250 TPACPI_HOTKEY_MAP_SIZE);
3251 } else {
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003252 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003253 "using IBM default hot key map\n");
3254 memcpy(hotkey_keycode_map, &ibm_keycode_map,
3255 TPACPI_HOTKEY_MAP_SIZE);
3256 }
3257
3258 set_bit(EV_KEY, tpacpi_inputdev->evbit);
3259 set_bit(EV_MSC, tpacpi_inputdev->evbit);
3260 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
3261 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
3262 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3263 tpacpi_inputdev->keycode = hotkey_keycode_map;
3264 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3265 if (hotkey_keycode_map[i] != KEY_RESERVED) {
3266 set_bit(hotkey_keycode_map[i],
3267 tpacpi_inputdev->keybit);
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003268 } else {
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003269 if (i < sizeof(hotkey_reserved_mask)*8)
3270 hotkey_reserved_mask |= 1 << i;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003271 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003272 }
3273
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003274 if (tp_features.hotkey_wlsw) {
3275 set_bit(EV_SW, tpacpi_inputdev->evbit);
3276 set_bit(SW_RFKILL_ALL, tpacpi_inputdev->swbit);
3277 }
3278 if (tp_features.hotkey_tablet) {
3279 set_bit(EV_SW, tpacpi_inputdev->evbit);
3280 set_bit(SW_TABLET_MODE, tpacpi_inputdev->swbit);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003281 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003282
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003283 /* Do not issue duplicate brightness change events to
3284 * userspace */
3285 if (!tp_features.bright_acpimode)
3286 /* update bright_acpimode... */
3287 tpacpi_check_std_acpi_brightness_support();
3288
Henrique de Moraes Holschuh8bf3d4c2009-05-30 13:25:09 -03003289 if (tp_features.bright_acpimode && acpi_video_backlight_support()) {
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003290 printk(TPACPI_INFO
3291 "This ThinkPad has standard ACPI backlight "
3292 "brightness control, supported by the ACPI "
3293 "video driver\n");
3294 printk(TPACPI_NOTICE
3295 "Disabling thinkpad-acpi brightness events "
3296 "by default...\n");
3297
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003298 /* Disable brightness up/down on Lenovo thinkpads when
3299 * ACPI is handling them, otherwise it is plain impossible
3300 * for userspace to do something even remotely sane */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003301 hotkey_reserved_mask |=
3302 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3303 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003304 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
3305 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003306 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003307
Henrique de Moraes Holschuh230d8cf252009-09-12 15:22:17 -03003308#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003309 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3310 & ~hotkey_all_mask
3311 & ~hotkey_reserved_mask;
Henrique de Moraes Holschuh230d8cf252009-09-12 15:22:17 -03003312
3313 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3314 "hotkey source mask 0x%08x, polling freq %u\n",
3315 hotkey_source_mask, hotkey_poll_freq);
3316#endif
3317
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003318 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3319 "enabling firmware HKEY event interface...\n");
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003320 res = hotkey_status_set(true);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003321 if (res) {
3322 hotkey_exit();
3323 return res;
3324 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003325 res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
3326 | hotkey_driver_mask)
3327 & ~hotkey_source_mask);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003328 if (res < 0 && res != -ENXIO) {
3329 hotkey_exit();
3330 return res;
3331 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003332 hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
3333 & ~hotkey_reserved_mask;
3334 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3335 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3336 hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003337
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003338 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3339 "legacy ibm/hotkey event reporting over procfs %s\n",
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003340 (hotkey_report_mode < 2) ?
3341 "enabled" : "disabled");
3342
3343 tpacpi_inputdev->open = &hotkey_inputdev_open;
3344 tpacpi_inputdev->close = &hotkey_inputdev_close;
3345
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003346 hotkey_poll_setup_safe(true);
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003347 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003348 tpacpi_input_send_tabletsw();
3349
3350 return 0;
3351
3352err_exit:
3353 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3354 hotkey_dev_attributes = NULL;
3355
3356 return (res < 0)? res : 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003357}
3358
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003359static bool hotkey_notify_hotkey(const u32 hkey,
3360 bool *send_acpi_ev,
3361 bool *ignore_acpi_ev)
3362{
3363 /* 0x1000-0x1FFF: key presses */
3364 unsigned int scancode = hkey & 0xfff;
3365 *send_acpi_ev = true;
3366 *ignore_acpi_ev = false;
3367
3368 if (scancode > 0 && scancode < 0x21) {
3369 scancode--;
3370 if (!(hotkey_source_mask & (1 << scancode))) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003371 tpacpi_input_send_key_masked(scancode);
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003372 *send_acpi_ev = false;
3373 } else {
3374 *ignore_acpi_ev = true;
3375 }
3376 return true;
3377 }
3378 return false;
3379}
3380
3381static bool hotkey_notify_wakeup(const u32 hkey,
3382 bool *send_acpi_ev,
3383 bool *ignore_acpi_ev)
3384{
3385 /* 0x2000-0x2FFF: Wakeup reason */
3386 *send_acpi_ev = true;
3387 *ignore_acpi_ev = false;
3388
3389 switch (hkey) {
3390 case 0x2304: /* suspend, undock */
3391 case 0x2404: /* hibernation, undock */
3392 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3393 *ignore_acpi_ev = true;
3394 break;
3395
3396 case 0x2305: /* suspend, bay eject */
3397 case 0x2405: /* hibernation, bay eject */
3398 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3399 *ignore_acpi_ev = true;
3400 break;
3401
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003402 case 0x2313: /* Battery on critical low level (S3) */
3403 case 0x2413: /* Battery on critical low level (S4) */
3404 printk(TPACPI_ALERT
3405 "EMERGENCY WAKEUP: battery almost empty\n");
3406 /* how to auto-heal: */
3407 /* 2313: woke up from S3, go to S4/S5 */
3408 /* 2413: woke up from S4, go to S5 */
3409 break;
3410
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003411 default:
3412 return false;
3413 }
3414
3415 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
3416 printk(TPACPI_INFO
3417 "woke up due to a hot-unplug "
3418 "request...\n");
3419 hotkey_wakeup_reason_notify_change();
3420 }
3421 return true;
3422}
3423
3424static bool hotkey_notify_usrevent(const u32 hkey,
3425 bool *send_acpi_ev,
3426 bool *ignore_acpi_ev)
3427{
3428 /* 0x5000-0x5FFF: human interface helpers */
3429 *send_acpi_ev = true;
3430 *ignore_acpi_ev = false;
3431
3432 switch (hkey) {
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003433 case 0x500b: /* X61t: tablet pen inserted into bay */
3434 case 0x500c: /* X61t: tablet pen removed from bay */
3435 return true;
3436
3437 case 0x5009: /* X41t-X61t: swivel up (tablet mode) */
3438 case 0x500a: /* X41t-X61t: swivel down (normal mode) */
3439 tpacpi_input_send_tabletsw();
3440 hotkey_tablet_mode_notify_change();
3441 *send_acpi_ev = false;
3442 return true;
3443
Henrique de Moraes Holschuh176dd982009-09-20 14:09:24 -03003444 case 0x5001: /* Lid close */
3445 case 0x5002: /* Lid open */
3446 case 0x5010: /* brightness control */
3447 /* do not propagate these events */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003448 *ignore_acpi_ev = true;
3449 return true;
3450
3451 default:
3452 return false;
3453 }
3454}
3455
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003456static bool hotkey_notify_thermal(const u32 hkey,
3457 bool *send_acpi_ev,
3458 bool *ignore_acpi_ev)
3459{
3460 /* 0x6000-0x6FFF: thermal alarms */
3461 *send_acpi_ev = true;
3462 *ignore_acpi_ev = false;
3463
3464 switch (hkey) {
3465 case 0x6011:
3466 printk(TPACPI_CRIT
3467 "THERMAL ALARM: battery is too hot!\n");
3468 /* recommended action: warn user through gui */
3469 return true;
3470 case 0x6012:
3471 printk(TPACPI_ALERT
3472 "THERMAL EMERGENCY: battery is extremely hot!\n");
3473 /* recommended action: immediate sleep/hibernate */
3474 return true;
3475 case 0x6021:
3476 printk(TPACPI_CRIT
3477 "THERMAL ALARM: "
3478 "a sensor reports something is too hot!\n");
3479 /* recommended action: warn user through gui, that */
3480 /* some internal component is too hot */
3481 return true;
3482 case 0x6022:
3483 printk(TPACPI_ALERT
3484 "THERMAL EMERGENCY: "
3485 "a sensor reports something is extremely hot!\n");
3486 /* recommended action: immediate sleep/hibernate */
3487 return true;
Henrique de Moraes Holschuh54926ce2009-01-11 03:01:09 -02003488 case 0x6030:
3489 printk(TPACPI_INFO
3490 "EC reports that Thermal Table has changed\n");
3491 /* recommended action: do nothing, we don't have
3492 * Lenovo ATM information */
3493 return true;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003494 default:
3495 printk(TPACPI_ALERT
3496 "THERMAL ALERT: unknown thermal alarm received\n");
3497 return false;
3498 }
3499}
3500
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003501static void hotkey_notify(struct ibm_struct *ibm, u32 event)
3502{
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003503 u32 hkey;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003504 bool send_acpi_ev;
3505 bool ignore_acpi_ev;
3506 bool known_ev;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003507
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003508 if (event != 0x80) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003509 printk(TPACPI_ERR
3510 "unknown HKEY notification event %d\n", event);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003511 /* forward it to userspace, maybe it knows how to handle it */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003512 acpi_bus_generate_netlink_event(
3513 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02003514 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003515 event, 0);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003516 return;
3517 }
3518
3519 while (1) {
3520 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003521 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003522 return;
3523 }
3524
3525 if (hkey == 0) {
3526 /* queue empty */
3527 return;
3528 }
3529
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003530 send_acpi_ev = true;
3531 ignore_acpi_ev = false;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003532
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003533 switch (hkey >> 12) {
3534 case 1:
3535 /* 0x1000-0x1FFF: key presses */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003536 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
3537 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003538 break;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003539 case 2:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003540 /* 0x2000-0x2FFF: Wakeup reason */
3541 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
3542 &ignore_acpi_ev);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003543 break;
3544 case 3:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003545 /* 0x3000-0x3FFF: bay-related wakeups */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003546 if (hkey == 0x3003) {
3547 hotkey_autosleep_ack = 1;
3548 printk(TPACPI_INFO
3549 "bay ejected\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003550 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003551 known_ev = true;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003552 } else {
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003553 known_ev = false;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003554 }
3555 break;
3556 case 4:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003557 /* 0x4000-0x4FFF: dock-related wakeups */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003558 if (hkey == 0x4003) {
3559 hotkey_autosleep_ack = 1;
3560 printk(TPACPI_INFO
3561 "undocked\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003562 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003563 known_ev = true;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003564 } else {
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003565 known_ev = false;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003566 }
3567 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003568 case 5:
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02003569 /* 0x5000-0x5FFF: human interface helpers */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003570 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
3571 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003572 break;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003573 case 6:
3574 /* 0x6000-0x6FFF: thermal alarms */
3575 known_ev = hotkey_notify_thermal(hkey, &send_acpi_ev,
3576 &ignore_acpi_ev);
3577 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003578 case 7:
3579 /* 0x7000-0x7FFF: misc */
3580 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003581 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02003582 send_acpi_ev = 0;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003583 known_ev = true;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003584 break;
3585 }
3586 /* fallthrough to default */
3587 default:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003588 known_ev = false;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02003589 }
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003590 if (!known_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003591 printk(TPACPI_NOTICE
3592 "unhandled HKEY event 0x%04x\n", hkey);
Henrique de Moraes Holschuhcb429352009-01-11 03:01:07 -02003593 printk(TPACPI_NOTICE
3594 "please report the conditions when this "
3595 "event happened to %s\n", TPACPI_MAIL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003596 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003597
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003598 /* Legacy events */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003599 if (!ignore_acpi_ev &&
3600 (send_acpi_ev || hotkey_report_mode < 2)) {
3601 acpi_bus_generate_proc_event(ibm->acpi->device,
3602 event, hkey);
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03003603 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003604
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003605 /* netlink events */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03003606 if (!ignore_acpi_ev && send_acpi_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003607 acpi_bus_generate_netlink_event(
3608 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02003609 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003610 event, hkey);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003611 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003612 }
3613}
3614
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003615static void hotkey_suspend(pm_message_t state)
3616{
3617 /* Do these on suspend, we get the events on early resume! */
3618 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
3619 hotkey_autosleep_ack = 0;
3620}
3621
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03003622static void hotkey_resume(void)
3623{
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03003624 tpacpi_disable_brightness_delay();
3625
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003626 if (hotkey_status_set(true) < 0 ||
3627 hotkey_mask_set(hotkey_acpi_mask) < 0)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003628 printk(TPACPI_ERR
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003629 "error while attempting to reset the event "
3630 "firmware interface\n");
3631
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003632 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02003633 hotkey_tablet_mode_notify_change();
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003634 hotkey_wakeup_reason_notify_change();
3635 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003636 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03003637}
3638
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003639/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003640static int hotkey_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03003642 int res, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643 int len = 0;
3644
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003645 if (!tp_features.hotkey) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003646 len += sprintf(p + len, "status:\t\tnot supported\n");
3647 return len;
3648 }
3649
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02003650 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02003651 return -ERESTARTSYS;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02003652 res = hotkey_status_get(&status);
3653 if (!res)
3654 res = hotkey_mask_get();
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003655 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03003656 if (res)
3657 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658
3659 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003660 if (hotkey_all_mask) {
3661 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_user_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662 len += sprintf(p + len,
3663 "commands:\tenable, disable, reset, <mask>\n");
3664 } else {
3665 len += sprintf(p + len, "mask:\t\tnot supported\n");
3666 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
3667 }
3668
3669 return len;
3670}
3671
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003672static void hotkey_enabledisable_warn(bool enable)
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003673{
3674 tpacpi_log_usertask("procfs hotkey enable/disable");
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003675 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
3676 TPACPI_WARN
3677 "hotkey enable/disable functionality has been "
3678 "removed from the driver. Hotkeys are always "
3679 "enabled\n"))
3680 printk(TPACPI_ERR
3681 "Please remove the hotkey=enable module "
3682 "parameter, it is deprecated. Hotkeys are always "
3683 "enabled\n");
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003684}
3685
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003686static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003688 int res;
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03003689 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003692 if (!tp_features.hotkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693 return -ENODEV;
3694
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02003695 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02003696 return -ERESTARTSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003697
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003698 mask = hotkey_user_mask;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003699
3700 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701 while ((cmd = next_cmd(&buf))) {
3702 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003703 hotkey_enabledisable_warn(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003705 hotkey_enabledisable_warn(0);
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003706 res = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707 } else if (strlencmp(cmd, "reset") == 0) {
Henrique de Moraes Holschuh20c9aa42009-09-12 15:22:16 -03003708 mask = (hotkey_all_mask | hotkey_source_mask)
3709 & ~hotkey_reserved_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
3711 /* mask set */
3712 } else if (sscanf(cmd, "%x", &mask) == 1) {
3713 /* mask set */
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003714 } else {
3715 res = -EINVAL;
3716 goto errexit;
3717 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718 }
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003719
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003720 if (!res) {
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003721 tpacpi_disclose_usertask("procfs hotkey",
3722 "set mask to 0x%08x\n", mask);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003723 res = hotkey_user_mask_set(mask);
3724 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003726errexit:
3727 mutex_unlock(&hotkey_mutex);
3728 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003729}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003731static const struct acpi_device_id ibm_htk_device_ids[] = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003732 {TPACPI_ACPI_HKEY_HID, 0},
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003733 {"", 0},
3734};
3735
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003736static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003737 .hid = ibm_htk_device_ids,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003738 .notify = hotkey_notify,
3739 .handle = &hkey_handle,
3740 .type = ACPI_DEVICE_NOTIFY,
3741};
3742
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003743static struct ibm_struct hotkey_driver_data = {
3744 .name = "hotkey",
3745 .read = hotkey_read,
3746 .write = hotkey_write,
3747 .exit = hotkey_exit,
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03003748 .resume = hotkey_resume,
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003749 .suspend = hotkey_suspend,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003750 .acpi = &ibm_hotkey_acpidriver,
3751};
3752
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003753/*************************************************************************
3754 * Bluetooth subdriver
3755 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003757enum {
3758 /* ACPI GBDC/SBDC bits */
3759 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
3760 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003761 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
3762 off / last state */
3763};
3764
3765enum {
3766 /* ACPI \BLTH commands */
3767 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
3768 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
3769 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
3770 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
3771 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003772};
3773
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003774#define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
3775
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003776static void bluetooth_suspend(pm_message_t state)
3777{
3778 /* Try to make sure radio will resume powered off */
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003779 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3780 TP_ACPI_BLTH_PWR_OFF_ON_RESUME))
3781 vdbg_printk(TPACPI_DBG_RFKILL,
3782 "bluetooth power down on resume request failed\n");
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003783}
3784
Johannes Berg19d337d2009-06-02 13:01:37 +02003785static int bluetooth_get_status(void)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003786{
3787 int status;
3788
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003789#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3790 if (dbg_bluetoothemul)
3791 return (tpacpi_bluetooth_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02003792 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003793#endif
3794
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003795 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
3796 return -EIO;
3797
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003798 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02003799 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003800}
3801
Johannes Berg19d337d2009-06-02 13:01:37 +02003802static int bluetooth_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003803{
3804 int status;
3805
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003806 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02003807 "will attempt to %s bluetooth\n",
3808 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003809
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003810#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3811 if (dbg_bluetoothemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02003812 tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003813 return 0;
3814 }
3815#endif
3816
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003817 /* We make sure to keep TP_ACPI_BLUETOOTH_RESUMECTRL off */
Johannes Berg19d337d2009-06-02 13:01:37 +02003818 if (state == TPACPI_RFK_RADIO_ON)
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003819 status = TP_ACPI_BLUETOOTH_RADIOSSW;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003820 else
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003821 status = 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02003822
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003823 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
3824 return -EIO;
3825
3826 return 0;
3827}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003828
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003829/* sysfs bluetooth enable ---------------------------------------------- */
3830static ssize_t bluetooth_enable_show(struct device *dev,
3831 struct device_attribute *attr,
3832 char *buf)
3833{
Johannes Berg19d337d2009-06-02 13:01:37 +02003834 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID,
3835 attr, buf);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003836}
3837
3838static ssize_t bluetooth_enable_store(struct device *dev,
3839 struct device_attribute *attr,
3840 const char *buf, size_t count)
3841{
Johannes Berg19d337d2009-06-02 13:01:37 +02003842 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID,
3843 attr, buf, count);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003844}
3845
3846static struct device_attribute dev_attr_bluetooth_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03003847 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003848 bluetooth_enable_show, bluetooth_enable_store);
3849
3850/* --------------------------------------------------------------------- */
3851
3852static struct attribute *bluetooth_attributes[] = {
3853 &dev_attr_bluetooth_enable.attr,
3854 NULL
3855};
3856
3857static const struct attribute_group bluetooth_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003858 .attrs = bluetooth_attributes,
3859};
3860
Johannes Berg19d337d2009-06-02 13:01:37 +02003861static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = {
3862 .get_status = bluetooth_get_status,
3863 .set_status = bluetooth_set_status,
3864};
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003865
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003866static void bluetooth_shutdown(void)
3867{
3868 /* Order firmware to save current state to NVRAM */
3869 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3870 TP_ACPI_BLTH_SAVE_STATE))
3871 printk(TPACPI_NOTICE
3872 "failed to save bluetooth state to NVRAM\n");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003873 else
3874 vdbg_printk(TPACPI_DBG_RFKILL,
3875 "bluestooth state saved to NVRAM\n");
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003876}
3877
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003878static void bluetooth_exit(void)
3879{
3880 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3881 &bluetooth_attr_group);
Johannes Berg19d337d2009-06-02 13:01:37 +02003882
3883 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
3884
3885 bluetooth_shutdown();
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003886}
3887
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003888static int __init bluetooth_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003890 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003891 int status = 0;
3892
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003893 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3894 "initializing bluetooth subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003895
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003896 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003897
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003898 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3899 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003900 tp_features.bluetooth = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003901 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003903 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3904 "bluetooth is %s, status 0x%02x\n",
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003905 str_supported(tp_features.bluetooth),
3906 status);
3907
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003908#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3909 if (dbg_bluetoothemul) {
3910 tp_features.bluetooth = 1;
3911 printk(TPACPI_INFO
3912 "bluetooth switch emulation enabled\n");
3913 } else
3914#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003915 if (tp_features.bluetooth &&
3916 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
3917 /* no bluetooth hardware present in system */
3918 tp_features.bluetooth = 0;
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003919 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003920 "bluetooth hardware not installed\n");
3921 }
3922
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003923 if (!tp_features.bluetooth)
3924 return 1;
3925
Johannes Berg19d337d2009-06-02 13:01:37 +02003926 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
3927 &bluetooth_tprfk_ops,
3928 RFKILL_TYPE_BLUETOOTH,
3929 TPACPI_RFK_BLUETOOTH_SW_NAME,
3930 true);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003931 if (res)
3932 return res;
3933
Johannes Berg19d337d2009-06-02 13:01:37 +02003934 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3935 &bluetooth_attr_group);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003936 if (res) {
Johannes Berg19d337d2009-06-02 13:01:37 +02003937 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003938 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003939 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003940
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003941 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942}
3943
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003944/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003945static int bluetooth_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946{
Johannes Berg19d337d2009-06-02 13:01:37 +02003947 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948}
3949
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003950static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951{
Johannes Berg19d337d2009-06-02 13:01:37 +02003952 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953}
3954
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003955static struct ibm_struct bluetooth_driver_data = {
3956 .name = "bluetooth",
3957 .read = bluetooth_read,
3958 .write = bluetooth_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003959 .exit = bluetooth_exit,
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003960 .suspend = bluetooth_suspend,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003961 .shutdown = bluetooth_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003962};
3963
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003964/*************************************************************************
3965 * Wan subdriver
3966 */
3967
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003968enum {
3969 /* ACPI GWAN/SWAN bits */
3970 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
3971 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003972 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
3973 off / last state */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003974};
3975
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003976#define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
3977
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003978static void wan_suspend(pm_message_t state)
3979{
3980 /* Try to make sure radio will resume powered off */
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003981 if (!acpi_evalf(NULL, NULL, "\\WGSV", "qvd",
3982 TP_ACPI_WGSV_PWR_OFF_ON_RESUME))
3983 vdbg_printk(TPACPI_DBG_RFKILL,
3984 "WWAN power down on resume request failed\n");
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003985}
3986
Johannes Berg19d337d2009-06-02 13:01:37 +02003987static int wan_get_status(void)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003988{
3989 int status;
3990
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003991#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3992 if (dbg_wwanemul)
3993 return (tpacpi_wwan_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02003994 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003995#endif
3996
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003997 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
3998 return -EIO;
3999
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004000 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004001 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004002}
4003
Johannes Berg19d337d2009-06-02 13:01:37 +02004004static int wan_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004005{
4006 int status;
4007
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004008 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02004009 "will attempt to %s wwan\n",
4010 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004011
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004012#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4013 if (dbg_wwanemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004014 tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004015 return 0;
4016 }
4017#endif
4018
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02004019 /* We make sure to keep TP_ACPI_WANCARD_RESUMECTRL off */
Johannes Berg19d337d2009-06-02 13:01:37 +02004020 if (state == TPACPI_RFK_RADIO_ON)
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02004021 status = TP_ACPI_WANCARD_RADIOSSW;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004022 else
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02004023 status = 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02004024
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004025 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
4026 return -EIO;
4027
4028 return 0;
4029}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004030
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004031/* sysfs wan enable ---------------------------------------------------- */
4032static ssize_t wan_enable_show(struct device *dev,
4033 struct device_attribute *attr,
4034 char *buf)
4035{
Johannes Berg19d337d2009-06-02 13:01:37 +02004036 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
4037 attr, buf);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004038}
4039
4040static ssize_t wan_enable_store(struct device *dev,
4041 struct device_attribute *attr,
4042 const char *buf, size_t count)
4043{
Johannes Berg19d337d2009-06-02 13:01:37 +02004044 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
4045 attr, buf, count);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004046}
4047
4048static struct device_attribute dev_attr_wan_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03004049 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004050 wan_enable_show, wan_enable_store);
4051
4052/* --------------------------------------------------------------------- */
4053
4054static struct attribute *wan_attributes[] = {
4055 &dev_attr_wan_enable.attr,
4056 NULL
4057};
4058
4059static const struct attribute_group wan_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004060 .attrs = wan_attributes,
4061};
4062
Johannes Berg19d337d2009-06-02 13:01:37 +02004063static const struct tpacpi_rfk_ops wan_tprfk_ops = {
4064 .get_status = wan_get_status,
4065 .set_status = wan_set_status,
4066};
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004067
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004068static void wan_shutdown(void)
4069{
4070 /* Order firmware to save current state to NVRAM */
4071 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
4072 TP_ACPI_WGSV_SAVE_STATE))
4073 printk(TPACPI_NOTICE
4074 "failed to save WWAN state to NVRAM\n");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004075 else
4076 vdbg_printk(TPACPI_DBG_RFKILL,
4077 "WWAN state saved to NVRAM\n");
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004078}
4079
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004080static void wan_exit(void)
4081{
4082 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4083 &wan_attr_group);
Johannes Berg19d337d2009-06-02 13:01:37 +02004084
4085 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4086
4087 wan_shutdown();
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004088}
4089
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004090static int __init wan_init(struct ibm_init_struct *iibm)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004091{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004092 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004093 int status = 0;
4094
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004095 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4096 "initializing wan subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004097
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004098 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004099
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004100 tp_features.wan = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004101 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004102
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004103 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4104 "wan is %s, status 0x%02x\n",
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004105 str_supported(tp_features.wan),
4106 status);
4107
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004108#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4109 if (dbg_wwanemul) {
4110 tp_features.wan = 1;
4111 printk(TPACPI_INFO
4112 "wwan switch emulation enabled\n");
4113 } else
4114#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004115 if (tp_features.wan &&
4116 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
4117 /* no wan hardware present in system */
4118 tp_features.wan = 0;
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004119 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004120 "wan hardware not installed\n");
4121 }
4122
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004123 if (!tp_features.wan)
4124 return 1;
4125
Johannes Berg19d337d2009-06-02 13:01:37 +02004126 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
4127 &wan_tprfk_ops,
4128 RFKILL_TYPE_WWAN,
4129 TPACPI_RFK_WWAN_SW_NAME,
4130 true);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004131 if (res)
4132 return res;
4133
Johannes Berg19d337d2009-06-02 13:01:37 +02004134 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4135 &wan_attr_group);
4136
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004137 if (res) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004138 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004139 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004140 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004141
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004142 return 0;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004143}
4144
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004145/* procfs -------------------------------------------------------------- */
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004146static int wan_read(char *p)
4147{
Johannes Berg19d337d2009-06-02 13:01:37 +02004148 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, p);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004149}
4150
4151static int wan_write(char *buf)
4152{
Johannes Berg19d337d2009-06-02 13:01:37 +02004153 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004154}
4155
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004156static struct ibm_struct wan_driver_data = {
4157 .name = "wan",
4158 .read = wan_read,
4159 .write = wan_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004160 .exit = wan_exit,
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02004161 .suspend = wan_suspend,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004162 .shutdown = wan_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004163};
4164
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004165/*************************************************************************
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004166 * UWB subdriver
4167 */
4168
4169enum {
4170 /* ACPI GUWB/SUWB bits */
4171 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4172 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4173};
4174
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004175#define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4176
Johannes Berg19d337d2009-06-02 13:01:37 +02004177static int uwb_get_status(void)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004178{
4179 int status;
4180
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004181#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4182 if (dbg_uwbemul)
4183 return (tpacpi_uwb_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004184 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004185#endif
4186
4187 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4188 return -EIO;
4189
4190 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004191 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004192}
4193
Johannes Berg19d337d2009-06-02 13:01:37 +02004194static int uwb_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004195{
4196 int status;
4197
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004198 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02004199 "will attempt to %s UWB\n",
4200 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004201
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004202#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4203 if (dbg_uwbemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004204 tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004205 return 0;
4206 }
4207#endif
4208
Johannes Berg19d337d2009-06-02 13:01:37 +02004209 if (state == TPACPI_RFK_RADIO_ON)
4210 status = TP_ACPI_UWB_RADIOSSW;
4211 else
4212 status = 0;
4213
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004214 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4215 return -EIO;
4216
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004217 return 0;
4218}
4219
4220/* --------------------------------------------------------------------- */
4221
Johannes Berg19d337d2009-06-02 13:01:37 +02004222static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
4223 .get_status = uwb_get_status,
4224 .set_status = uwb_set_status,
4225};
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004226
4227static void uwb_exit(void)
4228{
Johannes Berg19d337d2009-06-02 13:01:37 +02004229 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004230}
4231
4232static int __init uwb_init(struct ibm_init_struct *iibm)
4233{
4234 int res;
4235 int status = 0;
4236
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004237 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4238 "initializing uwb subdriver\n");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004239
4240 TPACPI_ACPIHANDLE_INIT(hkey);
4241
4242 tp_features.uwb = hkey_handle &&
4243 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4244
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004245 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4246 "uwb is %s, status 0x%02x\n",
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004247 str_supported(tp_features.uwb),
4248 status);
4249
4250#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4251 if (dbg_uwbemul) {
4252 tp_features.uwb = 1;
4253 printk(TPACPI_INFO
4254 "uwb switch emulation enabled\n");
4255 } else
4256#endif
4257 if (tp_features.uwb &&
4258 !(status & TP_ACPI_UWB_HWPRESENT)) {
4259 /* no uwb hardware present in system */
4260 tp_features.uwb = 0;
4261 dbg_printk(TPACPI_DBG_INIT,
4262 "uwb hardware not installed\n");
4263 }
4264
4265 if (!tp_features.uwb)
4266 return 1;
4267
4268 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
Johannes Berg19d337d2009-06-02 13:01:37 +02004269 &uwb_tprfk_ops,
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004270 RFKILL_TYPE_UWB,
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004271 TPACPI_RFK_UWB_SW_NAME,
Johannes Berg19d337d2009-06-02 13:01:37 +02004272 false);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004273 return res;
4274}
4275
4276static struct ibm_struct uwb_driver_data = {
4277 .name = "uwb",
4278 .exit = uwb_exit,
4279 .flags.experimental = 1,
4280};
4281
4282/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004283 * Video subdriver
4284 */
4285
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02004286#ifdef CONFIG_THINKPAD_ACPI_VIDEO
4287
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004288enum video_access_mode {
4289 TPACPI_VIDEO_NONE = 0,
4290 TPACPI_VIDEO_570, /* 570 */
4291 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4292 TPACPI_VIDEO_NEW, /* all others */
4293};
4294
4295enum { /* video status flags, based on VIDEO_570 */
4296 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4297 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4298 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4299};
4300
4301enum { /* TPACPI_VIDEO_570 constants */
4302 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4303 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4304 * video_status_flags */
4305 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4306 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4307};
4308
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02004309static enum video_access_mode video_supported;
4310static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004311
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004312static int video_autosw_get(void);
4313static int video_autosw_set(int enable);
4314
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004315TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004316
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004317static int __init video_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004319 int ivga;
4320
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004321 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4322
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004323 TPACPI_ACPIHANDLE_INIT(vid);
4324 TPACPI_ACPIHANDLE_INIT(vid2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004325
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004326 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4327 /* G41, assume IVGA doesn't change */
4328 vid_handle = vid2_handle;
4329
4330 if (!vid_handle)
4331 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004332 video_supported = TPACPI_VIDEO_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004333 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
4334 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004335 video_supported = TPACPI_VIDEO_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004336 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
4337 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004338 video_supported = TPACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004339 else
4340 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004341 video_supported = TPACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004343 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
4344 str_supported(video_supported != TPACPI_VIDEO_NONE),
4345 video_supported);
4346
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004347 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348}
4349
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004350static void video_exit(void)
4351{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004352 dbg_printk(TPACPI_DBG_EXIT,
4353 "restoring original video autoswitch mode\n");
4354 if (video_autosw_set(video_orig_autosw))
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004355 printk(TPACPI_ERR "error while trying to restore original "
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004356 "video autoswitch mode\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004357}
4358
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004359static int video_outputsw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360{
4361 int status = 0;
4362 int i;
4363
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004364 switch (video_supported) {
4365 case TPACPI_VIDEO_570:
4366 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
4367 TP_ACPI_VIDEO_570_PHSCMD))
4368 return -EIO;
4369 status = i & TP_ACPI_VIDEO_570_PHSMASK;
4370 break;
4371 case TPACPI_VIDEO_770:
4372 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4373 return -EIO;
4374 if (i)
4375 status |= TP_ACPI_VIDEO_S_LCD;
4376 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4377 return -EIO;
4378 if (i)
4379 status |= TP_ACPI_VIDEO_S_CRT;
4380 break;
4381 case TPACPI_VIDEO_NEW:
4382 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4383 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4384 return -EIO;
4385 if (i)
4386 status |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004388 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4389 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4390 return -EIO;
4391 if (i)
4392 status |= TP_ACPI_VIDEO_S_LCD;
4393 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4394 return -EIO;
4395 if (i)
4396 status |= TP_ACPI_VIDEO_S_DVI;
4397 break;
4398 default:
4399 return -ENOSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004400 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401
4402 return status;
4403}
4404
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004405static int video_outputsw_set(int status)
4406{
4407 int autosw;
4408 int res = 0;
4409
4410 switch (video_supported) {
4411 case TPACPI_VIDEO_570:
4412 res = acpi_evalf(NULL, NULL,
4413 "\\_SB.PHS2", "vdd",
4414 TP_ACPI_VIDEO_570_PHS2CMD,
4415 status | TP_ACPI_VIDEO_570_PHS2SET);
4416 break;
4417 case TPACPI_VIDEO_770:
4418 autosw = video_autosw_get();
4419 if (autosw < 0)
4420 return autosw;
4421
4422 res = video_autosw_set(1);
4423 if (res)
4424 return res;
4425 res = acpi_evalf(vid_handle, NULL,
4426 "ASWT", "vdd", status * 0x100, 0);
4427 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004428 printk(TPACPI_ERR
4429 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004430 return -EIO;
4431 }
4432 break;
4433 case TPACPI_VIDEO_NEW:
4434 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004435 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004436 break;
4437 default:
4438 return -ENOSYS;
4439 }
4440
4441 return (res)? 0 : -EIO;
4442}
4443
4444static int video_autosw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004446 int autosw = 0;
4447
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004448 switch (video_supported) {
4449 case TPACPI_VIDEO_570:
4450 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
4451 return -EIO;
4452 break;
4453 case TPACPI_VIDEO_770:
4454 case TPACPI_VIDEO_NEW:
4455 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4456 return -EIO;
4457 break;
4458 default:
4459 return -ENOSYS;
4460 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004461
4462 return autosw & 1;
4463}
4464
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004465static int video_autosw_set(int enable)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004466{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004467 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004468 return -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004469 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004470}
4471
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004472static int video_outputsw_cycle(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004473{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004474 int autosw = video_autosw_get();
4475 int res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004476
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004477 if (autosw < 0)
4478 return autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004479
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004480 switch (video_supported) {
4481 case TPACPI_VIDEO_570:
4482 res = video_autosw_set(1);
4483 if (res)
4484 return res;
4485 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4486 break;
4487 case TPACPI_VIDEO_770:
4488 case TPACPI_VIDEO_NEW:
4489 res = video_autosw_set(1);
4490 if (res)
4491 return res;
4492 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4493 break;
4494 default:
4495 return -ENOSYS;
4496 }
4497 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004498 printk(TPACPI_ERR
4499 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004500 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004501 }
4502
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004503 return (res)? 0 : -EIO;
4504}
4505
4506static int video_expand_toggle(void)
4507{
4508 switch (video_supported) {
4509 case TPACPI_VIDEO_570:
4510 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
4511 0 : -EIO;
4512 case TPACPI_VIDEO_770:
4513 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
4514 0 : -EIO;
4515 case TPACPI_VIDEO_NEW:
4516 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
4517 0 : -EIO;
4518 default:
4519 return -ENOSYS;
4520 }
4521 /* not reached */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004522}
4523
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004524static int video_read(char *p)
4525{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004526 int status, autosw;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004527 int len = 0;
4528
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004529 if (video_supported == TPACPI_VIDEO_NONE) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004530 len += sprintf(p + len, "status:\t\tnot supported\n");
4531 return len;
4532 }
4533
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004534 status = video_outputsw_get();
4535 if (status < 0)
4536 return status;
4537
4538 autosw = video_autosw_get();
4539 if (autosw < 0)
4540 return autosw;
4541
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004542 len += sprintf(p + len, "status:\t\tsupported\n");
4543 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
4544 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004545 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004546 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
4547 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
4548 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
4549 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004550 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004551 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
4552 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
4553 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
4554
4555 return len;
4556}
4557
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004558static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559{
4560 char *cmd;
4561 int enable, disable, status;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004562 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004564 if (video_supported == TPACPI_VIDEO_NONE)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004565 return -ENODEV;
4566
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004567 enable = 0;
4568 disable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569
4570 while ((cmd = next_cmd(&buf))) {
4571 if (strlencmp(cmd, "lcd_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004572 enable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004573 } else if (strlencmp(cmd, "lcd_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004574 disable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004575 } else if (strlencmp(cmd, "crt_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004576 enable |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004577 } else if (strlencmp(cmd, "crt_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004578 disable |= TP_ACPI_VIDEO_S_CRT;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004579 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004580 strlencmp(cmd, "dvi_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004581 enable |= TP_ACPI_VIDEO_S_DVI;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004582 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004583 strlencmp(cmd, "dvi_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004584 disable |= TP_ACPI_VIDEO_S_DVI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585 } else if (strlencmp(cmd, "auto_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004586 res = video_autosw_set(1);
4587 if (res)
4588 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589 } else if (strlencmp(cmd, "auto_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004590 res = video_autosw_set(0);
4591 if (res)
4592 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593 } else if (strlencmp(cmd, "video_switch") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004594 res = video_outputsw_cycle();
4595 if (res)
4596 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004598 res = video_expand_toggle();
4599 if (res)
4600 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004601 } else
4602 return -EINVAL;
4603 }
4604
4605 if (enable || disable) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004606 status = video_outputsw_get();
4607 if (status < 0)
4608 return status;
4609 res = video_outputsw_set((status & ~disable) | enable);
4610 if (res)
4611 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612 }
4613
4614 return 0;
4615}
4616
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004617static struct ibm_struct video_driver_data = {
4618 .name = "video",
4619 .read = video_read,
4620 .write = video_write,
4621 .exit = video_exit,
4622};
4623
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02004624#endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4625
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004626/*************************************************************************
4627 * Light (thinklight) subdriver
4628 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004630TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4631TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004632
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004633static int light_get_status(void)
4634{
4635 int status = 0;
4636
4637 if (tp_features.light_status) {
4638 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4639 return -EIO;
4640 return (!!status);
4641 }
4642
4643 return -ENXIO;
4644}
4645
4646static int light_set_status(int status)
4647{
4648 int rc;
4649
4650 if (tp_features.light) {
4651 if (cmos_handle) {
4652 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4653 (status)?
4654 TP_CMOS_THINKLIGHT_ON :
4655 TP_CMOS_THINKLIGHT_OFF);
4656 } else {
4657 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4658 (status)? 1 : 0);
4659 }
4660 return (rc)? 0 : -EIO;
4661 }
4662
4663 return -ENXIO;
4664}
4665
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004666static void light_set_status_worker(struct work_struct *work)
4667{
4668 struct tpacpi_led_classdev *data =
4669 container_of(work, struct tpacpi_led_classdev, work);
4670
4671 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00004672 light_set_status((data->new_state != TPACPI_LED_OFF));
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004673}
4674
4675static void light_sysfs_set(struct led_classdev *led_cdev,
4676 enum led_brightness brightness)
4677{
4678 struct tpacpi_led_classdev *data =
4679 container_of(led_cdev,
4680 struct tpacpi_led_classdev,
4681 led_classdev);
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00004682 data->new_state = (brightness != LED_OFF) ?
4683 TPACPI_LED_ON : TPACPI_LED_OFF;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004684 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004685}
4686
4687static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4688{
4689 return (light_get_status() == 1)? LED_FULL : LED_OFF;
4690}
4691
4692static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4693 .led_classdev = {
4694 .name = "tpacpi::thinklight",
4695 .brightness_set = &light_sysfs_set,
4696 .brightness_get = &light_sysfs_get,
4697 }
4698};
4699
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004700static int __init light_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004701{
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004702 int rc;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004703
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004704 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4705
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004706 TPACPI_ACPIHANDLE_INIT(ledb);
4707 TPACPI_ACPIHANDLE_INIT(lght);
4708 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004709 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004710
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004711 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004712 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004713
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004714 if (tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004715 /* light status not supported on
4716 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004717 tp_features.light_status =
4718 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004720 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4721 str_supported(tp_features.light),
4722 str_supported(tp_features.light_status));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004723
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004724 if (!tp_features.light)
4725 return 1;
4726
4727 rc = led_classdev_register(&tpacpi_pdev->dev,
4728 &tpacpi_led_thinklight.led_classdev);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004729
4730 if (rc < 0) {
4731 tp_features.light = 0;
4732 tp_features.light_status = 0;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004733 } else {
4734 rc = 0;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004735 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004736
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004737 return rc;
4738}
4739
4740static void light_exit(void)
4741{
4742 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
4743 if (work_pending(&tpacpi_led_thinklight.work))
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004744 flush_workqueue(tpacpi_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004745}
4746
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004747static int light_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004748{
4749 int len = 0;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004750 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004751
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004752 if (!tp_features.light) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004753 len += sprintf(p + len, "status:\t\tnot supported\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004754 } else if (!tp_features.light_status) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004755 len += sprintf(p + len, "status:\t\tunknown\n");
4756 len += sprintf(p + len, "commands:\ton, off\n");
4757 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004758 status = light_get_status();
4759 if (status < 0)
4760 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004761 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004762 len += sprintf(p + len, "commands:\ton, off\n");
4763 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764
4765 return len;
4766}
4767
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004768static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004771 int newstatus = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004772
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004773 if (!tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004774 return -ENODEV;
4775
Linus Torvalds1da177e2005-04-16 15:20:36 -07004776 while ((cmd = next_cmd(&buf))) {
4777 if (strlencmp(cmd, "on") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004778 newstatus = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779 } else if (strlencmp(cmd, "off") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004780 newstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781 } else
4782 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783 }
4784
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004785 return light_set_status(newstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786}
4787
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004788static struct ibm_struct light_driver_data = {
4789 .name = "light",
4790 .read = light_read,
4791 .write = light_write,
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004792 .exit = light_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004793};
4794
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004795/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004796 * CMOS subdriver
4797 */
4798
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004799/* sysfs cmos_command -------------------------------------------------- */
4800static ssize_t cmos_command_store(struct device *dev,
4801 struct device_attribute *attr,
4802 const char *buf, size_t count)
4803{
4804 unsigned long cmos_cmd;
4805 int res;
4806
4807 if (parse_strtoul(buf, 21, &cmos_cmd))
4808 return -EINVAL;
4809
4810 res = issue_thinkpad_cmos_command(cmos_cmd);
4811 return (res)? res : count;
4812}
4813
4814static struct device_attribute dev_attr_cmos_command =
4815 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4816
4817/* --------------------------------------------------------------------- */
4818
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004819static int __init cmos_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004820{
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004821 int res;
4822
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004823 vdbg_printk(TPACPI_DBG_INIT,
4824 "initializing cmos commands subdriver\n");
4825
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004826 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004827
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004828 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4829 str_supported(cmos_handle != NULL));
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004830
4831 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4832 if (res)
4833 return res;
4834
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004835 return (cmos_handle)? 0 : 1;
4836}
4837
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004838static void cmos_exit(void)
4839{
4840 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4841}
4842
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004843static int cmos_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844{
4845 int len = 0;
4846
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004847 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4848 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004849 if (!cmos_handle)
4850 len += sprintf(p + len, "status:\t\tnot supported\n");
4851 else {
4852 len += sprintf(p + len, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004853 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854 }
4855
4856 return len;
4857}
4858
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004859static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860{
4861 char *cmd;
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004862 int cmos_cmd, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863
4864 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004865 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
4866 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867 /* cmos_cmd set */
4868 } else
4869 return -EINVAL;
4870
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004871 res = issue_thinkpad_cmos_command(cmos_cmd);
4872 if (res)
4873 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874 }
4875
4876 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004877}
4878
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004879static struct ibm_struct cmos_driver_data = {
4880 .name = "cmos",
4881 .read = cmos_read,
4882 .write = cmos_write,
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004883 .exit = cmos_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004884};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004885
4886/*************************************************************************
4887 * LED subdriver
4888 */
4889
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004890enum led_access_mode {
4891 TPACPI_LED_NONE = 0,
4892 TPACPI_LED_570, /* 570 */
4893 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4894 TPACPI_LED_NEW, /* all others */
4895};
4896
4897enum { /* For TPACPI_LED_OLD */
4898 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
4899 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
4900 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
4901};
4902
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02004903static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004904
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004905TPACPI_HANDLE(led, ec, "SLED", /* 570 */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004906 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
4907 /* T20-22, X20-21 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004908 "LED", /* all others */
4909 ); /* R30, R31 */
4910
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03004911#define TPACPI_LED_NUMLEDS 16
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004912static struct tpacpi_led_classdev *tpacpi_leds;
4913static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
Harvey Harrisone3aa51f2008-05-29 17:51:57 -07004914static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004915 /* there's a limit of 19 chars + NULL before 2.6.26 */
4916 "tpacpi::power",
4917 "tpacpi:orange:batt",
4918 "tpacpi:green:batt",
4919 "tpacpi::dock_active",
4920 "tpacpi::bay_active",
4921 "tpacpi::dock_batt",
4922 "tpacpi::unknown_led",
4923 "tpacpi::standby",
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03004924 "tpacpi::dock_status1",
4925 "tpacpi::dock_status2",
4926 "tpacpi::unknown_led2",
4927 "tpacpi::unknown_led3",
4928 "tpacpi::thinkvantage",
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004929};
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03004930#define TPACPI_SAFE_LEDS 0x1081U
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004931
4932static inline bool tpacpi_is_led_restricted(const unsigned int led)
4933{
4934#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
4935 return false;
4936#else
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03004937 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004938#endif
4939}
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004940
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004941static int led_get_status(const unsigned int led)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004942{
4943 int status;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004944 enum led_status_t led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004945
4946 switch (led_supported) {
4947 case TPACPI_LED_570:
4948 if (!acpi_evalf(ec_handle,
4949 &status, "GLED", "dd", 1 << led))
4950 return -EIO;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004951 led_s = (status == 0)?
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004952 TPACPI_LED_OFF :
4953 ((status == 1)?
4954 TPACPI_LED_ON :
4955 TPACPI_LED_BLINK);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004956 tpacpi_led_state_cache[led] = led_s;
4957 return led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004958 default:
4959 return -ENXIO;
4960 }
4961
4962 /* not reached */
4963}
4964
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004965static int led_set_status(const unsigned int led,
4966 const enum led_status_t ledstatus)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004967{
4968 /* off, on, blink. Index is led_status_t */
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004969 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
4970 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004971
4972 int rc = 0;
4973
4974 switch (led_supported) {
4975 case TPACPI_LED_570:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004976 /* 570 */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004977 if (unlikely(led > 7))
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004978 return -EINVAL;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004979 if (unlikely(tpacpi_is_led_restricted(led)))
4980 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004981 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4982 (1 << led), led_sled_arg1[ledstatus]))
4983 rc = -EIO;
4984 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004985 case TPACPI_LED_OLD:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004986 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004987 if (unlikely(led > 7))
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004988 return -EINVAL;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004989 if (unlikely(tpacpi_is_led_restricted(led)))
4990 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004991 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
4992 if (rc >= 0)
4993 rc = ec_write(TPACPI_LED_EC_HLBL,
4994 (ledstatus == TPACPI_LED_BLINK) << led);
4995 if (rc >= 0)
4996 rc = ec_write(TPACPI_LED_EC_HLCL,
4997 (ledstatus != TPACPI_LED_OFF) << led);
4998 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004999 case TPACPI_LED_NEW:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005000 /* all others */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005001 if (unlikely(led >= TPACPI_LED_NUMLEDS))
5002 return -EINVAL;
5003 if (unlikely(tpacpi_is_led_restricted(led)))
5004 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005005 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5006 led, led_led_arg1[ledstatus]))
5007 rc = -EIO;
5008 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005009 default:
5010 rc = -ENXIO;
5011 }
5012
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005013 if (!rc)
5014 tpacpi_led_state_cache[led] = ledstatus;
5015
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005016 return rc;
5017}
5018
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005019static void led_set_status_worker(struct work_struct *work)
5020{
5021 struct tpacpi_led_classdev *data =
5022 container_of(work, struct tpacpi_led_classdev, work);
5023
5024 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005025 led_set_status(data->led, data->new_state);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005026}
5027
5028static void led_sysfs_set(struct led_classdev *led_cdev,
5029 enum led_brightness brightness)
5030{
5031 struct tpacpi_led_classdev *data = container_of(led_cdev,
5032 struct tpacpi_led_classdev, led_classdev);
5033
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005034 if (brightness == LED_OFF)
5035 data->new_state = TPACPI_LED_OFF;
5036 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
5037 data->new_state = TPACPI_LED_ON;
5038 else
5039 data->new_state = TPACPI_LED_BLINK;
5040
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03005041 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005042}
5043
5044static int led_sysfs_blink_set(struct led_classdev *led_cdev,
5045 unsigned long *delay_on, unsigned long *delay_off)
5046{
5047 struct tpacpi_led_classdev *data = container_of(led_cdev,
5048 struct tpacpi_led_classdev, led_classdev);
5049
5050 /* Can we choose the flash rate? */
5051 if (*delay_on == 0 && *delay_off == 0) {
5052 /* yes. set them to the hardware blink rate (1 Hz) */
5053 *delay_on = 500; /* ms */
5054 *delay_off = 500; /* ms */
5055 } else if ((*delay_on != 500) || (*delay_off != 500))
5056 return -EINVAL;
5057
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005058 data->new_state = TPACPI_LED_BLINK;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03005059 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005060
5061 return 0;
5062}
5063
5064static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
5065{
5066 int rc;
5067
5068 struct tpacpi_led_classdev *data = container_of(led_cdev,
5069 struct tpacpi_led_classdev, led_classdev);
5070
5071 rc = led_get_status(data->led);
5072
5073 if (rc == TPACPI_LED_OFF || rc < 0)
5074 rc = LED_OFF; /* no error handling in led class :( */
5075 else
5076 rc = LED_FULL;
5077
5078 return rc;
5079}
5080
5081static void led_exit(void)
5082{
5083 unsigned int i;
5084
5085 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5086 if (tpacpi_leds[i].led_classdev.name)
5087 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
5088 }
5089
5090 kfree(tpacpi_leds);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005091}
5092
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005093static int __init tpacpi_init_led(unsigned int led)
5094{
5095 int rc;
5096
5097 tpacpi_leds[led].led = led;
5098
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005099 /* LEDs with no name don't get registered */
5100 if (!tpacpi_led_names[led])
5101 return 0;
5102
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005103 tpacpi_leds[led].led_classdev.brightness_set = &led_sysfs_set;
5104 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5105 if (led_supported == TPACPI_LED_570)
5106 tpacpi_leds[led].led_classdev.brightness_get =
5107 &led_sysfs_get;
5108
5109 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5110
5111 INIT_WORK(&tpacpi_leds[led].work, led_set_status_worker);
5112
5113 rc = led_classdev_register(&tpacpi_pdev->dev,
5114 &tpacpi_leds[led].led_classdev);
5115 if (rc < 0)
5116 tpacpi_leds[led].led_classdev.name = NULL;
5117
5118 return rc;
5119}
5120
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005121static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
5122 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5123 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5124 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5125
5126 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5127 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5128 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5129 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5130 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5131 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5132 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5133 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5134
5135 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5136 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5137 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5138 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5139 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5140
5141 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5142 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5143 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5144 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5145
5146 /* (1) - may have excess leds enabled on MSB */
5147
5148 /* Defaults (order matters, keep last, don't reorder!) */
5149 { /* Lenovo */
5150 .vendor = PCI_VENDOR_ID_LENOVO,
5151 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5152 .quirks = 0x1fffU,
5153 },
5154 { /* IBM ThinkPads with no EC version string */
5155 .vendor = PCI_VENDOR_ID_IBM,
5156 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
5157 .quirks = 0x00ffU,
5158 },
5159 { /* IBM ThinkPads with EC version string */
5160 .vendor = PCI_VENDOR_ID_IBM,
5161 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5162 .quirks = 0x00bfU,
5163 },
5164};
5165
5166#undef TPACPI_LEDQ_IBM
5167#undef TPACPI_LEDQ_LNV
5168
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005169static int __init led_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005170{
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005171 unsigned int i;
5172 int rc;
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005173 unsigned long useful_leds;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005174
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005175 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
5176
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005177 TPACPI_ACPIHANDLE_INIT(led);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005178
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005179 if (!led_handle)
5180 /* led not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005181 led_supported = TPACPI_LED_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005182 else if (strlencmp(led_path, "SLED") == 0)
5183 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005184 led_supported = TPACPI_LED_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005185 else if (strlencmp(led_path, "SYSL") == 0)
5186 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005187 led_supported = TPACPI_LED_OLD;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005188 else
5189 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005190 led_supported = TPACPI_LED_NEW;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005191
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005192 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
5193 str_supported(led_supported), led_supported);
5194
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005195 if (led_supported == TPACPI_LED_NONE)
5196 return 1;
5197
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005198 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
5199 GFP_KERNEL);
5200 if (!tpacpi_leds) {
5201 printk(TPACPI_ERR "Out of memory for LED data\n");
5202 return -ENOMEM;
5203 }
5204
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005205 useful_leds = tpacpi_check_quirks(led_useful_qtable,
5206 ARRAY_SIZE(led_useful_qtable));
5207
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005208 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005209 if (!tpacpi_is_led_restricted(i) &&
5210 test_bit(i, &useful_leds)) {
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005211 rc = tpacpi_init_led(i);
5212 if (rc < 0) {
5213 led_exit();
5214 return rc;
5215 }
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005216 }
5217 }
5218
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005219#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005220 printk(TPACPI_NOTICE
5221 "warning: userspace override of important "
5222 "firmware LEDs is enabled\n");
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005223#endif
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005224 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005225}
5226
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005227#define str_led_status(s) \
5228 ((s) == TPACPI_LED_OFF ? "off" : \
5229 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005230
5231static int led_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005232{
5233 int len = 0;
5234
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005235 if (!led_supported) {
5236 len += sprintf(p + len, "status:\t\tnot supported\n");
5237 return len;
5238 }
5239 len += sprintf(p + len, "status:\t\tsupported\n");
5240
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005241 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005242 /* 570 */
5243 int i, status;
5244 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005245 status = led_get_status(i);
5246 if (status < 0)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005247 return -EIO;
5248 len += sprintf(p + len, "%d:\t\t%s\n",
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005249 i, str_led_status(status));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005250 }
5251 }
5252
Linus Torvalds1da177e2005-04-16 15:20:36 -07005253 len += sprintf(p + len, "commands:\t"
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005254 "<led> on, <led> off, <led> blink (<led> is 0-15)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005255
5256 return len;
5257}
5258
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005259static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005260{
5261 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005262 int led, rc;
5263 enum led_status_t s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005264
5265 if (!led_supported)
5266 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005267
5268 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005269 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 15)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005270 return -EINVAL;
5271
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005272 if (strstr(cmd, "off")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005273 s = TPACPI_LED_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005274 } else if (strstr(cmd, "on")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005275 s = TPACPI_LED_ON;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005276 } else if (strstr(cmd, "blink")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005277 s = TPACPI_LED_BLINK;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005278 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005279 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005280 }
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005281
5282 rc = led_set_status(led, s);
5283 if (rc < 0)
5284 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005285 }
5286
5287 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005288}
5289
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005290static struct ibm_struct led_driver_data = {
5291 .name = "led",
5292 .read = led_read,
5293 .write = led_write,
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005294 .exit = led_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005295};
5296
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005297/*************************************************************************
5298 * Beep subdriver
5299 */
5300
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005301TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005302
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005303#define TPACPI_BEEP_Q1 0x0001
5304
5305static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
5306 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
5307 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
5308};
5309
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005310static int __init beep_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005311{
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005312 unsigned long quirks;
5313
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005314 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
5315
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005316 TPACPI_ACPIHANDLE_INIT(beep);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005317
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005318 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
5319 str_supported(beep_handle != NULL));
5320
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005321 quirks = tpacpi_check_quirks(beep_quirk_table,
5322 ARRAY_SIZE(beep_quirk_table));
5323
5324 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
5325
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005326 return (beep_handle)? 0 : 1;
5327}
5328
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005329static int beep_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005330{
5331 int len = 0;
5332
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005333 if (!beep_handle)
5334 len += sprintf(p + len, "status:\t\tnot supported\n");
5335 else {
5336 len += sprintf(p + len, "status:\t\tsupported\n");
5337 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
5338 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005339
5340 return len;
5341}
5342
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005343static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005344{
5345 char *cmd;
5346 int beep_cmd;
5347
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005348 if (!beep_handle)
5349 return -ENODEV;
5350
Linus Torvalds1da177e2005-04-16 15:20:36 -07005351 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005352 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
5353 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005354 /* beep_cmd set */
5355 } else
5356 return -EINVAL;
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005357 if (tp_features.beep_needs_two_args) {
5358 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
5359 beep_cmd, 0))
5360 return -EIO;
5361 } else {
5362 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
5363 beep_cmd))
5364 return -EIO;
5365 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005366 }
5367
5368 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005369}
5370
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005371static struct ibm_struct beep_driver_data = {
5372 .name = "beep",
5373 .read = beep_read,
5374 .write = beep_write,
5375};
5376
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005377/*************************************************************************
5378 * Thermal subdriver
5379 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005380
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005381enum thermal_access_mode {
5382 TPACPI_THERMAL_NONE = 0, /* No thermal support */
5383 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
5384 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
5385 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
5386 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
5387};
5388
5389enum { /* TPACPI_THERMAL_TPEC_* */
5390 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
5391 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
5392 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
5393};
5394
5395#define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5396struct ibm_thermal_sensors_struct {
5397 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
5398};
5399
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005400static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005401
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005402/* idx is zero-based */
5403static int thermal_get_sensor(int idx, s32 *value)
5404{
5405 int t;
5406 s8 tmp;
5407 char tmpi[5];
5408
5409 t = TP_EC_THERMAL_TMP0;
5410
5411 switch (thermal_read_mode) {
5412#if TPACPI_MAX_THERMAL_SENSORS >= 16
5413 case TPACPI_THERMAL_TPEC_16:
5414 if (idx >= 8 && idx <= 15) {
5415 t = TP_EC_THERMAL_TMP8;
5416 idx -= 8;
5417 }
5418 /* fallthrough */
5419#endif
5420 case TPACPI_THERMAL_TPEC_8:
5421 if (idx <= 7) {
5422 if (!acpi_ec_read(t + idx, &tmp))
5423 return -EIO;
5424 *value = tmp * 1000;
5425 return 0;
5426 }
5427 break;
5428
5429 case TPACPI_THERMAL_ACPI_UPDT:
5430 if (idx <= 7) {
5431 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5432 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
5433 return -EIO;
5434 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5435 return -EIO;
5436 *value = (t - 2732) * 100;
5437 return 0;
5438 }
5439 break;
5440
5441 case TPACPI_THERMAL_ACPI_TMP07:
5442 if (idx <= 7) {
5443 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5444 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5445 return -EIO;
5446 if (t > 127 || t < -127)
5447 t = TP_EC_THERMAL_TMP_NA;
5448 *value = t * 1000;
5449 return 0;
5450 }
5451 break;
5452
5453 case TPACPI_THERMAL_NONE:
5454 default:
5455 return -ENOSYS;
5456 }
5457
5458 return -EINVAL;
5459}
5460
5461static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5462{
5463 int res, i;
5464 int n;
5465
5466 n = 8;
5467 i = 0;
5468
5469 if (!s)
5470 return -EINVAL;
5471
5472 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5473 n = 16;
5474
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005475 for (i = 0 ; i < n; i++) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005476 res = thermal_get_sensor(i, &s->temp[i]);
5477 if (res)
5478 return res;
5479 }
5480
5481 return n;
5482}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005483
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005484/* sysfs temp##_input -------------------------------------------------- */
5485
5486static ssize_t thermal_temp_input_show(struct device *dev,
5487 struct device_attribute *attr,
5488 char *buf)
5489{
5490 struct sensor_device_attribute *sensor_attr =
5491 to_sensor_dev_attr(attr);
5492 int idx = sensor_attr->index;
5493 s32 value;
5494 int res;
5495
5496 res = thermal_get_sensor(idx, &value);
5497 if (res)
5498 return res;
5499 if (value == TP_EC_THERMAL_TMP_NA * 1000)
5500 return -ENXIO;
5501
5502 return snprintf(buf, PAGE_SIZE, "%d\n", value);
5503}
5504
5505#define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005506 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5507 thermal_temp_input_show, NULL, _idxB)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005508
5509static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5510 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5511 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5512 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5513 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5514 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5515 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5516 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5517 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5518 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5519 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5520 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5521 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5522 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5523 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5524 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5525 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5526};
5527
5528#define THERMAL_ATTRS(X) \
5529 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5530
5531static struct attribute *thermal_temp_input_attr[] = {
5532 THERMAL_ATTRS(8),
5533 THERMAL_ATTRS(9),
5534 THERMAL_ATTRS(10),
5535 THERMAL_ATTRS(11),
5536 THERMAL_ATTRS(12),
5537 THERMAL_ATTRS(13),
5538 THERMAL_ATTRS(14),
5539 THERMAL_ATTRS(15),
5540 THERMAL_ATTRS(0),
5541 THERMAL_ATTRS(1),
5542 THERMAL_ATTRS(2),
5543 THERMAL_ATTRS(3),
5544 THERMAL_ATTRS(4),
5545 THERMAL_ATTRS(5),
5546 THERMAL_ATTRS(6),
5547 THERMAL_ATTRS(7),
5548 NULL
5549};
5550
5551static const struct attribute_group thermal_temp_input16_group = {
5552 .attrs = thermal_temp_input_attr
5553};
5554
5555static const struct attribute_group thermal_temp_input8_group = {
5556 .attrs = &thermal_temp_input_attr[8]
5557};
5558
5559#undef THERMAL_SENSOR_ATTR_TEMP
5560#undef THERMAL_ATTRS
5561
5562/* --------------------------------------------------------------------- */
5563
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005564static int __init thermal_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005565{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005566 u8 t, ta1, ta2;
5567 int i;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005568 int acpi_tmp7;
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005569 int res;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005570
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005571 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5572
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005573 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005574
Henrique de Moraes Holschuh3d6f99c2007-07-18 23:45:46 -03005575 if (thinkpad_id.ec_model) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005576 /*
5577 * Direct EC access mode: sensors at registers
5578 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5579 * non-implemented, thermal sensors return 0x80 when
5580 * not available
5581 */
5582
5583 ta1 = ta2 = 0;
5584 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03005585 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005586 ta1 |= t;
5587 } else {
5588 ta1 = 0;
5589 break;
5590 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03005591 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005592 ta2 |= t;
5593 } else {
5594 ta1 = 0;
5595 break;
5596 }
5597 }
5598 if (ta1 == 0) {
5599 /* This is sheer paranoia, but we handle it anyway */
5600 if (acpi_tmp7) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005601 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005602 "ThinkPad ACPI EC access misbehaving, "
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005603 "falling back to ACPI TMPx access "
5604 "mode\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005605 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005606 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005607 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005608 "ThinkPad ACPI EC access misbehaving, "
5609 "disabling thermal sensors access\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005610 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005611 }
5612 } else {
5613 thermal_read_mode =
5614 (ta2 != 0) ?
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005615 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005616 }
5617 } else if (acpi_tmp7) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005618 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
5619 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005620 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005621 } else {
5622 /* Standard ACPI TMPx access, max 8 sensors */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005623 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005624 }
5625 } else {
5626 /* temperatures not supported on 570, G4x, R30, R31, R32 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005627 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005628 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005629
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005630 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5631 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5632 thermal_read_mode);
5633
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005634 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005635 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005636 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005637 &thermal_temp_input16_group);
5638 if (res)
5639 return res;
5640 break;
5641 case TPACPI_THERMAL_TPEC_8:
5642 case TPACPI_THERMAL_ACPI_TMP07:
5643 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005644 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005645 &thermal_temp_input8_group);
5646 if (res)
5647 return res;
5648 break;
5649 case TPACPI_THERMAL_NONE:
5650 default:
5651 return 1;
5652 }
5653
5654 return 0;
5655}
5656
5657static void thermal_exit(void)
5658{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005659 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005660 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005661 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005662 &thermal_temp_input16_group);
5663 break;
5664 case TPACPI_THERMAL_TPEC_8:
5665 case TPACPI_THERMAL_ACPI_TMP07:
5666 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005667 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005668 &thermal_temp_input16_group);
5669 break;
5670 case TPACPI_THERMAL_NONE:
5671 default:
5672 break;
5673 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005674}
5675
5676static int thermal_read(char *p)
5677{
5678 int len = 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005679 int n, i;
5680 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005681
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005682 n = thermal_get_sensors(&t);
5683 if (unlikely(n < 0))
5684 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005685
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005686 len += sprintf(p + len, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005687
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005688 if (n > 0) {
5689 for (i = 0; i < (n - 1); i++)
5690 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
5691 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
5692 } else
5693 len += sprintf(p + len, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005694
5695 return len;
5696}
5697
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005698static struct ibm_struct thermal_driver_data = {
5699 .name = "thermal",
5700 .read = thermal_read,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005701 .exit = thermal_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005702};
5703
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005704/*************************************************************************
5705 * EC Dump subdriver
5706 */
5707
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005708static u8 ecdump_regs[256];
5709
5710static int ecdump_read(char *p)
5711{
5712 int len = 0;
5713 int i, j;
5714 u8 v;
5715
5716 len += sprintf(p + len, "EC "
5717 " +00 +01 +02 +03 +04 +05 +06 +07"
5718 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
5719 for (i = 0; i < 256; i += 16) {
5720 len += sprintf(p + len, "EC 0x%02x:", i);
5721 for (j = 0; j < 16; j++) {
5722 if (!acpi_ec_read(i + j, &v))
5723 break;
5724 if (v != ecdump_regs[i + j])
5725 len += sprintf(p + len, " *%02x", v);
5726 else
5727 len += sprintf(p + len, " %02x", v);
5728 ecdump_regs[i + j] = v;
5729 }
5730 len += sprintf(p + len, "\n");
5731 if (j != 16)
5732 break;
5733 }
5734
5735 /* These are way too dangerous to advertise openly... */
5736#if 0
5737 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
5738 " (<offset> is 00-ff, <value> is 00-ff)\n");
5739 len += sprintf(p + len, "commands:\t0x<offset> <value> "
5740 " (<offset> is 00-ff, <value> is 0-255)\n");
5741#endif
5742 return len;
5743}
5744
5745static int ecdump_write(char *buf)
5746{
5747 char *cmd;
5748 int i, v;
5749
5750 while ((cmd = next_cmd(&buf))) {
5751 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
5752 /* i and v set */
5753 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
5754 /* i and v set */
5755 } else
5756 return -EINVAL;
5757 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
5758 if (!acpi_ec_write(i, v))
5759 return -EIO;
5760 } else
5761 return -EINVAL;
5762 }
5763
5764 return 0;
5765}
5766
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005767static struct ibm_struct ecdump_driver_data = {
5768 .name = "ecdump",
5769 .read = ecdump_read,
5770 .write = ecdump_write,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03005771 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005772};
5773
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005774/*************************************************************************
5775 * Backlight/brightness subdriver
5776 */
Holger Macht8acb0252006-10-20 14:30:28 -07005777
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005778#define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5779
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005780/*
5781 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
5782 * CMOS NVRAM byte 0x5E, bits 0-3.
5783 *
5784 * EC HBRV (0x31) has the following layout
5785 * Bit 7: unknown function
5786 * Bit 6: unknown function
5787 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5788 * Bit 4: must be set to zero to avoid problems
5789 * Bit 3-0: backlight brightness level
5790 *
5791 * brightness_get_raw returns status data in the HBRV layout
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03005792 *
5793 * WARNING: The X61 has been verified to use HBRV for something else, so
5794 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
5795 * testing on the very early *60 Lenovo models...
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005796 */
5797
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005798enum {
5799 TP_EC_BACKLIGHT = 0x31,
5800
5801 /* TP_EC_BACKLIGHT bitmasks */
5802 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5803 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5804 TP_EC_BACKLIGHT_MAPSW = 0x20,
5805};
5806
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005807enum tpacpi_brightness_access_mode {
5808 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
5809 TPACPI_BRGHT_MODE_EC, /* EC control */
5810 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
5811 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
5812 TPACPI_BRGHT_MODE_MAX
5813};
5814
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -03005815static struct backlight_device *ibm_backlight_device;
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005816
5817static enum tpacpi_brightness_access_mode brightness_mode =
5818 TPACPI_BRGHT_MODE_MAX;
5819
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005820static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5821
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005822static struct mutex brightness_mutex;
5823
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005824/* NVRAM brightness access,
5825 * call with brightness_mutex held! */
5826static unsigned int tpacpi_brightness_nvram_get(void)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005827{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005828 u8 lnvram;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005829
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005830 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5831 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5832 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
5833 lnvram &= (tp_features.bright_16levels) ? 0x0f : 0x07;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005834
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005835 return lnvram;
5836}
5837
5838static void tpacpi_brightness_checkpoint_nvram(void)
5839{
5840 u8 lec = 0;
5841 u8 b_nvram;
5842
5843 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
5844 return;
5845
5846 vdbg_printk(TPACPI_DBG_BRGHT,
5847 "trying to checkpoint backlight level to NVRAM...\n");
5848
5849 if (mutex_lock_killable(&brightness_mutex) < 0)
5850 return;
5851
5852 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5853 goto unlock;
5854 lec &= TP_EC_BACKLIGHT_LVLMSK;
5855 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
5856
5857 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5858 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
5859 /* NVRAM needs update */
5860 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
5861 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
5862 b_nvram |= lec;
5863 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
5864 dbg_printk(TPACPI_DBG_BRGHT,
5865 "updated NVRAM backlight level to %u (0x%02x)\n",
5866 (unsigned int) lec, (unsigned int) b_nvram);
5867 } else
5868 vdbg_printk(TPACPI_DBG_BRGHT,
5869 "NVRAM backlight level already is %u (0x%02x)\n",
5870 (unsigned int) lec, (unsigned int) b_nvram);
5871
5872unlock:
5873 mutex_unlock(&brightness_mutex);
5874}
5875
5876
5877/* call with brightness_mutex held! */
5878static int tpacpi_brightness_get_raw(int *status)
5879{
5880 u8 lec = 0;
5881
5882 switch (brightness_mode) {
5883 case TPACPI_BRGHT_MODE_UCMS_STEP:
5884 *status = tpacpi_brightness_nvram_get();
5885 return 0;
5886 case TPACPI_BRGHT_MODE_EC:
5887 case TPACPI_BRGHT_MODE_ECNVRAM:
5888 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
Henrique de Moraes Holschuh2d5e94d2008-04-26 01:02:20 -03005889 return -EIO;
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005890 *status = lec;
5891 return 0;
5892 default:
5893 return -ENXIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005894 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005895}
5896
5897/* call with brightness_mutex held! */
5898/* do NOT call with illegal backlight level value */
5899static int tpacpi_brightness_set_ec(unsigned int value)
5900{
5901 u8 lec = 0;
5902
5903 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5904 return -EIO;
5905
5906 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
5907 (lec & TP_EC_BACKLIGHT_CMDMSK) |
5908 (value & TP_EC_BACKLIGHT_LVLMSK))))
5909 return -EIO;
5910
5911 return 0;
5912}
5913
5914/* call with brightness_mutex held! */
5915static int tpacpi_brightness_set_ucmsstep(unsigned int value)
5916{
5917 int cmos_cmd, inc;
5918 unsigned int current_value, i;
5919
5920 current_value = tpacpi_brightness_nvram_get();
5921
5922 if (value == current_value)
5923 return 0;
5924
5925 cmos_cmd = (value > current_value) ?
5926 TP_CMOS_BRIGHTNESS_UP :
5927 TP_CMOS_BRIGHTNESS_DOWN;
5928 inc = (value > current_value) ? 1 : -1;
5929
5930 for (i = current_value; i != value; i += inc)
5931 if (issue_thinkpad_cmos_command(cmos_cmd))
5932 return -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005933
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005934 return 0;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005935}
5936
5937/* May return EINTR which can always be mapped to ERESTARTSYS */
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005938static int brightness_set(unsigned int value)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005939{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005940 int res;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005941
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005942 if (value > ((tp_features.bright_16levels)? 15 : 7) ||
5943 value < 0)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005944 return -EINVAL;
5945
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005946 vdbg_printk(TPACPI_DBG_BRGHT,
5947 "set backlight level to %d\n", value);
5948
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02005949 res = mutex_lock_killable(&brightness_mutex);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005950 if (res < 0)
5951 return res;
5952
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005953 switch (brightness_mode) {
5954 case TPACPI_BRGHT_MODE_EC:
5955 case TPACPI_BRGHT_MODE_ECNVRAM:
5956 res = tpacpi_brightness_set_ec(value);
5957 break;
5958 case TPACPI_BRGHT_MODE_UCMS_STEP:
5959 res = tpacpi_brightness_set_ucmsstep(value);
5960 break;
5961 default:
5962 res = -ENXIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005963 }
5964
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005965 mutex_unlock(&brightness_mutex);
5966 return res;
5967}
5968
5969/* sysfs backlight class ----------------------------------------------- */
5970
5971static int brightness_update_status(struct backlight_device *bd)
5972{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005973 unsigned int level =
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005974 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
5975 bd->props.power == FB_BLANK_UNBLANK) ?
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005976 bd->props.brightness : 0;
5977
5978 dbg_printk(TPACPI_DBG_BRGHT,
5979 "backlight: attempt to set level to %d\n",
5980 level);
5981
5982 /* it is the backlight class's job (caller) to handle
5983 * EINTR and other errors properly */
5984 return brightness_set(level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005985}
Holger Macht8acb0252006-10-20 14:30:28 -07005986
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005987static int brightness_get(struct backlight_device *bd)
5988{
5989 int status, res;
5990
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005991 res = mutex_lock_killable(&brightness_mutex);
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005992 if (res < 0)
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005993 return 0;
5994
5995 res = tpacpi_brightness_get_raw(&status);
5996
5997 mutex_unlock(&brightness_mutex);
5998
5999 if (res < 0)
6000 return 0;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006001
6002 return status & TP_EC_BACKLIGHT_LVLMSK;
6003}
6004
Richard Purdie599a52d2007-02-10 23:07:48 +00006005static struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006006 .get_brightness = brightness_get,
6007 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006008};
6009
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006010/* --------------------------------------------------------------------- */
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03006011
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006012/*
6013 * These are only useful for models that have only one possibility
6014 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6015 * these quirks.
6016 */
6017#define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6018#define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6019#define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6020
6021static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
6022 /* Models with ATI GPUs known to require ECNVRAM mode */
6023 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
6024
Henrique de Moraes Holschuh6da25bf2009-09-12 15:22:11 -03006025 /* Models with ATI GPUs that can use ECNVRAM */
6026 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC),
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006027 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6028 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6029 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6030
Henrique de Moraes Holschuh6da25bf2009-09-12 15:22:11 -03006031 /* Models with Intel Extreme Graphics 2 */
6032 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC),
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006033 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_NOEC),
6034 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_NOEC),
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006035
6036 /* Models with Intel GMA900 */
6037 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
6038 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
6039 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
6040};
6041
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006042static int __init brightness_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006043{
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006044 int b;
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006045 unsigned long quirks;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006046
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006047 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
6048
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03006049 mutex_init(&brightness_mutex);
6050
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006051 quirks = tpacpi_check_quirks(brightness_quirk_table,
6052 ARRAY_SIZE(brightness_quirk_table));
6053
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03006054 /*
6055 * We always attempt to detect acpi support, so as to switch
6056 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6057 * going to publish a backlight interface
6058 */
6059 b = tpacpi_check_std_acpi_brightness_support();
6060 if (b > 0) {
Thomas Renninger2dba1b52008-08-01 17:38:03 +02006061
6062 if (acpi_video_backlight_support()) {
6063 if (brightness_enable > 1) {
6064 printk(TPACPI_NOTICE
6065 "Standard ACPI backlight interface "
6066 "available, not loading native one.\n");
6067 return 1;
6068 } else if (brightness_enable == 1) {
6069 printk(TPACPI_NOTICE
6070 "Backlight control force enabled, even if standard "
6071 "ACPI backlight interface is available\n");
6072 }
6073 } else {
6074 if (brightness_enable > 1) {
6075 printk(TPACPI_NOTICE
6076 "Standard ACPI backlight interface not "
6077 "available, thinkpad_acpi native "
6078 "brightness control enabled\n");
6079 }
Henrique de Moraes Holschuhe11e2112007-10-30 17:46:22 -02006080 }
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02006081 }
6082
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03006083 if (!brightness_enable) {
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006084 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03006085 "brightness support disabled by "
6086 "module parameter\n");
6087 return 1;
6088 }
6089
6090 if (b > 16) {
6091 printk(TPACPI_ERR
6092 "Unsupported brightness interface, "
6093 "please contact %s\n", TPACPI_MAIL);
6094 return 1;
6095 }
6096 if (b == 16)
6097 tp_features.bright_16levels = 1;
6098
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006099 /*
6100 * Check for module parameter bogosity, note that we
6101 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
6102 * able to detect "unspecified"
6103 */
6104 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03006105 return -EINVAL;
6106
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006107 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
6108 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
6109 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006110 if (quirks & TPACPI_BRGHT_Q_EC)
6111 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
6112 else
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006113 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
6114
6115 dbg_printk(TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006116 "driver auto-selected brightness_mode=%d\n",
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006117 brightness_mode);
6118 }
6119
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03006120 /* Safety */
6121 if (thinkpad_id.vendor != PCI_VENDOR_ID_IBM &&
6122 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
6123 brightness_mode == TPACPI_BRGHT_MODE_EC))
6124 return -EINVAL;
6125
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006126 if (tpacpi_brightness_get_raw(&b) < 0)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03006127 return 1;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006128
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02006129 if (tp_features.bright_16levels)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006130 printk(TPACPI_INFO
6131 "detected a 16-level brightness capable ThinkPad\n");
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02006132
Henrique de Moraes Holschuh7d5a0152007-04-24 11:48:20 -03006133 ibm_backlight_device = backlight_device_register(
6134 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
6135 &ibm_backlight_data);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006136 if (IS_ERR(ibm_backlight_device)) {
Henrique de Moraes Holschuh435c47e2009-09-20 14:09:22 -03006137 int rc = PTR_ERR(ibm_backlight_device);
6138 ibm_backlight_device = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006139 printk(TPACPI_ERR "Could not register backlight device\n");
Henrique de Moraes Holschuh435c47e2009-09-20 14:09:22 -03006140 return rc;
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006141 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006142 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6143 "brightness is supported\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006144
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006145 if (quirks & TPACPI_BRGHT_Q_ASK) {
6146 printk(TPACPI_NOTICE
6147 "brightness: will use unverified default: "
6148 "brightness_mode=%d\n", brightness_mode);
6149 printk(TPACPI_NOTICE
6150 "brightness: please report to %s whether it works well "
6151 "or not on your ThinkPad\n", TPACPI_MAIL);
6152 }
6153
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02006154 ibm_backlight_device->props.max_brightness =
6155 (tp_features.bright_16levels)? 15 : 7;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006156 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006157 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00006158
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006159 return 0;
6160}
6161
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006162static void brightness_suspend(pm_message_t state)
6163{
6164 tpacpi_brightness_checkpoint_nvram();
6165}
6166
6167static void brightness_shutdown(void)
6168{
6169 tpacpi_brightness_checkpoint_nvram();
6170}
6171
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006172static void brightness_exit(void)
6173{
6174 if (ibm_backlight_device) {
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006175 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006176 "calling backlight_device_unregister()\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006177 backlight_device_unregister(ibm_backlight_device);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006178 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006179
6180 tpacpi_brightness_checkpoint_nvram();
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006181}
6182
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006183static int brightness_read(char *p)
6184{
6185 int len = 0;
6186 int level;
6187
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006188 level = brightness_get(NULL);
6189 if (level < 0) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006190 len += sprintf(p + len, "level:\t\tunreadable\n");
6191 } else {
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02006192 len += sprintf(p + len, "level:\t\t%d\n", level);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006193 len += sprintf(p + len, "commands:\tup, down\n");
6194 len += sprintf(p + len, "commands:\tlevel <level>"
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02006195 " (<level> is 0-%d)\n",
6196 (tp_features.bright_16levels) ? 15 : 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006197 }
6198
6199 return len;
6200}
6201
6202static int brightness_write(char *buf)
6203{
6204 int level;
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006205 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006206 char *cmd;
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02006207 int max_level = (tp_features.bright_16levels) ? 15 : 7;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006208
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006209 level = brightness_get(NULL);
6210 if (level < 0)
6211 return level;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006212
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006213 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006214 if (strlencmp(cmd, "up") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006215 if (level < max_level)
6216 level++;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006217 } else if (strlencmp(cmd, "down") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006218 if (level > 0)
6219 level--;
6220 } else if (sscanf(cmd, "level %d", &level) == 1 &&
6221 level >= 0 && level <= max_level) {
6222 /* new level set */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006223 } else
6224 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006225 }
6226
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006227 tpacpi_disclose_usertask("procfs brightness",
6228 "set level to %d\n", level);
6229
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006230 /*
6231 * Now we know what the final level should be, so we try to set it.
6232 * Doing it this way makes the syscall restartable in case of EINTR
6233 */
6234 rc = brightness_set(level);
6235 return (rc == -EINTR)? ERESTARTSYS : rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006236}
6237
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006238static struct ibm_struct brightness_driver_data = {
6239 .name = "brightness",
6240 .read = brightness_read,
6241 .write = brightness_write,
6242 .exit = brightness_exit,
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006243 .suspend = brightness_suspend,
6244 .shutdown = brightness_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006245};
6246
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006247/*************************************************************************
6248 * Volume subdriver
6249 */
6250
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006251static int volume_offset = 0x30;
6252
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006253static int volume_read(char *p)
6254{
6255 int len = 0;
6256 u8 level;
6257
6258 if (!acpi_ec_read(volume_offset, &level)) {
6259 len += sprintf(p + len, "level:\t\tunreadable\n");
6260 } else {
6261 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
6262 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
6263 len += sprintf(p + len, "commands:\tup, down, mute\n");
6264 len += sprintf(p + len, "commands:\tlevel <level>"
6265 " (<level> is 0-15)\n");
6266 }
6267
6268 return len;
6269}
6270
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006271static int volume_write(char *buf)
6272{
6273 int cmos_cmd, inc, i;
6274 u8 level, mute;
6275 int new_level, new_mute;
6276 char *cmd;
6277
6278 while ((cmd = next_cmd(&buf))) {
6279 if (!acpi_ec_read(volume_offset, &level))
6280 return -EIO;
6281 new_mute = mute = level & 0x40;
6282 new_level = level = level & 0xf;
6283
6284 if (strlencmp(cmd, "up") == 0) {
6285 if (mute)
6286 new_mute = 0;
6287 else
6288 new_level = level == 15 ? 15 : level + 1;
6289 } else if (strlencmp(cmd, "down") == 0) {
6290 if (mute)
6291 new_mute = 0;
6292 else
6293 new_level = level == 0 ? 0 : level - 1;
6294 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
6295 new_level >= 0 && new_level <= 15) {
6296 /* new_level set */
6297 } else if (strlencmp(cmd, "mute") == 0) {
6298 new_mute = 0x40;
6299 } else
6300 return -EINVAL;
6301
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006302 if (new_level != level) {
6303 /* mute doesn't change */
6304
6305 cmos_cmd = (new_level > level) ?
6306 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006307 inc = new_level > level ? 1 : -1;
6308
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03006309 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006310 !acpi_ec_write(volume_offset, level)))
6311 return -EIO;
6312
6313 for (i = level; i != new_level; i += inc)
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03006314 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006315 !acpi_ec_write(volume_offset, i + inc))
6316 return -EIO;
6317
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006318 if (mute &&
6319 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
6320 !acpi_ec_write(volume_offset, new_level + mute))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006321 return -EIO;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006322 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006323 }
6324
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006325 if (new_mute != mute) {
6326 /* level doesn't change */
6327
6328 cmos_cmd = (new_mute) ?
6329 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006330
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03006331 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006332 !acpi_ec_write(volume_offset, level + new_mute))
6333 return -EIO;
6334 }
6335 }
6336
6337 return 0;
6338}
6339
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006340static struct ibm_struct volume_driver_data = {
6341 .name = "volume",
6342 .read = volume_read,
6343 .write = volume_write,
6344};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006345
6346/*************************************************************************
6347 * Fan subdriver
6348 */
6349
6350/*
6351 * FAN ACCESS MODES
6352 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006353 * TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006354 * ACPI GFAN method: returns fan level
6355 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006356 * see TPACPI_FAN_WR_ACPI_SFAN
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03006357 * EC 0x2f (HFSP) not available if GFAN exists
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006358 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006359 * TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006360 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
6361 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03006362 * EC 0x2f (HFSP) might be available *for reading*, but do not use
6363 * it for writing.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006364 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006365 * TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03006366 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
6367 * Supported on almost all ThinkPads
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006368 *
6369 * Fan speed changes of any sort (including those caused by the
6370 * disengaged mode) are usually done slowly by the firmware as the
6371 * maximum ammount of fan duty cycle change per second seems to be
6372 * limited.
6373 *
6374 * Reading is not available if GFAN exists.
6375 * Writing is not available if SFAN exists.
6376 *
6377 * Bits
6378 * 7 automatic mode engaged;
6379 * (default operation mode of the ThinkPad)
6380 * fan level is ignored in this mode.
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03006381 * 6 full speed mode (takes precedence over bit 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006382 * not available on all thinkpads. May disable
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03006383 * the tachometer while the fan controller ramps up
6384 * the speed (which can take up to a few *minutes*).
6385 * Speeds up fan to 100% duty-cycle, which is far above
6386 * the standard RPM levels. It is not impossible that
6387 * it could cause hardware damage.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006388 * 5-3 unused in some models. Extra bits for fan level
6389 * in others, but still useless as all values above
6390 * 7 map to the same speed as level 7 in these models.
6391 * 2-0 fan level (0..7 usually)
6392 * 0x00 = stop
6393 * 0x07 = max (set when temperatures critical)
6394 * Some ThinkPads may have other levels, see
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006395 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006396 *
6397 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
6398 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
6399 * does so, its initial value is meaningless (0x07).
6400 *
6401 * For firmware bugs, refer to:
6402 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
6403 *
6404 * ----
6405 *
6406 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
6407 * Main fan tachometer reading (in RPM)
6408 *
6409 * This register is present on all ThinkPads with a new-style EC, and
6410 * it is known not to be present on the A21m/e, and T22, as there is
6411 * something else in offset 0x84 according to the ACPI DSDT. Other
6412 * ThinkPads from this same time period (and earlier) probably lack the
6413 * tachometer as well.
6414 *
Nick Andrew877d0312009-01-26 11:06:57 +01006415 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006416 * was never fixed by IBM to report the EC firmware version string
6417 * probably support the tachometer (like the early X models), so
6418 * detecting it is quite hard. We need more data to know for sure.
6419 *
6420 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
6421 * might result.
6422 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03006423 * FIRMWARE BUG: may go stale while the EC is switching to full speed
6424 * mode.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006425 *
6426 * For firmware bugs, refer to:
6427 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
6428 *
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03006429 * ----
6430 *
6431 * ThinkPad EC register 0x31 bit 0 (only on select models)
6432 *
6433 * When bit 0 of EC register 0x31 is zero, the tachometer registers
6434 * show the speed of the main fan. When bit 0 of EC register 0x31
6435 * is one, the tachometer registers show the speed of the auxiliary
6436 * fan.
6437 *
6438 * Fan control seems to affect both fans, regardless of the state
6439 * of this bit.
6440 *
6441 * So far, only the firmware for the X60/X61 non-tablet versions
6442 * seem to support this (firmware TP-7M).
6443 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006444 * TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006445 * ThinkPad X31, X40, X41. Not available in the X60.
6446 *
6447 * FANS ACPI handle: takes three arguments: low speed, medium speed,
6448 * high speed. ACPI DSDT seems to map these three speeds to levels
6449 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
6450 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
6451 *
6452 * The speeds are stored on handles
6453 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
6454 *
6455 * There are three default speed sets, acessible as handles:
6456 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
6457 *
6458 * ACPI DSDT switches which set is in use depending on various
6459 * factors.
6460 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006461 * TPACPI_FAN_WR_TPEC is also available and should be used to
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006462 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
6463 * but the ACPI tables just mention level 7.
6464 */
6465
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006466enum { /* Fan control constants */
6467 fan_status_offset = 0x2f, /* EC register 0x2f */
6468 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
6469 * 0x84 must be read before 0x85 */
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03006470 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
6471 bit 0 selects which fan is active */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006472
6473 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
6474 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
6475
6476 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
6477};
6478
6479enum fan_status_access_mode {
6480 TPACPI_FAN_NONE = 0, /* No fan status or control */
6481 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
6482 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
6483};
6484
6485enum fan_control_access_mode {
6486 TPACPI_FAN_WR_NONE = 0, /* No fan control */
6487 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
6488 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
6489 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
6490};
6491
6492enum fan_control_commands {
6493 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
6494 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
6495 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
6496 * and also watchdog cmd */
6497};
6498
6499static int fan_control_allowed;
6500
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006501static enum fan_status_access_mode fan_status_access_mode;
6502static enum fan_control_access_mode fan_control_access_mode;
6503static enum fan_control_commands fan_control_commands;
6504
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02006505static u8 fan_control_initial_status;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006506static u8 fan_control_desired_level;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006507static u8 fan_control_resume_level;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02006508static int fan_watchdog_maxinterval;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006509
6510static struct mutex fan_mutex;
6511
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006512static void fan_watchdog_fire(struct work_struct *ignored);
Len Brown25c68a32006-12-08 04:43:41 -05006513static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02006514
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006515TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
6516TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006517 "\\FSPD", /* 600e/x, 770e, 770x */
6518 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006519TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006520 "JFNS", /* 770x-JL */
6521 ); /* all others */
6522
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006523/*
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006524 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
6525 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
6526 * be in auto mode (0x80).
6527 *
6528 * This is corrected by any write to HFSP either by the driver, or
6529 * by the firmware.
6530 *
6531 * We assume 0x07 really means auto mode while this quirk is active,
6532 * as this is far more likely than the ThinkPad being in level 7,
6533 * which is only used by the firmware during thermal emergencies.
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03006534 *
6535 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
6536 * TP-70 (T43, R52), which are known to be buggy.
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006537 */
6538
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03006539static void fan_quirk1_setup(void)
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006540{
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006541 if (fan_control_initial_status == 0x07) {
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03006542 printk(TPACPI_NOTICE
6543 "fan_init: initial fan status is unknown, "
6544 "assuming it is in auto mode\n");
6545 tp_features.fan_ctrl_status_undef = 1;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006546 }
6547}
6548
6549static void fan_quirk1_handle(u8 *fan_status)
6550{
6551 if (unlikely(tp_features.fan_ctrl_status_undef)) {
6552 if (*fan_status != fan_control_initial_status) {
6553 /* something changed the HFSP regisnter since
6554 * driver init time, so it is not undefined
6555 * anymore */
6556 tp_features.fan_ctrl_status_undef = 0;
6557 } else {
6558 /* Return most likely status. In fact, it
6559 * might be the only possible status */
6560 *fan_status = TP_EC_FAN_AUTO;
6561 }
6562 }
6563}
6564
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03006565/* Select main fan on X60/X61, NOOP on others */
6566static bool fan_select_fan1(void)
6567{
6568 if (tp_features.second_fan) {
6569 u8 val;
6570
6571 if (ec_read(fan_select_offset, &val) < 0)
6572 return false;
6573 val &= 0xFEU;
6574 if (ec_write(fan_select_offset, val) < 0)
6575 return false;
6576 }
6577 return true;
6578}
6579
6580/* Select secondary fan on X60/X61 */
6581static bool fan_select_fan2(void)
6582{
6583 u8 val;
6584
6585 if (!tp_features.second_fan)
6586 return false;
6587
6588 if (ec_read(fan_select_offset, &val) < 0)
6589 return false;
6590 val |= 0x01U;
6591 if (ec_write(fan_select_offset, val) < 0)
6592 return false;
6593
6594 return true;
6595}
6596
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006597/*
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006598 * Call with fan_mutex held
6599 */
6600static void fan_update_desired_level(u8 status)
6601{
6602 if ((status &
6603 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
6604 if (status > 7)
6605 fan_control_desired_level = 7;
6606 else
6607 fan_control_desired_level = status;
6608 }
6609}
6610
6611static int fan_get_status(u8 *status)
6612{
6613 u8 s;
6614
6615 /* TODO:
6616 * Add TPACPI_FAN_RD_ACPI_FANS ? */
6617
6618 switch (fan_status_access_mode) {
6619 case TPACPI_FAN_RD_ACPI_GFAN:
6620 /* 570, 600e/x, 770e, 770x */
6621
6622 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
6623 return -EIO;
6624
6625 if (likely(status))
6626 *status = s & 0x07;
6627
6628 break;
6629
6630 case TPACPI_FAN_RD_TPEC:
6631 /* all except 570, 600e/x, 770e, 770x */
6632 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
6633 return -EIO;
6634
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006635 if (likely(status)) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006636 *status = s;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006637 fan_quirk1_handle(status);
6638 }
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006639
6640 break;
6641
6642 default:
6643 return -ENXIO;
6644 }
6645
6646 return 0;
6647}
6648
6649static int fan_get_status_safe(u8 *status)
6650{
6651 int rc;
6652 u8 s;
6653
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006654 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006655 return -ERESTARTSYS;
6656 rc = fan_get_status(&s);
6657 if (!rc)
6658 fan_update_desired_level(s);
6659 mutex_unlock(&fan_mutex);
6660
6661 if (status)
6662 *status = s;
6663
6664 return rc;
6665}
6666
6667static int fan_get_speed(unsigned int *speed)
6668{
6669 u8 hi, lo;
6670
6671 switch (fan_status_access_mode) {
6672 case TPACPI_FAN_RD_TPEC:
6673 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03006674 if (unlikely(!fan_select_fan1()))
6675 return -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006676 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
6677 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
6678 return -EIO;
6679
6680 if (likely(speed))
6681 *speed = (hi << 8) | lo;
6682
6683 break;
6684
6685 default:
6686 return -ENXIO;
6687 }
6688
6689 return 0;
6690}
6691
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03006692static int fan2_get_speed(unsigned int *speed)
6693{
6694 u8 hi, lo;
6695 bool rc;
6696
6697 switch (fan_status_access_mode) {
6698 case TPACPI_FAN_RD_TPEC:
6699 /* all except 570, 600e/x, 770e, 770x */
6700 if (unlikely(!fan_select_fan2()))
6701 return -EIO;
6702 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
6703 !acpi_ec_read(fan_rpm_offset + 1, &hi);
6704 fan_select_fan1(); /* play it safe */
6705 if (rc)
6706 return -EIO;
6707
6708 if (likely(speed))
6709 *speed = (hi << 8) | lo;
6710
6711 break;
6712
6713 default:
6714 return -ENXIO;
6715 }
6716
6717 return 0;
6718}
6719
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006720static int fan_set_level(int level)
6721{
6722 if (!fan_control_allowed)
6723 return -EPERM;
6724
6725 switch (fan_control_access_mode) {
6726 case TPACPI_FAN_WR_ACPI_SFAN:
6727 if (level >= 0 && level <= 7) {
6728 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
6729 return -EIO;
6730 } else
6731 return -EINVAL;
6732 break;
6733
6734 case TPACPI_FAN_WR_ACPI_FANS:
6735 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006736 if (!(level & TP_EC_FAN_AUTO) &&
6737 !(level & TP_EC_FAN_FULLSPEED) &&
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006738 ((level < 0) || (level > 7)))
6739 return -EINVAL;
6740
6741 /* safety net should the EC not support AUTO
6742 * or FULLSPEED mode bits and just ignore them */
6743 if (level & TP_EC_FAN_FULLSPEED)
6744 level |= 7; /* safety min speed 7 */
Roel Kluin547266e2008-02-05 00:24:56 +01006745 else if (level & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006746 level |= 4; /* safety min speed 4 */
6747
6748 if (!acpi_ec_write(fan_status_offset, level))
6749 return -EIO;
6750 else
6751 tp_features.fan_ctrl_status_undef = 0;
6752 break;
6753
6754 default:
6755 return -ENXIO;
6756 }
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00006757
6758 vdbg_printk(TPACPI_DBG_FAN,
6759 "fan control: set fan control register to 0x%02x\n", level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006760 return 0;
6761}
6762
6763static int fan_set_level_safe(int level)
6764{
6765 int rc;
6766
6767 if (!fan_control_allowed)
6768 return -EPERM;
6769
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006770 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006771 return -ERESTARTSYS;
6772
6773 if (level == TPACPI_FAN_LAST_LEVEL)
6774 level = fan_control_desired_level;
6775
6776 rc = fan_set_level(level);
6777 if (!rc)
6778 fan_update_desired_level(level);
6779
6780 mutex_unlock(&fan_mutex);
6781 return rc;
6782}
6783
6784static int fan_set_enable(void)
6785{
6786 u8 s;
6787 int rc;
6788
6789 if (!fan_control_allowed)
6790 return -EPERM;
6791
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006792 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006793 return -ERESTARTSYS;
6794
6795 switch (fan_control_access_mode) {
6796 case TPACPI_FAN_WR_ACPI_FANS:
6797 case TPACPI_FAN_WR_TPEC:
6798 rc = fan_get_status(&s);
6799 if (rc < 0)
6800 break;
6801
6802 /* Don't go out of emergency fan mode */
6803 if (s != 7) {
6804 s &= 0x07;
6805 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
6806 }
6807
6808 if (!acpi_ec_write(fan_status_offset, s))
6809 rc = -EIO;
6810 else {
6811 tp_features.fan_ctrl_status_undef = 0;
6812 rc = 0;
6813 }
6814 break;
6815
6816 case TPACPI_FAN_WR_ACPI_SFAN:
6817 rc = fan_get_status(&s);
6818 if (rc < 0)
6819 break;
6820
6821 s &= 0x07;
6822
6823 /* Set fan to at least level 4 */
6824 s |= 4;
6825
6826 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006827 rc = -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006828 else
6829 rc = 0;
6830 break;
6831
6832 default:
6833 rc = -ENXIO;
6834 }
6835
6836 mutex_unlock(&fan_mutex);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00006837
6838 if (!rc)
6839 vdbg_printk(TPACPI_DBG_FAN,
6840 "fan control: set fan control register to 0x%02x\n",
6841 s);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006842 return rc;
6843}
6844
6845static int fan_set_disable(void)
6846{
6847 int rc;
6848
6849 if (!fan_control_allowed)
6850 return -EPERM;
6851
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006852 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006853 return -ERESTARTSYS;
6854
6855 rc = 0;
6856 switch (fan_control_access_mode) {
6857 case TPACPI_FAN_WR_ACPI_FANS:
6858 case TPACPI_FAN_WR_TPEC:
6859 if (!acpi_ec_write(fan_status_offset, 0x00))
6860 rc = -EIO;
6861 else {
6862 fan_control_desired_level = 0;
6863 tp_features.fan_ctrl_status_undef = 0;
6864 }
6865 break;
6866
6867 case TPACPI_FAN_WR_ACPI_SFAN:
6868 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
6869 rc = -EIO;
6870 else
6871 fan_control_desired_level = 0;
6872 break;
6873
6874 default:
6875 rc = -ENXIO;
6876 }
6877
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00006878 if (!rc)
6879 vdbg_printk(TPACPI_DBG_FAN,
6880 "fan control: set fan control register to 0\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006881
6882 mutex_unlock(&fan_mutex);
6883 return rc;
6884}
6885
6886static int fan_set_speed(int speed)
6887{
6888 int rc;
6889
6890 if (!fan_control_allowed)
6891 return -EPERM;
6892
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006893 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006894 return -ERESTARTSYS;
6895
6896 rc = 0;
6897 switch (fan_control_access_mode) {
6898 case TPACPI_FAN_WR_ACPI_FANS:
6899 if (speed >= 0 && speed <= 65535) {
6900 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
6901 speed, speed, speed))
6902 rc = -EIO;
6903 } else
6904 rc = -EINVAL;
6905 break;
6906
6907 default:
6908 rc = -ENXIO;
6909 }
6910
6911 mutex_unlock(&fan_mutex);
6912 return rc;
6913}
6914
6915static void fan_watchdog_reset(void)
6916{
6917 static int fan_watchdog_active;
6918
6919 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
6920 return;
6921
6922 if (fan_watchdog_active)
6923 cancel_delayed_work(&fan_watchdog_task);
6924
6925 if (fan_watchdog_maxinterval > 0 &&
6926 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
6927 fan_watchdog_active = 1;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03006928 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006929 msecs_to_jiffies(fan_watchdog_maxinterval
6930 * 1000))) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006931 printk(TPACPI_ERR
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03006932 "failed to queue the fan watchdog, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006933 "watchdog will not trigger\n");
6934 }
6935 } else
6936 fan_watchdog_active = 0;
6937}
6938
6939static void fan_watchdog_fire(struct work_struct *ignored)
6940{
6941 int rc;
6942
6943 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
6944 return;
6945
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006946 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006947 rc = fan_set_enable();
6948 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006949 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006950 "will try again later...\n", -rc);
6951 /* reschedule for later */
6952 fan_watchdog_reset();
6953 }
6954}
6955
6956/*
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006957 * SYSFS fan layout: hwmon compatible (device)
6958 *
6959 * pwm*_enable:
6960 * 0: "disengaged" mode
6961 * 1: manual mode
6962 * 2: native EC "auto" mode (recommended, hardware default)
6963 *
6964 * pwm*: set speed in manual mode, ignored otherwise.
6965 * 0 is level 0; 255 is level 7. Intermediate points done with linear
6966 * interpolation.
6967 *
6968 * fan*_input: tachometer reading, RPM
6969 *
6970 *
6971 * SYSFS fan layout: extensions
6972 *
6973 * fan_watchdog (driver):
6974 * fan watchdog interval in seconds, 0 disables (default), max 120
6975 */
6976
6977/* sysfs fan pwm1_enable ----------------------------------------------- */
6978static ssize_t fan_pwm1_enable_show(struct device *dev,
6979 struct device_attribute *attr,
6980 char *buf)
6981{
6982 int res, mode;
6983 u8 status;
6984
6985 res = fan_get_status_safe(&status);
6986 if (res)
6987 return res;
6988
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006989 if (status & TP_EC_FAN_FULLSPEED) {
6990 mode = 0;
6991 } else if (status & TP_EC_FAN_AUTO) {
6992 mode = 2;
6993 } else
6994 mode = 1;
6995
6996 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
6997}
6998
6999static ssize_t fan_pwm1_enable_store(struct device *dev,
7000 struct device_attribute *attr,
7001 const char *buf, size_t count)
7002{
7003 unsigned long t;
7004 int res, level;
7005
7006 if (parse_strtoul(buf, 2, &t))
7007 return -EINVAL;
7008
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007009 tpacpi_disclose_usertask("hwmon pwm1_enable",
7010 "set fan mode to %lu\n", t);
7011
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007012 switch (t) {
7013 case 0:
7014 level = TP_EC_FAN_FULLSPEED;
7015 break;
7016 case 1:
7017 level = TPACPI_FAN_LAST_LEVEL;
7018 break;
7019 case 2:
7020 level = TP_EC_FAN_AUTO;
7021 break;
7022 case 3:
7023 /* reserved for software-controlled auto mode */
7024 return -ENOSYS;
7025 default:
7026 return -EINVAL;
7027 }
7028
7029 res = fan_set_level_safe(level);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03007030 if (res == -ENXIO)
7031 return -EINVAL;
7032 else if (res < 0)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007033 return res;
7034
7035 fan_watchdog_reset();
7036
7037 return count;
7038}
7039
7040static struct device_attribute dev_attr_fan_pwm1_enable =
7041 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
7042 fan_pwm1_enable_show, fan_pwm1_enable_store);
7043
7044/* sysfs fan pwm1 ------------------------------------------------------ */
7045static ssize_t fan_pwm1_show(struct device *dev,
7046 struct device_attribute *attr,
7047 char *buf)
7048{
7049 int res;
7050 u8 status;
7051
7052 res = fan_get_status_safe(&status);
7053 if (res)
7054 return res;
7055
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007056 if ((status &
7057 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
7058 status = fan_control_desired_level;
7059
7060 if (status > 7)
7061 status = 7;
7062
7063 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
7064}
7065
7066static ssize_t fan_pwm1_store(struct device *dev,
7067 struct device_attribute *attr,
7068 const char *buf, size_t count)
7069{
7070 unsigned long s;
7071 int rc;
7072 u8 status, newlevel;
7073
7074 if (parse_strtoul(buf, 255, &s))
7075 return -EINVAL;
7076
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007077 tpacpi_disclose_usertask("hwmon pwm1",
7078 "set fan speed to %lu\n", s);
7079
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007080 /* scale down from 0-255 to 0-7 */
7081 newlevel = (s >> 5) & 0x07;
7082
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007083 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02007084 return -ERESTARTSYS;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007085
7086 rc = fan_get_status(&status);
7087 if (!rc && (status &
7088 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7089 rc = fan_set_level(newlevel);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03007090 if (rc == -ENXIO)
7091 rc = -EINVAL;
7092 else if (!rc) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007093 fan_update_desired_level(newlevel);
Henrique de Moraes Holschuhca4ac2f2007-04-27 22:00:11 -03007094 fan_watchdog_reset();
7095 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007096 }
7097
7098 mutex_unlock(&fan_mutex);
7099 return (rc)? rc : count;
7100}
7101
7102static struct device_attribute dev_attr_fan_pwm1 =
7103 __ATTR(pwm1, S_IWUSR | S_IRUGO,
7104 fan_pwm1_show, fan_pwm1_store);
7105
7106/* sysfs fan fan1_input ------------------------------------------------ */
7107static ssize_t fan_fan1_input_show(struct device *dev,
7108 struct device_attribute *attr,
7109 char *buf)
7110{
7111 int res;
7112 unsigned int speed;
7113
7114 res = fan_get_speed(&speed);
7115 if (res < 0)
7116 return res;
7117
7118 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7119}
7120
7121static struct device_attribute dev_attr_fan_fan1_input =
7122 __ATTR(fan1_input, S_IRUGO,
7123 fan_fan1_input_show, NULL);
7124
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007125/* sysfs fan fan2_input ------------------------------------------------ */
7126static ssize_t fan_fan2_input_show(struct device *dev,
7127 struct device_attribute *attr,
7128 char *buf)
7129{
7130 int res;
7131 unsigned int speed;
7132
7133 res = fan2_get_speed(&speed);
7134 if (res < 0)
7135 return res;
7136
7137 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7138}
7139
7140static struct device_attribute dev_attr_fan_fan2_input =
7141 __ATTR(fan2_input, S_IRUGO,
7142 fan_fan2_input_show, NULL);
7143
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007144/* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007145static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
7146 char *buf)
7147{
7148 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
7149}
7150
7151static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
7152 const char *buf, size_t count)
7153{
7154 unsigned long t;
7155
7156 if (parse_strtoul(buf, 120, &t))
7157 return -EINVAL;
7158
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03007159 if (!fan_control_allowed)
7160 return -EPERM;
7161
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007162 fan_watchdog_maxinterval = t;
7163 fan_watchdog_reset();
7164
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007165 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
7166
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007167 return count;
7168}
7169
7170static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
7171 fan_fan_watchdog_show, fan_fan_watchdog_store);
7172
7173/* --------------------------------------------------------------------- */
7174static struct attribute *fan_attributes[] = {
7175 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
7176 &dev_attr_fan_fan1_input.attr,
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007177 NULL, /* for fan2_input */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007178 NULL
7179};
7180
7181static const struct attribute_group fan_attr_group = {
7182 .attrs = fan_attributes,
7183};
7184
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007185#define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
7186#define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007187
7188#define TPACPI_FAN_QI(__id1, __id2, __quirks) \
7189 { .vendor = PCI_VENDOR_ID_IBM, \
7190 .bios = TPACPI_MATCH_ANY, \
7191 .ec = TPID(__id1, __id2), \
7192 .quirks = __quirks }
7193
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007194#define TPACPI_FAN_QL(__id1, __id2, __quirks) \
7195 { .vendor = PCI_VENDOR_ID_LENOVO, \
7196 .bios = TPACPI_MATCH_ANY, \
7197 .ec = TPID(__id1, __id2), \
7198 .quirks = __quirks }
7199
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007200static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
7201 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
7202 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
7203 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
7204 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007205 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007206};
7207
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007208#undef TPACPI_FAN_QL
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007209#undef TPACPI_FAN_QI
7210
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007211static int __init fan_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007212{
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007213 int rc;
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007214 unsigned long quirks;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007215
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007216 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7217 "initializing fan subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007218
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03007219 mutex_init(&fan_mutex);
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007220 fan_status_access_mode = TPACPI_FAN_NONE;
7221 fan_control_access_mode = TPACPI_FAN_WR_NONE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007222 fan_control_commands = 0;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007223 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03007224 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007225 tp_features.second_fan = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007226 fan_control_desired_level = 7;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007227
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007228 TPACPI_ACPIHANDLE_INIT(fans);
7229 TPACPI_ACPIHANDLE_INIT(gfan);
7230 TPACPI_ACPIHANDLE_INIT(sfan);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007231
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007232 quirks = tpacpi_check_quirks(fan_quirk_table,
7233 ARRAY_SIZE(fan_quirk_table));
7234
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007235 if (gfan_handle) {
7236 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007237 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007238 } else {
7239 /* all other ThinkPads: note that even old-style
7240 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02007241 if (likely(acpi_ec_read(fan_status_offset,
7242 &fan_control_initial_status))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007243 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007244 if (quirks & TPACPI_FAN_Q1)
7245 fan_quirk1_setup();
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007246 if (quirks & TPACPI_FAN_2FAN) {
7247 tp_features.second_fan = 1;
7248 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7249 "secondary fan support enabled\n");
7250 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007251 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007252 printk(TPACPI_ERR
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007253 "ThinkPad ACPI EC access misbehaving, "
7254 "fan status and control unavailable\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007255 return 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007256 }
7257 }
7258
7259 if (sfan_handle) {
7260 /* 570, 770x-JL */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007261 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02007262 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007263 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007264 } else {
7265 if (!gfan_handle) {
7266 /* gfan without sfan means no fan control */
7267 /* all other models implement TP EC 0x2f control */
7268
7269 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02007270 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007271 fan_control_access_mode =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007272 TPACPI_FAN_WR_ACPI_FANS;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007273 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007274 TPACPI_FAN_CMD_SPEED |
7275 TPACPI_FAN_CMD_LEVEL |
7276 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007277 } else {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007278 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02007279 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007280 TPACPI_FAN_CMD_LEVEL |
7281 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007282 }
7283 }
7284 }
7285
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007286 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7287 "fan is %s, modes %d, %d\n",
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007288 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
7289 fan_control_access_mode != TPACPI_FAN_WR_NONE),
7290 fan_status_access_mode, fan_control_access_mode);
7291
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03007292 /* fan control master switch */
7293 if (!fan_control_allowed) {
7294 fan_control_access_mode = TPACPI_FAN_WR_NONE;
7295 fan_control_commands = 0;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007296 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03007297 "fan control features disabled by parameter\n");
7298 }
7299
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007300 /* update fan_control_desired_level */
7301 if (fan_status_access_mode != TPACPI_FAN_NONE)
7302 fan_get_status_safe(NULL);
7303
7304 if (fan_status_access_mode != TPACPI_FAN_NONE ||
7305 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007306 if (tp_features.second_fan) {
7307 /* attach second fan tachometer */
7308 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
7309 &dev_attr_fan_fan2_input.attr;
7310 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007311 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007312 &fan_attr_group);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007313 if (rc < 0)
7314 return rc;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03007315
7316 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
7317 &driver_attr_fan_watchdog);
7318 if (rc < 0) {
7319 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
7320 &fan_attr_group);
7321 return rc;
7322 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007323 return 0;
7324 } else
7325 return 1;
7326}
7327
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007328static void fan_exit(void)
7329{
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007330 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007331 "cancelling any pending fan watchdog tasks\n");
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007332
7333 /* FIXME: can we really do this unconditionally? */
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007334 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007335 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
7336 &driver_attr_fan_watchdog);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007337
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007338 cancel_delayed_work(&fan_watchdog_task);
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03007339 flush_workqueue(tpacpi_wq);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007340}
7341
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007342static void fan_suspend(pm_message_t state)
7343{
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007344 int rc;
7345
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007346 if (!fan_control_allowed)
7347 return;
7348
7349 /* Store fan status in cache */
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007350 fan_control_resume_level = 0;
7351 rc = fan_get_status_safe(&fan_control_resume_level);
7352 if (rc < 0)
7353 printk(TPACPI_NOTICE
7354 "failed to read fan level for later "
7355 "restore during resume: %d\n", rc);
7356
7357 /* if it is undefined, don't attempt to restore it.
7358 * KEEP THIS LAST */
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007359 if (tp_features.fan_ctrl_status_undef)
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007360 fan_control_resume_level = 0;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007361}
7362
7363static void fan_resume(void)
7364{
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007365 u8 current_level = 7;
7366 bool do_set = false;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007367 int rc;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007368
7369 /* DSDT *always* updates status on resume */
7370 tp_features.fan_ctrl_status_undef = 0;
7371
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007372 if (!fan_control_allowed ||
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007373 !fan_control_resume_level ||
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007374 (fan_get_status_safe(&current_level) < 0))
7375 return;
7376
7377 switch (fan_control_access_mode) {
7378 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007379 /* never decrease fan level */
7380 do_set = (fan_control_resume_level > current_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007381 break;
7382 case TPACPI_FAN_WR_ACPI_FANS:
7383 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007384 /* never decrease fan level, scale is:
7385 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
7386 *
7387 * We expect the firmware to set either 7 or AUTO, but we
7388 * handle FULLSPEED out of paranoia.
7389 *
7390 * So, we can safely only restore FULLSPEED or 7, anything
7391 * else could slow the fan. Restoring AUTO is useless, at
7392 * best that's exactly what the DSDT already set (it is the
7393 * slower it uses).
7394 *
7395 * Always keep in mind that the DSDT *will* have set the
7396 * fans to what the vendor supposes is the best level. We
7397 * muck with it only to speed the fan up.
7398 */
7399 if (fan_control_resume_level != 7 &&
7400 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
7401 return;
7402 else
7403 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
7404 (current_level != fan_control_resume_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007405 break;
7406 default:
7407 return;
7408 }
7409 if (do_set) {
7410 printk(TPACPI_NOTICE
7411 "restoring fan level to 0x%02x\n",
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007412 fan_control_resume_level);
7413 rc = fan_set_level_safe(fan_control_resume_level);
7414 if (rc < 0)
7415 printk(TPACPI_NOTICE
7416 "failed to restore fan level: %d\n", rc);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007417 }
7418}
7419
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007420static int fan_read(char *p)
7421{
7422 int len = 0;
7423 int rc;
7424 u8 status;
7425 unsigned int speed = 0;
7426
7427 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007428 case TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007429 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007430 rc = fan_get_status_safe(&status);
7431 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007432 return rc;
7433
7434 len += sprintf(p + len, "status:\t\t%s\n"
7435 "level:\t\t%d\n",
7436 (status != 0) ? "enabled" : "disabled", status);
7437 break;
7438
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007439 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007440 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007441 rc = fan_get_status_safe(&status);
7442 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007443 return rc;
7444
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007445 len += sprintf(p + len, "status:\t\t%s\n",
7446 (status != 0) ? "enabled" : "disabled");
7447
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007448 rc = fan_get_speed(&speed);
7449 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007450 return rc;
7451
7452 len += sprintf(p + len, "speed:\t\t%d\n", speed);
7453
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007454 if (status & TP_EC_FAN_FULLSPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007455 /* Disengaged mode takes precedence */
7456 len += sprintf(p + len, "level:\t\tdisengaged\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007457 else if (status & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007458 len += sprintf(p + len, "level:\t\tauto\n");
7459 else
7460 len += sprintf(p + len, "level:\t\t%d\n", status);
7461 break;
7462
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007463 case TPACPI_FAN_NONE:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007464 default:
7465 len += sprintf(p + len, "status:\t\tnot supported\n");
7466 }
7467
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007468 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007469 len += sprintf(p + len, "commands:\tlevel <level>");
7470
7471 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007472 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007473 len += sprintf(p + len, " (<level> is 0-7)\n");
7474 break;
7475
7476 default:
7477 len += sprintf(p + len, " (<level> is 0-7, "
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007478 "auto, disengaged, full-speed)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007479 break;
7480 }
7481 }
7482
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007483 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007484 len += sprintf(p + len, "commands:\tenable, disable\n"
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007485 "commands:\twatchdog <timeout> (<timeout> "
7486 "is 0 (off), 1-120 (seconds))\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007487
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007488 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007489 len += sprintf(p + len, "commands:\tspeed <speed>"
7490 " (<speed> is 0-65535)\n");
7491
7492 return len;
7493}
7494
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007495static int fan_write_cmd_level(const char *cmd, int *rc)
7496{
7497 int level;
7498
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02007499 if (strlencmp(cmd, "level auto") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007500 level = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007501 else if ((strlencmp(cmd, "level disengaged") == 0) |
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007502 (strlencmp(cmd, "level full-speed") == 0))
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007503 level = TP_EC_FAN_FULLSPEED;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02007504 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007505 return 0;
7506
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007507 *rc = fan_set_level_safe(level);
7508 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007509 printk(TPACPI_ERR "level command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007510 "access mode %d", fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007511 else if (!*rc)
7512 tpacpi_disclose_usertask("procfs fan",
7513 "set level to %d\n", level);
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007514
7515 return 1;
7516}
7517
7518static int fan_write_cmd_enable(const char *cmd, int *rc)
7519{
7520 if (strlencmp(cmd, "enable") != 0)
7521 return 0;
7522
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007523 *rc = fan_set_enable();
7524 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007525 printk(TPACPI_ERR "enable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007526 "access mode %d", fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007527 else if (!*rc)
7528 tpacpi_disclose_usertask("procfs fan", "enable\n");
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007529
7530 return 1;
7531}
7532
7533static int fan_write_cmd_disable(const char *cmd, int *rc)
7534{
7535 if (strlencmp(cmd, "disable") != 0)
7536 return 0;
7537
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007538 *rc = fan_set_disable();
7539 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007540 printk(TPACPI_ERR "disable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007541 "access mode %d", fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007542 else if (!*rc)
7543 tpacpi_disclose_usertask("procfs fan", "disable\n");
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007544
7545 return 1;
7546}
7547
7548static int fan_write_cmd_speed(const char *cmd, int *rc)
7549{
7550 int speed;
7551
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02007552 /* TODO:
7553 * Support speed <low> <medium> <high> ? */
7554
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007555 if (sscanf(cmd, "speed %d", &speed) != 1)
7556 return 0;
7557
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007558 *rc = fan_set_speed(speed);
7559 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007560 printk(TPACPI_ERR "speed command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007561 "access mode %d", fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007562 else if (!*rc)
7563 tpacpi_disclose_usertask("procfs fan",
7564 "set speed to %d\n", speed);
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007565
7566 return 1;
7567}
7568
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007569static int fan_write_cmd_watchdog(const char *cmd, int *rc)
7570{
7571 int interval;
7572
7573 if (sscanf(cmd, "watchdog %d", &interval) != 1)
7574 return 0;
7575
7576 if (interval < 0 || interval > 120)
7577 *rc = -EINVAL;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007578 else {
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007579 fan_watchdog_maxinterval = interval;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007580 tpacpi_disclose_usertask("procfs fan",
7581 "set watchdog timer to %d\n",
7582 interval);
7583 }
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007584
7585 return 1;
7586}
7587
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007588static int fan_write(char *buf)
7589{
7590 char *cmd;
7591 int rc = 0;
7592
7593 while (!rc && (cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007594 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007595 fan_write_cmd_level(cmd, &rc)) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007596 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007597 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007598 fan_write_cmd_disable(cmd, &rc) ||
7599 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007600 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007601 fan_write_cmd_speed(cmd, &rc))
7602 )
7603 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007604 else if (!rc)
7605 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007606 }
7607
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007608 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007609}
7610
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007611static struct ibm_struct fan_driver_data = {
7612 .name = "fan",
7613 .read = fan_read,
7614 .write = fan_write,
7615 .exit = fan_exit,
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007616 .suspend = fan_suspend,
7617 .resume = fan_resume,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007618};
7619
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007620/****************************************************************************
7621 ****************************************************************************
7622 *
7623 * Infrastructure
7624 *
7625 ****************************************************************************
7626 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07007627
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007628/* sysfs name ---------------------------------------------------------- */
7629static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
7630 struct device_attribute *attr,
7631 char *buf)
7632{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007633 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007634}
7635
7636static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
7637 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
7638
7639/* --------------------------------------------------------------------- */
7640
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007641/* /proc support */
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -03007642static struct proc_dir_entry *proc_dir;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007643
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007644/*
7645 * Module and infrastructure proble, init and exit handling
7646 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007647
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007648static int force_load;
7649
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007650#ifdef CONFIG_THINKPAD_ACPI_DEBUG
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007651static const char * __init str_supported(int is_supported)
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007652{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007653 static char text_unsupported[] __initdata = "not supported";
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007654
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007655 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007656}
7657#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
7658
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007659static void ibm_exit(struct ibm_struct *ibm)
7660{
7661 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
7662
7663 list_del_init(&ibm->all_drivers);
7664
7665 if (ibm->flags.acpi_notify_installed) {
7666 dbg_printk(TPACPI_DBG_EXIT,
7667 "%s: acpi_remove_notify_handler\n", ibm->name);
7668 BUG_ON(!ibm->acpi);
7669 acpi_remove_notify_handler(*ibm->acpi->handle,
7670 ibm->acpi->type,
7671 dispatch_acpi_notify);
7672 ibm->flags.acpi_notify_installed = 0;
7673 ibm->flags.acpi_notify_installed = 0;
7674 }
7675
7676 if (ibm->flags.proc_created) {
7677 dbg_printk(TPACPI_DBG_EXIT,
7678 "%s: remove_proc_entry\n", ibm->name);
7679 remove_proc_entry(ibm->name, proc_dir);
7680 ibm->flags.proc_created = 0;
7681 }
7682
7683 if (ibm->flags.acpi_driver_registered) {
7684 dbg_printk(TPACPI_DBG_EXIT,
7685 "%s: acpi_bus_unregister_driver\n", ibm->name);
7686 BUG_ON(!ibm->acpi);
7687 acpi_bus_unregister_driver(ibm->acpi->driver);
7688 kfree(ibm->acpi->driver);
7689 ibm->acpi->driver = NULL;
7690 ibm->flags.acpi_driver_registered = 0;
7691 }
7692
7693 if (ibm->flags.init_called && ibm->exit) {
7694 ibm->exit();
7695 ibm->flags.init_called = 0;
7696 }
7697
7698 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
7699}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007700
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007701static int __init ibm_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007702{
7703 int ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007704 struct ibm_struct *ibm = iibm->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007705 struct proc_dir_entry *entry;
7706
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007707 BUG_ON(ibm == NULL);
7708
7709 INIT_LIST_HEAD(&ibm->all_drivers);
7710
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03007711 if (ibm->flags.experimental && !experimental)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007712 return 0;
7713
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007714 dbg_printk(TPACPI_DBG_INIT,
7715 "probing for %s\n", ibm->name);
7716
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007717 if (iibm->init) {
7718 ret = iibm->init(iibm);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007719 if (ret > 0)
7720 return 0; /* probe failed */
7721 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007722 return ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007723
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03007724 ibm->flags.init_called = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007725 }
7726
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03007727 if (ibm->acpi) {
7728 if (ibm->acpi->hid) {
7729 ret = register_tpacpi_subdriver(ibm);
7730 if (ret)
7731 goto err_out;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007732 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03007733
7734 if (ibm->acpi->notify) {
7735 ret = setup_acpi_notify(ibm);
7736 if (ret == -ENODEV) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007737 printk(TPACPI_NOTICE "disabling subdriver %s\n",
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03007738 ibm->name);
7739 ret = 0;
7740 goto err_out;
7741 }
7742 if (ret < 0)
7743 goto err_out;
7744 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007745 }
7746
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007747 dbg_printk(TPACPI_DBG_INIT,
7748 "%s installed\n", ibm->name);
7749
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007750 if (ibm->read) {
7751 entry = create_proc_entry(ibm->name,
7752 S_IFREG | S_IRUGO | S_IWUSR,
7753 proc_dir);
7754 if (!entry) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007755 printk(TPACPI_ERR "unable to create proc entry %s\n",
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007756 ibm->name);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007757 ret = -ENODEV;
7758 goto err_out;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007759 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007760 entry->data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03007761 entry->read_proc = &dispatch_procfs_read;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007762 if (ibm->write)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03007763 entry->write_proc = &dispatch_procfs_write;
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03007764 ibm->flags.proc_created = 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007765 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007766
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007767 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
7768
Linus Torvalds1da177e2005-04-16 15:20:36 -07007769 return 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007770
7771err_out:
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007772 dbg_printk(TPACPI_DBG_INIT,
7773 "%s: at error exit path with result %d\n",
7774 ibm->name, ret);
7775
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007776 ibm_exit(ibm);
7777 return (ret < 0)? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007778}
7779
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007780/* Probing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007781
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03007782static bool __pure __init tpacpi_is_fw_digit(const char c)
7783{
7784 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
7785}
7786
7787/* Most models: xxyTkkWW (#.##c); Ancient 570/600 and -SL lacks (#.##c) */
7788static bool __pure __init tpacpi_is_valid_fw_id(const char* const s,
7789 const char t)
7790{
7791 return s && strlen(s) >= 8 &&
7792 tpacpi_is_fw_digit(s[0]) &&
7793 tpacpi_is_fw_digit(s[1]) &&
7794 s[2] == t && s[3] == 'T' &&
7795 tpacpi_is_fw_digit(s[4]) &&
7796 tpacpi_is_fw_digit(s[5]) &&
7797 s[6] == 'W' && s[7] == 'W';
7798}
7799
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007800/* returns 0 - probe ok, or < 0 - probe error.
7801 * Probe ok doesn't mean thinkpad found.
7802 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
7803static int __must_check __init get_thinkpad_model_data(
7804 struct thinkpad_id_data *tp)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02007805{
Jeff Garzik18552562007-10-03 15:15:40 -04007806 const struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02007807 char ec_fw_string[18];
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007808 char const *s;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02007809
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007810 if (!tp)
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007811 return -EINVAL;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007812
7813 memset(tp, 0, sizeof(*tp));
7814
7815 if (dmi_name_in_vendors("IBM"))
7816 tp->vendor = PCI_VENDOR_ID_IBM;
7817 else if (dmi_name_in_vendors("LENOVO"))
7818 tp->vendor = PCI_VENDOR_ID_LENOVO;
7819 else
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007820 return 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007821
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007822 s = dmi_get_system_info(DMI_BIOS_VERSION);
7823 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
7824 if (s && !tp->bios_version_str)
7825 return -ENOMEM;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03007826
7827 /* Really ancient ThinkPad 240X will fail this, which is fine */
7828 if (!tpacpi_is_valid_fw_id(tp->bios_version_str, 'E'))
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007829 return 0;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03007830
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007831 tp->bios_model = tp->bios_version_str[0]
7832 | (tp->bios_version_str[1] << 8);
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03007833 tp->bios_release = (tp->bios_version_str[4] << 8)
7834 | tp->bios_version_str[5];
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007835
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02007836 /*
7837 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
7838 * X32 or newer, all Z series; Some models must have an
7839 * up-to-date BIOS or they will not be detected.
7840 *
7841 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
7842 */
7843 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02007844 if (sscanf(dev->name,
7845 "IBM ThinkPad Embedded Controller -[%17c",
7846 ec_fw_string) == 1) {
7847 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
7848 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007849
7850 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007851 if (!tp->ec_version_str)
7852 return -ENOMEM;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03007853
7854 if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) {
7855 tp->ec_model = ec_fw_string[0]
7856 | (ec_fw_string[1] << 8);
7857 tp->ec_release = (ec_fw_string[4] << 8)
7858 | ec_fw_string[5];
7859 } else {
7860 printk(TPACPI_NOTICE
7861 "ThinkPad firmware release %s "
7862 "doesn't match the known patterns\n",
7863 ec_fw_string);
7864 printk(TPACPI_NOTICE
7865 "please report this to %s\n",
7866 TPACPI_MAIL);
7867 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007868 break;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02007869 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02007870 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007871
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007872 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
7873 if (s && !strnicmp(s, "ThinkPad", 8)) {
7874 tp->model_str = kstrdup(s, GFP_KERNEL);
7875 if (!tp->model_str)
7876 return -ENOMEM;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007877 }
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03007878
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007879 s = dmi_get_system_info(DMI_PRODUCT_NAME);
7880 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
7881 if (s && !tp->nummodel_str)
7882 return -ENOMEM;
7883
7884 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007885}
7886
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007887static int __init probe_for_thinkpad(void)
7888{
7889 int is_thinkpad;
7890
7891 if (acpi_disabled)
7892 return -ENODEV;
7893
7894 /*
7895 * Non-ancient models have better DMI tagging, but very old models
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03007896 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007897 */
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03007898 is_thinkpad = (thinkpad_id.model_str != NULL) ||
7899 (thinkpad_id.ec_model != 0) ||
7900 tpacpi_is_fw_known();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007901
7902 /* ec is required because many other handles are relative to it */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007903 TPACPI_ACPIHANDLE_INIT(ec);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007904 if (!ec_handle) {
7905 if (is_thinkpad)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007906 printk(TPACPI_ERR
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007907 "Not yet supported ThinkPad detected!\n");
7908 return -ENODEV;
7909 }
7910
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03007911 if (!is_thinkpad && !force_load)
7912 return -ENODEV;
7913
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007914 return 0;
7915}
7916
7917
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007918/* Module init, exit, parameters */
7919
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007920static struct ibm_init_struct ibms_init[] __initdata = {
7921 {
7922 .init = thinkpad_acpi_driver_init,
7923 .data = &thinkpad_acpi_driver_data,
7924 },
7925 {
7926 .init = hotkey_init,
7927 .data = &hotkey_driver_data,
7928 },
7929 {
7930 .init = bluetooth_init,
7931 .data = &bluetooth_driver_data,
7932 },
7933 {
7934 .init = wan_init,
7935 .data = &wan_driver_data,
7936 },
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02007937 {
7938 .init = uwb_init,
7939 .data = &uwb_driver_data,
7940 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02007941#ifdef CONFIG_THINKPAD_ACPI_VIDEO
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007942 {
7943 .init = video_init,
7944 .data = &video_driver_data,
7945 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02007946#endif
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007947 {
7948 .init = light_init,
7949 .data = &light_driver_data,
7950 },
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007951 {
7952 .init = cmos_init,
7953 .data = &cmos_driver_data,
7954 },
7955 {
7956 .init = led_init,
7957 .data = &led_driver_data,
7958 },
7959 {
7960 .init = beep_init,
7961 .data = &beep_driver_data,
7962 },
7963 {
7964 .init = thermal_init,
7965 .data = &thermal_driver_data,
7966 },
7967 {
7968 .data = &ecdump_driver_data,
7969 },
7970 {
7971 .init = brightness_init,
7972 .data = &brightness_driver_data,
7973 },
7974 {
7975 .data = &volume_driver_data,
7976 },
7977 {
7978 .init = fan_init,
7979 .data = &fan_driver_data,
7980 },
7981};
7982
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007983static int __init set_ibm_param(const char *val, struct kernel_param *kp)
7984{
7985 unsigned int i;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007986 struct ibm_struct *ibm;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007987
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02007988 if (!kp || !kp->name || !val)
7989 return -EINVAL;
7990
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007991 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
7992 ibm = ibms_init[i].data;
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02007993 WARN_ON(ibm == NULL);
7994
7995 if (!ibm || !ibm->name)
7996 continue;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007997
7998 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
7999 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008000 return -ENOSPC;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008001 strcpy(ibms_init[i].param, val);
8002 strcat(ibms_init[i].param, ",");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008003 return 0;
8004 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008005 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008006
8007 return -EINVAL;
8008}
8009
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008010module_param(experimental, int, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008011MODULE_PARM_DESC(experimental,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008012 "Enables experimental features when non-zero");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008013
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03008014module_param_named(debug, dbg_level, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008015MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03008016
Henrique de Moraes Holschuh86cc9442007-07-18 23:45:41 -03008017module_param(force_load, bool, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008018MODULE_PARM_DESC(force_load,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008019 "Attempts to load the driver even on a "
8020 "mis-identified ThinkPad when true");
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03008021
Henrique de Moraes Holschuh86cc9442007-07-18 23:45:41 -03008022module_param_named(fan_control, fan_control_allowed, bool, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008023MODULE_PARM_DESC(fan_control,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008024 "Enables setting fan parameters features when true");
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008025
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00008026module_param_named(brightness_mode, brightness_mode, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008027MODULE_PARM_DESC(brightness_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008028 "Selects brightness control strategy: "
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00008029 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03008030
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02008031module_param(brightness_enable, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008032MODULE_PARM_DESC(brightness_enable,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008033 "Enables backlight control when 1, disables when 0");
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02008034
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03008035module_param(hotkey_report_mode, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008036MODULE_PARM_DESC(hotkey_report_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008037 "used for backwards compatibility with userspace, "
8038 "see documentation");
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03008039
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008040#define TPACPI_PARAM(feature) \
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008041 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
Henrique de Moraes Holschuhcbb14842008-02-16 02:17:50 -02008042 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008043 "at module load, see documentation")
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008044
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008045TPACPI_PARAM(hotkey);
8046TPACPI_PARAM(bluetooth);
8047TPACPI_PARAM(video);
8048TPACPI_PARAM(light);
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008049TPACPI_PARAM(cmos);
8050TPACPI_PARAM(led);
8051TPACPI_PARAM(beep);
8052TPACPI_PARAM(ecdump);
8053TPACPI_PARAM(brightness);
8054TPACPI_PARAM(volume);
8055TPACPI_PARAM(fan);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008056
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02008057#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
8058module_param(dbg_wlswemul, uint, 0);
8059MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
8060module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
8061MODULE_PARM_DESC(wlsw_state,
8062 "Initial state of the emulated WLSW switch");
8063
8064module_param(dbg_bluetoothemul, uint, 0);
8065MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
8066module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
8067MODULE_PARM_DESC(bluetooth_state,
8068 "Initial state of the emulated bluetooth switch");
8069
8070module_param(dbg_wwanemul, uint, 0);
8071MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
8072module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
8073MODULE_PARM_DESC(wwan_state,
8074 "Initial state of the emulated WWAN switch");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02008075
8076module_param(dbg_uwbemul, uint, 0);
8077MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
8078module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
8079MODULE_PARM_DESC(uwb_state,
8080 "Initial state of the emulated UWB switch");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02008081#endif
8082
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008083static void thinkpad_acpi_module_exit(void)
8084{
8085 struct ibm_struct *ibm, *itmp;
8086
8087 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
8088
8089 list_for_each_entry_safe_reverse(ibm, itmp,
8090 &tpacpi_all_drivers,
8091 all_drivers) {
8092 ibm_exit(ibm);
8093 }
8094
8095 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
8096
8097 if (tpacpi_inputdev) {
8098 if (tp_features.input_device_registered)
8099 input_unregister_device(tpacpi_inputdev);
8100 else
8101 input_free_device(tpacpi_inputdev);
8102 }
8103
8104 if (tpacpi_hwmon)
8105 hwmon_device_unregister(tpacpi_hwmon);
8106
8107 if (tp_features.sensors_pdev_attrs_registered)
8108 device_remove_file(&tpacpi_sensors_pdev->dev,
8109 &dev_attr_thinkpad_acpi_pdev_name);
8110 if (tpacpi_sensors_pdev)
8111 platform_device_unregister(tpacpi_sensors_pdev);
8112 if (tpacpi_pdev)
8113 platform_device_unregister(tpacpi_pdev);
8114
8115 if (tp_features.sensors_pdrv_attrs_registered)
8116 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
8117 if (tp_features.platform_drv_attrs_registered)
8118 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
8119
8120 if (tp_features.sensors_pdrv_registered)
8121 platform_driver_unregister(&tpacpi_hwmon_pdriver);
8122
8123 if (tp_features.platform_drv_registered)
8124 platform_driver_unregister(&tpacpi_pdriver);
8125
8126 if (proc_dir)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008127 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008128
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03008129 if (tpacpi_wq)
8130 destroy_workqueue(tpacpi_wq);
8131
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008132 kfree(thinkpad_id.bios_version_str);
8133 kfree(thinkpad_id.ec_version_str);
8134 kfree(thinkpad_id.model_str);
8135}
8136
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02008137
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03008138static int __init thinkpad_acpi_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008139{
8140 int ret, i;
8141
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03008142 tpacpi_lifecycle = TPACPI_LIFE_INIT;
8143
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03008144 /* Parameter checking */
8145 if (hotkey_report_mode > 2)
8146 return -EINVAL;
8147
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008148 /* Driver-level probe */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008149
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008150 ret = get_thinkpad_model_data(&thinkpad_id);
8151 if (ret) {
8152 printk(TPACPI_ERR
8153 "unable to get DMI data: %d\n", ret);
8154 thinkpad_acpi_module_exit();
8155 return ret;
8156 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008157 ret = probe_for_thinkpad();
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008158 if (ret) {
8159 thinkpad_acpi_module_exit();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008160 return ret;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008161 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008162
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008163 /* Driver initialization */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008164
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008165 TPACPI_ACPIHANDLE_INIT(ecrd);
8166 TPACPI_ACPIHANDLE_INIT(ecwr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008167
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03008168 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
8169 if (!tpacpi_wq) {
8170 thinkpad_acpi_module_exit();
8171 return -ENOMEM;
8172 }
8173
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008174 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008175 if (!proc_dir) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008176 printk(TPACPI_ERR
8177 "unable to create proc dir " TPACPI_PROC_DIR);
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03008178 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008179 return -ENODEV;
8180 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008181
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008182 ret = platform_driver_register(&tpacpi_pdriver);
8183 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008184 printk(TPACPI_ERR
8185 "unable to register main platform driver\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008186 thinkpad_acpi_module_exit();
8187 return ret;
8188 }
Henrique de Moraes Holschuhac363932007-07-27 17:04:40 -03008189 tp_features.platform_drv_registered = 1;
8190
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008191 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
8192 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008193 printk(TPACPI_ERR
8194 "unable to register hwmon platform driver\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008195 thinkpad_acpi_module_exit();
8196 return ret;
8197 }
8198 tp_features.sensors_pdrv_registered = 1;
8199
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03008200 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03008201 if (!ret) {
8202 tp_features.platform_drv_attrs_registered = 1;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008203 ret = tpacpi_create_driver_attributes(
8204 &tpacpi_hwmon_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03008205 }
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03008206 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008207 printk(TPACPI_ERR
8208 "unable to create sysfs driver attributes\n");
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03008209 thinkpad_acpi_module_exit();
8210 return ret;
8211 }
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03008212 tp_features.sensors_pdrv_attrs_registered = 1;
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03008213
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008214
8215 /* Device initialization */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008216 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008217 NULL, 0);
8218 if (IS_ERR(tpacpi_pdev)) {
8219 ret = PTR_ERR(tpacpi_pdev);
8220 tpacpi_pdev = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008221 printk(TPACPI_ERR "unable to register platform device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008222 thinkpad_acpi_module_exit();
8223 return ret;
8224 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008225 tpacpi_sensors_pdev = platform_device_register_simple(
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008226 TPACPI_HWMON_DRVR_NAME,
8227 -1, NULL, 0);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008228 if (IS_ERR(tpacpi_sensors_pdev)) {
8229 ret = PTR_ERR(tpacpi_sensors_pdev);
8230 tpacpi_sensors_pdev = NULL;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008231 printk(TPACPI_ERR
8232 "unable to register hwmon platform device\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008233 thinkpad_acpi_module_exit();
8234 return ret;
8235 }
8236 ret = device_create_file(&tpacpi_sensors_pdev->dev,
8237 &dev_attr_thinkpad_acpi_pdev_name);
8238 if (ret) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008239 printk(TPACPI_ERR
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008240 "unable to create sysfs hwmon device attributes\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008241 thinkpad_acpi_module_exit();
8242 return ret;
8243 }
8244 tp_features.sensors_pdev_attrs_registered = 1;
8245 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008246 if (IS_ERR(tpacpi_hwmon)) {
8247 ret = PTR_ERR(tpacpi_hwmon);
8248 tpacpi_hwmon = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008249 printk(TPACPI_ERR "unable to register hwmon device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008250 thinkpad_acpi_module_exit();
8251 return ret;
8252 }
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -03008253 mutex_init(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03008254 tpacpi_inputdev = input_allocate_device();
8255 if (!tpacpi_inputdev) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008256 printk(TPACPI_ERR "unable to allocate input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03008257 thinkpad_acpi_module_exit();
8258 return -ENOMEM;
8259 } else {
8260 /* Prepare input device, but don't register */
8261 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008262 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03008263 tpacpi_inputdev->id.bustype = BUS_HOST;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03008264 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
8265 thinkpad_id.vendor :
8266 PCI_VENDOR_ID_IBM;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03008267 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
8268 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
8269 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008270 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
8271 ret = ibm_init(&ibms_init[i]);
8272 if (ret >= 0 && *ibms_init[i].param)
8273 ret = ibms_init[i].data->write(ibms_init[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008274 if (ret < 0) {
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03008275 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008276 return ret;
8277 }
8278 }
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03008279 ret = input_register_device(tpacpi_inputdev);
8280 if (ret < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008281 printk(TPACPI_ERR "unable to register input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03008282 thinkpad_acpi_module_exit();
8283 return ret;
8284 } else {
8285 tp_features.input_device_registered = 1;
8286 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008287
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03008288 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008289 return 0;
8290}
8291
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03008292MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
8293
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008294/*
Henrique de Moraes Holschuh922fe092009-04-14 02:44:13 +00008295 * This will autoload the driver in almost every ThinkPad
8296 * in widespread use.
8297 *
8298 * Only _VERY_ old models, like the 240, 240x and 570 lack
8299 * the HKEY event interface.
8300 */
8301MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
8302
8303/*
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008304 * DMI matching for module autoloading
8305 *
8306 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
8307 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
8308 *
8309 * Only models listed in thinkwiki will be supported, so add yours
8310 * if it is not there yet.
8311 */
8312#define IBM_BIOS_MODULE_ALIAS(__type) \
Mathieu Chouquet-Stringerb36a50f2009-03-14 16:35:26 +01008313 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008314
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008315/* Ancient thinkpad BIOSes have to be identified by
8316 * BIOS type or model number, and there are far less
8317 * BIOS types than model numbers... */
Henrique de Moraes Holschuh922fe092009-04-14 02:44:13 +00008318IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008319
Henrique de Moraes Holschuhf68f53a2009-04-14 02:44:12 +00008320MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
8321MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008322MODULE_DESCRIPTION(TPACPI_DESC);
8323MODULE_VERSION(TPACPI_VERSION);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008324MODULE_LICENSE("GPL");
8325
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03008326module_init(thinkpad_acpi_module_init);
8327module_exit(thinkpad_acpi_module_exit);