blob: 66ba5f57d786fd793bee45341c6dd5b5a7bd0408 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -03002 * thinkpad_acpi.c - ThinkPad ACPI Extras
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 *
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005 * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
Henrique de Moraes Holschuh1c762ca2009-04-04 04:25:42 +00006 * Copyright (C) 2006-2009 Henrique de Moraes Holschuh <hmh@hmh.eng.br>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
Henrique de Moraes Holschuha62bc912007-03-23 17:33:58 -030020 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301, USA.
Borislav Deianov78f81cc2005-08-17 00:00:00 -040022 */
23
Henrique de Moraes Holschuhb57f7e72009-04-14 02:44:14 +000024#define TPACPI_VERSION "0.23"
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -030025#define TPACPI_SYSFS_VERSION 0x020400
Borislav Deianov78f81cc2005-08-17 00:00:00 -040026
27/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 * Changelog:
Henrique de Moraes Holschuh4b45cc02008-01-08 13:02:46 -020029 * 2007-10-20 changelog trimmed down
30 *
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -030031 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
32 * drivers/misc.
Henrique de Moraes Holschuhf9ff43a2006-11-25 16:37:38 -020033 *
34 * 2006-11-22 0.13 new maintainer
35 * changelog now lives in git commit history, and will
36 * not be updated further in-file.
Henrique de Moraes Holschuh837ca6d2007-03-23 17:33:54 -030037 *
Borislav Deianov78f81cc2005-08-17 00:00:00 -040038 * 2005-03-17 0.11 support for 600e, 770x
39 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
Henrique de Moraes Holschuh4b45cc02008-01-08 13:02:46 -020040 *
41 * 2005-01-16 0.9 use MODULE_VERSION
Borislav Deianov78f81cc2005-08-17 00:00:00 -040042 * thanks to Henrik Brix Andersen <brix@gentoo.org>
43 * fix parameter passing on module loading
44 * thanks to Rusty Russell <rusty@rustcorp.com.au>
45 * thanks to Jim Radford <radford@blackbean.org>
46 * 2004-11-08 0.8 fix init error case, don't return from a macro
47 * thanks to Chris Wright <chrisw@osdl.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 */
49
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020050#include <linux/kernel.h>
51#include <linux/module.h>
52#include <linux/init.h>
53#include <linux/types.h>
54#include <linux/string.h>
55#include <linux/list.h>
56#include <linux/mutex.h>
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +000057#include <linux/sched.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020058#include <linux/kthread.h>
59#include <linux/freezer.h>
60#include <linux/delay.h>
61
62#include <linux/nvram.h>
63#include <linux/proc_fs.h>
64#include <linux/sysfs.h>
65#include <linux/backlight.h>
66#include <linux/fb.h>
67#include <linux/platform_device.h>
68#include <linux/hwmon.h>
69#include <linux/hwmon-sysfs.h>
70#include <linux/input.h>
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -030071#include <linux/leds.h>
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -030072#include <linux/rfkill.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020073#include <asm/uaccess.h>
74
75#include <linux/dmi.h>
76#include <linux/jiffies.h>
77#include <linux/workqueue.h>
78
79#include <acpi/acpi_drivers.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020080
81#include <linux/pci_ids.h>
82
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020083
84/* ThinkPad CMOS commands */
85#define TP_CMOS_VOLUME_DOWN 0
86#define TP_CMOS_VOLUME_UP 1
87#define TP_CMOS_VOLUME_MUTE 2
88#define TP_CMOS_BRIGHTNESS_UP 4
89#define TP_CMOS_BRIGHTNESS_DOWN 5
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -030090#define TP_CMOS_THINKLIGHT_ON 12
91#define TP_CMOS_THINKLIGHT_OFF 13
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020092
93/* NVRAM Addresses */
94enum tp_nvram_addr {
95 TP_NVRAM_ADDR_HK2 = 0x57,
96 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
97 TP_NVRAM_ADDR_VIDEO = 0x59,
98 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
99 TP_NVRAM_ADDR_MIXER = 0x60,
100};
101
102/* NVRAM bit masks */
103enum {
104 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
105 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
106 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
107 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
108 TP_NVRAM_MASK_THINKLIGHT = 0x10,
109 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
110 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
111 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
112 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
113 TP_NVRAM_MASK_MUTE = 0x40,
114 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
115 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
116 TP_NVRAM_POS_LEVEL_VOLUME = 0,
117};
118
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200119/* ACPI HIDs */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200120#define TPACPI_ACPI_HKEY_HID "IBM0068"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200121
122/* Input IDs */
123#define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
124#define TPACPI_HKEY_INPUT_VERSION 0x4101
125
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -0200126/* ACPI \WGSV commands */
127enum {
128 TP_ACPI_WGSV_GET_STATE = 0x01, /* Get state information */
129 TP_ACPI_WGSV_PWR_ON_ON_RESUME = 0x02, /* Resume WWAN powered on */
130 TP_ACPI_WGSV_PWR_OFF_ON_RESUME = 0x03, /* Resume WWAN powered off */
131 TP_ACPI_WGSV_SAVE_STATE = 0x04, /* Save state for S4/S5 */
132};
133
134/* TP_ACPI_WGSV_GET_STATE bits */
135enum {
136 TP_ACPI_WGSV_STATE_WWANEXIST = 0x0001, /* WWAN hw available */
137 TP_ACPI_WGSV_STATE_WWANPWR = 0x0002, /* WWAN radio enabled */
138 TP_ACPI_WGSV_STATE_WWANPWRRES = 0x0004, /* WWAN state at resume */
139 TP_ACPI_WGSV_STATE_WWANBIOSOFF = 0x0008, /* WWAN disabled in BIOS */
140 TP_ACPI_WGSV_STATE_BLTHEXIST = 0x0001, /* BLTH hw available */
141 TP_ACPI_WGSV_STATE_BLTHPWR = 0x0002, /* BLTH radio enabled */
142 TP_ACPI_WGSV_STATE_BLTHPWRRES = 0x0004, /* BLTH state at resume */
143 TP_ACPI_WGSV_STATE_BLTHBIOSOFF = 0x0008, /* BLTH disabled in BIOS */
144 TP_ACPI_WGSV_STATE_UWBEXIST = 0x0010, /* UWB hw available */
145 TP_ACPI_WGSV_STATE_UWBPWR = 0x0020, /* UWB radio enabled */
146};
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200147
148/****************************************************************************
149 * Main driver
150 */
151
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200152#define TPACPI_NAME "thinkpad"
153#define TPACPI_DESC "ThinkPad ACPI Extras"
154#define TPACPI_FILE TPACPI_NAME "_acpi"
155#define TPACPI_URL "http://ibm-acpi.sf.net/"
156#define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200157
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200158#define TPACPI_PROC_DIR "ibm"
159#define TPACPI_ACPI_EVENT_PREFIX "ibm"
160#define TPACPI_DRVR_NAME TPACPI_FILE
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300161#define TPACPI_DRVR_SHORTNAME "tpacpi"
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200162#define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200163
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300164#define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -0300165#define TPACPI_WORKQUEUE_NAME "ktpacpid"
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300166
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200167#define TPACPI_MAX_ACPI_ARGS 3
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200168
Henrique de Moraes Holschuh7ff8d622009-04-04 04:25:46 +0000169/* printk headers */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200170#define TPACPI_LOG TPACPI_FILE ": "
Henrique de Moraes Holschuh7ff8d622009-04-04 04:25:46 +0000171#define TPACPI_EMERG KERN_EMERG TPACPI_LOG
172#define TPACPI_ALERT KERN_ALERT TPACPI_LOG
173#define TPACPI_CRIT KERN_CRIT TPACPI_LOG
174#define TPACPI_ERR KERN_ERR TPACPI_LOG
175#define TPACPI_WARN KERN_WARNING TPACPI_LOG
176#define TPACPI_NOTICE KERN_NOTICE TPACPI_LOG
177#define TPACPI_INFO KERN_INFO TPACPI_LOG
178#define TPACPI_DEBUG KERN_DEBUG TPACPI_LOG
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200179
Henrique de Moraes Holschuh7ff8d622009-04-04 04:25:46 +0000180/* Debugging printk groups */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200181#define TPACPI_DBG_ALL 0xffff
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +0000182#define TPACPI_DBG_DISCLOSETASK 0x8000
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200183#define TPACPI_DBG_INIT 0x0001
184#define TPACPI_DBG_EXIT 0x0002
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +0000185#define TPACPI_DBG_RFKILL 0x0004
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +0000186#define TPACPI_DBG_HKEY 0x0008
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +0000187#define TPACPI_DBG_FAN 0x0010
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +0000188#define TPACPI_DBG_BRGHT 0x0020
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200189
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200190#define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
191#define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
192#define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200193
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200194
195/****************************************************************************
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200196 * Driver-wide structs and misc. variables
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200197 */
198
199struct ibm_struct;
200
201struct tp_acpi_drv_struct {
202 const struct acpi_device_id *hid;
203 struct acpi_driver *driver;
204
205 void (*notify) (struct ibm_struct *, u32);
206 acpi_handle *handle;
207 u32 type;
208 struct acpi_device *device;
209};
210
211struct ibm_struct {
212 char *name;
213
214 int (*read) (char *);
215 int (*write) (char *);
216 void (*exit) (void);
217 void (*resume) (void);
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200218 void (*suspend) (pm_message_t state);
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200219 void (*shutdown) (void);
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200220
221 struct list_head all_drivers;
222
223 struct tp_acpi_drv_struct *acpi;
224
225 struct {
226 u8 acpi_driver_registered:1;
227 u8 acpi_notify_installed:1;
228 u8 proc_created:1;
229 u8 init_called:1;
230 u8 experimental:1;
231 } flags;
232};
233
234struct ibm_init_struct {
235 char param[32];
236
237 int (*init) (struct ibm_init_struct *);
238 struct ibm_struct *data;
239};
240
241static struct {
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200242 u32 bluetooth:1;
243 u32 hotkey:1;
244 u32 hotkey_mask:1;
245 u32 hotkey_wlsw:1;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -0200246 u32 hotkey_tablet:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200247 u32 light:1;
248 u32 light_status:1;
249 u32 bright_16levels:1;
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -0300250 u32 bright_acpimode:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200251 u32 wan:1;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -0200252 u32 uwb:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200253 u32 fan_ctrl_status_undef:1;
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -0300254 u32 second_fan:1;
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -0300255 u32 beep_needs_two_args:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200256 u32 input_device_registered:1;
257 u32 platform_drv_registered:1;
258 u32 platform_drv_attrs_registered:1;
259 u32 sensors_pdrv_registered:1;
260 u32 sensors_pdrv_attrs_registered:1;
261 u32 sensors_pdev_attrs_registered:1;
262 u32 hotkey_poll_active:1;
263} tp_features;
264
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -0300265static struct {
266 u16 hotkey_mask_ff:1;
267} tp_warned;
268
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200269struct thinkpad_id_data {
270 unsigned int vendor; /* ThinkPad vendor:
271 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
272
273 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
274 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
275
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -0300276 u16 bios_model; /* 1Y = 0x5931, 0 = unknown */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200277 u16 ec_model;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -0300278 u16 bios_release; /* 1ZETK1WW = 0x314b, 0 = unknown */
279 u16 ec_release;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200280
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -0300281 char *model_str; /* ThinkPad T43 */
282 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200283};
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200284static struct thinkpad_id_data thinkpad_id;
285
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300286static enum {
287 TPACPI_LIFE_INIT = 0,
288 TPACPI_LIFE_RUNNING,
289 TPACPI_LIFE_EXITING,
290} tpacpi_lifecycle;
291
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200292static int experimental;
293static u32 dbg_level;
294
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -0300295static struct workqueue_struct *tpacpi_wq;
296
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +0000297enum led_status_t {
298 TPACPI_LED_OFF = 0,
299 TPACPI_LED_ON,
300 TPACPI_LED_BLINK,
301};
302
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -0300303/* Special LED class that can defer work */
304struct tpacpi_led_classdev {
305 struct led_classdev led_classdev;
306 struct work_struct work;
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +0000307 enum led_status_t new_state;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -0300308 unsigned int led;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -0300309};
310
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200311#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
312static int dbg_wlswemul;
313static int tpacpi_wlsw_emulstate;
314static int dbg_bluetoothemul;
315static int tpacpi_bluetooth_emulstate;
316static int dbg_wwanemul;
317static int tpacpi_wwan_emulstate;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -0200318static int dbg_uwbemul;
319static int tpacpi_uwb_emulstate;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200320#endif
321
322
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000323/*************************************************************************
324 * Debugging helpers
325 */
326
327#define dbg_printk(a_dbg_level, format, arg...) \
328 do { if (dbg_level & (a_dbg_level)) \
329 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
330 } while (0)
331
332#ifdef CONFIG_THINKPAD_ACPI_DEBUG
333#define vdbg_printk dbg_printk
334static const char *str_supported(int is_supported);
335#else
336#define vdbg_printk(a_dbg_level, format, arg...) \
337 do { } while (0)
338#endif
339
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +0000340static void tpacpi_log_usertask(const char * const what)
341{
342 printk(TPACPI_DEBUG "%s: access by process with PID %d\n",
343 what, task_tgid_vnr(current));
344}
345
346#define tpacpi_disclose_usertask(what, format, arg...) \
347 do { \
348 if (unlikely( \
349 (dbg_level & TPACPI_DBG_DISCLOSETASK) && \
350 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
351 printk(TPACPI_DEBUG "%s: PID %d: " format, \
352 what, task_tgid_vnr(current), ## arg); \
353 } \
354 } while (0)
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000355
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -0300356/*
357 * Quirk handling helpers
358 *
359 * ThinkPad IDs and versions seen in the field so far
360 * are two-characters from the set [0-9A-Z], i.e. base 36.
361 *
362 * We use values well outside that range as specials.
363 */
364
365#define TPACPI_MATCH_ANY 0xffffU
366#define TPACPI_MATCH_UNKNOWN 0U
367
368/* TPID('1', 'Y') == 0x5931 */
369#define TPID(__c1, __c2) (((__c2) << 8) | (__c1))
370
371#define TPACPI_Q_IBM(__id1, __id2, __quirk) \
372 { .vendor = PCI_VENDOR_ID_IBM, \
373 .bios = TPID(__id1, __id2), \
374 .ec = TPACPI_MATCH_ANY, \
375 .quirks = (__quirk) }
376
377#define TPACPI_Q_LNV(__id1, __id2, __quirk) \
378 { .vendor = PCI_VENDOR_ID_LENOVO, \
379 .bios = TPID(__id1, __id2), \
380 .ec = TPACPI_MATCH_ANY, \
381 .quirks = (__quirk) }
382
383struct tpacpi_quirk {
384 unsigned int vendor;
385 u16 bios;
386 u16 ec;
387 unsigned long quirks;
388};
389
390/**
391 * tpacpi_check_quirks() - search BIOS/EC version on a list
392 * @qlist: array of &struct tpacpi_quirk
393 * @qlist_size: number of elements in @qlist
394 *
395 * Iterates over a quirks list until one is found that matches the
396 * ThinkPad's vendor, BIOS and EC model.
397 *
398 * Returns 0 if nothing matches, otherwise returns the quirks field of
399 * the matching &struct tpacpi_quirk entry.
400 *
401 * The match criteria is: vendor, ec and bios much match.
402 */
403static unsigned long __init tpacpi_check_quirks(
404 const struct tpacpi_quirk *qlist,
405 unsigned int qlist_size)
406{
407 while (qlist_size) {
408 if ((qlist->vendor == thinkpad_id.vendor ||
409 qlist->vendor == TPACPI_MATCH_ANY) &&
410 (qlist->bios == thinkpad_id.bios_model ||
411 qlist->bios == TPACPI_MATCH_ANY) &&
412 (qlist->ec == thinkpad_id.ec_model ||
413 qlist->ec == TPACPI_MATCH_ANY))
414 return qlist->quirks;
415
416 qlist_size--;
417 qlist++;
418 }
419 return 0;
420}
421
422
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300423/****************************************************************************
424 ****************************************************************************
425 *
426 * ACPI Helpers and device model
427 *
428 ****************************************************************************
429 ****************************************************************************/
430
431/*************************************************************************
432 * ACPI basic handles
433 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -0300435static acpi_handle root_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200437#define TPACPI_HANDLE(object, parent, paths...) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 static acpi_handle object##_handle; \
439 static acpi_handle *object##_parent = &parent##_handle; \
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400440 static char *object##_path; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 static char *object##_paths[] = { paths }
442
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200443TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400444 "\\_SB.PCI.ISA.EC", /* 570 */
445 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
446 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
447 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
448 "\\_SB.PCI0.ICH3.EC0", /* R31 */
449 "\\_SB.PCI0.LPC.EC", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300450 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200452TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
453TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200455TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
456 /* T4x, X31, X40 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400457 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
458 "\\CMS", /* R40, R40e */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300459 ); /* all others */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400460
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200461TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400462 "^HKEY", /* R30, R31 */
463 "HKEY", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300464 ); /* 570 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400465
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -0200466TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
467 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
468 "\\_SB.PCI0.VID0", /* 770e */
469 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
470 "\\_SB.PCI0.AGP.VID", /* all others */
471 ); /* R30, R31 */
472
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400473
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300474/*************************************************************************
475 * ACPI helpers
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -0200476 */
477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478static int acpi_evalf(acpi_handle handle,
479 void *res, char *method, char *fmt, ...)
480{
481 char *fmt0 = fmt;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400482 struct acpi_object_list params;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200483 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400484 struct acpi_buffer result, *resultp;
485 union acpi_object out_obj;
486 acpi_status status;
487 va_list ap;
488 char res_type;
489 int success;
490 int quiet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
492 if (!*fmt) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200493 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 return 0;
495 }
496
497 if (*fmt == 'q') {
498 quiet = 1;
499 fmt++;
500 } else
501 quiet = 0;
502
503 res_type = *(fmt++);
504
505 params.count = 0;
506 params.pointer = &in_objs[0];
507
508 va_start(ap, fmt);
509 while (*fmt) {
510 char c = *(fmt++);
511 switch (c) {
512 case 'd': /* int */
513 in_objs[params.count].integer.value = va_arg(ap, int);
514 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
515 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400516 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 default:
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200518 printk(TPACPI_ERR "acpi_evalf() called "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 "with invalid format character '%c'\n", c);
520 return 0;
521 }
522 }
523 va_end(ap);
524
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400525 if (res_type != 'v') {
526 result.length = sizeof(out_obj);
527 result.pointer = &out_obj;
528 resultp = &result;
529 } else
530 resultp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400532 status = acpi_evaluate_object(handle, method, &params, resultp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
534 switch (res_type) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400535 case 'd': /* int */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 if (res)
537 *(int *)res = out_obj.integer.value;
538 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
539 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400540 case 'v': /* void */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 success = status == AE_OK;
542 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400543 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 default:
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200545 printk(TPACPI_ERR "acpi_evalf() called "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 "with invalid format character '%c'\n", res_type);
547 return 0;
548 }
549
550 if (!success && !quiet)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200551 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 method, fmt0, status);
553
554 return success;
555}
556
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200557static int acpi_ec_read(int i, u8 *p)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300558{
559 int v;
560
561 if (ecrd_handle) {
562 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
563 return 0;
564 *p = v;
565 } else {
566 if (ec_read(i, p) < 0)
567 return 0;
568 }
569
570 return 1;
571}
572
573static int acpi_ec_write(int i, u8 v)
574{
575 if (ecwr_handle) {
576 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
577 return 0;
578 } else {
579 if (ec_write(i, v) < 0)
580 return 0;
581 }
582
583 return 1;
584}
585
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -0300586static int issue_thinkpad_cmos_command(int cmos_cmd)
587{
588 if (!cmos_handle)
589 return -ENXIO;
590
591 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
592 return -EIO;
593
594 return 0;
595}
596
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300597/*************************************************************************
598 * ACPI device model
599 */
600
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200601#define TPACPI_ACPIHANDLE_INIT(object) \
602 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200603 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
604
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300605static void drv_acpi_handle_init(char *name,
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300606 acpi_handle *handle, acpi_handle parent,
607 char **paths, int num_paths, char **path)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300608{
609 int i;
610 acpi_status status;
611
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300612 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
613 name);
614
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300615 for (i = 0; i < num_paths; i++) {
616 status = acpi_get_handle(parent, paths[i], handle);
617 if (ACPI_SUCCESS(status)) {
618 *path = paths[i];
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300619 dbg_printk(TPACPI_DBG_INIT,
620 "Found ACPI handle %s for %s\n",
621 *path, name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300622 return;
623 }
624 }
625
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300626 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
627 name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300628 *handle = NULL;
629}
630
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300631static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300632{
633 struct ibm_struct *ibm = data;
634
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300635 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
636 return;
637
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300638 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300639 return;
640
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300641 ibm->acpi->notify(ibm, event);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300642}
643
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300644static int __init setup_acpi_notify(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300645{
646 acpi_status status;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300647 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300648
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300649 BUG_ON(!ibm->acpi);
650
651 if (!*ibm->acpi->handle)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300652 return 0;
653
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300654 vdbg_printk(TPACPI_DBG_INIT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300655 "setting up ACPI notify for %s\n", ibm->name);
656
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300657 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
658 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200659 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300660 ibm->name, rc);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300661 return -ENODEV;
662 }
663
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700664 ibm->acpi->device->driver_data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300665 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200666 TPACPI_ACPI_EVENT_PREFIX,
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -0300667 ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300668
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300669 status = acpi_install_notify_handler(*ibm->acpi->handle,
670 ibm->acpi->type, dispatch_acpi_notify, ibm);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300671 if (ACPI_FAILURE(status)) {
672 if (status == AE_ALREADY_EXISTS) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200673 printk(TPACPI_NOTICE
674 "another device driver is already "
675 "handling %s events\n", ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300676 } else {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200677 printk(TPACPI_ERR
678 "acpi_install_notify_handler(%s) failed: %d\n",
679 ibm->name, status);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300680 }
681 return -ENODEV;
682 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300683 ibm->flags.acpi_notify_installed = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300684 return 0;
685}
686
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300687static int __init tpacpi_device_add(struct acpi_device *device)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300688{
689 return 0;
690}
691
Henrique de Moraes Holschuh67001212007-04-21 11:08:25 -0300692static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300693{
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300694 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300695
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300696 dbg_printk(TPACPI_DBG_INIT,
697 "registering %s as an ACPI driver\n", ibm->name);
698
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300699 BUG_ON(!ibm->acpi);
700
701 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
702 if (!ibm->acpi->driver) {
Mariusz Kozlowski4f778b92008-10-18 14:23:53 -0300703 printk(TPACPI_ERR
704 "failed to allocate memory for ibm->acpi->driver\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300705 return -ENOMEM;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300706 }
707
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200708 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300709 ibm->acpi->driver->ids = ibm->acpi->hid;
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200710
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300711 ibm->acpi->driver->ops.add = &tpacpi_device_add;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300712
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300713 rc = acpi_bus_register_driver(ibm->acpi->driver);
714 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200715 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200716 ibm->name, rc);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300717 kfree(ibm->acpi->driver);
718 ibm->acpi->driver = NULL;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300719 } else if (!rc)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300720 ibm->flags.acpi_driver_registered = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300721
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300722 return rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300723}
724
725
726/****************************************************************************
727 ****************************************************************************
728 *
729 * Procfs Helpers
730 *
731 ****************************************************************************
732 ****************************************************************************/
733
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300734static int dispatch_procfs_read(char *page, char **start, off_t off,
735 int count, int *eof, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300736{
737 struct ibm_struct *ibm = data;
738 int len;
739
740 if (!ibm || !ibm->read)
741 return -EINVAL;
742
743 len = ibm->read(page);
744 if (len < 0)
745 return len;
746
747 if (len <= off + count)
748 *eof = 1;
749 *start = page + off;
750 len -= off;
751 if (len > count)
752 len = count;
753 if (len < 0)
754 len = 0;
755
756 return len;
757}
758
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300759static int dispatch_procfs_write(struct file *file,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200760 const char __user *userbuf,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300761 unsigned long count, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300762{
763 struct ibm_struct *ibm = data;
764 char *kernbuf;
765 int ret;
766
767 if (!ibm || !ibm->write)
768 return -EINVAL;
Michael Buesch5b05d462009-08-01 12:04:19 -0300769 if (count > PAGE_SIZE - 2)
770 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300771
772 kernbuf = kmalloc(count + 2, GFP_KERNEL);
773 if (!kernbuf)
774 return -ENOMEM;
775
776 if (copy_from_user(kernbuf, userbuf, count)) {
777 kfree(kernbuf);
778 return -EFAULT;
779 }
780
781 kernbuf[count] = 0;
782 strcat(kernbuf, ",");
783 ret = ibm->write(kernbuf);
784 if (ret == 0)
785 ret = count;
786
787 kfree(kernbuf);
788
789 return ret;
790}
791
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792static char *next_cmd(char **cmds)
793{
794 char *start = *cmds;
795 char *end;
796
797 while ((end = strchr(start, ',')) && end == start)
798 start = end + 1;
799
800 if (!end)
801 return NULL;
802
803 *end = 0;
804 *cmds = end + 1;
805 return start;
806}
807
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300808
809/****************************************************************************
810 ****************************************************************************
811 *
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300812 * Device model: input, hwmon and platform
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300813 *
814 ****************************************************************************
815 ****************************************************************************/
816
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -0300817static struct platform_device *tpacpi_pdev;
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300818static struct platform_device *tpacpi_sensors_pdev;
Tony Jones1beeffe2007-08-20 13:46:20 -0700819static struct device *tpacpi_hwmon;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300820static struct input_dev *tpacpi_inputdev;
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -0300821static struct mutex tpacpi_inputdev_send_mutex;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200822static LIST_HEAD(tpacpi_all_drivers);
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300823
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200824static int tpacpi_suspend_handler(struct platform_device *pdev,
825 pm_message_t state)
826{
827 struct ibm_struct *ibm, *itmp;
828
829 list_for_each_entry_safe(ibm, itmp,
830 &tpacpi_all_drivers,
831 all_drivers) {
832 if (ibm->suspend)
833 (ibm->suspend)(state);
834 }
835
836 return 0;
837}
838
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300839static int tpacpi_resume_handler(struct platform_device *pdev)
840{
841 struct ibm_struct *ibm, *itmp;
842
843 list_for_each_entry_safe(ibm, itmp,
844 &tpacpi_all_drivers,
845 all_drivers) {
846 if (ibm->resume)
847 (ibm->resume)();
848 }
849
850 return 0;
851}
852
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200853static void tpacpi_shutdown_handler(struct platform_device *pdev)
854{
855 struct ibm_struct *ibm, *itmp;
856
857 list_for_each_entry_safe(ibm, itmp,
858 &tpacpi_all_drivers,
859 all_drivers) {
860 if (ibm->shutdown)
861 (ibm->shutdown)();
862 }
863}
864
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300865static struct platform_driver tpacpi_pdriver = {
866 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200867 .name = TPACPI_DRVR_NAME,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300868 .owner = THIS_MODULE,
869 },
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200870 .suspend = tpacpi_suspend_handler,
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300871 .resume = tpacpi_resume_handler,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200872 .shutdown = tpacpi_shutdown_handler,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300873};
874
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300875static struct platform_driver tpacpi_hwmon_pdriver = {
876 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200877 .name = TPACPI_HWMON_DRVR_NAME,
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300878 .owner = THIS_MODULE,
879 },
880};
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300881
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -0300882/*************************************************************************
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300883 * sysfs support helpers
884 */
885
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200886struct attribute_set {
887 unsigned int members, max_members;
888 struct attribute_group group;
889};
890
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300891struct attribute_set_obj {
892 struct attribute_set s;
893 struct attribute *a;
894} __attribute__((packed));
895
896static struct attribute_set *create_attr_set(unsigned int max_members,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200897 const char *name)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300898{
899 struct attribute_set_obj *sobj;
900
901 if (max_members == 0)
902 return NULL;
903
904 /* Allocates space for implicit NULL at the end too */
905 sobj = kzalloc(sizeof(struct attribute_set_obj) +
906 max_members * sizeof(struct attribute *),
907 GFP_KERNEL);
908 if (!sobj)
909 return NULL;
910 sobj->s.max_members = max_members;
911 sobj->s.group.attrs = &sobj->a;
912 sobj->s.group.name = name;
913
914 return &sobj->s;
915}
916
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200917#define destroy_attr_set(_set) \
918 kfree(_set);
919
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300920/* not multi-threaded safe, use it in a single thread per set */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200921static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300922{
923 if (!s || !attr)
924 return -EINVAL;
925
926 if (s->members >= s->max_members)
927 return -ENOMEM;
928
929 s->group.attrs[s->members] = attr;
930 s->members++;
931
932 return 0;
933}
934
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200935static int add_many_to_attr_set(struct attribute_set *s,
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300936 struct attribute **attr,
937 unsigned int count)
938{
939 int i, res;
940
941 for (i = 0; i < count; i++) {
942 res = add_to_attr_set(s, attr[i]);
943 if (res)
944 return res;
945 }
946
947 return 0;
948}
949
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200950static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300951{
952 sysfs_remove_group(kobj, &s->group);
953 destroy_attr_set(s);
954}
955
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200956#define register_attr_set_with_sysfs(_attr_set, _kobj) \
957 sysfs_create_group(_kobj, &_attr_set->group)
958
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300959static int parse_strtoul(const char *buf,
960 unsigned long max, unsigned long *value)
961{
962 char *endp;
963
Henrique de Moraes Holschuh32afbf02007-10-08 10:12:56 -0300964 while (*buf && isspace(*buf))
965 buf++;
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300966 *value = simple_strtoul(buf, &endp, 0);
967 while (*endp && isspace(*endp))
968 endp++;
969 if (*endp || *value > max)
970 return -EINVAL;
971
972 return 0;
973}
974
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -0300975static void tpacpi_disable_brightness_delay(void)
976{
977 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
978 printk(TPACPI_NOTICE
979 "ACPI backlight control delay disabled\n");
980}
981
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -0300982static int __init tpacpi_query_bcl_levels(acpi_handle handle)
983{
984 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
985 union acpi_object *obj;
986 int rc;
987
988 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
989 obj = (union acpi_object *)buffer.pointer;
990 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
991 printk(TPACPI_ERR "Unknown _BCL data, "
992 "please report this to %s\n", TPACPI_MAIL);
993 rc = 0;
994 } else {
995 rc = obj->package.count;
996 }
997 } else {
998 return 0;
999 }
1000
1001 kfree(buffer.pointer);
1002 return rc;
1003}
1004
1005static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
1006 u32 lvl, void *context, void **rv)
1007{
1008 char name[ACPI_PATH_SEGMENT_LENGTH];
1009 struct acpi_buffer buffer = { sizeof(name), &name };
1010
1011 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
1012 !strncmp("_BCL", name, sizeof(name) - 1)) {
1013 BUG_ON(!rv || !*rv);
1014 **(int **)rv = tpacpi_query_bcl_levels(handle);
1015 return AE_CTRL_TERMINATE;
1016 } else {
1017 return AE_OK;
1018 }
1019}
1020
1021/*
1022 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
1023 */
1024static int __init tpacpi_check_std_acpi_brightness_support(void)
1025{
1026 int status;
1027 int bcl_levels = 0;
1028 void *bcl_ptr = &bcl_levels;
1029
1030 if (!vid_handle) {
1031 TPACPI_ACPIHANDLE_INIT(vid);
1032 }
1033 if (!vid_handle)
1034 return 0;
1035
1036 /*
1037 * Search for a _BCL method, and execute it. This is safe on all
1038 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
1039 * BIOS in ACPI backlight control mode. We do NOT have to care
1040 * about calling the _BCL method in an enabled video device, any
1041 * will do for our purposes.
1042 */
1043
1044 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
1045 tpacpi_acpi_walk_find_bcl, NULL,
1046 &bcl_ptr);
1047
1048 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
1049 tp_features.bright_acpimode = 1;
1050 return (bcl_levels - 2);
1051 }
1052
1053 return 0;
1054}
1055
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00001056static void printk_deprecated_attribute(const char * const what,
1057 const char * const details)
1058{
1059 tpacpi_log_usertask("deprecated sysfs attribute");
1060 printk(TPACPI_WARN "WARNING: sysfs attribute %s is deprecated and "
1061 "will be removed. %s\n",
1062 what, details);
1063}
1064
Johannes Berg19d337d2009-06-02 13:01:37 +02001065/*************************************************************************
1066 * rfkill and radio control support helpers
1067 */
1068
1069/*
1070 * ThinkPad-ACPI firmware handling model:
1071 *
1072 * WLSW (master wireless switch) is event-driven, and is common to all
1073 * firmware-controlled radios. It cannot be controlled, just monitored,
1074 * as expected. It overrides all radio state in firmware
1075 *
1076 * The kernel, a masked-off hotkey, and WLSW can change the radio state
1077 * (TODO: verify how WLSW interacts with the returned radio state).
1078 *
1079 * The only time there are shadow radio state changes, is when
1080 * masked-off hotkeys are used.
1081 */
1082
1083/*
1084 * Internal driver API for radio state:
1085 *
1086 * int: < 0 = error, otherwise enum tpacpi_rfkill_state
1087 * bool: true means radio blocked (off)
1088 */
1089enum tpacpi_rfkill_state {
1090 TPACPI_RFK_RADIO_OFF = 0,
1091 TPACPI_RFK_RADIO_ON
1092};
1093
1094/* rfkill switches */
1095enum tpacpi_rfk_id {
1096 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
1097 TPACPI_RFK_WWAN_SW_ID,
1098 TPACPI_RFK_UWB_SW_ID,
1099 TPACPI_RFK_SW_MAX
1100};
1101
1102static const char *tpacpi_rfkill_names[] = {
1103 [TPACPI_RFK_BLUETOOTH_SW_ID] = "bluetooth",
1104 [TPACPI_RFK_WWAN_SW_ID] = "wwan",
1105 [TPACPI_RFK_UWB_SW_ID] = "uwb",
1106 [TPACPI_RFK_SW_MAX] = NULL
1107};
1108
1109/* ThinkPad-ACPI rfkill subdriver */
1110struct tpacpi_rfk {
1111 struct rfkill *rfkill;
1112 enum tpacpi_rfk_id id;
1113 const struct tpacpi_rfk_ops *ops;
1114};
1115
1116struct tpacpi_rfk_ops {
1117 /* firmware interface */
1118 int (*get_status)(void);
1119 int (*set_status)(const enum tpacpi_rfkill_state);
1120};
1121
1122static struct tpacpi_rfk *tpacpi_rfkill_switches[TPACPI_RFK_SW_MAX];
1123
1124/* Query FW and update rfkill sw state for a given rfkill switch */
1125static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk *tp_rfk)
1126{
1127 int status;
1128
1129 if (!tp_rfk)
1130 return -ENODEV;
1131
1132 status = (tp_rfk->ops->get_status)();
1133 if (status < 0)
1134 return status;
1135
1136 rfkill_set_sw_state(tp_rfk->rfkill,
1137 (status == TPACPI_RFK_RADIO_OFF));
1138
1139 return status;
1140}
1141
1142/* Query FW and update rfkill sw state for all rfkill switches */
1143static void tpacpi_rfk_update_swstate_all(void)
1144{
1145 unsigned int i;
1146
1147 for (i = 0; i < TPACPI_RFK_SW_MAX; i++)
1148 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[i]);
1149}
1150
1151/*
1152 * Sync the HW-blocking state of all rfkill switches,
1153 * do notice it causes the rfkill core to schedule uevents
1154 */
1155static void tpacpi_rfk_update_hwblock_state(bool blocked)
1156{
1157 unsigned int i;
1158 struct tpacpi_rfk *tp_rfk;
1159
1160 for (i = 0; i < TPACPI_RFK_SW_MAX; i++) {
1161 tp_rfk = tpacpi_rfkill_switches[i];
1162 if (tp_rfk) {
1163 if (rfkill_set_hw_state(tp_rfk->rfkill,
1164 blocked)) {
1165 /* ignore -- we track sw block */
1166 }
1167 }
1168 }
1169}
1170
1171/* Call to get the WLSW state from the firmware */
1172static int hotkey_get_wlsw(void);
1173
1174/* Call to query WLSW state and update all rfkill switches */
1175static bool tpacpi_rfk_check_hwblock_state(void)
1176{
1177 int res = hotkey_get_wlsw();
1178 int hw_blocked;
1179
1180 /* When unknown or unsupported, we have to assume it is unblocked */
1181 if (res < 0)
1182 return false;
1183
1184 hw_blocked = (res == TPACPI_RFK_RADIO_OFF);
1185 tpacpi_rfk_update_hwblock_state(hw_blocked);
1186
1187 return hw_blocked;
1188}
1189
1190static int tpacpi_rfk_hook_set_block(void *data, bool blocked)
1191{
1192 struct tpacpi_rfk *tp_rfk = data;
1193 int res;
1194
1195 dbg_printk(TPACPI_DBG_RFKILL,
1196 "request to change radio state to %s\n",
1197 blocked ? "blocked" : "unblocked");
1198
1199 /* try to set radio state */
1200 res = (tp_rfk->ops->set_status)(blocked ?
1201 TPACPI_RFK_RADIO_OFF : TPACPI_RFK_RADIO_ON);
1202
1203 /* and update the rfkill core with whatever the FW really did */
1204 tpacpi_rfk_update_swstate(tp_rfk);
1205
1206 return (res < 0) ? res : 0;
1207}
1208
1209static const struct rfkill_ops tpacpi_rfk_rfkill_ops = {
1210 .set_block = tpacpi_rfk_hook_set_block,
1211};
1212
1213static int __init tpacpi_new_rfkill(const enum tpacpi_rfk_id id,
1214 const struct tpacpi_rfk_ops *tp_rfkops,
1215 const enum rfkill_type rfktype,
1216 const char *name,
1217 const bool set_default)
1218{
1219 struct tpacpi_rfk *atp_rfk;
1220 int res;
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001221 bool sw_state = false;
1222 int sw_status;
Johannes Berg19d337d2009-06-02 13:01:37 +02001223
1224 BUG_ON(id >= TPACPI_RFK_SW_MAX || tpacpi_rfkill_switches[id]);
1225
Johannes Berg19d337d2009-06-02 13:01:37 +02001226 atp_rfk = kzalloc(sizeof(struct tpacpi_rfk), GFP_KERNEL);
1227 if (atp_rfk)
1228 atp_rfk->rfkill = rfkill_alloc(name,
1229 &tpacpi_pdev->dev,
1230 rfktype,
1231 &tpacpi_rfk_rfkill_ops,
1232 atp_rfk);
1233 if (!atp_rfk || !atp_rfk->rfkill) {
1234 printk(TPACPI_ERR
1235 "failed to allocate memory for rfkill class\n");
1236 kfree(atp_rfk);
1237 return -ENOMEM;
1238 }
1239
1240 atp_rfk->id = id;
1241 atp_rfk->ops = tp_rfkops;
1242
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001243 sw_status = (tp_rfkops->get_status)();
1244 if (sw_status < 0) {
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001245 printk(TPACPI_ERR
1246 "failed to read initial state for %s, error %d\n",
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001247 name, sw_status);
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001248 } else {
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001249 sw_state = (sw_status == TPACPI_RFK_RADIO_OFF);
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001250 if (set_default) {
1251 /* try to keep the initial state, since we ask the
1252 * firmware to preserve it across S5 in NVRAM */
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001253 rfkill_init_sw_state(atp_rfk->rfkill, sw_state);
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001254 }
1255 }
1256 rfkill_set_hw_state(atp_rfk->rfkill, tpacpi_rfk_check_hwblock_state());
Johannes Berg19d337d2009-06-02 13:01:37 +02001257
1258 res = rfkill_register(atp_rfk->rfkill);
1259 if (res < 0) {
1260 printk(TPACPI_ERR
1261 "failed to register %s rfkill switch: %d\n",
1262 name, res);
1263 rfkill_destroy(atp_rfk->rfkill);
1264 kfree(atp_rfk);
1265 return res;
1266 }
1267
1268 tpacpi_rfkill_switches[id] = atp_rfk;
1269 return 0;
1270}
1271
1272static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id)
1273{
1274 struct tpacpi_rfk *tp_rfk;
1275
1276 BUG_ON(id >= TPACPI_RFK_SW_MAX);
1277
1278 tp_rfk = tpacpi_rfkill_switches[id];
1279 if (tp_rfk) {
1280 rfkill_unregister(tp_rfk->rfkill);
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 Holschuhf74a27d2008-01-08 13:02:44 -02001851enum { /* hot key scan codes (derived from ACPI DSDT) */
1852 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1853 TP_ACPI_HOTKEYSCAN_FNF2,
1854 TP_ACPI_HOTKEYSCAN_FNF3,
1855 TP_ACPI_HOTKEYSCAN_FNF4,
1856 TP_ACPI_HOTKEYSCAN_FNF5,
1857 TP_ACPI_HOTKEYSCAN_FNF6,
1858 TP_ACPI_HOTKEYSCAN_FNF7,
1859 TP_ACPI_HOTKEYSCAN_FNF8,
1860 TP_ACPI_HOTKEYSCAN_FNF9,
1861 TP_ACPI_HOTKEYSCAN_FNF10,
1862 TP_ACPI_HOTKEYSCAN_FNF11,
1863 TP_ACPI_HOTKEYSCAN_FNF12,
1864 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1865 TP_ACPI_HOTKEYSCAN_FNINSERT,
1866 TP_ACPI_HOTKEYSCAN_FNDELETE,
1867 TP_ACPI_HOTKEYSCAN_FNHOME,
1868 TP_ACPI_HOTKEYSCAN_FNEND,
1869 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1870 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1871 TP_ACPI_HOTKEYSCAN_FNSPACE,
1872 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1873 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1874 TP_ACPI_HOTKEYSCAN_MUTE,
1875 TP_ACPI_HOTKEYSCAN_THINKPAD,
1876};
1877
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001878enum { /* Keys available through NVRAM polling */
1879 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1880 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1881};
1882
1883enum { /* Positions of some of the keys in hotkey masks */
1884 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1885 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1886 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1887 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1888 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1889 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1890 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1891 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1892 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1893 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1894 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1895};
1896
1897enum { /* NVRAM to ACPI HKEY group map */
1898 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1899 TP_ACPI_HKEY_ZOOM_MASK |
1900 TP_ACPI_HKEY_DISPSWTCH_MASK |
1901 TP_ACPI_HKEY_HIBERNATE_MASK,
1902 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1903 TP_ACPI_HKEY_BRGHTDWN_MASK,
1904 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1905 TP_ACPI_HKEY_VOLDWN_MASK |
1906 TP_ACPI_HKEY_MUTE_MASK,
1907};
1908
1909#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1910struct tp_nvram_state {
1911 u16 thinkpad_toggle:1;
1912 u16 zoom_toggle:1;
1913 u16 display_toggle:1;
1914 u16 thinklight_toggle:1;
1915 u16 hibernate_toggle:1;
1916 u16 displayexp_toggle:1;
1917 u16 display_state:1;
1918 u16 brightness_toggle:1;
1919 u16 volume_toggle:1;
1920 u16 mute:1;
1921
1922 u8 brightness_level;
1923 u8 volume_level;
1924};
1925
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001926/* kthread for the hotkey poller */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001927static struct task_struct *tpacpi_hotkey_task;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001928
1929/* Acquired while the poller kthread is running, use to sync start/stop */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001930static struct mutex hotkey_thread_mutex;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001931
1932/*
1933 * Acquire mutex to write poller control variables.
1934 * Increment hotkey_config_change when changing them.
1935 *
1936 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1937 */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001938static struct mutex hotkey_thread_data_mutex;
1939static unsigned int hotkey_config_change;
1940
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001941/*
1942 * hotkey poller control variables
1943 *
1944 * Must be atomic or readers will also need to acquire mutex
1945 */
1946static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1947static unsigned int hotkey_poll_freq = 10; /* Hz */
1948
1949#define HOTKEY_CONFIG_CRITICAL_START \
1950 do { \
1951 mutex_lock(&hotkey_thread_data_mutex); \
1952 hotkey_config_change++; \
1953 } while (0);
1954#define HOTKEY_CONFIG_CRITICAL_END \
1955 mutex_unlock(&hotkey_thread_data_mutex);
1956
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001957#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1958
1959#define hotkey_source_mask 0U
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001960#define HOTKEY_CONFIG_CRITICAL_START
1961#define HOTKEY_CONFIG_CRITICAL_END
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001962
1963#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1964
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001965static struct mutex hotkey_mutex;
1966
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02001967static enum { /* Reasons for waking up */
1968 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1969 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1970 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1971} hotkey_wakeup_reason;
1972
1973static int hotkey_autosleep_ack;
1974
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001975static u32 hotkey_orig_mask;
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001976static u32 hotkey_all_mask;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001977static u32 hotkey_reserved_mask;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001978static u32 hotkey_mask;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001979
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001980static unsigned int hotkey_report_mode;
1981
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001982static u16 *hotkey_keycode_map;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001983
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -03001984static struct attribute_set *hotkey_dev_attributes;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001985
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001986/* HKEY.MHKG() return bits */
1987#define TP_HOTKEY_TABLET_MASK (1 << 3)
1988
Johannes Berg19d337d2009-06-02 13:01:37 +02001989static int hotkey_get_wlsw(void)
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001990{
Johannes Berg19d337d2009-06-02 13:01:37 +02001991 int status;
1992
1993 if (!tp_features.hotkey_wlsw)
1994 return -ENODEV;
1995
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001996#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
Johannes Berg19d337d2009-06-02 13:01:37 +02001997 if (dbg_wlswemul)
1998 return (tpacpi_wlsw_emulstate) ?
1999 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02002000#endif
Johannes Berg19d337d2009-06-02 13:01:37 +02002001
2002 if (!acpi_evalf(hkey_handle, &status, "WLSW", "d"))
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002003 return -EIO;
Johannes Berg19d337d2009-06-02 13:01:37 +02002004
2005 return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002006}
2007
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002008static int hotkey_get_tablet_mode(int *status)
2009{
2010 int s;
2011
2012 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
2013 return -EIO;
2014
Henrique de Moraes Holschuhcee47f52008-03-04 14:29:21 -08002015 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
2016 return 0;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002017}
2018
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002019/*
2020 * Call with hotkey_mutex held
2021 */
2022static int hotkey_mask_get(void)
2023{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002024 u32 m = 0;
2025
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002026 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002027 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002028 return -EIO;
2029 }
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002030 HOTKEY_CONFIG_CRITICAL_START
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002031 hotkey_mask = m | (hotkey_source_mask & hotkey_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002032 HOTKEY_CONFIG_CRITICAL_END
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002033
2034 return 0;
2035}
2036
2037/*
2038 * Call with hotkey_mutex held
2039 */
2040static int hotkey_mask_set(u32 mask)
2041{
2042 int i;
2043 int rc = 0;
2044
2045 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -03002046 if (!tp_warned.hotkey_mask_ff &&
2047 (mask == 0xffff || mask == 0xffffff ||
2048 mask == 0xffffffff)) {
2049 tp_warned.hotkey_mask_ff = 1;
2050 printk(TPACPI_NOTICE
2051 "setting the hotkey mask to 0x%08x is likely "
2052 "not the best way to go about it\n", mask);
2053 printk(TPACPI_NOTICE
2054 "please consider using the driver defaults, "
2055 "and refer to up-to-date thinkpad-acpi "
2056 "documentation\n");
2057 }
2058
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002059 HOTKEY_CONFIG_CRITICAL_START
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002060 for (i = 0; i < 32; i++) {
2061 u32 m = 1 << i;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002062 /* enable in firmware mask only keys not in NVRAM
2063 * mode, but enable the key in the cached hotkey_mask
2064 * regardless of mode, or the key will end up
2065 * disabled by hotkey_mask_get() */
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002066 if (!acpi_evalf(hkey_handle,
2067 NULL, "MHKM", "vdd", i + 1,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002068 !!((mask & ~hotkey_source_mask) & m))) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002069 rc = -EIO;
2070 break;
2071 } else {
2072 hotkey_mask = (hotkey_mask & ~m) | (mask & m);
2073 }
2074 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002075 HOTKEY_CONFIG_CRITICAL_END
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002076
2077 /* hotkey_mask_get must be called unconditionally below */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002078 if (!hotkey_mask_get() && !rc &&
2079 (hotkey_mask & ~hotkey_source_mask) !=
2080 (mask & ~hotkey_source_mask)) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002081 printk(TPACPI_NOTICE
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002082 "requested hot key mask 0x%08x, but "
2083 "firmware forced it to 0x%08x\n",
2084 mask, hotkey_mask);
2085 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002086 } else {
2087#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2088 HOTKEY_CONFIG_CRITICAL_START
2089 hotkey_mask = mask & hotkey_source_mask;
2090 HOTKEY_CONFIG_CRITICAL_END
2091 hotkey_mask_get();
2092 if (hotkey_mask != mask) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002093 printk(TPACPI_NOTICE
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002094 "requested hot key mask 0x%08x, "
2095 "forced to 0x%08x (NVRAM poll mask is "
2096 "0x%08x): no firmware mask support\n",
2097 mask, hotkey_mask, hotkey_source_mask);
2098 }
2099#else
2100 hotkey_mask_get();
2101 rc = -ENXIO;
2102#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002103 }
2104
2105 return rc;
2106}
2107
2108static int hotkey_status_get(int *status)
2109{
2110 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
2111 return -EIO;
2112
2113 return 0;
2114}
2115
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002116static int hotkey_status_set(bool enable)
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002117{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002118 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002119 return -EIO;
2120
2121 return 0;
2122}
2123
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002124static void tpacpi_input_send_tabletsw(void)
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002125{
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002126 int state;
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002127
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002128 if (tp_features.hotkey_tablet &&
2129 !hotkey_get_tablet_mode(&state)) {
2130 mutex_lock(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002131
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002132 input_report_switch(tpacpi_inputdev,
2133 SW_TABLET_MODE, !!state);
2134 input_sync(tpacpi_inputdev);
2135
2136 mutex_unlock(&tpacpi_inputdev_send_mutex);
2137 }
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002138}
2139
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002140static void tpacpi_input_send_key(unsigned int scancode)
2141{
2142 unsigned int keycode;
2143
2144 keycode = hotkey_keycode_map[scancode];
2145
2146 if (keycode != KEY_RESERVED) {
2147 mutex_lock(&tpacpi_inputdev_send_mutex);
2148
2149 input_report_key(tpacpi_inputdev, keycode, 1);
2150 if (keycode == KEY_UNKNOWN)
2151 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
2152 scancode);
2153 input_sync(tpacpi_inputdev);
2154
2155 input_report_key(tpacpi_inputdev, keycode, 0);
2156 if (keycode == KEY_UNKNOWN)
2157 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
2158 scancode);
2159 input_sync(tpacpi_inputdev);
2160
2161 mutex_unlock(&tpacpi_inputdev_send_mutex);
2162 }
2163}
2164
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002165#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2166static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
2167
2168static void tpacpi_hotkey_send_key(unsigned int scancode)
2169{
2170 tpacpi_input_send_key(scancode);
2171 if (hotkey_report_mode < 2) {
2172 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
2173 0x80, 0x1001 + scancode);
2174 }
2175}
2176
2177static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
2178{
2179 u8 d;
2180
2181 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
2182 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
2183 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
2184 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
2185 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
2186 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
2187 }
2188 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
2189 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
2190 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
2191 }
2192 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
2193 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
2194 n->displayexp_toggle =
2195 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
2196 }
2197 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
2198 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
2199 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
2200 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
2201 n->brightness_toggle =
2202 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
2203 }
2204 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
2205 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
2206 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
2207 >> TP_NVRAM_POS_LEVEL_VOLUME;
2208 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2209 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2210 }
2211}
2212
2213#define TPACPI_COMPARE_KEY(__scancode, __member) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002214 do { \
2215 if ((mask & (1 << __scancode)) && \
2216 oldn->__member != newn->__member) \
2217 tpacpi_hotkey_send_key(__scancode); \
2218 } while (0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002219
2220#define TPACPI_MAY_SEND_KEY(__scancode) \
2221 do { if (mask & (1 << __scancode)) \
2222 tpacpi_hotkey_send_key(__scancode); } while (0)
2223
2224static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002225 struct tp_nvram_state *newn,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002226 u32 mask)
2227{
2228 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2229 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2230 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2231 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2232
2233 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2234
2235 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2236
2237 /* handle volume */
2238 if (oldn->volume_toggle != newn->volume_toggle) {
2239 if (oldn->mute != newn->mute) {
2240 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2241 }
2242 if (oldn->volume_level > newn->volume_level) {
2243 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2244 } else if (oldn->volume_level < newn->volume_level) {
2245 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2246 } else if (oldn->mute == newn->mute) {
2247 /* repeated key presses that didn't change state */
2248 if (newn->mute) {
2249 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2250 } else if (newn->volume_level != 0) {
2251 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2252 } else {
2253 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2254 }
2255 }
2256 }
2257
2258 /* handle brightness */
2259 if (oldn->brightness_toggle != newn->brightness_toggle) {
2260 if (oldn->brightness_level < newn->brightness_level) {
2261 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2262 } else if (oldn->brightness_level > newn->brightness_level) {
2263 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2264 } else {
2265 /* repeated key presses that didn't change state */
2266 if (newn->brightness_level != 0) {
2267 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2268 } else {
2269 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2270 }
2271 }
2272 }
2273}
2274
2275#undef TPACPI_COMPARE_KEY
2276#undef TPACPI_MAY_SEND_KEY
2277
2278static int hotkey_kthread(void *data)
2279{
2280 struct tp_nvram_state s[2];
2281 u32 mask;
2282 unsigned int si, so;
2283 unsigned long t;
2284 unsigned int change_detector, must_reset;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002285 unsigned int poll_freq;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002286
2287 mutex_lock(&hotkey_thread_mutex);
2288
2289 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2290 goto exit;
2291
2292 set_freezable();
2293
2294 so = 0;
2295 si = 1;
2296 t = 0;
2297
2298 /* Initial state for compares */
2299 mutex_lock(&hotkey_thread_data_mutex);
2300 change_detector = hotkey_config_change;
2301 mask = hotkey_source_mask & hotkey_mask;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002302 poll_freq = hotkey_poll_freq;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002303 mutex_unlock(&hotkey_thread_data_mutex);
2304 hotkey_read_nvram(&s[so], mask);
2305
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002306 while (!kthread_should_stop()) {
2307 if (t == 0) {
2308 if (likely(poll_freq))
2309 t = 1000/poll_freq;
2310 else
2311 t = 100; /* should never happen... */
2312 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002313 t = msleep_interruptible(t);
2314 if (unlikely(kthread_should_stop()))
2315 break;
2316 must_reset = try_to_freeze();
2317 if (t > 0 && !must_reset)
2318 continue;
2319
2320 mutex_lock(&hotkey_thread_data_mutex);
2321 if (must_reset || hotkey_config_change != change_detector) {
2322 /* forget old state on thaw or config change */
2323 si = so;
2324 t = 0;
2325 change_detector = hotkey_config_change;
2326 }
2327 mask = hotkey_source_mask & hotkey_mask;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002328 poll_freq = hotkey_poll_freq;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002329 mutex_unlock(&hotkey_thread_data_mutex);
2330
2331 if (likely(mask)) {
2332 hotkey_read_nvram(&s[si], mask);
2333 if (likely(si != so)) {
2334 hotkey_compare_and_issue_event(&s[so], &s[si],
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002335 mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002336 }
2337 }
2338
2339 so = si;
2340 si ^= 1;
2341 }
2342
2343exit:
2344 mutex_unlock(&hotkey_thread_mutex);
2345 return 0;
2346}
2347
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002348/* call with hotkey_mutex held */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002349static void hotkey_poll_stop_sync(void)
2350{
2351 if (tpacpi_hotkey_task) {
2352 if (frozen(tpacpi_hotkey_task) ||
2353 freezing(tpacpi_hotkey_task))
2354 thaw_process(tpacpi_hotkey_task);
2355
2356 kthread_stop(tpacpi_hotkey_task);
2357 tpacpi_hotkey_task = NULL;
2358 mutex_lock(&hotkey_thread_mutex);
2359 /* at this point, the thread did exit */
2360 mutex_unlock(&hotkey_thread_mutex);
2361 }
2362}
2363
2364/* call with hotkey_mutex held */
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002365static void hotkey_poll_setup(bool may_warn)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002366{
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002367 u32 hotkeys_to_poll = hotkey_source_mask & hotkey_mask;
2368
2369 if (hotkeys_to_poll != 0 && hotkey_poll_freq > 0 &&
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002370 (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
2371 if (!tpacpi_hotkey_task) {
2372 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03002373 NULL, TPACPI_NVRAM_KTHREAD_NAME);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002374 if (IS_ERR(tpacpi_hotkey_task)) {
2375 tpacpi_hotkey_task = NULL;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002376 printk(TPACPI_ERR
2377 "could not create kernel thread "
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002378 "for hotkey polling\n");
2379 }
2380 }
2381 } else {
2382 hotkey_poll_stop_sync();
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002383 if (may_warn && hotkeys_to_poll != 0 &&
2384 hotkey_poll_freq == 0) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002385 printk(TPACPI_NOTICE
2386 "hot keys 0x%08x require polling, "
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002387 "which is currently disabled\n",
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002388 hotkeys_to_poll);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002389 }
2390 }
2391}
2392
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002393static void hotkey_poll_setup_safe(bool may_warn)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002394{
2395 mutex_lock(&hotkey_mutex);
2396 hotkey_poll_setup(may_warn);
2397 mutex_unlock(&hotkey_mutex);
2398}
2399
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002400/* call with hotkey_mutex held */
2401static void hotkey_poll_set_freq(unsigned int freq)
2402{
2403 if (!freq)
2404 hotkey_poll_stop_sync();
2405
2406 HOTKEY_CONFIG_CRITICAL_START
2407 hotkey_poll_freq = freq;
2408 HOTKEY_CONFIG_CRITICAL_END
2409}
2410
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02002411#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2412
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002413static void hotkey_poll_setup_safe(bool __unused)
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02002414{
2415}
2416
2417#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2418
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002419static int hotkey_inputdev_open(struct input_dev *dev)
2420{
2421 switch (tpacpi_lifecycle) {
2422 case TPACPI_LIFE_INIT:
2423 /*
2424 * hotkey_init will call hotkey_poll_setup_safe
2425 * at the appropriate moment
2426 */
2427 return 0;
2428 case TPACPI_LIFE_EXITING:
2429 return -EBUSY;
2430 case TPACPI_LIFE_RUNNING:
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002431 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002432 return 0;
2433 }
2434
2435 /* Should only happen if tpacpi_lifecycle is corrupt */
2436 BUG();
2437 return -EBUSY;
2438}
2439
2440static void hotkey_inputdev_close(struct input_dev *dev)
2441{
2442 /* disable hotkey polling when possible */
2443 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002444 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002445}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002446
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002447/* sysfs hotkey enable ------------------------------------------------- */
2448static ssize_t hotkey_enable_show(struct device *dev,
2449 struct device_attribute *attr,
2450 char *buf)
2451{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002452 int res, status;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002453
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002454 printk_deprecated_attribute("hotkey_enable",
2455 "Hotkey reporting is always enabled");
2456
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002457 res = hotkey_status_get(&status);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002458 if (res)
2459 return res;
2460
2461 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2462}
2463
2464static ssize_t hotkey_enable_store(struct device *dev,
2465 struct device_attribute *attr,
2466 const char *buf, size_t count)
2467{
2468 unsigned long t;
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002469
2470 printk_deprecated_attribute("hotkey_enable",
2471 "Hotkeys can be disabled through hotkey_mask");
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002472
2473 if (parse_strtoul(buf, 1, &t))
2474 return -EINVAL;
2475
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002476 if (t == 0)
2477 return -EPERM;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002478
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002479 return count;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002480}
2481
2482static struct device_attribute dev_attr_hotkey_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002483 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002484 hotkey_enable_show, hotkey_enable_store);
2485
2486/* sysfs hotkey mask --------------------------------------------------- */
2487static ssize_t hotkey_mask_show(struct device *dev,
2488 struct device_attribute *attr,
2489 char *buf)
2490{
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002491 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002492
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002493 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002494 return -ERESTARTSYS;
2495 res = hotkey_mask_get();
2496 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002497
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002498 return (res)?
2499 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002500}
2501
2502static ssize_t hotkey_mask_store(struct device *dev,
2503 struct device_attribute *attr,
2504 const char *buf, size_t count)
2505{
2506 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002507 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002508
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002509 if (parse_strtoul(buf, 0xffffffffUL, &t))
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002510 return -EINVAL;
2511
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002512 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002513 return -ERESTARTSYS;
2514
2515 res = hotkey_mask_set(t);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002516
2517#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002518 hotkey_poll_setup(true);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002519#endif
2520
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002521 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002522
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002523 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2524
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002525 return (res) ? res : count;
2526}
2527
2528static struct device_attribute dev_attr_hotkey_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002529 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002530 hotkey_mask_show, hotkey_mask_store);
2531
2532/* sysfs hotkey bios_enabled ------------------------------------------- */
2533static ssize_t hotkey_bios_enabled_show(struct device *dev,
2534 struct device_attribute *attr,
2535 char *buf)
2536{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002537 return sprintf(buf, "0\n");
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002538}
2539
2540static struct device_attribute dev_attr_hotkey_bios_enabled =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002541 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002542
2543/* sysfs hotkey bios_mask ---------------------------------------------- */
2544static ssize_t hotkey_bios_mask_show(struct device *dev,
2545 struct device_attribute *attr,
2546 char *buf)
2547{
Henrique de Moraes Holschuh06777be2009-09-12 15:22:15 -03002548 printk_deprecated_attribute("hotkey_bios_mask",
2549 "This attribute is useless.");
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002550 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002551}
2552
2553static struct device_attribute dev_attr_hotkey_bios_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002554 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002555
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002556/* sysfs hotkey all_mask ----------------------------------------------- */
2557static ssize_t hotkey_all_mask_show(struct device *dev,
2558 struct device_attribute *attr,
2559 char *buf)
2560{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002561 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2562 hotkey_all_mask | hotkey_source_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002563}
2564
2565static struct device_attribute dev_attr_hotkey_all_mask =
2566 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
2567
2568/* sysfs hotkey recommended_mask --------------------------------------- */
2569static ssize_t hotkey_recommended_mask_show(struct device *dev,
2570 struct device_attribute *attr,
2571 char *buf)
2572{
2573 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002574 (hotkey_all_mask | hotkey_source_mask)
2575 & ~hotkey_reserved_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002576}
2577
2578static struct device_attribute dev_attr_hotkey_recommended_mask =
2579 __ATTR(hotkey_recommended_mask, S_IRUGO,
2580 hotkey_recommended_mask_show, NULL);
2581
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002582#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2583
2584/* sysfs hotkey hotkey_source_mask ------------------------------------- */
2585static ssize_t hotkey_source_mask_show(struct device *dev,
2586 struct device_attribute *attr,
2587 char *buf)
2588{
2589 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2590}
2591
2592static ssize_t hotkey_source_mask_store(struct device *dev,
2593 struct device_attribute *attr,
2594 const char *buf, size_t count)
2595{
2596 unsigned long t;
2597
2598 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2599 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2600 return -EINVAL;
2601
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002602 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002603 return -ERESTARTSYS;
2604
2605 HOTKEY_CONFIG_CRITICAL_START
2606 hotkey_source_mask = t;
2607 HOTKEY_CONFIG_CRITICAL_END
2608
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002609 hotkey_poll_setup(true);
2610 hotkey_mask_set(hotkey_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002611
2612 mutex_unlock(&hotkey_mutex);
2613
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002614 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2615
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002616 return count;
2617}
2618
2619static struct device_attribute dev_attr_hotkey_source_mask =
2620 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2621 hotkey_source_mask_show, hotkey_source_mask_store);
2622
2623/* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2624static ssize_t hotkey_poll_freq_show(struct device *dev,
2625 struct device_attribute *attr,
2626 char *buf)
2627{
2628 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2629}
2630
2631static ssize_t hotkey_poll_freq_store(struct device *dev,
2632 struct device_attribute *attr,
2633 const char *buf, size_t count)
2634{
2635 unsigned long t;
2636
2637 if (parse_strtoul(buf, 25, &t))
2638 return -EINVAL;
2639
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002640 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002641 return -ERESTARTSYS;
2642
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002643 hotkey_poll_set_freq(t);
2644 hotkey_poll_setup(true);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002645
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002646 mutex_unlock(&hotkey_mutex);
2647
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002648 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2649
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002650 return count;
2651}
2652
2653static struct device_attribute dev_attr_hotkey_poll_freq =
2654 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2655 hotkey_poll_freq_show, hotkey_poll_freq_store);
2656
2657#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2658
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002659/* sysfs hotkey radio_sw (pollable) ------------------------------------ */
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002660static ssize_t hotkey_radio_sw_show(struct device *dev,
2661 struct device_attribute *attr,
2662 char *buf)
2663{
Johannes Berg19d337d2009-06-02 13:01:37 +02002664 int res;
2665 res = hotkey_get_wlsw();
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002666 if (res < 0)
2667 return res;
2668
Johannes Berg19d337d2009-06-02 13:01:37 +02002669 /* Opportunistic update */
2670 tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
2671
2672 return snprintf(buf, PAGE_SIZE, "%d\n",
2673 (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002674}
2675
2676static struct device_attribute dev_attr_hotkey_radio_sw =
2677 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2678
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002679static void hotkey_radio_sw_notify_change(void)
2680{
2681 if (tp_features.hotkey_wlsw)
2682 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2683 "hotkey_radio_sw");
2684}
2685
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002686/* sysfs hotkey tablet mode (pollable) --------------------------------- */
2687static ssize_t hotkey_tablet_mode_show(struct device *dev,
2688 struct device_attribute *attr,
2689 char *buf)
2690{
2691 int res, s;
2692 res = hotkey_get_tablet_mode(&s);
2693 if (res < 0)
2694 return res;
2695
2696 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2697}
2698
2699static struct device_attribute dev_attr_hotkey_tablet_mode =
2700 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2701
2702static void hotkey_tablet_mode_notify_change(void)
2703{
2704 if (tp_features.hotkey_tablet)
2705 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2706 "hotkey_tablet_mode");
2707}
2708
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002709/* sysfs hotkey report_mode -------------------------------------------- */
2710static ssize_t hotkey_report_mode_show(struct device *dev,
2711 struct device_attribute *attr,
2712 char *buf)
2713{
2714 return snprintf(buf, PAGE_SIZE, "%d\n",
2715 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2716}
2717
2718static struct device_attribute dev_attr_hotkey_report_mode =
2719 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2720
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002721/* sysfs wakeup reason (pollable) -------------------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002722static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2723 struct device_attribute *attr,
2724 char *buf)
2725{
2726 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2727}
2728
2729static struct device_attribute dev_attr_hotkey_wakeup_reason =
2730 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2731
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02002732static void hotkey_wakeup_reason_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002733{
2734 if (tp_features.hotkey_mask)
2735 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2736 "wakeup_reason");
2737}
2738
2739/* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002740static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2741 struct device_attribute *attr,
2742 char *buf)
2743{
2744 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2745}
2746
2747static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2748 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2749 hotkey_wakeup_hotunplug_complete_show, NULL);
2750
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02002751static void hotkey_wakeup_hotunplug_complete_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002752{
2753 if (tp_features.hotkey_mask)
2754 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2755 "wakeup_hotunplug_complete");
2756}
2757
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002758/* --------------------------------------------------------------------- */
2759
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002760static struct attribute *hotkey_attributes[] __initdata = {
2761 &dev_attr_hotkey_enable.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002762 &dev_attr_hotkey_bios_enabled.attr,
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002763 &dev_attr_hotkey_report_mode.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002764#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2765 &dev_attr_hotkey_mask.attr,
2766 &dev_attr_hotkey_all_mask.attr,
2767 &dev_attr_hotkey_recommended_mask.attr,
2768 &dev_attr_hotkey_source_mask.attr,
2769 &dev_attr_hotkey_poll_freq.attr,
2770#endif
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002771};
2772
2773static struct attribute *hotkey_mask_attributes[] __initdata = {
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002774 &dev_attr_hotkey_bios_mask.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002775#ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2776 &dev_attr_hotkey_mask.attr,
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002777 &dev_attr_hotkey_all_mask.attr,
2778 &dev_attr_hotkey_recommended_mask.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002779#endif
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002780 &dev_attr_hotkey_wakeup_reason.attr,
2781 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002782};
2783
Johannes Berg19d337d2009-06-02 13:01:37 +02002784/*
2785 * Sync both the hw and sw blocking state of all switches
2786 */
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002787static void tpacpi_send_radiosw_update(void)
2788{
2789 int wlsw;
2790
Johannes Berg19d337d2009-06-02 13:01:37 +02002791 /*
2792 * We must sync all rfkill controllers *before* issuing any
2793 * rfkill input events, or we will race the rfkill core input
2794 * handler.
2795 *
2796 * tpacpi_inputdev_send_mutex works as a syncronization point
2797 * for the above.
2798 *
2799 * We optimize to avoid numerous calls to hotkey_get_wlsw.
2800 */
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002801
Johannes Berg19d337d2009-06-02 13:01:37 +02002802 wlsw = hotkey_get_wlsw();
2803
2804 /* Sync hw blocking state first if it is hw-blocked */
2805 if (wlsw == TPACPI_RFK_RADIO_OFF)
2806 tpacpi_rfk_update_hwblock_state(true);
2807
2808 /* Sync sw blocking state */
2809 tpacpi_rfk_update_swstate_all();
2810
2811 /* Sync hw blocking state last if it is hw-unblocked */
2812 if (wlsw == TPACPI_RFK_RADIO_ON)
2813 tpacpi_rfk_update_hwblock_state(false);
2814
2815 /* Issue rfkill input event for WLSW switch */
2816 if (!(wlsw < 0)) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002817 mutex_lock(&tpacpi_inputdev_send_mutex);
2818
2819 input_report_switch(tpacpi_inputdev,
Johannes Berg19d337d2009-06-02 13:01:37 +02002820 SW_RFKILL_ALL, (wlsw > 0));
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002821 input_sync(tpacpi_inputdev);
2822
2823 mutex_unlock(&tpacpi_inputdev_send_mutex);
2824 }
Johannes Berg19d337d2009-06-02 13:01:37 +02002825
2826 /*
2827 * this can be unconditional, as we will poll state again
2828 * if userspace uses the notify to read data
2829 */
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002830 hotkey_radio_sw_notify_change();
2831}
2832
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002833static void hotkey_exit(void)
2834{
2835#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002836 mutex_lock(&hotkey_mutex);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002837 hotkey_poll_stop_sync();
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002838 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002839#endif
2840
2841 if (hotkey_dev_attributes)
2842 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2843
2844 kfree(hotkey_keycode_map);
2845
Henrique de Moraes Holschuh4be73002009-09-20 14:09:23 -03002846 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
2847 "restoring original hot key mask\n");
2848 /* no short-circuit boolean operator below! */
2849 if (((tp_features.hotkey_mask && hotkey_mask_set(hotkey_orig_mask))
2850 | hotkey_status_set(false)) != 0)
2851 printk(TPACPI_ERR
2852 "failed to restore hot key mask "
2853 "to BIOS defaults\n");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002854}
2855
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03002856static void __init hotkey_unmap(const unsigned int scancode)
2857{
2858 if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
2859 clear_bit(hotkey_keycode_map[scancode],
2860 tpacpi_inputdev->keybit);
2861 hotkey_keycode_map[scancode] = KEY_RESERVED;
2862 }
2863}
2864
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002865static int __init hotkey_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002866{
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002867 /* Requirements for changing the default keymaps:
2868 *
2869 * 1. Many of the keys are mapped to KEY_RESERVED for very
2870 * good reasons. Do not change them unless you have deep
2871 * knowledge on the IBM and Lenovo ThinkPad firmware for
2872 * the various ThinkPad models. The driver behaves
2873 * differently for KEY_RESERVED: such keys have their
2874 * hot key mask *unset* in mask_recommended, and also
2875 * in the initial hot key mask programmed into the
2876 * firmware at driver load time, which means the firm-
2877 * ware may react very differently if you change them to
2878 * something else;
2879 *
2880 * 2. You must be subscribed to the linux-thinkpad and
2881 * ibm-acpi-devel mailing lists, and you should read the
2882 * list archives since 2007 if you want to change the
2883 * keymaps. This requirement exists so that you will
2884 * know the past history of problems with the thinkpad-
2885 * acpi driver keymaps, and also that you will be
2886 * listening to any bug reports;
2887 *
2888 * 3. Do not send thinkpad-acpi specific patches directly to
2889 * for merging, *ever*. Send them to the linux-acpi
2890 * mailinglist for comments. Merging is to be done only
2891 * through acpi-test and the ACPI maintainer.
2892 *
2893 * If the above is too much to ask, don't change the keymap.
2894 * Ask the thinkpad-acpi maintainer to do it, instead.
2895 */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002896 static u16 ibm_keycode_map[] __initdata = {
2897 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2898 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
2899 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2900 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002901
2902 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002903 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2904 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2905 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002906
2907 /* brightness: firmware always reacts to them, unless
2908 * X.org did some tricks in the radeon BIOS scratch
2909 * registers of *some* models */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02002910 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02002911 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002912
2913 /* Thinklight: firmware always react to it */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002914 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002915
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002916 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2917 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002918
2919 /* Volume: firmware always react to it and reprograms
2920 * the built-in *extra* mixer. Never map it to control
2921 * another mixer by default. */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02002922 KEY_RESERVED, /* 0x14: VOLUME UP */
2923 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2924 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002925
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002926 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002927
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002928 /* (assignments unknown, please report if found) */
2929 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2930 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2931 };
2932 static u16 lenovo_keycode_map[] __initdata = {
2933 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2934 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
2935 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2936 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002937
2938 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002939 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2940 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2941 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002942
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03002943 /* These should be enabled --only-- when ACPI video
2944 * is disabled (i.e. in "vendor" mode), and are handled
2945 * in a special way by the init code */
2946 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
2947 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002948
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002949 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002950
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002951 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2952 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002953
2954 /* Volume: z60/z61, T60 (BIOS version?): firmware always
2955 * react to it and reprograms the built-in *extra* mixer.
2956 * Never map it to control another mixer by default.
2957 *
2958 * T60?, T61, R60?, R61: firmware and EC tries to send
2959 * these over the regular keyboard, so these are no-ops,
2960 * but there are still weird bugs re. MUTE, so do not
2961 * change unless you get test reports from all Lenovo
2962 * models. May cause the BIOS to interfere with the
2963 * HDA mixer.
2964 */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02002965 KEY_RESERVED, /* 0x14: VOLUME UP */
2966 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2967 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002968
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002969 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002970
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002971 /* (assignments unknown, please report if found) */
2972 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2973 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2974 };
2975
2976#define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
2977#define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
2978#define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
2979
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002980 int res, i;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002981 int status;
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03002982 int hkeyv;
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03002983
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002984 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2985 "initializing hotkey subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002986
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002987 BUG_ON(!tpacpi_inputdev);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002988 BUG_ON(tpacpi_inputdev->open != NULL ||
2989 tpacpi_inputdev->close != NULL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002990
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002991 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002992 mutex_init(&hotkey_mutex);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002993
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002994#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2995 mutex_init(&hotkey_thread_mutex);
2996 mutex_init(&hotkey_thread_data_mutex);
2997#endif
2998
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002999 /* hotkey not supported on 570 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003000 tp_features.hotkey = hkey_handle != NULL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003001
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003002 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3003 "hotkeys are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003004 str_supported(tp_features.hotkey));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003005
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003006 if (!tp_features.hotkey)
3007 return 1;
3008
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03003009 tpacpi_disable_brightness_delay();
3010
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003011 hotkey_dev_attributes = create_attr_set(13, NULL);
3012 if (!hotkey_dev_attributes)
3013 return -ENOMEM;
3014 res = add_many_to_attr_set(hotkey_dev_attributes,
3015 hotkey_attributes,
3016 ARRAY_SIZE(hotkey_attributes));
3017 if (res)
3018 goto err_exit;
3019
3020 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3021 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
3022 for HKEY interface version 0x100 */
3023 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
3024 if ((hkeyv >> 8) != 1) {
3025 printk(TPACPI_ERR "unknown version of the "
3026 "HKEY interface: 0x%x\n", hkeyv);
3027 printk(TPACPI_ERR "please report this to %s\n",
3028 TPACPI_MAIL);
3029 } else {
3030 /*
3031 * MHKV 0x100 in A31, R40, R40e,
3032 * T4x, X31, and later
3033 */
3034 tp_features.hotkey_mask = 1;
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003035 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3036 "firmware HKEY interface version: 0x%x\n",
3037 hkeyv);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003038 }
3039 }
3040
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003041 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3042 "hotkey masks are %s\n",
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003043 str_supported(tp_features.hotkey_mask));
3044
3045 if (tp_features.hotkey_mask) {
3046 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3047 "MHKA", "qd")) {
3048 printk(TPACPI_ERR
3049 "missing MHKA handler, "
3050 "please report this to %s\n",
3051 TPACPI_MAIL);
3052 /* FN+F12, FN+F4, FN+F3 */
3053 hotkey_all_mask = 0x080cU;
3054 }
3055 }
3056
3057 /* hotkey_source_mask *must* be zero for
3058 * the first hotkey_mask_get */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003059 if (tp_features.hotkey_mask) {
3060 res = hotkey_mask_get();
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003061 if (res)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003062 goto err_exit;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003063
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003064 hotkey_orig_mask = hotkey_mask;
3065 res = add_many_to_attr_set(
3066 hotkey_dev_attributes,
3067 hotkey_mask_attributes,
3068 ARRAY_SIZE(hotkey_mask_attributes));
3069 if (res)
3070 goto err_exit;
3071 }
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003072
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003073#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3074 if (dbg_wlswemul) {
3075 tp_features.hotkey_wlsw = 1;
3076 printk(TPACPI_INFO
3077 "radio switch emulation enabled\n");
3078 } else
3079#endif
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003080 /* Not all thinkpads have a hardware radio switch */
3081 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
3082 tp_features.hotkey_wlsw = 1;
3083 printk(TPACPI_INFO
3084 "radio switch found; radios are %s\n",
3085 enabled(status, 0));
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003086 }
3087 if (tp_features.hotkey_wlsw)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003088 res = add_to_attr_set(hotkey_dev_attributes,
3089 &dev_attr_hotkey_radio_sw.attr);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003090
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003091 /* For X41t, X60t, X61t Tablets... */
3092 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
3093 tp_features.hotkey_tablet = 1;
3094 printk(TPACPI_INFO
3095 "possible tablet mode switch found; "
3096 "ThinkPad in %s mode\n",
3097 (status & TP_HOTKEY_TABLET_MASK)?
3098 "tablet" : "laptop");
3099 res = add_to_attr_set(hotkey_dev_attributes,
3100 &dev_attr_hotkey_tablet_mode.attr);
3101 }
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02003102
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003103 if (!res)
3104 res = register_attr_set_with_sysfs(
3105 hotkey_dev_attributes,
3106 &tpacpi_pdev->dev.kobj);
3107 if (res)
3108 goto err_exit;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003109
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003110 /* Set up key map */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003111
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003112 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
3113 GFP_KERNEL);
3114 if (!hotkey_keycode_map) {
3115 printk(TPACPI_ERR
3116 "failed to allocate memory for key map\n");
3117 res = -ENOMEM;
3118 goto err_exit;
3119 }
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003120
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003121 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003122 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003123 "using Lenovo default hot key map\n");
3124 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
3125 TPACPI_HOTKEY_MAP_SIZE);
3126 } else {
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003127 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003128 "using IBM default hot key map\n");
3129 memcpy(hotkey_keycode_map, &ibm_keycode_map,
3130 TPACPI_HOTKEY_MAP_SIZE);
3131 }
3132
3133 set_bit(EV_KEY, tpacpi_inputdev->evbit);
3134 set_bit(EV_MSC, tpacpi_inputdev->evbit);
3135 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
3136 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
3137 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3138 tpacpi_inputdev->keycode = hotkey_keycode_map;
3139 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3140 if (hotkey_keycode_map[i] != KEY_RESERVED) {
3141 set_bit(hotkey_keycode_map[i],
3142 tpacpi_inputdev->keybit);
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003143 } else {
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003144 if (i < sizeof(hotkey_reserved_mask)*8)
3145 hotkey_reserved_mask |= 1 << i;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003146 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003147 }
3148
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003149 if (tp_features.hotkey_wlsw) {
3150 set_bit(EV_SW, tpacpi_inputdev->evbit);
3151 set_bit(SW_RFKILL_ALL, tpacpi_inputdev->swbit);
3152 }
3153 if (tp_features.hotkey_tablet) {
3154 set_bit(EV_SW, tpacpi_inputdev->evbit);
3155 set_bit(SW_TABLET_MODE, tpacpi_inputdev->swbit);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003156 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003157
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003158 /* Do not issue duplicate brightness change events to
3159 * userspace */
3160 if (!tp_features.bright_acpimode)
3161 /* update bright_acpimode... */
3162 tpacpi_check_std_acpi_brightness_support();
3163
Henrique de Moraes Holschuh8bf3d4c2009-05-30 13:25:09 -03003164 if (tp_features.bright_acpimode && acpi_video_backlight_support()) {
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003165 printk(TPACPI_INFO
3166 "This ThinkPad has standard ACPI backlight "
3167 "brightness control, supported by the ACPI "
3168 "video driver\n");
3169 printk(TPACPI_NOTICE
3170 "Disabling thinkpad-acpi brightness events "
3171 "by default...\n");
3172
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003173 /* Disable brightness up/down on Lenovo thinkpads when
3174 * ACPI is handling them, otherwise it is plain impossible
3175 * for userspace to do something even remotely sane */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003176 hotkey_reserved_mask |=
3177 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3178 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003179 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
3180 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003181 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003182
Henrique de Moraes Holschuh230d8cf252009-09-12 15:22:17 -03003183#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3184 if (tp_features.hotkey_mask) {
3185 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3186 & ~hotkey_all_mask
3187 & ~hotkey_reserved_mask;
3188 } else {
3189 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3190 & ~hotkey_reserved_mask;
3191 }
3192
3193 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3194 "hotkey source mask 0x%08x, polling freq %u\n",
3195 hotkey_source_mask, hotkey_poll_freq);
3196#endif
3197
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003198 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3199 "enabling firmware HKEY event interface...\n");
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003200 res = hotkey_status_set(true);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003201 if (res) {
3202 hotkey_exit();
3203 return res;
3204 }
3205 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
3206 & ~hotkey_reserved_mask)
3207 | hotkey_orig_mask);
3208 if (res < 0 && res != -ENXIO) {
3209 hotkey_exit();
3210 return res;
3211 }
3212
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003213 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3214 "legacy ibm/hotkey event reporting over procfs %s\n",
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003215 (hotkey_report_mode < 2) ?
3216 "enabled" : "disabled");
3217
3218 tpacpi_inputdev->open = &hotkey_inputdev_open;
3219 tpacpi_inputdev->close = &hotkey_inputdev_close;
3220
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003221 hotkey_poll_setup_safe(true);
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003222 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003223 tpacpi_input_send_tabletsw();
3224
3225 return 0;
3226
3227err_exit:
3228 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3229 hotkey_dev_attributes = NULL;
3230
3231 return (res < 0)? res : 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003232}
3233
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003234static bool hotkey_notify_hotkey(const u32 hkey,
3235 bool *send_acpi_ev,
3236 bool *ignore_acpi_ev)
3237{
3238 /* 0x1000-0x1FFF: key presses */
3239 unsigned int scancode = hkey & 0xfff;
3240 *send_acpi_ev = true;
3241 *ignore_acpi_ev = false;
3242
3243 if (scancode > 0 && scancode < 0x21) {
3244 scancode--;
3245 if (!(hotkey_source_mask & (1 << scancode))) {
3246 tpacpi_input_send_key(scancode);
3247 *send_acpi_ev = false;
3248 } else {
3249 *ignore_acpi_ev = true;
3250 }
3251 return true;
3252 }
3253 return false;
3254}
3255
3256static bool hotkey_notify_wakeup(const u32 hkey,
3257 bool *send_acpi_ev,
3258 bool *ignore_acpi_ev)
3259{
3260 /* 0x2000-0x2FFF: Wakeup reason */
3261 *send_acpi_ev = true;
3262 *ignore_acpi_ev = false;
3263
3264 switch (hkey) {
3265 case 0x2304: /* suspend, undock */
3266 case 0x2404: /* hibernation, undock */
3267 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3268 *ignore_acpi_ev = true;
3269 break;
3270
3271 case 0x2305: /* suspend, bay eject */
3272 case 0x2405: /* hibernation, bay eject */
3273 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3274 *ignore_acpi_ev = true;
3275 break;
3276
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003277 case 0x2313: /* Battery on critical low level (S3) */
3278 case 0x2413: /* Battery on critical low level (S4) */
3279 printk(TPACPI_ALERT
3280 "EMERGENCY WAKEUP: battery almost empty\n");
3281 /* how to auto-heal: */
3282 /* 2313: woke up from S3, go to S4/S5 */
3283 /* 2413: woke up from S4, go to S5 */
3284 break;
3285
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003286 default:
3287 return false;
3288 }
3289
3290 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
3291 printk(TPACPI_INFO
3292 "woke up due to a hot-unplug "
3293 "request...\n");
3294 hotkey_wakeup_reason_notify_change();
3295 }
3296 return true;
3297}
3298
3299static bool hotkey_notify_usrevent(const u32 hkey,
3300 bool *send_acpi_ev,
3301 bool *ignore_acpi_ev)
3302{
3303 /* 0x5000-0x5FFF: human interface helpers */
3304 *send_acpi_ev = true;
3305 *ignore_acpi_ev = false;
3306
3307 switch (hkey) {
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003308 case 0x500b: /* X61t: tablet pen inserted into bay */
3309 case 0x500c: /* X61t: tablet pen removed from bay */
3310 return true;
3311
3312 case 0x5009: /* X41t-X61t: swivel up (tablet mode) */
3313 case 0x500a: /* X41t-X61t: swivel down (normal mode) */
3314 tpacpi_input_send_tabletsw();
3315 hotkey_tablet_mode_notify_change();
3316 *send_acpi_ev = false;
3317 return true;
3318
Henrique de Moraes Holschuh176dd982009-09-20 14:09:24 -03003319 case 0x5001: /* Lid close */
3320 case 0x5002: /* Lid open */
3321 case 0x5010: /* brightness control */
3322 /* do not propagate these events */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003323 *ignore_acpi_ev = true;
3324 return true;
3325
3326 default:
3327 return false;
3328 }
3329}
3330
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003331static bool hotkey_notify_thermal(const u32 hkey,
3332 bool *send_acpi_ev,
3333 bool *ignore_acpi_ev)
3334{
3335 /* 0x6000-0x6FFF: thermal alarms */
3336 *send_acpi_ev = true;
3337 *ignore_acpi_ev = false;
3338
3339 switch (hkey) {
3340 case 0x6011:
3341 printk(TPACPI_CRIT
3342 "THERMAL ALARM: battery is too hot!\n");
3343 /* recommended action: warn user through gui */
3344 return true;
3345 case 0x6012:
3346 printk(TPACPI_ALERT
3347 "THERMAL EMERGENCY: battery is extremely hot!\n");
3348 /* recommended action: immediate sleep/hibernate */
3349 return true;
3350 case 0x6021:
3351 printk(TPACPI_CRIT
3352 "THERMAL ALARM: "
3353 "a sensor reports something is too hot!\n");
3354 /* recommended action: warn user through gui, that */
3355 /* some internal component is too hot */
3356 return true;
3357 case 0x6022:
3358 printk(TPACPI_ALERT
3359 "THERMAL EMERGENCY: "
3360 "a sensor reports something is extremely hot!\n");
3361 /* recommended action: immediate sleep/hibernate */
3362 return true;
Henrique de Moraes Holschuh54926ce2009-01-11 03:01:09 -02003363 case 0x6030:
3364 printk(TPACPI_INFO
3365 "EC reports that Thermal Table has changed\n");
3366 /* recommended action: do nothing, we don't have
3367 * Lenovo ATM information */
3368 return true;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003369 default:
3370 printk(TPACPI_ALERT
3371 "THERMAL ALERT: unknown thermal alarm received\n");
3372 return false;
3373 }
3374}
3375
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003376static void hotkey_notify(struct ibm_struct *ibm, u32 event)
3377{
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003378 u32 hkey;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003379 bool send_acpi_ev;
3380 bool ignore_acpi_ev;
3381 bool known_ev;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003382
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003383 if (event != 0x80) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003384 printk(TPACPI_ERR
3385 "unknown HKEY notification event %d\n", event);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003386 /* forward it to userspace, maybe it knows how to handle it */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003387 acpi_bus_generate_netlink_event(
3388 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02003389 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003390 event, 0);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003391 return;
3392 }
3393
3394 while (1) {
3395 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003396 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003397 return;
3398 }
3399
3400 if (hkey == 0) {
3401 /* queue empty */
3402 return;
3403 }
3404
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003405 send_acpi_ev = true;
3406 ignore_acpi_ev = false;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003407
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003408 switch (hkey >> 12) {
3409 case 1:
3410 /* 0x1000-0x1FFF: key presses */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003411 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
3412 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003413 break;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003414 case 2:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003415 /* 0x2000-0x2FFF: Wakeup reason */
3416 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
3417 &ignore_acpi_ev);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003418 break;
3419 case 3:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003420 /* 0x3000-0x3FFF: bay-related wakeups */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003421 if (hkey == 0x3003) {
3422 hotkey_autosleep_ack = 1;
3423 printk(TPACPI_INFO
3424 "bay ejected\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003425 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003426 known_ev = true;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003427 } else {
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003428 known_ev = false;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003429 }
3430 break;
3431 case 4:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003432 /* 0x4000-0x4FFF: dock-related wakeups */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003433 if (hkey == 0x4003) {
3434 hotkey_autosleep_ack = 1;
3435 printk(TPACPI_INFO
3436 "undocked\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003437 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003438 known_ev = true;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003439 } else {
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003440 known_ev = false;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003441 }
3442 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003443 case 5:
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02003444 /* 0x5000-0x5FFF: human interface helpers */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003445 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
3446 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003447 break;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003448 case 6:
3449 /* 0x6000-0x6FFF: thermal alarms */
3450 known_ev = hotkey_notify_thermal(hkey, &send_acpi_ev,
3451 &ignore_acpi_ev);
3452 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003453 case 7:
3454 /* 0x7000-0x7FFF: misc */
3455 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003456 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02003457 send_acpi_ev = 0;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003458 known_ev = true;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003459 break;
3460 }
3461 /* fallthrough to default */
3462 default:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003463 known_ev = false;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02003464 }
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003465 if (!known_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003466 printk(TPACPI_NOTICE
3467 "unhandled HKEY event 0x%04x\n", hkey);
Henrique de Moraes Holschuhcb429352009-01-11 03:01:07 -02003468 printk(TPACPI_NOTICE
3469 "please report the conditions when this "
3470 "event happened to %s\n", TPACPI_MAIL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003471 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003472
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003473 /* Legacy events */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003474 if (!ignore_acpi_ev &&
3475 (send_acpi_ev || hotkey_report_mode < 2)) {
3476 acpi_bus_generate_proc_event(ibm->acpi->device,
3477 event, hkey);
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03003478 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003479
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003480 /* netlink events */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03003481 if (!ignore_acpi_ev && send_acpi_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003482 acpi_bus_generate_netlink_event(
3483 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02003484 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003485 event, hkey);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003486 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003487 }
3488}
3489
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003490static void hotkey_suspend(pm_message_t state)
3491{
3492 /* Do these on suspend, we get the events on early resume! */
3493 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
3494 hotkey_autosleep_ack = 0;
3495}
3496
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03003497static void hotkey_resume(void)
3498{
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03003499 tpacpi_disable_brightness_delay();
3500
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02003501 if (hotkey_mask_get())
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003502 printk(TPACPI_ERR
3503 "error while trying to read hot key mask "
3504 "from firmware\n");
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003505 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02003506 hotkey_tablet_mode_notify_change();
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003507 hotkey_wakeup_reason_notify_change();
3508 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003509 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03003510}
3511
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003512/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003513static int hotkey_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03003515 int res, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516 int len = 0;
3517
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003518 if (!tp_features.hotkey) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003519 len += sprintf(p + len, "status:\t\tnot supported\n");
3520 return len;
3521 }
3522
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02003523 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02003524 return -ERESTARTSYS;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02003525 res = hotkey_status_get(&status);
3526 if (!res)
3527 res = hotkey_mask_get();
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003528 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03003529 if (res)
3530 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531
3532 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003533 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02003534 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535 len += sprintf(p + len,
3536 "commands:\tenable, disable, reset, <mask>\n");
3537 } else {
3538 len += sprintf(p + len, "mask:\t\tnot supported\n");
3539 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
3540 }
3541
3542 return len;
3543}
3544
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003545static void hotkey_enabledisable_warn(bool enable)
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003546{
3547 tpacpi_log_usertask("procfs hotkey enable/disable");
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003548 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
3549 TPACPI_WARN
3550 "hotkey enable/disable functionality has been "
3551 "removed from the driver. Hotkeys are always "
3552 "enabled\n"))
3553 printk(TPACPI_ERR
3554 "Please remove the hotkey=enable module "
3555 "parameter, it is deprecated. Hotkeys are always "
3556 "enabled\n");
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003557}
3558
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003559static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003561 int res;
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03003562 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003565 if (!tp_features.hotkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566 return -ENODEV;
3567
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02003568 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02003569 return -ERESTARTSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003570
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02003571 mask = hotkey_mask;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003572
3573 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574 while ((cmd = next_cmd(&buf))) {
3575 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003576 hotkey_enabledisable_warn(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003578 hotkey_enabledisable_warn(0);
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003579 res = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580 } else if (strlencmp(cmd, "reset") == 0) {
Henrique de Moraes Holschuh20c9aa42009-09-12 15:22:16 -03003581 mask = (hotkey_all_mask | hotkey_source_mask)
3582 & ~hotkey_reserved_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
3584 /* mask set */
3585 } else if (sscanf(cmd, "%x", &mask) == 1) {
3586 /* mask set */
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003587 } else {
3588 res = -EINVAL;
3589 goto errexit;
3590 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591 }
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003592
3593 if (!res)
3594 tpacpi_disclose_usertask("procfs hotkey",
3595 "set mask to 0x%08x\n", mask);
3596
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02003597 if (!res && mask != hotkey_mask)
3598 res = hotkey_mask_set(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003600errexit:
3601 mutex_unlock(&hotkey_mutex);
3602 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003603}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003605static const struct acpi_device_id ibm_htk_device_ids[] = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003606 {TPACPI_ACPI_HKEY_HID, 0},
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003607 {"", 0},
3608};
3609
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003610static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003611 .hid = ibm_htk_device_ids,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003612 .notify = hotkey_notify,
3613 .handle = &hkey_handle,
3614 .type = ACPI_DEVICE_NOTIFY,
3615};
3616
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003617static struct ibm_struct hotkey_driver_data = {
3618 .name = "hotkey",
3619 .read = hotkey_read,
3620 .write = hotkey_write,
3621 .exit = hotkey_exit,
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03003622 .resume = hotkey_resume,
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003623 .suspend = hotkey_suspend,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003624 .acpi = &ibm_hotkey_acpidriver,
3625};
3626
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003627/*************************************************************************
3628 * Bluetooth subdriver
3629 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003631enum {
3632 /* ACPI GBDC/SBDC bits */
3633 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
3634 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003635 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
3636 off / last state */
3637};
3638
3639enum {
3640 /* ACPI \BLTH commands */
3641 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
3642 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
3643 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
3644 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
3645 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003646};
3647
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003648#define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
3649
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003650static void bluetooth_suspend(pm_message_t state)
3651{
3652 /* Try to make sure radio will resume powered off */
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003653 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3654 TP_ACPI_BLTH_PWR_OFF_ON_RESUME))
3655 vdbg_printk(TPACPI_DBG_RFKILL,
3656 "bluetooth power down on resume request failed\n");
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003657}
3658
Johannes Berg19d337d2009-06-02 13:01:37 +02003659static int bluetooth_get_status(void)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003660{
3661 int status;
3662
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003663#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3664 if (dbg_bluetoothemul)
3665 return (tpacpi_bluetooth_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02003666 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003667#endif
3668
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003669 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
3670 return -EIO;
3671
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003672 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02003673 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003674}
3675
Johannes Berg19d337d2009-06-02 13:01:37 +02003676static int bluetooth_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003677{
3678 int status;
3679
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003680 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02003681 "will attempt to %s bluetooth\n",
3682 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003683
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003684#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3685 if (dbg_bluetoothemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02003686 tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003687 return 0;
3688 }
3689#endif
3690
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003691 /* We make sure to keep TP_ACPI_BLUETOOTH_RESUMECTRL off */
Johannes Berg19d337d2009-06-02 13:01:37 +02003692 if (state == TPACPI_RFK_RADIO_ON)
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003693 status = TP_ACPI_BLUETOOTH_RADIOSSW;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003694 else
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003695 status = 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02003696
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003697 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
3698 return -EIO;
3699
3700 return 0;
3701}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003702
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003703/* sysfs bluetooth enable ---------------------------------------------- */
3704static ssize_t bluetooth_enable_show(struct device *dev,
3705 struct device_attribute *attr,
3706 char *buf)
3707{
Johannes Berg19d337d2009-06-02 13:01:37 +02003708 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID,
3709 attr, buf);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003710}
3711
3712static ssize_t bluetooth_enable_store(struct device *dev,
3713 struct device_attribute *attr,
3714 const char *buf, size_t count)
3715{
Johannes Berg19d337d2009-06-02 13:01:37 +02003716 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID,
3717 attr, buf, count);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003718}
3719
3720static struct device_attribute dev_attr_bluetooth_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03003721 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003722 bluetooth_enable_show, bluetooth_enable_store);
3723
3724/* --------------------------------------------------------------------- */
3725
3726static struct attribute *bluetooth_attributes[] = {
3727 &dev_attr_bluetooth_enable.attr,
3728 NULL
3729};
3730
3731static const struct attribute_group bluetooth_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003732 .attrs = bluetooth_attributes,
3733};
3734
Johannes Berg19d337d2009-06-02 13:01:37 +02003735static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = {
3736 .get_status = bluetooth_get_status,
3737 .set_status = bluetooth_set_status,
3738};
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003739
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003740static void bluetooth_shutdown(void)
3741{
3742 /* Order firmware to save current state to NVRAM */
3743 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3744 TP_ACPI_BLTH_SAVE_STATE))
3745 printk(TPACPI_NOTICE
3746 "failed to save bluetooth state to NVRAM\n");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003747 else
3748 vdbg_printk(TPACPI_DBG_RFKILL,
3749 "bluestooth state saved to NVRAM\n");
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003750}
3751
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003752static void bluetooth_exit(void)
3753{
3754 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3755 &bluetooth_attr_group);
Johannes Berg19d337d2009-06-02 13:01:37 +02003756
3757 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
3758
3759 bluetooth_shutdown();
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003760}
3761
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003762static int __init bluetooth_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003764 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003765 int status = 0;
3766
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003767 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3768 "initializing bluetooth subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003769
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003770 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003771
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003772 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3773 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003774 tp_features.bluetooth = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003775 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003777 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3778 "bluetooth is %s, status 0x%02x\n",
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003779 str_supported(tp_features.bluetooth),
3780 status);
3781
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003782#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3783 if (dbg_bluetoothemul) {
3784 tp_features.bluetooth = 1;
3785 printk(TPACPI_INFO
3786 "bluetooth switch emulation enabled\n");
3787 } else
3788#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003789 if (tp_features.bluetooth &&
3790 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
3791 /* no bluetooth hardware present in system */
3792 tp_features.bluetooth = 0;
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003793 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003794 "bluetooth hardware not installed\n");
3795 }
3796
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003797 if (!tp_features.bluetooth)
3798 return 1;
3799
Johannes Berg19d337d2009-06-02 13:01:37 +02003800 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
3801 &bluetooth_tprfk_ops,
3802 RFKILL_TYPE_BLUETOOTH,
3803 TPACPI_RFK_BLUETOOTH_SW_NAME,
3804 true);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003805 if (res)
3806 return res;
3807
Johannes Berg19d337d2009-06-02 13:01:37 +02003808 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3809 &bluetooth_attr_group);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003810 if (res) {
Johannes Berg19d337d2009-06-02 13:01:37 +02003811 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003812 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003813 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003814
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003815 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816}
3817
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003818/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003819static int bluetooth_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820{
Johannes Berg19d337d2009-06-02 13:01:37 +02003821 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822}
3823
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003824static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825{
Johannes Berg19d337d2009-06-02 13:01:37 +02003826 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827}
3828
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003829static struct ibm_struct bluetooth_driver_data = {
3830 .name = "bluetooth",
3831 .read = bluetooth_read,
3832 .write = bluetooth_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003833 .exit = bluetooth_exit,
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003834 .suspend = bluetooth_suspend,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003835 .shutdown = bluetooth_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003836};
3837
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003838/*************************************************************************
3839 * Wan subdriver
3840 */
3841
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003842enum {
3843 /* ACPI GWAN/SWAN bits */
3844 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
3845 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003846 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
3847 off / last state */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003848};
3849
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003850#define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
3851
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003852static void wan_suspend(pm_message_t state)
3853{
3854 /* Try to make sure radio will resume powered off */
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003855 if (!acpi_evalf(NULL, NULL, "\\WGSV", "qvd",
3856 TP_ACPI_WGSV_PWR_OFF_ON_RESUME))
3857 vdbg_printk(TPACPI_DBG_RFKILL,
3858 "WWAN power down on resume request failed\n");
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003859}
3860
Johannes Berg19d337d2009-06-02 13:01:37 +02003861static int wan_get_status(void)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003862{
3863 int status;
3864
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003865#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3866 if (dbg_wwanemul)
3867 return (tpacpi_wwan_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02003868 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003869#endif
3870
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003871 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
3872 return -EIO;
3873
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003874 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02003875 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003876}
3877
Johannes Berg19d337d2009-06-02 13:01:37 +02003878static int wan_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003879{
3880 int status;
3881
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003882 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02003883 "will attempt to %s wwan\n",
3884 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003885
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003886#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3887 if (dbg_wwanemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02003888 tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003889 return 0;
3890 }
3891#endif
3892
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003893 /* We make sure to keep TP_ACPI_WANCARD_RESUMECTRL off */
Johannes Berg19d337d2009-06-02 13:01:37 +02003894 if (state == TPACPI_RFK_RADIO_ON)
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003895 status = TP_ACPI_WANCARD_RADIOSSW;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003896 else
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003897 status = 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02003898
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003899 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
3900 return -EIO;
3901
3902 return 0;
3903}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003904
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003905/* sysfs wan enable ---------------------------------------------------- */
3906static ssize_t wan_enable_show(struct device *dev,
3907 struct device_attribute *attr,
3908 char *buf)
3909{
Johannes Berg19d337d2009-06-02 13:01:37 +02003910 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
3911 attr, buf);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003912}
3913
3914static ssize_t wan_enable_store(struct device *dev,
3915 struct device_attribute *attr,
3916 const char *buf, size_t count)
3917{
Johannes Berg19d337d2009-06-02 13:01:37 +02003918 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
3919 attr, buf, count);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003920}
3921
3922static struct device_attribute dev_attr_wan_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03003923 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003924 wan_enable_show, wan_enable_store);
3925
3926/* --------------------------------------------------------------------- */
3927
3928static struct attribute *wan_attributes[] = {
3929 &dev_attr_wan_enable.attr,
3930 NULL
3931};
3932
3933static const struct attribute_group wan_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003934 .attrs = wan_attributes,
3935};
3936
Johannes Berg19d337d2009-06-02 13:01:37 +02003937static const struct tpacpi_rfk_ops wan_tprfk_ops = {
3938 .get_status = wan_get_status,
3939 .set_status = wan_set_status,
3940};
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003941
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003942static void wan_shutdown(void)
3943{
3944 /* Order firmware to save current state to NVRAM */
3945 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
3946 TP_ACPI_WGSV_SAVE_STATE))
3947 printk(TPACPI_NOTICE
3948 "failed to save WWAN state to NVRAM\n");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003949 else
3950 vdbg_printk(TPACPI_DBG_RFKILL,
3951 "WWAN state saved to NVRAM\n");
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003952}
3953
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003954static void wan_exit(void)
3955{
3956 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3957 &wan_attr_group);
Johannes Berg19d337d2009-06-02 13:01:37 +02003958
3959 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
3960
3961 wan_shutdown();
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003962}
3963
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003964static int __init wan_init(struct ibm_init_struct *iibm)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003965{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003966 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003967 int status = 0;
3968
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003969 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3970 "initializing wan subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003971
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003972 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003973
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003974 tp_features.wan = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003975 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003976
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003977 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3978 "wan is %s, status 0x%02x\n",
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003979 str_supported(tp_features.wan),
3980 status);
3981
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003982#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3983 if (dbg_wwanemul) {
3984 tp_features.wan = 1;
3985 printk(TPACPI_INFO
3986 "wwan switch emulation enabled\n");
3987 } else
3988#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003989 if (tp_features.wan &&
3990 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
3991 /* no wan hardware present in system */
3992 tp_features.wan = 0;
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003993 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003994 "wan hardware not installed\n");
3995 }
3996
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003997 if (!tp_features.wan)
3998 return 1;
3999
Johannes Berg19d337d2009-06-02 13:01:37 +02004000 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
4001 &wan_tprfk_ops,
4002 RFKILL_TYPE_WWAN,
4003 TPACPI_RFK_WWAN_SW_NAME,
4004 true);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004005 if (res)
4006 return res;
4007
Johannes Berg19d337d2009-06-02 13:01:37 +02004008 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4009 &wan_attr_group);
4010
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004011 if (res) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004012 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004013 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004014 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004015
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004016 return 0;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004017}
4018
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004019/* procfs -------------------------------------------------------------- */
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004020static int wan_read(char *p)
4021{
Johannes Berg19d337d2009-06-02 13:01:37 +02004022 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, p);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004023}
4024
4025static int wan_write(char *buf)
4026{
Johannes Berg19d337d2009-06-02 13:01:37 +02004027 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004028}
4029
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004030static struct ibm_struct wan_driver_data = {
4031 .name = "wan",
4032 .read = wan_read,
4033 .write = wan_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004034 .exit = wan_exit,
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02004035 .suspend = wan_suspend,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004036 .shutdown = wan_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004037};
4038
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004039/*************************************************************************
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004040 * UWB subdriver
4041 */
4042
4043enum {
4044 /* ACPI GUWB/SUWB bits */
4045 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4046 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4047};
4048
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004049#define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4050
Johannes Berg19d337d2009-06-02 13:01:37 +02004051static int uwb_get_status(void)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004052{
4053 int status;
4054
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004055#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4056 if (dbg_uwbemul)
4057 return (tpacpi_uwb_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004058 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004059#endif
4060
4061 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4062 return -EIO;
4063
4064 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004065 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004066}
4067
Johannes Berg19d337d2009-06-02 13:01:37 +02004068static int uwb_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004069{
4070 int status;
4071
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004072 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02004073 "will attempt to %s UWB\n",
4074 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004075
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004076#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4077 if (dbg_uwbemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004078 tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004079 return 0;
4080 }
4081#endif
4082
Johannes Berg19d337d2009-06-02 13:01:37 +02004083 if (state == TPACPI_RFK_RADIO_ON)
4084 status = TP_ACPI_UWB_RADIOSSW;
4085 else
4086 status = 0;
4087
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004088 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4089 return -EIO;
4090
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004091 return 0;
4092}
4093
4094/* --------------------------------------------------------------------- */
4095
Johannes Berg19d337d2009-06-02 13:01:37 +02004096static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
4097 .get_status = uwb_get_status,
4098 .set_status = uwb_set_status,
4099};
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004100
4101static void uwb_exit(void)
4102{
Johannes Berg19d337d2009-06-02 13:01:37 +02004103 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004104}
4105
4106static int __init uwb_init(struct ibm_init_struct *iibm)
4107{
4108 int res;
4109 int status = 0;
4110
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004111 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4112 "initializing uwb subdriver\n");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004113
4114 TPACPI_ACPIHANDLE_INIT(hkey);
4115
4116 tp_features.uwb = hkey_handle &&
4117 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4118
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004119 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4120 "uwb is %s, status 0x%02x\n",
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004121 str_supported(tp_features.uwb),
4122 status);
4123
4124#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4125 if (dbg_uwbemul) {
4126 tp_features.uwb = 1;
4127 printk(TPACPI_INFO
4128 "uwb switch emulation enabled\n");
4129 } else
4130#endif
4131 if (tp_features.uwb &&
4132 !(status & TP_ACPI_UWB_HWPRESENT)) {
4133 /* no uwb hardware present in system */
4134 tp_features.uwb = 0;
4135 dbg_printk(TPACPI_DBG_INIT,
4136 "uwb hardware not installed\n");
4137 }
4138
4139 if (!tp_features.uwb)
4140 return 1;
4141
4142 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
Johannes Berg19d337d2009-06-02 13:01:37 +02004143 &uwb_tprfk_ops,
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004144 RFKILL_TYPE_UWB,
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004145 TPACPI_RFK_UWB_SW_NAME,
Johannes Berg19d337d2009-06-02 13:01:37 +02004146 false);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004147 return res;
4148}
4149
4150static struct ibm_struct uwb_driver_data = {
4151 .name = "uwb",
4152 .exit = uwb_exit,
4153 .flags.experimental = 1,
4154};
4155
4156/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004157 * Video subdriver
4158 */
4159
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02004160#ifdef CONFIG_THINKPAD_ACPI_VIDEO
4161
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004162enum video_access_mode {
4163 TPACPI_VIDEO_NONE = 0,
4164 TPACPI_VIDEO_570, /* 570 */
4165 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4166 TPACPI_VIDEO_NEW, /* all others */
4167};
4168
4169enum { /* video status flags, based on VIDEO_570 */
4170 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4171 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4172 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4173};
4174
4175enum { /* TPACPI_VIDEO_570 constants */
4176 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4177 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4178 * video_status_flags */
4179 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4180 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4181};
4182
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02004183static enum video_access_mode video_supported;
4184static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004185
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004186static int video_autosw_get(void);
4187static int video_autosw_set(int enable);
4188
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004189TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004190
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004191static int __init video_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004193 int ivga;
4194
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004195 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4196
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004197 TPACPI_ACPIHANDLE_INIT(vid);
4198 TPACPI_ACPIHANDLE_INIT(vid2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004199
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004200 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4201 /* G41, assume IVGA doesn't change */
4202 vid_handle = vid2_handle;
4203
4204 if (!vid_handle)
4205 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004206 video_supported = TPACPI_VIDEO_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004207 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
4208 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004209 video_supported = TPACPI_VIDEO_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004210 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
4211 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004212 video_supported = TPACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004213 else
4214 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004215 video_supported = TPACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004217 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
4218 str_supported(video_supported != TPACPI_VIDEO_NONE),
4219 video_supported);
4220
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004221 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004222}
4223
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004224static void video_exit(void)
4225{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004226 dbg_printk(TPACPI_DBG_EXIT,
4227 "restoring original video autoswitch mode\n");
4228 if (video_autosw_set(video_orig_autosw))
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004229 printk(TPACPI_ERR "error while trying to restore original "
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004230 "video autoswitch mode\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004231}
4232
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004233static int video_outputsw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234{
4235 int status = 0;
4236 int i;
4237
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004238 switch (video_supported) {
4239 case TPACPI_VIDEO_570:
4240 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
4241 TP_ACPI_VIDEO_570_PHSCMD))
4242 return -EIO;
4243 status = i & TP_ACPI_VIDEO_570_PHSMASK;
4244 break;
4245 case TPACPI_VIDEO_770:
4246 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4247 return -EIO;
4248 if (i)
4249 status |= TP_ACPI_VIDEO_S_LCD;
4250 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4251 return -EIO;
4252 if (i)
4253 status |= TP_ACPI_VIDEO_S_CRT;
4254 break;
4255 case TPACPI_VIDEO_NEW:
4256 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4257 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4258 return -EIO;
4259 if (i)
4260 status |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004262 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4263 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4264 return -EIO;
4265 if (i)
4266 status |= TP_ACPI_VIDEO_S_LCD;
4267 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4268 return -EIO;
4269 if (i)
4270 status |= TP_ACPI_VIDEO_S_DVI;
4271 break;
4272 default:
4273 return -ENOSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004274 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275
4276 return status;
4277}
4278
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004279static int video_outputsw_set(int status)
4280{
4281 int autosw;
4282 int res = 0;
4283
4284 switch (video_supported) {
4285 case TPACPI_VIDEO_570:
4286 res = acpi_evalf(NULL, NULL,
4287 "\\_SB.PHS2", "vdd",
4288 TP_ACPI_VIDEO_570_PHS2CMD,
4289 status | TP_ACPI_VIDEO_570_PHS2SET);
4290 break;
4291 case TPACPI_VIDEO_770:
4292 autosw = video_autosw_get();
4293 if (autosw < 0)
4294 return autosw;
4295
4296 res = video_autosw_set(1);
4297 if (res)
4298 return res;
4299 res = acpi_evalf(vid_handle, NULL,
4300 "ASWT", "vdd", status * 0x100, 0);
4301 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004302 printk(TPACPI_ERR
4303 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004304 return -EIO;
4305 }
4306 break;
4307 case TPACPI_VIDEO_NEW:
4308 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004309 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004310 break;
4311 default:
4312 return -ENOSYS;
4313 }
4314
4315 return (res)? 0 : -EIO;
4316}
4317
4318static int video_autosw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004320 int autosw = 0;
4321
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004322 switch (video_supported) {
4323 case TPACPI_VIDEO_570:
4324 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
4325 return -EIO;
4326 break;
4327 case TPACPI_VIDEO_770:
4328 case TPACPI_VIDEO_NEW:
4329 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4330 return -EIO;
4331 break;
4332 default:
4333 return -ENOSYS;
4334 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004335
4336 return autosw & 1;
4337}
4338
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004339static int video_autosw_set(int enable)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004340{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004341 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004342 return -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004343 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004344}
4345
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004346static int video_outputsw_cycle(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004347{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004348 int autosw = video_autosw_get();
4349 int res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004350
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004351 if (autosw < 0)
4352 return autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004353
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004354 switch (video_supported) {
4355 case TPACPI_VIDEO_570:
4356 res = video_autosw_set(1);
4357 if (res)
4358 return res;
4359 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4360 break;
4361 case TPACPI_VIDEO_770:
4362 case TPACPI_VIDEO_NEW:
4363 res = video_autosw_set(1);
4364 if (res)
4365 return res;
4366 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4367 break;
4368 default:
4369 return -ENOSYS;
4370 }
4371 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004372 printk(TPACPI_ERR
4373 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004374 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004375 }
4376
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004377 return (res)? 0 : -EIO;
4378}
4379
4380static int video_expand_toggle(void)
4381{
4382 switch (video_supported) {
4383 case TPACPI_VIDEO_570:
4384 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
4385 0 : -EIO;
4386 case TPACPI_VIDEO_770:
4387 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
4388 0 : -EIO;
4389 case TPACPI_VIDEO_NEW:
4390 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
4391 0 : -EIO;
4392 default:
4393 return -ENOSYS;
4394 }
4395 /* not reached */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004396}
4397
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004398static int video_read(char *p)
4399{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004400 int status, autosw;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004401 int len = 0;
4402
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004403 if (video_supported == TPACPI_VIDEO_NONE) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004404 len += sprintf(p + len, "status:\t\tnot supported\n");
4405 return len;
4406 }
4407
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004408 status = video_outputsw_get();
4409 if (status < 0)
4410 return status;
4411
4412 autosw = video_autosw_get();
4413 if (autosw < 0)
4414 return autosw;
4415
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004416 len += sprintf(p + len, "status:\t\tsupported\n");
4417 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
4418 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004419 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004420 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
4421 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
4422 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
4423 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004424 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004425 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
4426 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
4427 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
4428
4429 return len;
4430}
4431
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004432static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433{
4434 char *cmd;
4435 int enable, disable, status;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004436 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004438 if (video_supported == TPACPI_VIDEO_NONE)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004439 return -ENODEV;
4440
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004441 enable = 0;
4442 disable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443
4444 while ((cmd = next_cmd(&buf))) {
4445 if (strlencmp(cmd, "lcd_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004446 enable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447 } else if (strlencmp(cmd, "lcd_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004448 disable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449 } else if (strlencmp(cmd, "crt_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004450 enable |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451 } else if (strlencmp(cmd, "crt_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004452 disable |= TP_ACPI_VIDEO_S_CRT;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004453 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004454 strlencmp(cmd, "dvi_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004455 enable |= TP_ACPI_VIDEO_S_DVI;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004456 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004457 strlencmp(cmd, "dvi_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004458 disable |= TP_ACPI_VIDEO_S_DVI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459 } else if (strlencmp(cmd, "auto_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004460 res = video_autosw_set(1);
4461 if (res)
4462 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463 } else if (strlencmp(cmd, "auto_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004464 res = video_autosw_set(0);
4465 if (res)
4466 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004467 } else if (strlencmp(cmd, "video_switch") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004468 res = video_outputsw_cycle();
4469 if (res)
4470 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004472 res = video_expand_toggle();
4473 if (res)
4474 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475 } else
4476 return -EINVAL;
4477 }
4478
4479 if (enable || disable) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004480 status = video_outputsw_get();
4481 if (status < 0)
4482 return status;
4483 res = video_outputsw_set((status & ~disable) | enable);
4484 if (res)
4485 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486 }
4487
4488 return 0;
4489}
4490
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004491static struct ibm_struct video_driver_data = {
4492 .name = "video",
4493 .read = video_read,
4494 .write = video_write,
4495 .exit = video_exit,
4496};
4497
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02004498#endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4499
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004500/*************************************************************************
4501 * Light (thinklight) subdriver
4502 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004504TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4505TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004506
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004507static int light_get_status(void)
4508{
4509 int status = 0;
4510
4511 if (tp_features.light_status) {
4512 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4513 return -EIO;
4514 return (!!status);
4515 }
4516
4517 return -ENXIO;
4518}
4519
4520static int light_set_status(int status)
4521{
4522 int rc;
4523
4524 if (tp_features.light) {
4525 if (cmos_handle) {
4526 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4527 (status)?
4528 TP_CMOS_THINKLIGHT_ON :
4529 TP_CMOS_THINKLIGHT_OFF);
4530 } else {
4531 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4532 (status)? 1 : 0);
4533 }
4534 return (rc)? 0 : -EIO;
4535 }
4536
4537 return -ENXIO;
4538}
4539
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004540static void light_set_status_worker(struct work_struct *work)
4541{
4542 struct tpacpi_led_classdev *data =
4543 container_of(work, struct tpacpi_led_classdev, work);
4544
4545 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00004546 light_set_status((data->new_state != TPACPI_LED_OFF));
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004547}
4548
4549static void light_sysfs_set(struct led_classdev *led_cdev,
4550 enum led_brightness brightness)
4551{
4552 struct tpacpi_led_classdev *data =
4553 container_of(led_cdev,
4554 struct tpacpi_led_classdev,
4555 led_classdev);
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00004556 data->new_state = (brightness != LED_OFF) ?
4557 TPACPI_LED_ON : TPACPI_LED_OFF;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004558 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004559}
4560
4561static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4562{
4563 return (light_get_status() == 1)? LED_FULL : LED_OFF;
4564}
4565
4566static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4567 .led_classdev = {
4568 .name = "tpacpi::thinklight",
4569 .brightness_set = &light_sysfs_set,
4570 .brightness_get = &light_sysfs_get,
4571 }
4572};
4573
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004574static int __init light_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004575{
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004576 int rc;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004577
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004578 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4579
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004580 TPACPI_ACPIHANDLE_INIT(ledb);
4581 TPACPI_ACPIHANDLE_INIT(lght);
4582 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004583 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004584
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004585 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004586 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004587
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004588 if (tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004589 /* light status not supported on
4590 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004591 tp_features.light_status =
4592 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004594 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4595 str_supported(tp_features.light),
4596 str_supported(tp_features.light_status));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004597
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004598 if (!tp_features.light)
4599 return 1;
4600
4601 rc = led_classdev_register(&tpacpi_pdev->dev,
4602 &tpacpi_led_thinklight.led_classdev);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004603
4604 if (rc < 0) {
4605 tp_features.light = 0;
4606 tp_features.light_status = 0;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004607 } else {
4608 rc = 0;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004609 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004610
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004611 return rc;
4612}
4613
4614static void light_exit(void)
4615{
4616 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
4617 if (work_pending(&tpacpi_led_thinklight.work))
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004618 flush_workqueue(tpacpi_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004619}
4620
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004621static int light_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622{
4623 int len = 0;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004624 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004625
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004626 if (!tp_features.light) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004627 len += sprintf(p + len, "status:\t\tnot supported\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004628 } else if (!tp_features.light_status) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004629 len += sprintf(p + len, "status:\t\tunknown\n");
4630 len += sprintf(p + len, "commands:\ton, off\n");
4631 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004632 status = light_get_status();
4633 if (status < 0)
4634 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004636 len += sprintf(p + len, "commands:\ton, off\n");
4637 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004638
4639 return len;
4640}
4641
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004642static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004645 int newstatus = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004646
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004647 if (!tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004648 return -ENODEV;
4649
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650 while ((cmd = next_cmd(&buf))) {
4651 if (strlencmp(cmd, "on") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004652 newstatus = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653 } else if (strlencmp(cmd, "off") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004654 newstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655 } else
4656 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657 }
4658
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004659 return light_set_status(newstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004660}
4661
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004662static struct ibm_struct light_driver_data = {
4663 .name = "light",
4664 .read = light_read,
4665 .write = light_write,
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004666 .exit = light_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004667};
4668
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004669/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004670 * CMOS subdriver
4671 */
4672
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004673/* sysfs cmos_command -------------------------------------------------- */
4674static ssize_t cmos_command_store(struct device *dev,
4675 struct device_attribute *attr,
4676 const char *buf, size_t count)
4677{
4678 unsigned long cmos_cmd;
4679 int res;
4680
4681 if (parse_strtoul(buf, 21, &cmos_cmd))
4682 return -EINVAL;
4683
4684 res = issue_thinkpad_cmos_command(cmos_cmd);
4685 return (res)? res : count;
4686}
4687
4688static struct device_attribute dev_attr_cmos_command =
4689 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4690
4691/* --------------------------------------------------------------------- */
4692
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004693static int __init cmos_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004694{
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004695 int res;
4696
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004697 vdbg_printk(TPACPI_DBG_INIT,
4698 "initializing cmos commands subdriver\n");
4699
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004700 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004701
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004702 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4703 str_supported(cmos_handle != NULL));
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004704
4705 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4706 if (res)
4707 return res;
4708
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004709 return (cmos_handle)? 0 : 1;
4710}
4711
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004712static void cmos_exit(void)
4713{
4714 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4715}
4716
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004717static int cmos_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004718{
4719 int len = 0;
4720
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004721 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4722 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004723 if (!cmos_handle)
4724 len += sprintf(p + len, "status:\t\tnot supported\n");
4725 else {
4726 len += sprintf(p + len, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004727 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728 }
4729
4730 return len;
4731}
4732
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004733static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004734{
4735 char *cmd;
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004736 int cmos_cmd, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004737
4738 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004739 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
4740 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741 /* cmos_cmd set */
4742 } else
4743 return -EINVAL;
4744
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004745 res = issue_thinkpad_cmos_command(cmos_cmd);
4746 if (res)
4747 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004748 }
4749
4750 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004751}
4752
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004753static struct ibm_struct cmos_driver_data = {
4754 .name = "cmos",
4755 .read = cmos_read,
4756 .write = cmos_write,
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004757 .exit = cmos_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004758};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004759
4760/*************************************************************************
4761 * LED subdriver
4762 */
4763
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004764enum led_access_mode {
4765 TPACPI_LED_NONE = 0,
4766 TPACPI_LED_570, /* 570 */
4767 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4768 TPACPI_LED_NEW, /* all others */
4769};
4770
4771enum { /* For TPACPI_LED_OLD */
4772 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
4773 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
4774 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
4775};
4776
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02004777static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004778
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004779TPACPI_HANDLE(led, ec, "SLED", /* 570 */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004780 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
4781 /* T20-22, X20-21 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004782 "LED", /* all others */
4783 ); /* R30, R31 */
4784
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03004785#define TPACPI_LED_NUMLEDS 16
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004786static struct tpacpi_led_classdev *tpacpi_leds;
4787static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
Harvey Harrisone3aa51f2008-05-29 17:51:57 -07004788static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004789 /* there's a limit of 19 chars + NULL before 2.6.26 */
4790 "tpacpi::power",
4791 "tpacpi:orange:batt",
4792 "tpacpi:green:batt",
4793 "tpacpi::dock_active",
4794 "tpacpi::bay_active",
4795 "tpacpi::dock_batt",
4796 "tpacpi::unknown_led",
4797 "tpacpi::standby",
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03004798 "tpacpi::dock_status1",
4799 "tpacpi::dock_status2",
4800 "tpacpi::unknown_led2",
4801 "tpacpi::unknown_led3",
4802 "tpacpi::thinkvantage",
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004803};
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03004804#define TPACPI_SAFE_LEDS 0x1081U
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004805
4806static inline bool tpacpi_is_led_restricted(const unsigned int led)
4807{
4808#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
4809 return false;
4810#else
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03004811 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004812#endif
4813}
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004814
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004815static int led_get_status(const unsigned int led)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004816{
4817 int status;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004818 enum led_status_t led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004819
4820 switch (led_supported) {
4821 case TPACPI_LED_570:
4822 if (!acpi_evalf(ec_handle,
4823 &status, "GLED", "dd", 1 << led))
4824 return -EIO;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004825 led_s = (status == 0)?
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004826 TPACPI_LED_OFF :
4827 ((status == 1)?
4828 TPACPI_LED_ON :
4829 TPACPI_LED_BLINK);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004830 tpacpi_led_state_cache[led] = led_s;
4831 return led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004832 default:
4833 return -ENXIO;
4834 }
4835
4836 /* not reached */
4837}
4838
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004839static int led_set_status(const unsigned int led,
4840 const enum led_status_t ledstatus)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004841{
4842 /* off, on, blink. Index is led_status_t */
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004843 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
4844 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004845
4846 int rc = 0;
4847
4848 switch (led_supported) {
4849 case TPACPI_LED_570:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004850 /* 570 */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004851 if (unlikely(led > 7))
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004852 return -EINVAL;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004853 if (unlikely(tpacpi_is_led_restricted(led)))
4854 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004855 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4856 (1 << led), led_sled_arg1[ledstatus]))
4857 rc = -EIO;
4858 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004859 case TPACPI_LED_OLD:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004860 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004861 if (unlikely(led > 7))
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004862 return -EINVAL;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004863 if (unlikely(tpacpi_is_led_restricted(led)))
4864 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004865 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
4866 if (rc >= 0)
4867 rc = ec_write(TPACPI_LED_EC_HLBL,
4868 (ledstatus == TPACPI_LED_BLINK) << led);
4869 if (rc >= 0)
4870 rc = ec_write(TPACPI_LED_EC_HLCL,
4871 (ledstatus != TPACPI_LED_OFF) << led);
4872 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004873 case TPACPI_LED_NEW:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004874 /* all others */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004875 if (unlikely(led >= TPACPI_LED_NUMLEDS))
4876 return -EINVAL;
4877 if (unlikely(tpacpi_is_led_restricted(led)))
4878 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004879 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4880 led, led_led_arg1[ledstatus]))
4881 rc = -EIO;
4882 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004883 default:
4884 rc = -ENXIO;
4885 }
4886
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004887 if (!rc)
4888 tpacpi_led_state_cache[led] = ledstatus;
4889
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004890 return rc;
4891}
4892
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004893static void led_set_status_worker(struct work_struct *work)
4894{
4895 struct tpacpi_led_classdev *data =
4896 container_of(work, struct tpacpi_led_classdev, work);
4897
4898 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00004899 led_set_status(data->led, data->new_state);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004900}
4901
4902static void led_sysfs_set(struct led_classdev *led_cdev,
4903 enum led_brightness brightness)
4904{
4905 struct tpacpi_led_classdev *data = container_of(led_cdev,
4906 struct tpacpi_led_classdev, led_classdev);
4907
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00004908 if (brightness == LED_OFF)
4909 data->new_state = TPACPI_LED_OFF;
4910 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
4911 data->new_state = TPACPI_LED_ON;
4912 else
4913 data->new_state = TPACPI_LED_BLINK;
4914
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004915 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004916}
4917
4918static int led_sysfs_blink_set(struct led_classdev *led_cdev,
4919 unsigned long *delay_on, unsigned long *delay_off)
4920{
4921 struct tpacpi_led_classdev *data = container_of(led_cdev,
4922 struct tpacpi_led_classdev, led_classdev);
4923
4924 /* Can we choose the flash rate? */
4925 if (*delay_on == 0 && *delay_off == 0) {
4926 /* yes. set them to the hardware blink rate (1 Hz) */
4927 *delay_on = 500; /* ms */
4928 *delay_off = 500; /* ms */
4929 } else if ((*delay_on != 500) || (*delay_off != 500))
4930 return -EINVAL;
4931
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00004932 data->new_state = TPACPI_LED_BLINK;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004933 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004934
4935 return 0;
4936}
4937
4938static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
4939{
4940 int rc;
4941
4942 struct tpacpi_led_classdev *data = container_of(led_cdev,
4943 struct tpacpi_led_classdev, led_classdev);
4944
4945 rc = led_get_status(data->led);
4946
4947 if (rc == TPACPI_LED_OFF || rc < 0)
4948 rc = LED_OFF; /* no error handling in led class :( */
4949 else
4950 rc = LED_FULL;
4951
4952 return rc;
4953}
4954
4955static void led_exit(void)
4956{
4957 unsigned int i;
4958
4959 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4960 if (tpacpi_leds[i].led_classdev.name)
4961 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
4962 }
4963
4964 kfree(tpacpi_leds);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004965}
4966
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004967static int __init tpacpi_init_led(unsigned int led)
4968{
4969 int rc;
4970
4971 tpacpi_leds[led].led = led;
4972
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03004973 /* LEDs with no name don't get registered */
4974 if (!tpacpi_led_names[led])
4975 return 0;
4976
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004977 tpacpi_leds[led].led_classdev.brightness_set = &led_sysfs_set;
4978 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
4979 if (led_supported == TPACPI_LED_570)
4980 tpacpi_leds[led].led_classdev.brightness_get =
4981 &led_sysfs_get;
4982
4983 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
4984
4985 INIT_WORK(&tpacpi_leds[led].work, led_set_status_worker);
4986
4987 rc = led_classdev_register(&tpacpi_pdev->dev,
4988 &tpacpi_leds[led].led_classdev);
4989 if (rc < 0)
4990 tpacpi_leds[led].led_classdev.name = NULL;
4991
4992 return rc;
4993}
4994
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03004995static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
4996 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
4997 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
4998 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
4999
5000 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5001 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5002 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5003 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5004 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5005 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5006 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5007 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5008
5009 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5010 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5011 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5012 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5013 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5014
5015 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5016 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5017 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5018 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5019
5020 /* (1) - may have excess leds enabled on MSB */
5021
5022 /* Defaults (order matters, keep last, don't reorder!) */
5023 { /* Lenovo */
5024 .vendor = PCI_VENDOR_ID_LENOVO,
5025 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5026 .quirks = 0x1fffU,
5027 },
5028 { /* IBM ThinkPads with no EC version string */
5029 .vendor = PCI_VENDOR_ID_IBM,
5030 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
5031 .quirks = 0x00ffU,
5032 },
5033 { /* IBM ThinkPads with EC version string */
5034 .vendor = PCI_VENDOR_ID_IBM,
5035 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5036 .quirks = 0x00bfU,
5037 },
5038};
5039
5040#undef TPACPI_LEDQ_IBM
5041#undef TPACPI_LEDQ_LNV
5042
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005043static int __init led_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005044{
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005045 unsigned int i;
5046 int rc;
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005047 unsigned long useful_leds;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005048
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005049 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
5050
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005051 TPACPI_ACPIHANDLE_INIT(led);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005052
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005053 if (!led_handle)
5054 /* led not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005055 led_supported = TPACPI_LED_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005056 else if (strlencmp(led_path, "SLED") == 0)
5057 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005058 led_supported = TPACPI_LED_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005059 else if (strlencmp(led_path, "SYSL") == 0)
5060 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005061 led_supported = TPACPI_LED_OLD;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005062 else
5063 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005064 led_supported = TPACPI_LED_NEW;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005065
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005066 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
5067 str_supported(led_supported), led_supported);
5068
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005069 if (led_supported == TPACPI_LED_NONE)
5070 return 1;
5071
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005072 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
5073 GFP_KERNEL);
5074 if (!tpacpi_leds) {
5075 printk(TPACPI_ERR "Out of memory for LED data\n");
5076 return -ENOMEM;
5077 }
5078
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005079 useful_leds = tpacpi_check_quirks(led_useful_qtable,
5080 ARRAY_SIZE(led_useful_qtable));
5081
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005082 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005083 if (!tpacpi_is_led_restricted(i) &&
5084 test_bit(i, &useful_leds)) {
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005085 rc = tpacpi_init_led(i);
5086 if (rc < 0) {
5087 led_exit();
5088 return rc;
5089 }
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005090 }
5091 }
5092
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005093#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005094 printk(TPACPI_NOTICE
5095 "warning: userspace override of important "
5096 "firmware LEDs is enabled\n");
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005097#endif
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005098 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005099}
5100
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005101#define str_led_status(s) \
5102 ((s) == TPACPI_LED_OFF ? "off" : \
5103 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005104
5105static int led_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005106{
5107 int len = 0;
5108
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005109 if (!led_supported) {
5110 len += sprintf(p + len, "status:\t\tnot supported\n");
5111 return len;
5112 }
5113 len += sprintf(p + len, "status:\t\tsupported\n");
5114
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005115 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005116 /* 570 */
5117 int i, status;
5118 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005119 status = led_get_status(i);
5120 if (status < 0)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005121 return -EIO;
5122 len += sprintf(p + len, "%d:\t\t%s\n",
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005123 i, str_led_status(status));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005124 }
5125 }
5126
Linus Torvalds1da177e2005-04-16 15:20:36 -07005127 len += sprintf(p + len, "commands:\t"
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005128 "<led> on, <led> off, <led> blink (<led> is 0-15)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005129
5130 return len;
5131}
5132
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005133static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005134{
5135 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005136 int led, rc;
5137 enum led_status_t s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005138
5139 if (!led_supported)
5140 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005141
5142 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005143 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 15)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005144 return -EINVAL;
5145
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005146 if (strstr(cmd, "off")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005147 s = TPACPI_LED_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005148 } else if (strstr(cmd, "on")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005149 s = TPACPI_LED_ON;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005150 } else if (strstr(cmd, "blink")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005151 s = TPACPI_LED_BLINK;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005152 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005153 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005154 }
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005155
5156 rc = led_set_status(led, s);
5157 if (rc < 0)
5158 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005159 }
5160
5161 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005162}
5163
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005164static struct ibm_struct led_driver_data = {
5165 .name = "led",
5166 .read = led_read,
5167 .write = led_write,
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005168 .exit = led_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005169};
5170
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005171/*************************************************************************
5172 * Beep subdriver
5173 */
5174
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005175TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005176
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005177#define TPACPI_BEEP_Q1 0x0001
5178
5179static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
5180 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
5181 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
5182};
5183
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005184static int __init beep_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005185{
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005186 unsigned long quirks;
5187
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005188 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
5189
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005190 TPACPI_ACPIHANDLE_INIT(beep);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005191
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005192 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
5193 str_supported(beep_handle != NULL));
5194
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005195 quirks = tpacpi_check_quirks(beep_quirk_table,
5196 ARRAY_SIZE(beep_quirk_table));
5197
5198 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
5199
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005200 return (beep_handle)? 0 : 1;
5201}
5202
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005203static int beep_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005204{
5205 int len = 0;
5206
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005207 if (!beep_handle)
5208 len += sprintf(p + len, "status:\t\tnot supported\n");
5209 else {
5210 len += sprintf(p + len, "status:\t\tsupported\n");
5211 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
5212 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005213
5214 return len;
5215}
5216
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005217static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005218{
5219 char *cmd;
5220 int beep_cmd;
5221
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005222 if (!beep_handle)
5223 return -ENODEV;
5224
Linus Torvalds1da177e2005-04-16 15:20:36 -07005225 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005226 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
5227 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005228 /* beep_cmd set */
5229 } else
5230 return -EINVAL;
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005231 if (tp_features.beep_needs_two_args) {
5232 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
5233 beep_cmd, 0))
5234 return -EIO;
5235 } else {
5236 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
5237 beep_cmd))
5238 return -EIO;
5239 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005240 }
5241
5242 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005243}
5244
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005245static struct ibm_struct beep_driver_data = {
5246 .name = "beep",
5247 .read = beep_read,
5248 .write = beep_write,
5249};
5250
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005251/*************************************************************************
5252 * Thermal subdriver
5253 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005254
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005255enum thermal_access_mode {
5256 TPACPI_THERMAL_NONE = 0, /* No thermal support */
5257 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
5258 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
5259 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
5260 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
5261};
5262
5263enum { /* TPACPI_THERMAL_TPEC_* */
5264 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
5265 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
5266 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
5267};
5268
5269#define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5270struct ibm_thermal_sensors_struct {
5271 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
5272};
5273
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005274static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005275
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005276/* idx is zero-based */
5277static int thermal_get_sensor(int idx, s32 *value)
5278{
5279 int t;
5280 s8 tmp;
5281 char tmpi[5];
5282
5283 t = TP_EC_THERMAL_TMP0;
5284
5285 switch (thermal_read_mode) {
5286#if TPACPI_MAX_THERMAL_SENSORS >= 16
5287 case TPACPI_THERMAL_TPEC_16:
5288 if (idx >= 8 && idx <= 15) {
5289 t = TP_EC_THERMAL_TMP8;
5290 idx -= 8;
5291 }
5292 /* fallthrough */
5293#endif
5294 case TPACPI_THERMAL_TPEC_8:
5295 if (idx <= 7) {
5296 if (!acpi_ec_read(t + idx, &tmp))
5297 return -EIO;
5298 *value = tmp * 1000;
5299 return 0;
5300 }
5301 break;
5302
5303 case TPACPI_THERMAL_ACPI_UPDT:
5304 if (idx <= 7) {
5305 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5306 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
5307 return -EIO;
5308 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5309 return -EIO;
5310 *value = (t - 2732) * 100;
5311 return 0;
5312 }
5313 break;
5314
5315 case TPACPI_THERMAL_ACPI_TMP07:
5316 if (idx <= 7) {
5317 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5318 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5319 return -EIO;
5320 if (t > 127 || t < -127)
5321 t = TP_EC_THERMAL_TMP_NA;
5322 *value = t * 1000;
5323 return 0;
5324 }
5325 break;
5326
5327 case TPACPI_THERMAL_NONE:
5328 default:
5329 return -ENOSYS;
5330 }
5331
5332 return -EINVAL;
5333}
5334
5335static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5336{
5337 int res, i;
5338 int n;
5339
5340 n = 8;
5341 i = 0;
5342
5343 if (!s)
5344 return -EINVAL;
5345
5346 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5347 n = 16;
5348
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005349 for (i = 0 ; i < n; i++) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005350 res = thermal_get_sensor(i, &s->temp[i]);
5351 if (res)
5352 return res;
5353 }
5354
5355 return n;
5356}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005357
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005358/* sysfs temp##_input -------------------------------------------------- */
5359
5360static ssize_t thermal_temp_input_show(struct device *dev,
5361 struct device_attribute *attr,
5362 char *buf)
5363{
5364 struct sensor_device_attribute *sensor_attr =
5365 to_sensor_dev_attr(attr);
5366 int idx = sensor_attr->index;
5367 s32 value;
5368 int res;
5369
5370 res = thermal_get_sensor(idx, &value);
5371 if (res)
5372 return res;
5373 if (value == TP_EC_THERMAL_TMP_NA * 1000)
5374 return -ENXIO;
5375
5376 return snprintf(buf, PAGE_SIZE, "%d\n", value);
5377}
5378
5379#define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005380 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5381 thermal_temp_input_show, NULL, _idxB)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005382
5383static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5384 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5385 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5386 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5387 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5388 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5389 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5390 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5391 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5392 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5393 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5394 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5395 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5396 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5397 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5398 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5399 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5400};
5401
5402#define THERMAL_ATTRS(X) \
5403 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5404
5405static struct attribute *thermal_temp_input_attr[] = {
5406 THERMAL_ATTRS(8),
5407 THERMAL_ATTRS(9),
5408 THERMAL_ATTRS(10),
5409 THERMAL_ATTRS(11),
5410 THERMAL_ATTRS(12),
5411 THERMAL_ATTRS(13),
5412 THERMAL_ATTRS(14),
5413 THERMAL_ATTRS(15),
5414 THERMAL_ATTRS(0),
5415 THERMAL_ATTRS(1),
5416 THERMAL_ATTRS(2),
5417 THERMAL_ATTRS(3),
5418 THERMAL_ATTRS(4),
5419 THERMAL_ATTRS(5),
5420 THERMAL_ATTRS(6),
5421 THERMAL_ATTRS(7),
5422 NULL
5423};
5424
5425static const struct attribute_group thermal_temp_input16_group = {
5426 .attrs = thermal_temp_input_attr
5427};
5428
5429static const struct attribute_group thermal_temp_input8_group = {
5430 .attrs = &thermal_temp_input_attr[8]
5431};
5432
5433#undef THERMAL_SENSOR_ATTR_TEMP
5434#undef THERMAL_ATTRS
5435
5436/* --------------------------------------------------------------------- */
5437
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005438static int __init thermal_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005439{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005440 u8 t, ta1, ta2;
5441 int i;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005442 int acpi_tmp7;
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005443 int res;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005444
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005445 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5446
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005447 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005448
Henrique de Moraes Holschuh3d6f99c2007-07-18 23:45:46 -03005449 if (thinkpad_id.ec_model) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005450 /*
5451 * Direct EC access mode: sensors at registers
5452 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5453 * non-implemented, thermal sensors return 0x80 when
5454 * not available
5455 */
5456
5457 ta1 = ta2 = 0;
5458 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03005459 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005460 ta1 |= t;
5461 } else {
5462 ta1 = 0;
5463 break;
5464 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03005465 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005466 ta2 |= t;
5467 } else {
5468 ta1 = 0;
5469 break;
5470 }
5471 }
5472 if (ta1 == 0) {
5473 /* This is sheer paranoia, but we handle it anyway */
5474 if (acpi_tmp7) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005475 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005476 "ThinkPad ACPI EC access misbehaving, "
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005477 "falling back to ACPI TMPx access "
5478 "mode\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005479 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005480 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005481 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005482 "ThinkPad ACPI EC access misbehaving, "
5483 "disabling thermal sensors access\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005484 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005485 }
5486 } else {
5487 thermal_read_mode =
5488 (ta2 != 0) ?
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005489 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005490 }
5491 } else if (acpi_tmp7) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005492 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
5493 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005494 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005495 } else {
5496 /* Standard ACPI TMPx access, max 8 sensors */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005497 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005498 }
5499 } else {
5500 /* temperatures not supported on 570, G4x, R30, R31, R32 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005501 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005502 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005503
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005504 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5505 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5506 thermal_read_mode);
5507
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005508 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005509 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005510 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005511 &thermal_temp_input16_group);
5512 if (res)
5513 return res;
5514 break;
5515 case TPACPI_THERMAL_TPEC_8:
5516 case TPACPI_THERMAL_ACPI_TMP07:
5517 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005518 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005519 &thermal_temp_input8_group);
5520 if (res)
5521 return res;
5522 break;
5523 case TPACPI_THERMAL_NONE:
5524 default:
5525 return 1;
5526 }
5527
5528 return 0;
5529}
5530
5531static void thermal_exit(void)
5532{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005533 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005534 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005535 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005536 &thermal_temp_input16_group);
5537 break;
5538 case TPACPI_THERMAL_TPEC_8:
5539 case TPACPI_THERMAL_ACPI_TMP07:
5540 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005541 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005542 &thermal_temp_input16_group);
5543 break;
5544 case TPACPI_THERMAL_NONE:
5545 default:
5546 break;
5547 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005548}
5549
5550static int thermal_read(char *p)
5551{
5552 int len = 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005553 int n, i;
5554 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005555
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005556 n = thermal_get_sensors(&t);
5557 if (unlikely(n < 0))
5558 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005559
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005560 len += sprintf(p + len, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005561
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005562 if (n > 0) {
5563 for (i = 0; i < (n - 1); i++)
5564 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
5565 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
5566 } else
5567 len += sprintf(p + len, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005568
5569 return len;
5570}
5571
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005572static struct ibm_struct thermal_driver_data = {
5573 .name = "thermal",
5574 .read = thermal_read,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005575 .exit = thermal_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005576};
5577
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005578/*************************************************************************
5579 * EC Dump subdriver
5580 */
5581
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005582static u8 ecdump_regs[256];
5583
5584static int ecdump_read(char *p)
5585{
5586 int len = 0;
5587 int i, j;
5588 u8 v;
5589
5590 len += sprintf(p + len, "EC "
5591 " +00 +01 +02 +03 +04 +05 +06 +07"
5592 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
5593 for (i = 0; i < 256; i += 16) {
5594 len += sprintf(p + len, "EC 0x%02x:", i);
5595 for (j = 0; j < 16; j++) {
5596 if (!acpi_ec_read(i + j, &v))
5597 break;
5598 if (v != ecdump_regs[i + j])
5599 len += sprintf(p + len, " *%02x", v);
5600 else
5601 len += sprintf(p + len, " %02x", v);
5602 ecdump_regs[i + j] = v;
5603 }
5604 len += sprintf(p + len, "\n");
5605 if (j != 16)
5606 break;
5607 }
5608
5609 /* These are way too dangerous to advertise openly... */
5610#if 0
5611 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
5612 " (<offset> is 00-ff, <value> is 00-ff)\n");
5613 len += sprintf(p + len, "commands:\t0x<offset> <value> "
5614 " (<offset> is 00-ff, <value> is 0-255)\n");
5615#endif
5616 return len;
5617}
5618
5619static int ecdump_write(char *buf)
5620{
5621 char *cmd;
5622 int i, v;
5623
5624 while ((cmd = next_cmd(&buf))) {
5625 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
5626 /* i and v set */
5627 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
5628 /* i and v set */
5629 } else
5630 return -EINVAL;
5631 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
5632 if (!acpi_ec_write(i, v))
5633 return -EIO;
5634 } else
5635 return -EINVAL;
5636 }
5637
5638 return 0;
5639}
5640
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005641static struct ibm_struct ecdump_driver_data = {
5642 .name = "ecdump",
5643 .read = ecdump_read,
5644 .write = ecdump_write,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03005645 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005646};
5647
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005648/*************************************************************************
5649 * Backlight/brightness subdriver
5650 */
Holger Macht8acb0252006-10-20 14:30:28 -07005651
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005652#define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5653
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005654/*
5655 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
5656 * CMOS NVRAM byte 0x5E, bits 0-3.
5657 *
5658 * EC HBRV (0x31) has the following layout
5659 * Bit 7: unknown function
5660 * Bit 6: unknown function
5661 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5662 * Bit 4: must be set to zero to avoid problems
5663 * Bit 3-0: backlight brightness level
5664 *
5665 * brightness_get_raw returns status data in the HBRV layout
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03005666 *
5667 * WARNING: The X61 has been verified to use HBRV for something else, so
5668 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
5669 * testing on the very early *60 Lenovo models...
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005670 */
5671
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005672enum {
5673 TP_EC_BACKLIGHT = 0x31,
5674
5675 /* TP_EC_BACKLIGHT bitmasks */
5676 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5677 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5678 TP_EC_BACKLIGHT_MAPSW = 0x20,
5679};
5680
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005681enum tpacpi_brightness_access_mode {
5682 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
5683 TPACPI_BRGHT_MODE_EC, /* EC control */
5684 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
5685 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
5686 TPACPI_BRGHT_MODE_MAX
5687};
5688
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -03005689static struct backlight_device *ibm_backlight_device;
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005690
5691static enum tpacpi_brightness_access_mode brightness_mode =
5692 TPACPI_BRGHT_MODE_MAX;
5693
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005694static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5695
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005696static struct mutex brightness_mutex;
5697
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005698/* NVRAM brightness access,
5699 * call with brightness_mutex held! */
5700static unsigned int tpacpi_brightness_nvram_get(void)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005701{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005702 u8 lnvram;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005703
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005704 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5705 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5706 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
5707 lnvram &= (tp_features.bright_16levels) ? 0x0f : 0x07;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005708
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005709 return lnvram;
5710}
5711
5712static void tpacpi_brightness_checkpoint_nvram(void)
5713{
5714 u8 lec = 0;
5715 u8 b_nvram;
5716
5717 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
5718 return;
5719
5720 vdbg_printk(TPACPI_DBG_BRGHT,
5721 "trying to checkpoint backlight level to NVRAM...\n");
5722
5723 if (mutex_lock_killable(&brightness_mutex) < 0)
5724 return;
5725
5726 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5727 goto unlock;
5728 lec &= TP_EC_BACKLIGHT_LVLMSK;
5729 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
5730
5731 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5732 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
5733 /* NVRAM needs update */
5734 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
5735 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
5736 b_nvram |= lec;
5737 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
5738 dbg_printk(TPACPI_DBG_BRGHT,
5739 "updated NVRAM backlight level to %u (0x%02x)\n",
5740 (unsigned int) lec, (unsigned int) b_nvram);
5741 } else
5742 vdbg_printk(TPACPI_DBG_BRGHT,
5743 "NVRAM backlight level already is %u (0x%02x)\n",
5744 (unsigned int) lec, (unsigned int) b_nvram);
5745
5746unlock:
5747 mutex_unlock(&brightness_mutex);
5748}
5749
5750
5751/* call with brightness_mutex held! */
5752static int tpacpi_brightness_get_raw(int *status)
5753{
5754 u8 lec = 0;
5755
5756 switch (brightness_mode) {
5757 case TPACPI_BRGHT_MODE_UCMS_STEP:
5758 *status = tpacpi_brightness_nvram_get();
5759 return 0;
5760 case TPACPI_BRGHT_MODE_EC:
5761 case TPACPI_BRGHT_MODE_ECNVRAM:
5762 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
Henrique de Moraes Holschuh2d5e94d2008-04-26 01:02:20 -03005763 return -EIO;
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005764 *status = lec;
5765 return 0;
5766 default:
5767 return -ENXIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005768 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005769}
5770
5771/* call with brightness_mutex held! */
5772/* do NOT call with illegal backlight level value */
5773static int tpacpi_brightness_set_ec(unsigned int value)
5774{
5775 u8 lec = 0;
5776
5777 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5778 return -EIO;
5779
5780 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
5781 (lec & TP_EC_BACKLIGHT_CMDMSK) |
5782 (value & TP_EC_BACKLIGHT_LVLMSK))))
5783 return -EIO;
5784
5785 return 0;
5786}
5787
5788/* call with brightness_mutex held! */
5789static int tpacpi_brightness_set_ucmsstep(unsigned int value)
5790{
5791 int cmos_cmd, inc;
5792 unsigned int current_value, i;
5793
5794 current_value = tpacpi_brightness_nvram_get();
5795
5796 if (value == current_value)
5797 return 0;
5798
5799 cmos_cmd = (value > current_value) ?
5800 TP_CMOS_BRIGHTNESS_UP :
5801 TP_CMOS_BRIGHTNESS_DOWN;
5802 inc = (value > current_value) ? 1 : -1;
5803
5804 for (i = current_value; i != value; i += inc)
5805 if (issue_thinkpad_cmos_command(cmos_cmd))
5806 return -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005807
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005808 return 0;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005809}
5810
5811/* May return EINTR which can always be mapped to ERESTARTSYS */
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005812static int brightness_set(unsigned int value)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005813{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005814 int res;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005815
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005816 if (value > ((tp_features.bright_16levels)? 15 : 7) ||
5817 value < 0)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005818 return -EINVAL;
5819
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005820 vdbg_printk(TPACPI_DBG_BRGHT,
5821 "set backlight level to %d\n", value);
5822
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02005823 res = mutex_lock_killable(&brightness_mutex);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005824 if (res < 0)
5825 return res;
5826
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005827 switch (brightness_mode) {
5828 case TPACPI_BRGHT_MODE_EC:
5829 case TPACPI_BRGHT_MODE_ECNVRAM:
5830 res = tpacpi_brightness_set_ec(value);
5831 break;
5832 case TPACPI_BRGHT_MODE_UCMS_STEP:
5833 res = tpacpi_brightness_set_ucmsstep(value);
5834 break;
5835 default:
5836 res = -ENXIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005837 }
5838
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005839 mutex_unlock(&brightness_mutex);
5840 return res;
5841}
5842
5843/* sysfs backlight class ----------------------------------------------- */
5844
5845static int brightness_update_status(struct backlight_device *bd)
5846{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005847 unsigned int level =
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005848 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
5849 bd->props.power == FB_BLANK_UNBLANK) ?
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005850 bd->props.brightness : 0;
5851
5852 dbg_printk(TPACPI_DBG_BRGHT,
5853 "backlight: attempt to set level to %d\n",
5854 level);
5855
5856 /* it is the backlight class's job (caller) to handle
5857 * EINTR and other errors properly */
5858 return brightness_set(level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005859}
Holger Macht8acb0252006-10-20 14:30:28 -07005860
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005861static int brightness_get(struct backlight_device *bd)
5862{
5863 int status, res;
5864
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005865 res = mutex_lock_killable(&brightness_mutex);
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005866 if (res < 0)
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005867 return 0;
5868
5869 res = tpacpi_brightness_get_raw(&status);
5870
5871 mutex_unlock(&brightness_mutex);
5872
5873 if (res < 0)
5874 return 0;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005875
5876 return status & TP_EC_BACKLIGHT_LVLMSK;
5877}
5878
Richard Purdie599a52d2007-02-10 23:07:48 +00005879static struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005880 .get_brightness = brightness_get,
5881 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005882};
5883
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005884/* --------------------------------------------------------------------- */
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03005885
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03005886/*
5887 * These are only useful for models that have only one possibility
5888 * of GPU. If the BIOS model handles both ATI and Intel, don't use
5889 * these quirks.
5890 */
5891#define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
5892#define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
5893#define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
5894
5895static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
5896 /* Models with ATI GPUs known to require ECNVRAM mode */
5897 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
5898
Henrique de Moraes Holschuh6da25bf2009-09-12 15:22:11 -03005899 /* Models with ATI GPUs that can use ECNVRAM */
5900 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC),
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03005901 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
5902 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
5903 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
5904
Henrique de Moraes Holschuh6da25bf2009-09-12 15:22:11 -03005905 /* Models with Intel Extreme Graphics 2 */
5906 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC),
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03005907 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_NOEC),
5908 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_NOEC),
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03005909
5910 /* Models with Intel GMA900 */
5911 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
5912 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
5913 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
5914};
5915
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005916static int __init brightness_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005917{
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02005918 int b;
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03005919 unsigned long quirks;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02005920
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005921 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
5922
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03005923 mutex_init(&brightness_mutex);
5924
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03005925 quirks = tpacpi_check_quirks(brightness_quirk_table,
5926 ARRAY_SIZE(brightness_quirk_table));
5927
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03005928 /*
5929 * We always attempt to detect acpi support, so as to switch
5930 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
5931 * going to publish a backlight interface
5932 */
5933 b = tpacpi_check_std_acpi_brightness_support();
5934 if (b > 0) {
Thomas Renninger2dba1b52008-08-01 17:38:03 +02005935
5936 if (acpi_video_backlight_support()) {
5937 if (brightness_enable > 1) {
5938 printk(TPACPI_NOTICE
5939 "Standard ACPI backlight interface "
5940 "available, not loading native one.\n");
5941 return 1;
5942 } else if (brightness_enable == 1) {
5943 printk(TPACPI_NOTICE
5944 "Backlight control force enabled, even if standard "
5945 "ACPI backlight interface is available\n");
5946 }
5947 } else {
5948 if (brightness_enable > 1) {
5949 printk(TPACPI_NOTICE
5950 "Standard ACPI backlight interface not "
5951 "available, thinkpad_acpi native "
5952 "brightness control enabled\n");
5953 }
Henrique de Moraes Holschuhe11e2112007-10-30 17:46:22 -02005954 }
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02005955 }
5956
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03005957 if (!brightness_enable) {
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005958 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03005959 "brightness support disabled by "
5960 "module parameter\n");
5961 return 1;
5962 }
5963
5964 if (b > 16) {
5965 printk(TPACPI_ERR
5966 "Unsupported brightness interface, "
5967 "please contact %s\n", TPACPI_MAIL);
5968 return 1;
5969 }
5970 if (b == 16)
5971 tp_features.bright_16levels = 1;
5972
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005973 /*
5974 * Check for module parameter bogosity, note that we
5975 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
5976 * able to detect "unspecified"
5977 */
5978 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03005979 return -EINVAL;
5980
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005981 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
5982 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
5983 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03005984 if (quirks & TPACPI_BRGHT_Q_EC)
5985 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
5986 else
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005987 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
5988
5989 dbg_printk(TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03005990 "driver auto-selected brightness_mode=%d\n",
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005991 brightness_mode);
5992 }
5993
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03005994 /* Safety */
5995 if (thinkpad_id.vendor != PCI_VENDOR_ID_IBM &&
5996 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
5997 brightness_mode == TPACPI_BRGHT_MODE_EC))
5998 return -EINVAL;
5999
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006000 if (tpacpi_brightness_get_raw(&b) < 0)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03006001 return 1;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006002
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02006003 if (tp_features.bright_16levels)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006004 printk(TPACPI_INFO
6005 "detected a 16-level brightness capable ThinkPad\n");
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02006006
Henrique de Moraes Holschuh7d5a0152007-04-24 11:48:20 -03006007 ibm_backlight_device = backlight_device_register(
6008 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
6009 &ibm_backlight_data);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006010 if (IS_ERR(ibm_backlight_device)) {
Henrique de Moraes Holschuh435c47e2009-09-20 14:09:22 -03006011 int rc = PTR_ERR(ibm_backlight_device);
6012 ibm_backlight_device = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006013 printk(TPACPI_ERR "Could not register backlight device\n");
Henrique de Moraes Holschuh435c47e2009-09-20 14:09:22 -03006014 return rc;
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006015 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006016 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6017 "brightness is supported\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006018
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006019 if (quirks & TPACPI_BRGHT_Q_ASK) {
6020 printk(TPACPI_NOTICE
6021 "brightness: will use unverified default: "
6022 "brightness_mode=%d\n", brightness_mode);
6023 printk(TPACPI_NOTICE
6024 "brightness: please report to %s whether it works well "
6025 "or not on your ThinkPad\n", TPACPI_MAIL);
6026 }
6027
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02006028 ibm_backlight_device->props.max_brightness =
6029 (tp_features.bright_16levels)? 15 : 7;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006030 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006031 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00006032
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006033 return 0;
6034}
6035
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006036static void brightness_suspend(pm_message_t state)
6037{
6038 tpacpi_brightness_checkpoint_nvram();
6039}
6040
6041static void brightness_shutdown(void)
6042{
6043 tpacpi_brightness_checkpoint_nvram();
6044}
6045
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006046static void brightness_exit(void)
6047{
6048 if (ibm_backlight_device) {
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006049 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006050 "calling backlight_device_unregister()\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006051 backlight_device_unregister(ibm_backlight_device);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006052 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006053
6054 tpacpi_brightness_checkpoint_nvram();
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006055}
6056
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006057static int brightness_read(char *p)
6058{
6059 int len = 0;
6060 int level;
6061
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006062 level = brightness_get(NULL);
6063 if (level < 0) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006064 len += sprintf(p + len, "level:\t\tunreadable\n");
6065 } else {
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02006066 len += sprintf(p + len, "level:\t\t%d\n", level);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006067 len += sprintf(p + len, "commands:\tup, down\n");
6068 len += sprintf(p + len, "commands:\tlevel <level>"
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02006069 " (<level> is 0-%d)\n",
6070 (tp_features.bright_16levels) ? 15 : 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006071 }
6072
6073 return len;
6074}
6075
6076static int brightness_write(char *buf)
6077{
6078 int level;
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006079 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006080 char *cmd;
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02006081 int max_level = (tp_features.bright_16levels) ? 15 : 7;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006082
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006083 level = brightness_get(NULL);
6084 if (level < 0)
6085 return level;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006086
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006087 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006088 if (strlencmp(cmd, "up") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006089 if (level < max_level)
6090 level++;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006091 } else if (strlencmp(cmd, "down") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006092 if (level > 0)
6093 level--;
6094 } else if (sscanf(cmd, "level %d", &level) == 1 &&
6095 level >= 0 && level <= max_level) {
6096 /* new level set */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006097 } else
6098 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006099 }
6100
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006101 tpacpi_disclose_usertask("procfs brightness",
6102 "set level to %d\n", level);
6103
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006104 /*
6105 * Now we know what the final level should be, so we try to set it.
6106 * Doing it this way makes the syscall restartable in case of EINTR
6107 */
6108 rc = brightness_set(level);
6109 return (rc == -EINTR)? ERESTARTSYS : rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006110}
6111
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006112static struct ibm_struct brightness_driver_data = {
6113 .name = "brightness",
6114 .read = brightness_read,
6115 .write = brightness_write,
6116 .exit = brightness_exit,
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006117 .suspend = brightness_suspend,
6118 .shutdown = brightness_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006119};
6120
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006121/*************************************************************************
6122 * Volume subdriver
6123 */
6124
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006125static int volume_offset = 0x30;
6126
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006127static int volume_read(char *p)
6128{
6129 int len = 0;
6130 u8 level;
6131
6132 if (!acpi_ec_read(volume_offset, &level)) {
6133 len += sprintf(p + len, "level:\t\tunreadable\n");
6134 } else {
6135 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
6136 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
6137 len += sprintf(p + len, "commands:\tup, down, mute\n");
6138 len += sprintf(p + len, "commands:\tlevel <level>"
6139 " (<level> is 0-15)\n");
6140 }
6141
6142 return len;
6143}
6144
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006145static int volume_write(char *buf)
6146{
6147 int cmos_cmd, inc, i;
6148 u8 level, mute;
6149 int new_level, new_mute;
6150 char *cmd;
6151
6152 while ((cmd = next_cmd(&buf))) {
6153 if (!acpi_ec_read(volume_offset, &level))
6154 return -EIO;
6155 new_mute = mute = level & 0x40;
6156 new_level = level = level & 0xf;
6157
6158 if (strlencmp(cmd, "up") == 0) {
6159 if (mute)
6160 new_mute = 0;
6161 else
6162 new_level = level == 15 ? 15 : level + 1;
6163 } else if (strlencmp(cmd, "down") == 0) {
6164 if (mute)
6165 new_mute = 0;
6166 else
6167 new_level = level == 0 ? 0 : level - 1;
6168 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
6169 new_level >= 0 && new_level <= 15) {
6170 /* new_level set */
6171 } else if (strlencmp(cmd, "mute") == 0) {
6172 new_mute = 0x40;
6173 } else
6174 return -EINVAL;
6175
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006176 if (new_level != level) {
6177 /* mute doesn't change */
6178
6179 cmos_cmd = (new_level > level) ?
6180 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006181 inc = new_level > level ? 1 : -1;
6182
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03006183 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006184 !acpi_ec_write(volume_offset, level)))
6185 return -EIO;
6186
6187 for (i = level; i != new_level; i += inc)
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03006188 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006189 !acpi_ec_write(volume_offset, i + inc))
6190 return -EIO;
6191
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006192 if (mute &&
6193 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
6194 !acpi_ec_write(volume_offset, new_level + mute))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006195 return -EIO;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006196 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006197 }
6198
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006199 if (new_mute != mute) {
6200 /* level doesn't change */
6201
6202 cmos_cmd = (new_mute) ?
6203 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006204
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03006205 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006206 !acpi_ec_write(volume_offset, level + new_mute))
6207 return -EIO;
6208 }
6209 }
6210
6211 return 0;
6212}
6213
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006214static struct ibm_struct volume_driver_data = {
6215 .name = "volume",
6216 .read = volume_read,
6217 .write = volume_write,
6218};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006219
6220/*************************************************************************
6221 * Fan subdriver
6222 */
6223
6224/*
6225 * FAN ACCESS MODES
6226 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006227 * TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006228 * ACPI GFAN method: returns fan level
6229 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006230 * see TPACPI_FAN_WR_ACPI_SFAN
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03006231 * EC 0x2f (HFSP) not available if GFAN exists
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006232 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006233 * TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006234 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
6235 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03006236 * EC 0x2f (HFSP) might be available *for reading*, but do not use
6237 * it for writing.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006238 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006239 * TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03006240 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
6241 * Supported on almost all ThinkPads
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006242 *
6243 * Fan speed changes of any sort (including those caused by the
6244 * disengaged mode) are usually done slowly by the firmware as the
6245 * maximum ammount of fan duty cycle change per second seems to be
6246 * limited.
6247 *
6248 * Reading is not available if GFAN exists.
6249 * Writing is not available if SFAN exists.
6250 *
6251 * Bits
6252 * 7 automatic mode engaged;
6253 * (default operation mode of the ThinkPad)
6254 * fan level is ignored in this mode.
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03006255 * 6 full speed mode (takes precedence over bit 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006256 * not available on all thinkpads. May disable
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03006257 * the tachometer while the fan controller ramps up
6258 * the speed (which can take up to a few *minutes*).
6259 * Speeds up fan to 100% duty-cycle, which is far above
6260 * the standard RPM levels. It is not impossible that
6261 * it could cause hardware damage.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006262 * 5-3 unused in some models. Extra bits for fan level
6263 * in others, but still useless as all values above
6264 * 7 map to the same speed as level 7 in these models.
6265 * 2-0 fan level (0..7 usually)
6266 * 0x00 = stop
6267 * 0x07 = max (set when temperatures critical)
6268 * Some ThinkPads may have other levels, see
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006269 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006270 *
6271 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
6272 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
6273 * does so, its initial value is meaningless (0x07).
6274 *
6275 * For firmware bugs, refer to:
6276 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
6277 *
6278 * ----
6279 *
6280 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
6281 * Main fan tachometer reading (in RPM)
6282 *
6283 * This register is present on all ThinkPads with a new-style EC, and
6284 * it is known not to be present on the A21m/e, and T22, as there is
6285 * something else in offset 0x84 according to the ACPI DSDT. Other
6286 * ThinkPads from this same time period (and earlier) probably lack the
6287 * tachometer as well.
6288 *
Nick Andrew877d0312009-01-26 11:06:57 +01006289 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006290 * was never fixed by IBM to report the EC firmware version string
6291 * probably support the tachometer (like the early X models), so
6292 * detecting it is quite hard. We need more data to know for sure.
6293 *
6294 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
6295 * might result.
6296 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03006297 * FIRMWARE BUG: may go stale while the EC is switching to full speed
6298 * mode.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006299 *
6300 * For firmware bugs, refer to:
6301 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
6302 *
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03006303 * ----
6304 *
6305 * ThinkPad EC register 0x31 bit 0 (only on select models)
6306 *
6307 * When bit 0 of EC register 0x31 is zero, the tachometer registers
6308 * show the speed of the main fan. When bit 0 of EC register 0x31
6309 * is one, the tachometer registers show the speed of the auxiliary
6310 * fan.
6311 *
6312 * Fan control seems to affect both fans, regardless of the state
6313 * of this bit.
6314 *
6315 * So far, only the firmware for the X60/X61 non-tablet versions
6316 * seem to support this (firmware TP-7M).
6317 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006318 * TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006319 * ThinkPad X31, X40, X41. Not available in the X60.
6320 *
6321 * FANS ACPI handle: takes three arguments: low speed, medium speed,
6322 * high speed. ACPI DSDT seems to map these three speeds to levels
6323 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
6324 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
6325 *
6326 * The speeds are stored on handles
6327 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
6328 *
6329 * There are three default speed sets, acessible as handles:
6330 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
6331 *
6332 * ACPI DSDT switches which set is in use depending on various
6333 * factors.
6334 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006335 * TPACPI_FAN_WR_TPEC is also available and should be used to
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006336 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
6337 * but the ACPI tables just mention level 7.
6338 */
6339
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006340enum { /* Fan control constants */
6341 fan_status_offset = 0x2f, /* EC register 0x2f */
6342 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
6343 * 0x84 must be read before 0x85 */
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03006344 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
6345 bit 0 selects which fan is active */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006346
6347 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
6348 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
6349
6350 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
6351};
6352
6353enum fan_status_access_mode {
6354 TPACPI_FAN_NONE = 0, /* No fan status or control */
6355 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
6356 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
6357};
6358
6359enum fan_control_access_mode {
6360 TPACPI_FAN_WR_NONE = 0, /* No fan control */
6361 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
6362 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
6363 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
6364};
6365
6366enum fan_control_commands {
6367 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
6368 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
6369 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
6370 * and also watchdog cmd */
6371};
6372
6373static int fan_control_allowed;
6374
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006375static enum fan_status_access_mode fan_status_access_mode;
6376static enum fan_control_access_mode fan_control_access_mode;
6377static enum fan_control_commands fan_control_commands;
6378
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02006379static u8 fan_control_initial_status;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006380static u8 fan_control_desired_level;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006381static u8 fan_control_resume_level;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02006382static int fan_watchdog_maxinterval;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006383
6384static struct mutex fan_mutex;
6385
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006386static void fan_watchdog_fire(struct work_struct *ignored);
Len Brown25c68a32006-12-08 04:43:41 -05006387static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02006388
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006389TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
6390TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006391 "\\FSPD", /* 600e/x, 770e, 770x */
6392 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006393TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006394 "JFNS", /* 770x-JL */
6395 ); /* all others */
6396
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006397/*
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006398 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
6399 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
6400 * be in auto mode (0x80).
6401 *
6402 * This is corrected by any write to HFSP either by the driver, or
6403 * by the firmware.
6404 *
6405 * We assume 0x07 really means auto mode while this quirk is active,
6406 * as this is far more likely than the ThinkPad being in level 7,
6407 * which is only used by the firmware during thermal emergencies.
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03006408 *
6409 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
6410 * TP-70 (T43, R52), which are known to be buggy.
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006411 */
6412
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03006413static void fan_quirk1_setup(void)
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006414{
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006415 if (fan_control_initial_status == 0x07) {
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03006416 printk(TPACPI_NOTICE
6417 "fan_init: initial fan status is unknown, "
6418 "assuming it is in auto mode\n");
6419 tp_features.fan_ctrl_status_undef = 1;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006420 }
6421}
6422
6423static void fan_quirk1_handle(u8 *fan_status)
6424{
6425 if (unlikely(tp_features.fan_ctrl_status_undef)) {
6426 if (*fan_status != fan_control_initial_status) {
6427 /* something changed the HFSP regisnter since
6428 * driver init time, so it is not undefined
6429 * anymore */
6430 tp_features.fan_ctrl_status_undef = 0;
6431 } else {
6432 /* Return most likely status. In fact, it
6433 * might be the only possible status */
6434 *fan_status = TP_EC_FAN_AUTO;
6435 }
6436 }
6437}
6438
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03006439/* Select main fan on X60/X61, NOOP on others */
6440static bool fan_select_fan1(void)
6441{
6442 if (tp_features.second_fan) {
6443 u8 val;
6444
6445 if (ec_read(fan_select_offset, &val) < 0)
6446 return false;
6447 val &= 0xFEU;
6448 if (ec_write(fan_select_offset, val) < 0)
6449 return false;
6450 }
6451 return true;
6452}
6453
6454/* Select secondary fan on X60/X61 */
6455static bool fan_select_fan2(void)
6456{
6457 u8 val;
6458
6459 if (!tp_features.second_fan)
6460 return false;
6461
6462 if (ec_read(fan_select_offset, &val) < 0)
6463 return false;
6464 val |= 0x01U;
6465 if (ec_write(fan_select_offset, val) < 0)
6466 return false;
6467
6468 return true;
6469}
6470
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006471/*
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006472 * Call with fan_mutex held
6473 */
6474static void fan_update_desired_level(u8 status)
6475{
6476 if ((status &
6477 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
6478 if (status > 7)
6479 fan_control_desired_level = 7;
6480 else
6481 fan_control_desired_level = status;
6482 }
6483}
6484
6485static int fan_get_status(u8 *status)
6486{
6487 u8 s;
6488
6489 /* TODO:
6490 * Add TPACPI_FAN_RD_ACPI_FANS ? */
6491
6492 switch (fan_status_access_mode) {
6493 case TPACPI_FAN_RD_ACPI_GFAN:
6494 /* 570, 600e/x, 770e, 770x */
6495
6496 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
6497 return -EIO;
6498
6499 if (likely(status))
6500 *status = s & 0x07;
6501
6502 break;
6503
6504 case TPACPI_FAN_RD_TPEC:
6505 /* all except 570, 600e/x, 770e, 770x */
6506 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
6507 return -EIO;
6508
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006509 if (likely(status)) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006510 *status = s;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006511 fan_quirk1_handle(status);
6512 }
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006513
6514 break;
6515
6516 default:
6517 return -ENXIO;
6518 }
6519
6520 return 0;
6521}
6522
6523static int fan_get_status_safe(u8 *status)
6524{
6525 int rc;
6526 u8 s;
6527
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006528 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006529 return -ERESTARTSYS;
6530 rc = fan_get_status(&s);
6531 if (!rc)
6532 fan_update_desired_level(s);
6533 mutex_unlock(&fan_mutex);
6534
6535 if (status)
6536 *status = s;
6537
6538 return rc;
6539}
6540
6541static int fan_get_speed(unsigned int *speed)
6542{
6543 u8 hi, lo;
6544
6545 switch (fan_status_access_mode) {
6546 case TPACPI_FAN_RD_TPEC:
6547 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03006548 if (unlikely(!fan_select_fan1()))
6549 return -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006550 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
6551 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
6552 return -EIO;
6553
6554 if (likely(speed))
6555 *speed = (hi << 8) | lo;
6556
6557 break;
6558
6559 default:
6560 return -ENXIO;
6561 }
6562
6563 return 0;
6564}
6565
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03006566static int fan2_get_speed(unsigned int *speed)
6567{
6568 u8 hi, lo;
6569 bool rc;
6570
6571 switch (fan_status_access_mode) {
6572 case TPACPI_FAN_RD_TPEC:
6573 /* all except 570, 600e/x, 770e, 770x */
6574 if (unlikely(!fan_select_fan2()))
6575 return -EIO;
6576 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
6577 !acpi_ec_read(fan_rpm_offset + 1, &hi);
6578 fan_select_fan1(); /* play it safe */
6579 if (rc)
6580 return -EIO;
6581
6582 if (likely(speed))
6583 *speed = (hi << 8) | lo;
6584
6585 break;
6586
6587 default:
6588 return -ENXIO;
6589 }
6590
6591 return 0;
6592}
6593
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006594static int fan_set_level(int level)
6595{
6596 if (!fan_control_allowed)
6597 return -EPERM;
6598
6599 switch (fan_control_access_mode) {
6600 case TPACPI_FAN_WR_ACPI_SFAN:
6601 if (level >= 0 && level <= 7) {
6602 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
6603 return -EIO;
6604 } else
6605 return -EINVAL;
6606 break;
6607
6608 case TPACPI_FAN_WR_ACPI_FANS:
6609 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006610 if (!(level & TP_EC_FAN_AUTO) &&
6611 !(level & TP_EC_FAN_FULLSPEED) &&
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006612 ((level < 0) || (level > 7)))
6613 return -EINVAL;
6614
6615 /* safety net should the EC not support AUTO
6616 * or FULLSPEED mode bits and just ignore them */
6617 if (level & TP_EC_FAN_FULLSPEED)
6618 level |= 7; /* safety min speed 7 */
Roel Kluin547266e2008-02-05 00:24:56 +01006619 else if (level & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006620 level |= 4; /* safety min speed 4 */
6621
6622 if (!acpi_ec_write(fan_status_offset, level))
6623 return -EIO;
6624 else
6625 tp_features.fan_ctrl_status_undef = 0;
6626 break;
6627
6628 default:
6629 return -ENXIO;
6630 }
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00006631
6632 vdbg_printk(TPACPI_DBG_FAN,
6633 "fan control: set fan control register to 0x%02x\n", level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006634 return 0;
6635}
6636
6637static int fan_set_level_safe(int level)
6638{
6639 int rc;
6640
6641 if (!fan_control_allowed)
6642 return -EPERM;
6643
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006644 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006645 return -ERESTARTSYS;
6646
6647 if (level == TPACPI_FAN_LAST_LEVEL)
6648 level = fan_control_desired_level;
6649
6650 rc = fan_set_level(level);
6651 if (!rc)
6652 fan_update_desired_level(level);
6653
6654 mutex_unlock(&fan_mutex);
6655 return rc;
6656}
6657
6658static int fan_set_enable(void)
6659{
6660 u8 s;
6661 int rc;
6662
6663 if (!fan_control_allowed)
6664 return -EPERM;
6665
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006666 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006667 return -ERESTARTSYS;
6668
6669 switch (fan_control_access_mode) {
6670 case TPACPI_FAN_WR_ACPI_FANS:
6671 case TPACPI_FAN_WR_TPEC:
6672 rc = fan_get_status(&s);
6673 if (rc < 0)
6674 break;
6675
6676 /* Don't go out of emergency fan mode */
6677 if (s != 7) {
6678 s &= 0x07;
6679 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
6680 }
6681
6682 if (!acpi_ec_write(fan_status_offset, s))
6683 rc = -EIO;
6684 else {
6685 tp_features.fan_ctrl_status_undef = 0;
6686 rc = 0;
6687 }
6688 break;
6689
6690 case TPACPI_FAN_WR_ACPI_SFAN:
6691 rc = fan_get_status(&s);
6692 if (rc < 0)
6693 break;
6694
6695 s &= 0x07;
6696
6697 /* Set fan to at least level 4 */
6698 s |= 4;
6699
6700 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006701 rc = -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006702 else
6703 rc = 0;
6704 break;
6705
6706 default:
6707 rc = -ENXIO;
6708 }
6709
6710 mutex_unlock(&fan_mutex);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00006711
6712 if (!rc)
6713 vdbg_printk(TPACPI_DBG_FAN,
6714 "fan control: set fan control register to 0x%02x\n",
6715 s);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006716 return rc;
6717}
6718
6719static int fan_set_disable(void)
6720{
6721 int rc;
6722
6723 if (!fan_control_allowed)
6724 return -EPERM;
6725
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006726 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006727 return -ERESTARTSYS;
6728
6729 rc = 0;
6730 switch (fan_control_access_mode) {
6731 case TPACPI_FAN_WR_ACPI_FANS:
6732 case TPACPI_FAN_WR_TPEC:
6733 if (!acpi_ec_write(fan_status_offset, 0x00))
6734 rc = -EIO;
6735 else {
6736 fan_control_desired_level = 0;
6737 tp_features.fan_ctrl_status_undef = 0;
6738 }
6739 break;
6740
6741 case TPACPI_FAN_WR_ACPI_SFAN:
6742 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
6743 rc = -EIO;
6744 else
6745 fan_control_desired_level = 0;
6746 break;
6747
6748 default:
6749 rc = -ENXIO;
6750 }
6751
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00006752 if (!rc)
6753 vdbg_printk(TPACPI_DBG_FAN,
6754 "fan control: set fan control register to 0\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006755
6756 mutex_unlock(&fan_mutex);
6757 return rc;
6758}
6759
6760static int fan_set_speed(int speed)
6761{
6762 int rc;
6763
6764 if (!fan_control_allowed)
6765 return -EPERM;
6766
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006767 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006768 return -ERESTARTSYS;
6769
6770 rc = 0;
6771 switch (fan_control_access_mode) {
6772 case TPACPI_FAN_WR_ACPI_FANS:
6773 if (speed >= 0 && speed <= 65535) {
6774 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
6775 speed, speed, speed))
6776 rc = -EIO;
6777 } else
6778 rc = -EINVAL;
6779 break;
6780
6781 default:
6782 rc = -ENXIO;
6783 }
6784
6785 mutex_unlock(&fan_mutex);
6786 return rc;
6787}
6788
6789static void fan_watchdog_reset(void)
6790{
6791 static int fan_watchdog_active;
6792
6793 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
6794 return;
6795
6796 if (fan_watchdog_active)
6797 cancel_delayed_work(&fan_watchdog_task);
6798
6799 if (fan_watchdog_maxinterval > 0 &&
6800 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
6801 fan_watchdog_active = 1;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03006802 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006803 msecs_to_jiffies(fan_watchdog_maxinterval
6804 * 1000))) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006805 printk(TPACPI_ERR
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03006806 "failed to queue the fan watchdog, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006807 "watchdog will not trigger\n");
6808 }
6809 } else
6810 fan_watchdog_active = 0;
6811}
6812
6813static void fan_watchdog_fire(struct work_struct *ignored)
6814{
6815 int rc;
6816
6817 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
6818 return;
6819
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006820 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006821 rc = fan_set_enable();
6822 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006823 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006824 "will try again later...\n", -rc);
6825 /* reschedule for later */
6826 fan_watchdog_reset();
6827 }
6828}
6829
6830/*
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006831 * SYSFS fan layout: hwmon compatible (device)
6832 *
6833 * pwm*_enable:
6834 * 0: "disengaged" mode
6835 * 1: manual mode
6836 * 2: native EC "auto" mode (recommended, hardware default)
6837 *
6838 * pwm*: set speed in manual mode, ignored otherwise.
6839 * 0 is level 0; 255 is level 7. Intermediate points done with linear
6840 * interpolation.
6841 *
6842 * fan*_input: tachometer reading, RPM
6843 *
6844 *
6845 * SYSFS fan layout: extensions
6846 *
6847 * fan_watchdog (driver):
6848 * fan watchdog interval in seconds, 0 disables (default), max 120
6849 */
6850
6851/* sysfs fan pwm1_enable ----------------------------------------------- */
6852static ssize_t fan_pwm1_enable_show(struct device *dev,
6853 struct device_attribute *attr,
6854 char *buf)
6855{
6856 int res, mode;
6857 u8 status;
6858
6859 res = fan_get_status_safe(&status);
6860 if (res)
6861 return res;
6862
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006863 if (status & TP_EC_FAN_FULLSPEED) {
6864 mode = 0;
6865 } else if (status & TP_EC_FAN_AUTO) {
6866 mode = 2;
6867 } else
6868 mode = 1;
6869
6870 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
6871}
6872
6873static ssize_t fan_pwm1_enable_store(struct device *dev,
6874 struct device_attribute *attr,
6875 const char *buf, size_t count)
6876{
6877 unsigned long t;
6878 int res, level;
6879
6880 if (parse_strtoul(buf, 2, &t))
6881 return -EINVAL;
6882
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00006883 tpacpi_disclose_usertask("hwmon pwm1_enable",
6884 "set fan mode to %lu\n", t);
6885
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006886 switch (t) {
6887 case 0:
6888 level = TP_EC_FAN_FULLSPEED;
6889 break;
6890 case 1:
6891 level = TPACPI_FAN_LAST_LEVEL;
6892 break;
6893 case 2:
6894 level = TP_EC_FAN_AUTO;
6895 break;
6896 case 3:
6897 /* reserved for software-controlled auto mode */
6898 return -ENOSYS;
6899 default:
6900 return -EINVAL;
6901 }
6902
6903 res = fan_set_level_safe(level);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03006904 if (res == -ENXIO)
6905 return -EINVAL;
6906 else if (res < 0)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006907 return res;
6908
6909 fan_watchdog_reset();
6910
6911 return count;
6912}
6913
6914static struct device_attribute dev_attr_fan_pwm1_enable =
6915 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
6916 fan_pwm1_enable_show, fan_pwm1_enable_store);
6917
6918/* sysfs fan pwm1 ------------------------------------------------------ */
6919static ssize_t fan_pwm1_show(struct device *dev,
6920 struct device_attribute *attr,
6921 char *buf)
6922{
6923 int res;
6924 u8 status;
6925
6926 res = fan_get_status_safe(&status);
6927 if (res)
6928 return res;
6929
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006930 if ((status &
6931 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
6932 status = fan_control_desired_level;
6933
6934 if (status > 7)
6935 status = 7;
6936
6937 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
6938}
6939
6940static ssize_t fan_pwm1_store(struct device *dev,
6941 struct device_attribute *attr,
6942 const char *buf, size_t count)
6943{
6944 unsigned long s;
6945 int rc;
6946 u8 status, newlevel;
6947
6948 if (parse_strtoul(buf, 255, &s))
6949 return -EINVAL;
6950
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00006951 tpacpi_disclose_usertask("hwmon pwm1",
6952 "set fan speed to %lu\n", s);
6953
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006954 /* scale down from 0-255 to 0-7 */
6955 newlevel = (s >> 5) & 0x07;
6956
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006957 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02006958 return -ERESTARTSYS;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006959
6960 rc = fan_get_status(&status);
6961 if (!rc && (status &
6962 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
6963 rc = fan_set_level(newlevel);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03006964 if (rc == -ENXIO)
6965 rc = -EINVAL;
6966 else if (!rc) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006967 fan_update_desired_level(newlevel);
Henrique de Moraes Holschuhca4ac2f2007-04-27 22:00:11 -03006968 fan_watchdog_reset();
6969 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006970 }
6971
6972 mutex_unlock(&fan_mutex);
6973 return (rc)? rc : count;
6974}
6975
6976static struct device_attribute dev_attr_fan_pwm1 =
6977 __ATTR(pwm1, S_IWUSR | S_IRUGO,
6978 fan_pwm1_show, fan_pwm1_store);
6979
6980/* sysfs fan fan1_input ------------------------------------------------ */
6981static ssize_t fan_fan1_input_show(struct device *dev,
6982 struct device_attribute *attr,
6983 char *buf)
6984{
6985 int res;
6986 unsigned int speed;
6987
6988 res = fan_get_speed(&speed);
6989 if (res < 0)
6990 return res;
6991
6992 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
6993}
6994
6995static struct device_attribute dev_attr_fan_fan1_input =
6996 __ATTR(fan1_input, S_IRUGO,
6997 fan_fan1_input_show, NULL);
6998
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03006999/* sysfs fan fan2_input ------------------------------------------------ */
7000static ssize_t fan_fan2_input_show(struct device *dev,
7001 struct device_attribute *attr,
7002 char *buf)
7003{
7004 int res;
7005 unsigned int speed;
7006
7007 res = fan2_get_speed(&speed);
7008 if (res < 0)
7009 return res;
7010
7011 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7012}
7013
7014static struct device_attribute dev_attr_fan_fan2_input =
7015 __ATTR(fan2_input, S_IRUGO,
7016 fan_fan2_input_show, NULL);
7017
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007018/* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007019static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
7020 char *buf)
7021{
7022 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
7023}
7024
7025static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
7026 const char *buf, size_t count)
7027{
7028 unsigned long t;
7029
7030 if (parse_strtoul(buf, 120, &t))
7031 return -EINVAL;
7032
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03007033 if (!fan_control_allowed)
7034 return -EPERM;
7035
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007036 fan_watchdog_maxinterval = t;
7037 fan_watchdog_reset();
7038
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007039 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
7040
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007041 return count;
7042}
7043
7044static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
7045 fan_fan_watchdog_show, fan_fan_watchdog_store);
7046
7047/* --------------------------------------------------------------------- */
7048static struct attribute *fan_attributes[] = {
7049 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
7050 &dev_attr_fan_fan1_input.attr,
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007051 NULL, /* for fan2_input */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007052 NULL
7053};
7054
7055static const struct attribute_group fan_attr_group = {
7056 .attrs = fan_attributes,
7057};
7058
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007059#define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
7060#define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007061
7062#define TPACPI_FAN_QI(__id1, __id2, __quirks) \
7063 { .vendor = PCI_VENDOR_ID_IBM, \
7064 .bios = TPACPI_MATCH_ANY, \
7065 .ec = TPID(__id1, __id2), \
7066 .quirks = __quirks }
7067
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007068#define TPACPI_FAN_QL(__id1, __id2, __quirks) \
7069 { .vendor = PCI_VENDOR_ID_LENOVO, \
7070 .bios = TPACPI_MATCH_ANY, \
7071 .ec = TPID(__id1, __id2), \
7072 .quirks = __quirks }
7073
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007074static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
7075 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
7076 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
7077 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
7078 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007079 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007080};
7081
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007082#undef TPACPI_FAN_QL
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007083#undef TPACPI_FAN_QI
7084
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007085static int __init fan_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007086{
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007087 int rc;
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007088 unsigned long quirks;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007089
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007090 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7091 "initializing fan subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007092
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03007093 mutex_init(&fan_mutex);
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007094 fan_status_access_mode = TPACPI_FAN_NONE;
7095 fan_control_access_mode = TPACPI_FAN_WR_NONE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007096 fan_control_commands = 0;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007097 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03007098 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007099 tp_features.second_fan = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007100 fan_control_desired_level = 7;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007101
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007102 TPACPI_ACPIHANDLE_INIT(fans);
7103 TPACPI_ACPIHANDLE_INIT(gfan);
7104 TPACPI_ACPIHANDLE_INIT(sfan);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007105
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007106 quirks = tpacpi_check_quirks(fan_quirk_table,
7107 ARRAY_SIZE(fan_quirk_table));
7108
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007109 if (gfan_handle) {
7110 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007111 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007112 } else {
7113 /* all other ThinkPads: note that even old-style
7114 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02007115 if (likely(acpi_ec_read(fan_status_offset,
7116 &fan_control_initial_status))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007117 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007118 if (quirks & TPACPI_FAN_Q1)
7119 fan_quirk1_setup();
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007120 if (quirks & TPACPI_FAN_2FAN) {
7121 tp_features.second_fan = 1;
7122 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7123 "secondary fan support enabled\n");
7124 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007125 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007126 printk(TPACPI_ERR
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007127 "ThinkPad ACPI EC access misbehaving, "
7128 "fan status and control unavailable\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007129 return 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007130 }
7131 }
7132
7133 if (sfan_handle) {
7134 /* 570, 770x-JL */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007135 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02007136 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007137 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007138 } else {
7139 if (!gfan_handle) {
7140 /* gfan without sfan means no fan control */
7141 /* all other models implement TP EC 0x2f control */
7142
7143 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02007144 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007145 fan_control_access_mode =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007146 TPACPI_FAN_WR_ACPI_FANS;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007147 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007148 TPACPI_FAN_CMD_SPEED |
7149 TPACPI_FAN_CMD_LEVEL |
7150 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007151 } else {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007152 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02007153 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007154 TPACPI_FAN_CMD_LEVEL |
7155 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007156 }
7157 }
7158 }
7159
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007160 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7161 "fan is %s, modes %d, %d\n",
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007162 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
7163 fan_control_access_mode != TPACPI_FAN_WR_NONE),
7164 fan_status_access_mode, fan_control_access_mode);
7165
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03007166 /* fan control master switch */
7167 if (!fan_control_allowed) {
7168 fan_control_access_mode = TPACPI_FAN_WR_NONE;
7169 fan_control_commands = 0;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007170 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03007171 "fan control features disabled by parameter\n");
7172 }
7173
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007174 /* update fan_control_desired_level */
7175 if (fan_status_access_mode != TPACPI_FAN_NONE)
7176 fan_get_status_safe(NULL);
7177
7178 if (fan_status_access_mode != TPACPI_FAN_NONE ||
7179 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007180 if (tp_features.second_fan) {
7181 /* attach second fan tachometer */
7182 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
7183 &dev_attr_fan_fan2_input.attr;
7184 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007185 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007186 &fan_attr_group);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007187 if (rc < 0)
7188 return rc;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03007189
7190 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
7191 &driver_attr_fan_watchdog);
7192 if (rc < 0) {
7193 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
7194 &fan_attr_group);
7195 return rc;
7196 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007197 return 0;
7198 } else
7199 return 1;
7200}
7201
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007202static void fan_exit(void)
7203{
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007204 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007205 "cancelling any pending fan watchdog tasks\n");
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007206
7207 /* FIXME: can we really do this unconditionally? */
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007208 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007209 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
7210 &driver_attr_fan_watchdog);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007211
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007212 cancel_delayed_work(&fan_watchdog_task);
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03007213 flush_workqueue(tpacpi_wq);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007214}
7215
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007216static void fan_suspend(pm_message_t state)
7217{
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007218 int rc;
7219
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007220 if (!fan_control_allowed)
7221 return;
7222
7223 /* Store fan status in cache */
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007224 fan_control_resume_level = 0;
7225 rc = fan_get_status_safe(&fan_control_resume_level);
7226 if (rc < 0)
7227 printk(TPACPI_NOTICE
7228 "failed to read fan level for later "
7229 "restore during resume: %d\n", rc);
7230
7231 /* if it is undefined, don't attempt to restore it.
7232 * KEEP THIS LAST */
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007233 if (tp_features.fan_ctrl_status_undef)
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007234 fan_control_resume_level = 0;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007235}
7236
7237static void fan_resume(void)
7238{
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007239 u8 current_level = 7;
7240 bool do_set = false;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007241 int rc;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007242
7243 /* DSDT *always* updates status on resume */
7244 tp_features.fan_ctrl_status_undef = 0;
7245
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007246 if (!fan_control_allowed ||
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007247 !fan_control_resume_level ||
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007248 (fan_get_status_safe(&current_level) < 0))
7249 return;
7250
7251 switch (fan_control_access_mode) {
7252 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007253 /* never decrease fan level */
7254 do_set = (fan_control_resume_level > current_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007255 break;
7256 case TPACPI_FAN_WR_ACPI_FANS:
7257 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007258 /* never decrease fan level, scale is:
7259 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
7260 *
7261 * We expect the firmware to set either 7 or AUTO, but we
7262 * handle FULLSPEED out of paranoia.
7263 *
7264 * So, we can safely only restore FULLSPEED or 7, anything
7265 * else could slow the fan. Restoring AUTO is useless, at
7266 * best that's exactly what the DSDT already set (it is the
7267 * slower it uses).
7268 *
7269 * Always keep in mind that the DSDT *will* have set the
7270 * fans to what the vendor supposes is the best level. We
7271 * muck with it only to speed the fan up.
7272 */
7273 if (fan_control_resume_level != 7 &&
7274 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
7275 return;
7276 else
7277 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
7278 (current_level != fan_control_resume_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007279 break;
7280 default:
7281 return;
7282 }
7283 if (do_set) {
7284 printk(TPACPI_NOTICE
7285 "restoring fan level to 0x%02x\n",
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007286 fan_control_resume_level);
7287 rc = fan_set_level_safe(fan_control_resume_level);
7288 if (rc < 0)
7289 printk(TPACPI_NOTICE
7290 "failed to restore fan level: %d\n", rc);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007291 }
7292}
7293
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007294static int fan_read(char *p)
7295{
7296 int len = 0;
7297 int rc;
7298 u8 status;
7299 unsigned int speed = 0;
7300
7301 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007302 case TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007303 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007304 rc = fan_get_status_safe(&status);
7305 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007306 return rc;
7307
7308 len += sprintf(p + len, "status:\t\t%s\n"
7309 "level:\t\t%d\n",
7310 (status != 0) ? "enabled" : "disabled", status);
7311 break;
7312
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007313 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007314 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007315 rc = fan_get_status_safe(&status);
7316 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007317 return rc;
7318
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007319 len += sprintf(p + len, "status:\t\t%s\n",
7320 (status != 0) ? "enabled" : "disabled");
7321
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007322 rc = fan_get_speed(&speed);
7323 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007324 return rc;
7325
7326 len += sprintf(p + len, "speed:\t\t%d\n", speed);
7327
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007328 if (status & TP_EC_FAN_FULLSPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007329 /* Disengaged mode takes precedence */
7330 len += sprintf(p + len, "level:\t\tdisengaged\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007331 else if (status & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007332 len += sprintf(p + len, "level:\t\tauto\n");
7333 else
7334 len += sprintf(p + len, "level:\t\t%d\n", status);
7335 break;
7336
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007337 case TPACPI_FAN_NONE:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007338 default:
7339 len += sprintf(p + len, "status:\t\tnot supported\n");
7340 }
7341
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007342 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007343 len += sprintf(p + len, "commands:\tlevel <level>");
7344
7345 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007346 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007347 len += sprintf(p + len, " (<level> is 0-7)\n");
7348 break;
7349
7350 default:
7351 len += sprintf(p + len, " (<level> is 0-7, "
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007352 "auto, disengaged, full-speed)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007353 break;
7354 }
7355 }
7356
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007357 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007358 len += sprintf(p + len, "commands:\tenable, disable\n"
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007359 "commands:\twatchdog <timeout> (<timeout> "
7360 "is 0 (off), 1-120 (seconds))\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007361
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007362 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007363 len += sprintf(p + len, "commands:\tspeed <speed>"
7364 " (<speed> is 0-65535)\n");
7365
7366 return len;
7367}
7368
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007369static int fan_write_cmd_level(const char *cmd, int *rc)
7370{
7371 int level;
7372
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02007373 if (strlencmp(cmd, "level auto") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007374 level = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007375 else if ((strlencmp(cmd, "level disengaged") == 0) |
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007376 (strlencmp(cmd, "level full-speed") == 0))
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007377 level = TP_EC_FAN_FULLSPEED;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02007378 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007379 return 0;
7380
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007381 *rc = fan_set_level_safe(level);
7382 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007383 printk(TPACPI_ERR "level command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007384 "access mode %d", fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007385 else if (!*rc)
7386 tpacpi_disclose_usertask("procfs fan",
7387 "set level to %d\n", level);
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007388
7389 return 1;
7390}
7391
7392static int fan_write_cmd_enable(const char *cmd, int *rc)
7393{
7394 if (strlencmp(cmd, "enable") != 0)
7395 return 0;
7396
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007397 *rc = fan_set_enable();
7398 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007399 printk(TPACPI_ERR "enable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007400 "access mode %d", fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007401 else if (!*rc)
7402 tpacpi_disclose_usertask("procfs fan", "enable\n");
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007403
7404 return 1;
7405}
7406
7407static int fan_write_cmd_disable(const char *cmd, int *rc)
7408{
7409 if (strlencmp(cmd, "disable") != 0)
7410 return 0;
7411
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007412 *rc = fan_set_disable();
7413 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007414 printk(TPACPI_ERR "disable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007415 "access mode %d", fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007416 else if (!*rc)
7417 tpacpi_disclose_usertask("procfs fan", "disable\n");
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007418
7419 return 1;
7420}
7421
7422static int fan_write_cmd_speed(const char *cmd, int *rc)
7423{
7424 int speed;
7425
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02007426 /* TODO:
7427 * Support speed <low> <medium> <high> ? */
7428
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007429 if (sscanf(cmd, "speed %d", &speed) != 1)
7430 return 0;
7431
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007432 *rc = fan_set_speed(speed);
7433 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007434 printk(TPACPI_ERR "speed command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007435 "access mode %d", fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007436 else if (!*rc)
7437 tpacpi_disclose_usertask("procfs fan",
7438 "set speed to %d\n", speed);
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007439
7440 return 1;
7441}
7442
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007443static int fan_write_cmd_watchdog(const char *cmd, int *rc)
7444{
7445 int interval;
7446
7447 if (sscanf(cmd, "watchdog %d", &interval) != 1)
7448 return 0;
7449
7450 if (interval < 0 || interval > 120)
7451 *rc = -EINVAL;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007452 else {
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007453 fan_watchdog_maxinterval = interval;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007454 tpacpi_disclose_usertask("procfs fan",
7455 "set watchdog timer to %d\n",
7456 interval);
7457 }
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007458
7459 return 1;
7460}
7461
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007462static int fan_write(char *buf)
7463{
7464 char *cmd;
7465 int rc = 0;
7466
7467 while (!rc && (cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007468 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007469 fan_write_cmd_level(cmd, &rc)) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007470 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007471 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007472 fan_write_cmd_disable(cmd, &rc) ||
7473 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007474 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007475 fan_write_cmd_speed(cmd, &rc))
7476 )
7477 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007478 else if (!rc)
7479 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007480 }
7481
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007482 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007483}
7484
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007485static struct ibm_struct fan_driver_data = {
7486 .name = "fan",
7487 .read = fan_read,
7488 .write = fan_write,
7489 .exit = fan_exit,
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007490 .suspend = fan_suspend,
7491 .resume = fan_resume,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007492};
7493
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007494/****************************************************************************
7495 ****************************************************************************
7496 *
7497 * Infrastructure
7498 *
7499 ****************************************************************************
7500 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07007501
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007502/* sysfs name ---------------------------------------------------------- */
7503static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
7504 struct device_attribute *attr,
7505 char *buf)
7506{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007507 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007508}
7509
7510static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
7511 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
7512
7513/* --------------------------------------------------------------------- */
7514
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007515/* /proc support */
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -03007516static struct proc_dir_entry *proc_dir;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007517
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007518/*
7519 * Module and infrastructure proble, init and exit handling
7520 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007521
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007522static int force_load;
7523
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007524#ifdef CONFIG_THINKPAD_ACPI_DEBUG
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007525static const char * __init str_supported(int is_supported)
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007526{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007527 static char text_unsupported[] __initdata = "not supported";
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007528
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007529 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007530}
7531#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
7532
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007533static void ibm_exit(struct ibm_struct *ibm)
7534{
7535 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
7536
7537 list_del_init(&ibm->all_drivers);
7538
7539 if (ibm->flags.acpi_notify_installed) {
7540 dbg_printk(TPACPI_DBG_EXIT,
7541 "%s: acpi_remove_notify_handler\n", ibm->name);
7542 BUG_ON(!ibm->acpi);
7543 acpi_remove_notify_handler(*ibm->acpi->handle,
7544 ibm->acpi->type,
7545 dispatch_acpi_notify);
7546 ibm->flags.acpi_notify_installed = 0;
7547 ibm->flags.acpi_notify_installed = 0;
7548 }
7549
7550 if (ibm->flags.proc_created) {
7551 dbg_printk(TPACPI_DBG_EXIT,
7552 "%s: remove_proc_entry\n", ibm->name);
7553 remove_proc_entry(ibm->name, proc_dir);
7554 ibm->flags.proc_created = 0;
7555 }
7556
7557 if (ibm->flags.acpi_driver_registered) {
7558 dbg_printk(TPACPI_DBG_EXIT,
7559 "%s: acpi_bus_unregister_driver\n", ibm->name);
7560 BUG_ON(!ibm->acpi);
7561 acpi_bus_unregister_driver(ibm->acpi->driver);
7562 kfree(ibm->acpi->driver);
7563 ibm->acpi->driver = NULL;
7564 ibm->flags.acpi_driver_registered = 0;
7565 }
7566
7567 if (ibm->flags.init_called && ibm->exit) {
7568 ibm->exit();
7569 ibm->flags.init_called = 0;
7570 }
7571
7572 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
7573}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007574
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007575static int __init ibm_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007576{
7577 int ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007578 struct ibm_struct *ibm = iibm->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007579 struct proc_dir_entry *entry;
7580
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007581 BUG_ON(ibm == NULL);
7582
7583 INIT_LIST_HEAD(&ibm->all_drivers);
7584
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03007585 if (ibm->flags.experimental && !experimental)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007586 return 0;
7587
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007588 dbg_printk(TPACPI_DBG_INIT,
7589 "probing for %s\n", ibm->name);
7590
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007591 if (iibm->init) {
7592 ret = iibm->init(iibm);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007593 if (ret > 0)
7594 return 0; /* probe failed */
7595 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007596 return ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007597
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03007598 ibm->flags.init_called = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007599 }
7600
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03007601 if (ibm->acpi) {
7602 if (ibm->acpi->hid) {
7603 ret = register_tpacpi_subdriver(ibm);
7604 if (ret)
7605 goto err_out;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007606 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03007607
7608 if (ibm->acpi->notify) {
7609 ret = setup_acpi_notify(ibm);
7610 if (ret == -ENODEV) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007611 printk(TPACPI_NOTICE "disabling subdriver %s\n",
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03007612 ibm->name);
7613 ret = 0;
7614 goto err_out;
7615 }
7616 if (ret < 0)
7617 goto err_out;
7618 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007619 }
7620
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007621 dbg_printk(TPACPI_DBG_INIT,
7622 "%s installed\n", ibm->name);
7623
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007624 if (ibm->read) {
7625 entry = create_proc_entry(ibm->name,
7626 S_IFREG | S_IRUGO | S_IWUSR,
7627 proc_dir);
7628 if (!entry) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007629 printk(TPACPI_ERR "unable to create proc entry %s\n",
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007630 ibm->name);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007631 ret = -ENODEV;
7632 goto err_out;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007633 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007634 entry->data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03007635 entry->read_proc = &dispatch_procfs_read;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007636 if (ibm->write)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03007637 entry->write_proc = &dispatch_procfs_write;
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03007638 ibm->flags.proc_created = 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007639 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007640
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007641 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
7642
Linus Torvalds1da177e2005-04-16 15:20:36 -07007643 return 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007644
7645err_out:
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007646 dbg_printk(TPACPI_DBG_INIT,
7647 "%s: at error exit path with result %d\n",
7648 ibm->name, ret);
7649
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007650 ibm_exit(ibm);
7651 return (ret < 0)? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007652}
7653
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007654/* Probing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007655
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03007656static bool __pure __init tpacpi_is_fw_digit(const char c)
7657{
7658 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
7659}
7660
7661/* Most models: xxyTkkWW (#.##c); Ancient 570/600 and -SL lacks (#.##c) */
7662static bool __pure __init tpacpi_is_valid_fw_id(const char* const s,
7663 const char t)
7664{
7665 return s && strlen(s) >= 8 &&
7666 tpacpi_is_fw_digit(s[0]) &&
7667 tpacpi_is_fw_digit(s[1]) &&
7668 s[2] == t && s[3] == 'T' &&
7669 tpacpi_is_fw_digit(s[4]) &&
7670 tpacpi_is_fw_digit(s[5]) &&
7671 s[6] == 'W' && s[7] == 'W';
7672}
7673
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007674/* returns 0 - probe ok, or < 0 - probe error.
7675 * Probe ok doesn't mean thinkpad found.
7676 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
7677static int __must_check __init get_thinkpad_model_data(
7678 struct thinkpad_id_data *tp)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02007679{
Jeff Garzik18552562007-10-03 15:15:40 -04007680 const struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02007681 char ec_fw_string[18];
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007682 char const *s;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02007683
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007684 if (!tp)
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007685 return -EINVAL;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007686
7687 memset(tp, 0, sizeof(*tp));
7688
7689 if (dmi_name_in_vendors("IBM"))
7690 tp->vendor = PCI_VENDOR_ID_IBM;
7691 else if (dmi_name_in_vendors("LENOVO"))
7692 tp->vendor = PCI_VENDOR_ID_LENOVO;
7693 else
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007694 return 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007695
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007696 s = dmi_get_system_info(DMI_BIOS_VERSION);
7697 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
7698 if (s && !tp->bios_version_str)
7699 return -ENOMEM;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03007700
7701 /* Really ancient ThinkPad 240X will fail this, which is fine */
7702 if (!tpacpi_is_valid_fw_id(tp->bios_version_str, 'E'))
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007703 return 0;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03007704
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007705 tp->bios_model = tp->bios_version_str[0]
7706 | (tp->bios_version_str[1] << 8);
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03007707 tp->bios_release = (tp->bios_version_str[4] << 8)
7708 | tp->bios_version_str[5];
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007709
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02007710 /*
7711 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
7712 * X32 or newer, all Z series; Some models must have an
7713 * up-to-date BIOS or they will not be detected.
7714 *
7715 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
7716 */
7717 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02007718 if (sscanf(dev->name,
7719 "IBM ThinkPad Embedded Controller -[%17c",
7720 ec_fw_string) == 1) {
7721 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
7722 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007723
7724 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007725 if (!tp->ec_version_str)
7726 return -ENOMEM;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03007727
7728 if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) {
7729 tp->ec_model = ec_fw_string[0]
7730 | (ec_fw_string[1] << 8);
7731 tp->ec_release = (ec_fw_string[4] << 8)
7732 | ec_fw_string[5];
7733 } else {
7734 printk(TPACPI_NOTICE
7735 "ThinkPad firmware release %s "
7736 "doesn't match the known patterns\n",
7737 ec_fw_string);
7738 printk(TPACPI_NOTICE
7739 "please report this to %s\n",
7740 TPACPI_MAIL);
7741 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007742 break;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02007743 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02007744 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007745
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007746 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
7747 if (s && !strnicmp(s, "ThinkPad", 8)) {
7748 tp->model_str = kstrdup(s, GFP_KERNEL);
7749 if (!tp->model_str)
7750 return -ENOMEM;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007751 }
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03007752
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007753 s = dmi_get_system_info(DMI_PRODUCT_NAME);
7754 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
7755 if (s && !tp->nummodel_str)
7756 return -ENOMEM;
7757
7758 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007759}
7760
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007761static int __init probe_for_thinkpad(void)
7762{
7763 int is_thinkpad;
7764
7765 if (acpi_disabled)
7766 return -ENODEV;
7767
7768 /*
7769 * Non-ancient models have better DMI tagging, but very old models
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03007770 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007771 */
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03007772 is_thinkpad = (thinkpad_id.model_str != NULL) ||
7773 (thinkpad_id.ec_model != 0) ||
7774 tpacpi_is_fw_known();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007775
7776 /* ec is required because many other handles are relative to it */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007777 TPACPI_ACPIHANDLE_INIT(ec);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007778 if (!ec_handle) {
7779 if (is_thinkpad)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007780 printk(TPACPI_ERR
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007781 "Not yet supported ThinkPad detected!\n");
7782 return -ENODEV;
7783 }
7784
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03007785 if (!is_thinkpad && !force_load)
7786 return -ENODEV;
7787
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007788 return 0;
7789}
7790
7791
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007792/* Module init, exit, parameters */
7793
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007794static struct ibm_init_struct ibms_init[] __initdata = {
7795 {
7796 .init = thinkpad_acpi_driver_init,
7797 .data = &thinkpad_acpi_driver_data,
7798 },
7799 {
7800 .init = hotkey_init,
7801 .data = &hotkey_driver_data,
7802 },
7803 {
7804 .init = bluetooth_init,
7805 .data = &bluetooth_driver_data,
7806 },
7807 {
7808 .init = wan_init,
7809 .data = &wan_driver_data,
7810 },
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02007811 {
7812 .init = uwb_init,
7813 .data = &uwb_driver_data,
7814 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02007815#ifdef CONFIG_THINKPAD_ACPI_VIDEO
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007816 {
7817 .init = video_init,
7818 .data = &video_driver_data,
7819 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02007820#endif
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007821 {
7822 .init = light_init,
7823 .data = &light_driver_data,
7824 },
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007825 {
7826 .init = cmos_init,
7827 .data = &cmos_driver_data,
7828 },
7829 {
7830 .init = led_init,
7831 .data = &led_driver_data,
7832 },
7833 {
7834 .init = beep_init,
7835 .data = &beep_driver_data,
7836 },
7837 {
7838 .init = thermal_init,
7839 .data = &thermal_driver_data,
7840 },
7841 {
7842 .data = &ecdump_driver_data,
7843 },
7844 {
7845 .init = brightness_init,
7846 .data = &brightness_driver_data,
7847 },
7848 {
7849 .data = &volume_driver_data,
7850 },
7851 {
7852 .init = fan_init,
7853 .data = &fan_driver_data,
7854 },
7855};
7856
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007857static int __init set_ibm_param(const char *val, struct kernel_param *kp)
7858{
7859 unsigned int i;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007860 struct ibm_struct *ibm;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007861
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02007862 if (!kp || !kp->name || !val)
7863 return -EINVAL;
7864
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007865 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
7866 ibm = ibms_init[i].data;
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02007867 WARN_ON(ibm == NULL);
7868
7869 if (!ibm || !ibm->name)
7870 continue;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007871
7872 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
7873 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007874 return -ENOSPC;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007875 strcpy(ibms_init[i].param, val);
7876 strcat(ibms_init[i].param, ",");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007877 return 0;
7878 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007879 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007880
7881 return -EINVAL;
7882}
7883
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007884module_param(experimental, int, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007885MODULE_PARM_DESC(experimental,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007886 "Enables experimental features when non-zero");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007887
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03007888module_param_named(debug, dbg_level, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007889MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03007890
Henrique de Moraes Holschuh86cc9442007-07-18 23:45:41 -03007891module_param(force_load, bool, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007892MODULE_PARM_DESC(force_load,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007893 "Attempts to load the driver even on a "
7894 "mis-identified ThinkPad when true");
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03007895
Henrique de Moraes Holschuh86cc9442007-07-18 23:45:41 -03007896module_param_named(fan_control, fan_control_allowed, bool, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007897MODULE_PARM_DESC(fan_control,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007898 "Enables setting fan parameters features when true");
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03007899
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00007900module_param_named(brightness_mode, brightness_mode, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007901MODULE_PARM_DESC(brightness_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007902 "Selects brightness control strategy: "
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00007903 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03007904
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02007905module_param(brightness_enable, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007906MODULE_PARM_DESC(brightness_enable,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007907 "Enables backlight control when 1, disables when 0");
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02007908
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03007909module_param(hotkey_report_mode, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007910MODULE_PARM_DESC(hotkey_report_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007911 "used for backwards compatibility with userspace, "
7912 "see documentation");
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03007913
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007914#define TPACPI_PARAM(feature) \
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007915 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
Henrique de Moraes Holschuhcbb14842008-02-16 02:17:50 -02007916 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007917 "at module load, see documentation")
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007918
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007919TPACPI_PARAM(hotkey);
7920TPACPI_PARAM(bluetooth);
7921TPACPI_PARAM(video);
7922TPACPI_PARAM(light);
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007923TPACPI_PARAM(cmos);
7924TPACPI_PARAM(led);
7925TPACPI_PARAM(beep);
7926TPACPI_PARAM(ecdump);
7927TPACPI_PARAM(brightness);
7928TPACPI_PARAM(volume);
7929TPACPI_PARAM(fan);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007930
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02007931#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
7932module_param(dbg_wlswemul, uint, 0);
7933MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
7934module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
7935MODULE_PARM_DESC(wlsw_state,
7936 "Initial state of the emulated WLSW switch");
7937
7938module_param(dbg_bluetoothemul, uint, 0);
7939MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
7940module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
7941MODULE_PARM_DESC(bluetooth_state,
7942 "Initial state of the emulated bluetooth switch");
7943
7944module_param(dbg_wwanemul, uint, 0);
7945MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
7946module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
7947MODULE_PARM_DESC(wwan_state,
7948 "Initial state of the emulated WWAN switch");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02007949
7950module_param(dbg_uwbemul, uint, 0);
7951MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
7952module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
7953MODULE_PARM_DESC(uwb_state,
7954 "Initial state of the emulated UWB switch");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02007955#endif
7956
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007957static void thinkpad_acpi_module_exit(void)
7958{
7959 struct ibm_struct *ibm, *itmp;
7960
7961 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
7962
7963 list_for_each_entry_safe_reverse(ibm, itmp,
7964 &tpacpi_all_drivers,
7965 all_drivers) {
7966 ibm_exit(ibm);
7967 }
7968
7969 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
7970
7971 if (tpacpi_inputdev) {
7972 if (tp_features.input_device_registered)
7973 input_unregister_device(tpacpi_inputdev);
7974 else
7975 input_free_device(tpacpi_inputdev);
7976 }
7977
7978 if (tpacpi_hwmon)
7979 hwmon_device_unregister(tpacpi_hwmon);
7980
7981 if (tp_features.sensors_pdev_attrs_registered)
7982 device_remove_file(&tpacpi_sensors_pdev->dev,
7983 &dev_attr_thinkpad_acpi_pdev_name);
7984 if (tpacpi_sensors_pdev)
7985 platform_device_unregister(tpacpi_sensors_pdev);
7986 if (tpacpi_pdev)
7987 platform_device_unregister(tpacpi_pdev);
7988
7989 if (tp_features.sensors_pdrv_attrs_registered)
7990 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
7991 if (tp_features.platform_drv_attrs_registered)
7992 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
7993
7994 if (tp_features.sensors_pdrv_registered)
7995 platform_driver_unregister(&tpacpi_hwmon_pdriver);
7996
7997 if (tp_features.platform_drv_registered)
7998 platform_driver_unregister(&tpacpi_pdriver);
7999
8000 if (proc_dir)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008001 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008002
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03008003 if (tpacpi_wq)
8004 destroy_workqueue(tpacpi_wq);
8005
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008006 kfree(thinkpad_id.bios_version_str);
8007 kfree(thinkpad_id.ec_version_str);
8008 kfree(thinkpad_id.model_str);
8009}
8010
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02008011
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03008012static int __init thinkpad_acpi_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008013{
8014 int ret, i;
8015
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03008016 tpacpi_lifecycle = TPACPI_LIFE_INIT;
8017
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03008018 /* Parameter checking */
8019 if (hotkey_report_mode > 2)
8020 return -EINVAL;
8021
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008022 /* Driver-level probe */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008023
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008024 ret = get_thinkpad_model_data(&thinkpad_id);
8025 if (ret) {
8026 printk(TPACPI_ERR
8027 "unable to get DMI data: %d\n", ret);
8028 thinkpad_acpi_module_exit();
8029 return ret;
8030 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008031 ret = probe_for_thinkpad();
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008032 if (ret) {
8033 thinkpad_acpi_module_exit();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008034 return ret;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008035 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008036
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008037 /* Driver initialization */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008038
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008039 TPACPI_ACPIHANDLE_INIT(ecrd);
8040 TPACPI_ACPIHANDLE_INIT(ecwr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008041
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03008042 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
8043 if (!tpacpi_wq) {
8044 thinkpad_acpi_module_exit();
8045 return -ENOMEM;
8046 }
8047
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008048 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008049 if (!proc_dir) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008050 printk(TPACPI_ERR
8051 "unable to create proc dir " TPACPI_PROC_DIR);
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03008052 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008053 return -ENODEV;
8054 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008055
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008056 ret = platform_driver_register(&tpacpi_pdriver);
8057 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008058 printk(TPACPI_ERR
8059 "unable to register main platform driver\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008060 thinkpad_acpi_module_exit();
8061 return ret;
8062 }
Henrique de Moraes Holschuhac363932007-07-27 17:04:40 -03008063 tp_features.platform_drv_registered = 1;
8064
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008065 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
8066 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008067 printk(TPACPI_ERR
8068 "unable to register hwmon platform driver\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008069 thinkpad_acpi_module_exit();
8070 return ret;
8071 }
8072 tp_features.sensors_pdrv_registered = 1;
8073
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03008074 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03008075 if (!ret) {
8076 tp_features.platform_drv_attrs_registered = 1;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008077 ret = tpacpi_create_driver_attributes(
8078 &tpacpi_hwmon_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03008079 }
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03008080 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008081 printk(TPACPI_ERR
8082 "unable to create sysfs driver attributes\n");
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03008083 thinkpad_acpi_module_exit();
8084 return ret;
8085 }
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03008086 tp_features.sensors_pdrv_attrs_registered = 1;
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03008087
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008088
8089 /* Device initialization */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008090 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008091 NULL, 0);
8092 if (IS_ERR(tpacpi_pdev)) {
8093 ret = PTR_ERR(tpacpi_pdev);
8094 tpacpi_pdev = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008095 printk(TPACPI_ERR "unable to register platform device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008096 thinkpad_acpi_module_exit();
8097 return ret;
8098 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008099 tpacpi_sensors_pdev = platform_device_register_simple(
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008100 TPACPI_HWMON_DRVR_NAME,
8101 -1, NULL, 0);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008102 if (IS_ERR(tpacpi_sensors_pdev)) {
8103 ret = PTR_ERR(tpacpi_sensors_pdev);
8104 tpacpi_sensors_pdev = NULL;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008105 printk(TPACPI_ERR
8106 "unable to register hwmon platform device\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008107 thinkpad_acpi_module_exit();
8108 return ret;
8109 }
8110 ret = device_create_file(&tpacpi_sensors_pdev->dev,
8111 &dev_attr_thinkpad_acpi_pdev_name);
8112 if (ret) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008113 printk(TPACPI_ERR
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008114 "unable to create sysfs hwmon device attributes\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008115 thinkpad_acpi_module_exit();
8116 return ret;
8117 }
8118 tp_features.sensors_pdev_attrs_registered = 1;
8119 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008120 if (IS_ERR(tpacpi_hwmon)) {
8121 ret = PTR_ERR(tpacpi_hwmon);
8122 tpacpi_hwmon = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008123 printk(TPACPI_ERR "unable to register hwmon device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008124 thinkpad_acpi_module_exit();
8125 return ret;
8126 }
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -03008127 mutex_init(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03008128 tpacpi_inputdev = input_allocate_device();
8129 if (!tpacpi_inputdev) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008130 printk(TPACPI_ERR "unable to allocate input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03008131 thinkpad_acpi_module_exit();
8132 return -ENOMEM;
8133 } else {
8134 /* Prepare input device, but don't register */
8135 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008136 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03008137 tpacpi_inputdev->id.bustype = BUS_HOST;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03008138 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
8139 thinkpad_id.vendor :
8140 PCI_VENDOR_ID_IBM;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03008141 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
8142 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
8143 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008144 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
8145 ret = ibm_init(&ibms_init[i]);
8146 if (ret >= 0 && *ibms_init[i].param)
8147 ret = ibms_init[i].data->write(ibms_init[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008148 if (ret < 0) {
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03008149 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008150 return ret;
8151 }
8152 }
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03008153 ret = input_register_device(tpacpi_inputdev);
8154 if (ret < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008155 printk(TPACPI_ERR "unable to register input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03008156 thinkpad_acpi_module_exit();
8157 return ret;
8158 } else {
8159 tp_features.input_device_registered = 1;
8160 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008161
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03008162 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008163 return 0;
8164}
8165
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03008166MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
8167
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008168/*
Henrique de Moraes Holschuh922fe092009-04-14 02:44:13 +00008169 * This will autoload the driver in almost every ThinkPad
8170 * in widespread use.
8171 *
8172 * Only _VERY_ old models, like the 240, 240x and 570 lack
8173 * the HKEY event interface.
8174 */
8175MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
8176
8177/*
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008178 * DMI matching for module autoloading
8179 *
8180 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
8181 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
8182 *
8183 * Only models listed in thinkwiki will be supported, so add yours
8184 * if it is not there yet.
8185 */
8186#define IBM_BIOS_MODULE_ALIAS(__type) \
Mathieu Chouquet-Stringerb36a50f2009-03-14 16:35:26 +01008187 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008188
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008189/* Ancient thinkpad BIOSes have to be identified by
8190 * BIOS type or model number, and there are far less
8191 * BIOS types than model numbers... */
Henrique de Moraes Holschuh922fe092009-04-14 02:44:13 +00008192IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008193
Henrique de Moraes Holschuhf68f53a2009-04-14 02:44:12 +00008194MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
8195MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008196MODULE_DESCRIPTION(TPACPI_DESC);
8197MODULE_VERSION(TPACPI_VERSION);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008198MODULE_LICENSE("GPL");
8199
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03008200module_init(thinkpad_acpi_module_init);
8201module_exit(thinkpad_acpi_module_exit);