blob: 6b667891fc3b09161950d6d723fe71ace56ddacb [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 Holschuh94954cc62007-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 Holschuh94954cc62007-07-18 23:45:27 -0300817static struct platform_device *tpacpi_pdev;
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300818static struct platform_device *tpacpi_sensors_pdev;
Tony Jones1beeffe2007-08-20 13:46:20 -0700819static struct device *tpacpi_hwmon;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300820static struct input_dev *tpacpi_inputdev;
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -0300821static struct mutex tpacpi_inputdev_send_mutex;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200822static LIST_HEAD(tpacpi_all_drivers);
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300823
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200824static int tpacpi_suspend_handler(struct platform_device *pdev,
825 pm_message_t state)
826{
827 struct ibm_struct *ibm, *itmp;
828
829 list_for_each_entry_safe(ibm, itmp,
830 &tpacpi_all_drivers,
831 all_drivers) {
832 if (ibm->suspend)
833 (ibm->suspend)(state);
834 }
835
836 return 0;
837}
838
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300839static int tpacpi_resume_handler(struct platform_device *pdev)
840{
841 struct ibm_struct *ibm, *itmp;
842
843 list_for_each_entry_safe(ibm, itmp,
844 &tpacpi_all_drivers,
845 all_drivers) {
846 if (ibm->resume)
847 (ibm->resume)();
848 }
849
850 return 0;
851}
852
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200853static void tpacpi_shutdown_handler(struct platform_device *pdev)
854{
855 struct ibm_struct *ibm, *itmp;
856
857 list_for_each_entry_safe(ibm, itmp,
858 &tpacpi_all_drivers,
859 all_drivers) {
860 if (ibm->shutdown)
861 (ibm->shutdown)();
862 }
863}
864
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300865static struct platform_driver tpacpi_pdriver = {
866 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200867 .name = TPACPI_DRVR_NAME,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300868 .owner = THIS_MODULE,
869 },
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200870 .suspend = tpacpi_suspend_handler,
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300871 .resume = tpacpi_resume_handler,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200872 .shutdown = tpacpi_shutdown_handler,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300873};
874
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300875static struct platform_driver tpacpi_hwmon_pdriver = {
876 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200877 .name = TPACPI_HWMON_DRVR_NAME,
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300878 .owner = THIS_MODULE,
879 },
880};
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300881
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -0300882/*************************************************************************
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300883 * sysfs support helpers
884 */
885
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200886struct attribute_set {
887 unsigned int members, max_members;
888 struct attribute_group group;
889};
890
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300891struct attribute_set_obj {
892 struct attribute_set s;
893 struct attribute *a;
894} __attribute__((packed));
895
896static struct attribute_set *create_attr_set(unsigned int max_members,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200897 const char *name)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300898{
899 struct attribute_set_obj *sobj;
900
901 if (max_members == 0)
902 return NULL;
903
904 /* Allocates space for implicit NULL at the end too */
905 sobj = kzalloc(sizeof(struct attribute_set_obj) +
906 max_members * sizeof(struct attribute *),
907 GFP_KERNEL);
908 if (!sobj)
909 return NULL;
910 sobj->s.max_members = max_members;
911 sobj->s.group.attrs = &sobj->a;
912 sobj->s.group.name = name;
913
914 return &sobj->s;
915}
916
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200917#define destroy_attr_set(_set) \
918 kfree(_set);
919
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300920/* not multi-threaded safe, use it in a single thread per set */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200921static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300922{
923 if (!s || !attr)
924 return -EINVAL;
925
926 if (s->members >= s->max_members)
927 return -ENOMEM;
928
929 s->group.attrs[s->members] = attr;
930 s->members++;
931
932 return 0;
933}
934
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200935static int add_many_to_attr_set(struct attribute_set *s,
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300936 struct attribute **attr,
937 unsigned int count)
938{
939 int i, res;
940
941 for (i = 0; i < count; i++) {
942 res = add_to_attr_set(s, attr[i]);
943 if (res)
944 return res;
945 }
946
947 return 0;
948}
949
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200950static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300951{
952 sysfs_remove_group(kobj, &s->group);
953 destroy_attr_set(s);
954}
955
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200956#define register_attr_set_with_sysfs(_attr_set, _kobj) \
957 sysfs_create_group(_kobj, &_attr_set->group)
958
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300959static int parse_strtoul(const char *buf,
960 unsigned long max, unsigned long *value)
961{
962 char *endp;
963
Henrique de Moraes Holschuh32afbf02007-10-08 10:12:56 -0300964 while (*buf && isspace(*buf))
965 buf++;
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300966 *value = simple_strtoul(buf, &endp, 0);
967 while (*endp && isspace(*endp))
968 endp++;
969 if (*endp || *value > max)
970 return -EINVAL;
971
972 return 0;
973}
974
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -0300975static void tpacpi_disable_brightness_delay(void)
976{
977 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
978 printk(TPACPI_NOTICE
979 "ACPI backlight control delay disabled\n");
980}
981
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -0300982static int __init tpacpi_query_bcl_levels(acpi_handle handle)
983{
984 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
985 union acpi_object *obj;
986 int rc;
987
988 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
989 obj = (union acpi_object *)buffer.pointer;
990 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
991 printk(TPACPI_ERR "Unknown _BCL data, "
992 "please report this to %s\n", TPACPI_MAIL);
993 rc = 0;
994 } else {
995 rc = obj->package.count;
996 }
997 } else {
998 return 0;
999 }
1000
1001 kfree(buffer.pointer);
1002 return rc;
1003}
1004
1005static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
1006 u32 lvl, void *context, void **rv)
1007{
1008 char name[ACPI_PATH_SEGMENT_LENGTH];
1009 struct acpi_buffer buffer = { sizeof(name), &name };
1010
1011 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
1012 !strncmp("_BCL", name, sizeof(name) - 1)) {
1013 BUG_ON(!rv || !*rv);
1014 **(int **)rv = tpacpi_query_bcl_levels(handle);
1015 return AE_CTRL_TERMINATE;
1016 } else {
1017 return AE_OK;
1018 }
1019}
1020
1021/*
1022 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
1023 */
1024static int __init tpacpi_check_std_acpi_brightness_support(void)
1025{
1026 int status;
1027 int bcl_levels = 0;
1028 void *bcl_ptr = &bcl_levels;
1029
1030 if (!vid_handle) {
1031 TPACPI_ACPIHANDLE_INIT(vid);
1032 }
1033 if (!vid_handle)
1034 return 0;
1035
1036 /*
1037 * Search for a _BCL method, and execute it. This is safe on all
1038 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
1039 * BIOS in ACPI backlight control mode. We do NOT have to care
1040 * about calling the _BCL method in an enabled video device, any
1041 * will do for our purposes.
1042 */
1043
1044 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
1045 tpacpi_acpi_walk_find_bcl, NULL,
1046 &bcl_ptr);
1047
1048 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
1049 tp_features.bright_acpimode = 1;
1050 return (bcl_levels - 2);
1051 }
1052
1053 return 0;
1054}
1055
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00001056static void printk_deprecated_attribute(const char * const what,
1057 const char * const details)
1058{
1059 tpacpi_log_usertask("deprecated sysfs attribute");
1060 printk(TPACPI_WARN "WARNING: sysfs attribute %s is deprecated and "
1061 "will be removed. %s\n",
1062 what, details);
1063}
1064
Johannes Berg19d337d2009-06-02 13:01:37 +02001065/*************************************************************************
1066 * rfkill and radio control support helpers
1067 */
1068
1069/*
1070 * ThinkPad-ACPI firmware handling model:
1071 *
1072 * WLSW (master wireless switch) is event-driven, and is common to all
1073 * firmware-controlled radios. It cannot be controlled, just monitored,
1074 * as expected. It overrides all radio state in firmware
1075 *
1076 * The kernel, a masked-off hotkey, and WLSW can change the radio state
1077 * (TODO: verify how WLSW interacts with the returned radio state).
1078 *
1079 * The only time there are shadow radio state changes, is when
1080 * masked-off hotkeys are used.
1081 */
1082
1083/*
1084 * Internal driver API for radio state:
1085 *
1086 * int: < 0 = error, otherwise enum tpacpi_rfkill_state
1087 * bool: true means radio blocked (off)
1088 */
1089enum tpacpi_rfkill_state {
1090 TPACPI_RFK_RADIO_OFF = 0,
1091 TPACPI_RFK_RADIO_ON
1092};
1093
1094/* rfkill switches */
1095enum tpacpi_rfk_id {
1096 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
1097 TPACPI_RFK_WWAN_SW_ID,
1098 TPACPI_RFK_UWB_SW_ID,
1099 TPACPI_RFK_SW_MAX
1100};
1101
1102static const char *tpacpi_rfkill_names[] = {
1103 [TPACPI_RFK_BLUETOOTH_SW_ID] = "bluetooth",
1104 [TPACPI_RFK_WWAN_SW_ID] = "wwan",
1105 [TPACPI_RFK_UWB_SW_ID] = "uwb",
1106 [TPACPI_RFK_SW_MAX] = NULL
1107};
1108
1109/* ThinkPad-ACPI rfkill subdriver */
1110struct tpacpi_rfk {
1111 struct rfkill *rfkill;
1112 enum tpacpi_rfk_id id;
1113 const struct tpacpi_rfk_ops *ops;
1114};
1115
1116struct tpacpi_rfk_ops {
1117 /* firmware interface */
1118 int (*get_status)(void);
1119 int (*set_status)(const enum tpacpi_rfkill_state);
1120};
1121
1122static struct tpacpi_rfk *tpacpi_rfkill_switches[TPACPI_RFK_SW_MAX];
1123
1124/* Query FW and update rfkill sw state for a given rfkill switch */
1125static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk *tp_rfk)
1126{
1127 int status;
1128
1129 if (!tp_rfk)
1130 return -ENODEV;
1131
1132 status = (tp_rfk->ops->get_status)();
1133 if (status < 0)
1134 return status;
1135
1136 rfkill_set_sw_state(tp_rfk->rfkill,
1137 (status == TPACPI_RFK_RADIO_OFF));
1138
1139 return status;
1140}
1141
1142/* Query FW and update rfkill sw state for all rfkill switches */
1143static void tpacpi_rfk_update_swstate_all(void)
1144{
1145 unsigned int i;
1146
1147 for (i = 0; i < TPACPI_RFK_SW_MAX; i++)
1148 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[i]);
1149}
1150
1151/*
1152 * Sync the HW-blocking state of all rfkill switches,
1153 * do notice it causes the rfkill core to schedule uevents
1154 */
1155static void tpacpi_rfk_update_hwblock_state(bool blocked)
1156{
1157 unsigned int i;
1158 struct tpacpi_rfk *tp_rfk;
1159
1160 for (i = 0; i < TPACPI_RFK_SW_MAX; i++) {
1161 tp_rfk = tpacpi_rfkill_switches[i];
1162 if (tp_rfk) {
1163 if (rfkill_set_hw_state(tp_rfk->rfkill,
1164 blocked)) {
1165 /* ignore -- we track sw block */
1166 }
1167 }
1168 }
1169}
1170
1171/* Call to get the WLSW state from the firmware */
1172static int hotkey_get_wlsw(void);
1173
1174/* Call to query WLSW state and update all rfkill switches */
1175static bool tpacpi_rfk_check_hwblock_state(void)
1176{
1177 int res = hotkey_get_wlsw();
1178 int hw_blocked;
1179
1180 /* When unknown or unsupported, we have to assume it is unblocked */
1181 if (res < 0)
1182 return false;
1183
1184 hw_blocked = (res == TPACPI_RFK_RADIO_OFF);
1185 tpacpi_rfk_update_hwblock_state(hw_blocked);
1186
1187 return hw_blocked;
1188}
1189
1190static int tpacpi_rfk_hook_set_block(void *data, bool blocked)
1191{
1192 struct tpacpi_rfk *tp_rfk = data;
1193 int res;
1194
1195 dbg_printk(TPACPI_DBG_RFKILL,
1196 "request to change radio state to %s\n",
1197 blocked ? "blocked" : "unblocked");
1198
1199 /* try to set radio state */
1200 res = (tp_rfk->ops->set_status)(blocked ?
1201 TPACPI_RFK_RADIO_OFF : TPACPI_RFK_RADIO_ON);
1202
1203 /* and update the rfkill core with whatever the FW really did */
1204 tpacpi_rfk_update_swstate(tp_rfk);
1205
1206 return (res < 0) ? res : 0;
1207}
1208
1209static const struct rfkill_ops tpacpi_rfk_rfkill_ops = {
1210 .set_block = tpacpi_rfk_hook_set_block,
1211};
1212
1213static int __init tpacpi_new_rfkill(const enum tpacpi_rfk_id id,
1214 const struct tpacpi_rfk_ops *tp_rfkops,
1215 const enum rfkill_type rfktype,
1216 const char *name,
1217 const bool set_default)
1218{
1219 struct tpacpi_rfk *atp_rfk;
1220 int res;
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001221 bool sw_state = false;
1222 int sw_status;
Johannes Berg19d337d2009-06-02 13:01:37 +02001223
1224 BUG_ON(id >= TPACPI_RFK_SW_MAX || tpacpi_rfkill_switches[id]);
1225
Johannes Berg19d337d2009-06-02 13:01:37 +02001226 atp_rfk = kzalloc(sizeof(struct tpacpi_rfk), GFP_KERNEL);
1227 if (atp_rfk)
1228 atp_rfk->rfkill = rfkill_alloc(name,
1229 &tpacpi_pdev->dev,
1230 rfktype,
1231 &tpacpi_rfk_rfkill_ops,
1232 atp_rfk);
1233 if (!atp_rfk || !atp_rfk->rfkill) {
1234 printk(TPACPI_ERR
1235 "failed to allocate memory for rfkill class\n");
1236 kfree(atp_rfk);
1237 return -ENOMEM;
1238 }
1239
1240 atp_rfk->id = id;
1241 atp_rfk->ops = tp_rfkops;
1242
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001243 sw_status = (tp_rfkops->get_status)();
1244 if (sw_status < 0) {
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001245 printk(TPACPI_ERR
1246 "failed to read initial state for %s, error %d\n",
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001247 name, sw_status);
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001248 } else {
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001249 sw_state = (sw_status == TPACPI_RFK_RADIO_OFF);
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001250 if (set_default) {
1251 /* try to keep the initial state, since we ask the
1252 * firmware to preserve it across S5 in NVRAM */
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001253 rfkill_init_sw_state(atp_rfk->rfkill, sw_state);
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001254 }
1255 }
1256 rfkill_set_hw_state(atp_rfk->rfkill, tpacpi_rfk_check_hwblock_state());
Johannes Berg19d337d2009-06-02 13:01:37 +02001257
1258 res = rfkill_register(atp_rfk->rfkill);
1259 if (res < 0) {
1260 printk(TPACPI_ERR
1261 "failed to register %s rfkill switch: %d\n",
1262 name, res);
1263 rfkill_destroy(atp_rfk->rfkill);
1264 kfree(atp_rfk);
1265 return res;
1266 }
1267
1268 tpacpi_rfkill_switches[id] = atp_rfk;
1269 return 0;
1270}
1271
1272static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id)
1273{
1274 struct tpacpi_rfk *tp_rfk;
1275
1276 BUG_ON(id >= TPACPI_RFK_SW_MAX);
1277
1278 tp_rfk = tpacpi_rfkill_switches[id];
1279 if (tp_rfk) {
1280 rfkill_unregister(tp_rfk->rfkill);
1281 tpacpi_rfkill_switches[id] = NULL;
1282 kfree(tp_rfk);
1283 }
1284}
1285
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00001286static void printk_deprecated_rfkill_attribute(const char * const what)
1287{
1288 printk_deprecated_attribute(what,
1289 "Please switch to generic rfkill before year 2010");
1290}
1291
Johannes Berg19d337d2009-06-02 13:01:37 +02001292/* sysfs <radio> enable ------------------------------------------------ */
1293static ssize_t tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id,
1294 struct device_attribute *attr,
1295 char *buf)
1296{
1297 int status;
1298
1299 printk_deprecated_rfkill_attribute(attr->attr.name);
1300
1301 /* This is in the ABI... */
1302 if (tpacpi_rfk_check_hwblock_state()) {
1303 status = TPACPI_RFK_RADIO_OFF;
1304 } else {
1305 status = tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1306 if (status < 0)
1307 return status;
1308 }
1309
1310 return snprintf(buf, PAGE_SIZE, "%d\n",
1311 (status == TPACPI_RFK_RADIO_ON) ? 1 : 0);
1312}
1313
1314static ssize_t tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id,
1315 struct device_attribute *attr,
1316 const char *buf, size_t count)
1317{
1318 unsigned long t;
1319 int res;
1320
1321 printk_deprecated_rfkill_attribute(attr->attr.name);
1322
1323 if (parse_strtoul(buf, 1, &t))
1324 return -EINVAL;
1325
1326 tpacpi_disclose_usertask(attr->attr.name, "set to %ld\n", t);
1327
1328 /* This is in the ABI... */
1329 if (tpacpi_rfk_check_hwblock_state() && !!t)
1330 return -EPERM;
1331
1332 res = tpacpi_rfkill_switches[id]->ops->set_status((!!t) ?
1333 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF);
1334 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1335
1336 return (res < 0) ? res : count;
1337}
1338
1339/* procfs -------------------------------------------------------------- */
1340static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id, char *p)
1341{
1342 int len = 0;
1343
1344 if (id >= TPACPI_RFK_SW_MAX)
1345 len += sprintf(p + len, "status:\t\tnot supported\n");
1346 else {
1347 int status;
1348
1349 /* This is in the ABI... */
1350 if (tpacpi_rfk_check_hwblock_state()) {
1351 status = TPACPI_RFK_RADIO_OFF;
1352 } else {
1353 status = tpacpi_rfk_update_swstate(
1354 tpacpi_rfkill_switches[id]);
1355 if (status < 0)
1356 return status;
1357 }
1358
1359 len += sprintf(p + len, "status:\t\t%s\n",
1360 (status == TPACPI_RFK_RADIO_ON) ?
1361 "enabled" : "disabled");
1362 len += sprintf(p + len, "commands:\tenable, disable\n");
1363 }
1364
1365 return len;
1366}
1367
1368static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id, char *buf)
1369{
1370 char *cmd;
1371 int status = -1;
1372 int res = 0;
1373
1374 if (id >= TPACPI_RFK_SW_MAX)
1375 return -ENODEV;
1376
1377 while ((cmd = next_cmd(&buf))) {
1378 if (strlencmp(cmd, "enable") == 0)
1379 status = TPACPI_RFK_RADIO_ON;
1380 else if (strlencmp(cmd, "disable") == 0)
1381 status = TPACPI_RFK_RADIO_OFF;
1382 else
1383 return -EINVAL;
1384 }
1385
1386 if (status != -1) {
1387 tpacpi_disclose_usertask("procfs", "attempt to %s %s\n",
1388 (status == TPACPI_RFK_RADIO_ON) ?
1389 "enable" : "disable",
1390 tpacpi_rfkill_names[id]);
1391 res = (tpacpi_rfkill_switches[id]->ops->set_status)(status);
1392 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1393 }
1394
1395 return res;
1396}
1397
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001398/*************************************************************************
1399 * thinkpad-acpi driver attributes
1400 */
1401
1402/* interface_version --------------------------------------------------- */
1403static ssize_t tpacpi_driver_interface_version_show(
1404 struct device_driver *drv,
1405 char *buf)
1406{
1407 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1408}
1409
1410static DRIVER_ATTR(interface_version, S_IRUGO,
1411 tpacpi_driver_interface_version_show, NULL);
1412
1413/* debug_level --------------------------------------------------------- */
1414static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1415 char *buf)
1416{
1417 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1418}
1419
1420static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1421 const char *buf, size_t count)
1422{
1423 unsigned long t;
1424
1425 if (parse_strtoul(buf, 0xffff, &t))
1426 return -EINVAL;
1427
1428 dbg_level = t;
1429
1430 return count;
1431}
1432
1433static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1434 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1435
1436/* version ------------------------------------------------------------- */
1437static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1438 char *buf)
1439{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001440 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1441 TPACPI_DESC, TPACPI_VERSION);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001442}
1443
1444static DRIVER_ATTR(version, S_IRUGO,
1445 tpacpi_driver_version_show, NULL);
1446
1447/* --------------------------------------------------------------------- */
1448
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001449#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1450
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001451/* wlsw_emulstate ------------------------------------------------------ */
1452static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1453 char *buf)
1454{
1455 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1456}
1457
1458static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1459 const char *buf, size_t count)
1460{
1461 unsigned long t;
1462
1463 if (parse_strtoul(buf, 1, &t))
1464 return -EINVAL;
1465
Johannes Berg19d337d2009-06-02 13:01:37 +02001466 if (tpacpi_wlsw_emulstate != !!t) {
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001467 tpacpi_wlsw_emulstate = !!t;
Johannes Berg19d337d2009-06-02 13:01:37 +02001468 tpacpi_rfk_update_hwblock_state(!t); /* negative logic */
1469 }
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001470
1471 return count;
1472}
1473
1474static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1475 tpacpi_driver_wlsw_emulstate_show,
1476 tpacpi_driver_wlsw_emulstate_store);
1477
1478/* bluetooth_emulstate ------------------------------------------------- */
1479static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1480 struct device_driver *drv,
1481 char *buf)
1482{
1483 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1484}
1485
1486static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1487 struct device_driver *drv,
1488 const char *buf, size_t count)
1489{
1490 unsigned long t;
1491
1492 if (parse_strtoul(buf, 1, &t))
1493 return -EINVAL;
1494
1495 tpacpi_bluetooth_emulstate = !!t;
1496
1497 return count;
1498}
1499
1500static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1501 tpacpi_driver_bluetooth_emulstate_show,
1502 tpacpi_driver_bluetooth_emulstate_store);
1503
1504/* wwan_emulstate ------------------------------------------------- */
1505static ssize_t tpacpi_driver_wwan_emulstate_show(
1506 struct device_driver *drv,
1507 char *buf)
1508{
1509 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1510}
1511
1512static ssize_t tpacpi_driver_wwan_emulstate_store(
1513 struct device_driver *drv,
1514 const char *buf, size_t count)
1515{
1516 unsigned long t;
1517
1518 if (parse_strtoul(buf, 1, &t))
1519 return -EINVAL;
1520
1521 tpacpi_wwan_emulstate = !!t;
1522
1523 return count;
1524}
1525
1526static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1527 tpacpi_driver_wwan_emulstate_show,
1528 tpacpi_driver_wwan_emulstate_store);
1529
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001530/* uwb_emulstate ------------------------------------------------- */
1531static ssize_t tpacpi_driver_uwb_emulstate_show(
1532 struct device_driver *drv,
1533 char *buf)
1534{
1535 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1536}
1537
1538static ssize_t tpacpi_driver_uwb_emulstate_store(
1539 struct device_driver *drv,
1540 const char *buf, size_t count)
1541{
1542 unsigned long t;
1543
1544 if (parse_strtoul(buf, 1, &t))
1545 return -EINVAL;
1546
1547 tpacpi_uwb_emulstate = !!t;
1548
1549 return count;
1550}
1551
1552static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
1553 tpacpi_driver_uwb_emulstate_show,
1554 tpacpi_driver_uwb_emulstate_store);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001555#endif
1556
1557/* --------------------------------------------------------------------- */
1558
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001559static struct driver_attribute *tpacpi_driver_attributes[] = {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001560 &driver_attr_debug_level, &driver_attr_version,
1561 &driver_attr_interface_version,
1562};
1563
1564static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1565{
1566 int i, res;
1567
1568 i = 0;
1569 res = 0;
1570 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1571 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1572 i++;
1573 }
1574
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001575#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1576 if (!res && dbg_wlswemul)
1577 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1578 if (!res && dbg_bluetoothemul)
1579 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1580 if (!res && dbg_wwanemul)
1581 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001582 if (!res && dbg_uwbemul)
1583 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001584#endif
1585
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001586 return res;
1587}
1588
1589static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1590{
1591 int i;
1592
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001593 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001594 driver_remove_file(drv, tpacpi_driver_attributes[i]);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001595
1596#ifdef THINKPAD_ACPI_DEBUGFACILITIES
1597 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1598 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1599 driver_remove_file(drv, &driver_attr_wwan_emulstate);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001600 driver_remove_file(drv, &driver_attr_uwb_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001601#endif
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001602}
1603
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001604/*************************************************************************
1605 * Firmware Data
1606 */
1607
1608/*
1609 * Table of recommended minimum BIOS versions
1610 *
1611 * Reasons for listing:
1612 * 1. Stable BIOS, listed because the unknown ammount of
1613 * bugs and bad ACPI behaviour on older versions
1614 *
1615 * 2. BIOS or EC fw with known bugs that trigger on Linux
1616 *
1617 * 3. BIOS with known reduced functionality in older versions
1618 *
1619 * We recommend the latest BIOS and EC version.
1620 * We only support the latest BIOS and EC fw version as a rule.
1621 *
1622 * Sources: IBM ThinkPad Public Web Documents (update changelogs),
1623 * Information from users in ThinkWiki
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03001624 *
1625 * WARNING: we use this table also to detect that the machine is
1626 * a ThinkPad in some cases, so don't remove entries lightly.
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001627 */
1628
1629#define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1630 { .vendor = (__v), \
1631 .bios = TPID(__id1, __id2), \
1632 .ec = TPACPI_MATCH_ANY, \
1633 .quirks = TPACPI_MATCH_ANY << 16 \
1634 | (__bv1) << 8 | (__bv2) }
1635
1636#define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
1637 __eid1, __eid2, __ev1, __ev2) \
1638 { .vendor = (__v), \
1639 .bios = TPID(__bid1, __bid2), \
1640 .ec = TPID(__eid1, __eid2), \
1641 .quirks = (__ev1) << 24 | (__ev2) << 16 \
1642 | (__bv1) << 8 | (__bv2) }
1643
1644#define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1645 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1646
1647#define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1648 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1649 __bv1, __bv2, __id1, __id2, __ev1, __ev2)
1650
1651#define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1652 __eid1, __eid2, __ev1, __ev2) \
1653 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1654 __bv1, __bv2, __eid1, __eid2, __ev1, __ev2)
1655
1656#define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1657 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1658
1659#define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1660 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
1661 __bv1, __bv2, __id1, __id2, __ev1, __ev2)
1662
1663#define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1664 __eid1, __eid2, __ev1, __ev2) \
1665 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
1666 __bv1, __bv2, __eid1, __eid2, __ev1, __ev2)
1667
1668static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
1669 /* Numeric models ------------------ */
1670 /* FW MODEL BIOS VERS */
1671 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1672 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1673 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1674 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1675 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1676 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1677 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1678 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1679 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1680
1681 /* A-series ------------------------- */
1682 /* FW MODEL BIOS VERS EC VERS */
1683 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1684 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1685 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1686 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1687 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1688 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1689 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1690 TPV_QI0('1', '3', '2', '0'), /* A22m */
1691 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1692 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1693 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1694
1695 /* G-series ------------------------- */
1696 /* FW MODEL BIOS VERS */
1697 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1698 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1699
1700 /* R-series, T-series --------------- */
1701 /* FW MODEL BIOS VERS EC VERS */
1702 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1703 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1704 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1705 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1706 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1707 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1708 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1709 T40/p, T41/p, T42/p (1) */
1710 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1711 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1712 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1713 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1714
1715 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1716 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1717 TPV_QI0('1', '6', '3', '2'), /* T22 */
1718 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1719 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1720 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1721
1722 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1723 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
1724 TPV_QL0('7', 'E', 'D', '0'), /* R60e, R60i */
1725
1726 /* BIOS FW BIOS VERS EC FW EC VERS */
1727 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1728 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1729
1730 /* X-series ------------------------- */
1731 /* FW MODEL BIOS VERS EC VERS */
1732 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1733 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1734 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1735 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1736 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1737 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1738 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1739
1740 TPV_QL0('7', 'B', 'D', '7'), /* X60/s */
1741 TPV_QL0('7', 'J', '3', '0'), /* X60t */
1742
1743 /* (0) - older versions lack DMI EC fw string and functionality */
1744 /* (1) - older versions known to lack functionality */
1745};
1746
1747#undef TPV_QL1
1748#undef TPV_QL0
1749#undef TPV_QI2
1750#undef TPV_QI1
1751#undef TPV_QI0
1752#undef TPV_Q_X
1753#undef TPV_Q
1754
1755static void __init tpacpi_check_outdated_fw(void)
1756{
1757 unsigned long fwvers;
1758 u16 ec_version, bios_version;
1759
1760 fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
1761 ARRAY_SIZE(tpacpi_bios_version_qtable));
1762
1763 if (!fwvers)
1764 return;
1765
1766 bios_version = fwvers & 0xffffU;
1767 ec_version = (fwvers >> 16) & 0xffffU;
1768
1769 /* note that unknown versions are set to 0x0000 and we use that */
1770 if ((bios_version > thinkpad_id.bios_release) ||
1771 (ec_version > thinkpad_id.ec_release &&
1772 ec_version != TPACPI_MATCH_ANY)) {
1773 /*
1774 * The changelogs would let us track down the exact
1775 * reason, but it is just too much of a pain to track
1776 * it. We only list BIOSes that are either really
1777 * broken, or really stable to begin with, so it is
1778 * best if the user upgrades the firmware anyway.
1779 */
1780 printk(TPACPI_WARN
1781 "WARNING: Outdated ThinkPad BIOS/EC firmware\n");
1782 printk(TPACPI_WARN
1783 "WARNING: This firmware may be missing critical bug "
1784 "fixes and/or important features\n");
1785 }
1786}
1787
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03001788static bool __init tpacpi_is_fw_known(void)
1789{
1790 return tpacpi_check_quirks(tpacpi_bios_version_qtable,
1791 ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
1792}
1793
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03001794/****************************************************************************
1795 ****************************************************************************
1796 *
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001797 * Subdrivers
1798 *
1799 ****************************************************************************
1800 ****************************************************************************/
1801
1802/*************************************************************************
Henrique de Moraes Holschuh142cfc92007-04-21 11:08:26 -03001803 * thinkpad-acpi init subdriver
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001804 */
1805
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001806static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001808 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
1809 printk(TPACPI_INFO "%s\n", TPACPI_URL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001811 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03001812 (thinkpad_id.bios_version_str) ?
1813 thinkpad_id.bios_version_str : "unknown",
1814 (thinkpad_id.ec_version_str) ?
1815 thinkpad_id.ec_version_str : "unknown");
1816
1817 if (thinkpad_id.vendor && thinkpad_id.model_str)
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03001818 printk(TPACPI_INFO "%s %s, model %s\n",
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03001819 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
1820 "IBM" : ((thinkpad_id.vendor ==
1821 PCI_VENDOR_ID_LENOVO) ?
1822 "Lenovo" : "Unknown vendor"),
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03001823 thinkpad_id.model_str,
1824 (thinkpad_id.nummodel_str) ?
1825 thinkpad_id.nummodel_str : "unknown");
Henrique de Moraes Holschuh3945ac32007-02-06 19:13:44 -02001826
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001827 tpacpi_check_outdated_fw();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 return 0;
1829}
1830
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -03001831static int thinkpad_acpi_driver_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832{
1833 int len = 0;
1834
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001835 len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
1836 len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837
1838 return len;
1839}
1840
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001841static struct ibm_struct thinkpad_acpi_driver_data = {
1842 .name = "driver",
1843 .read = thinkpad_acpi_driver_read,
1844};
1845
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001846/*************************************************************************
1847 * Hotkey subdriver
1848 */
1849
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001850enum { /* hot key scan codes (derived from ACPI DSDT) */
1851 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1852 TP_ACPI_HOTKEYSCAN_FNF2,
1853 TP_ACPI_HOTKEYSCAN_FNF3,
1854 TP_ACPI_HOTKEYSCAN_FNF4,
1855 TP_ACPI_HOTKEYSCAN_FNF5,
1856 TP_ACPI_HOTKEYSCAN_FNF6,
1857 TP_ACPI_HOTKEYSCAN_FNF7,
1858 TP_ACPI_HOTKEYSCAN_FNF8,
1859 TP_ACPI_HOTKEYSCAN_FNF9,
1860 TP_ACPI_HOTKEYSCAN_FNF10,
1861 TP_ACPI_HOTKEYSCAN_FNF11,
1862 TP_ACPI_HOTKEYSCAN_FNF12,
1863 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1864 TP_ACPI_HOTKEYSCAN_FNINSERT,
1865 TP_ACPI_HOTKEYSCAN_FNDELETE,
1866 TP_ACPI_HOTKEYSCAN_FNHOME,
1867 TP_ACPI_HOTKEYSCAN_FNEND,
1868 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1869 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1870 TP_ACPI_HOTKEYSCAN_FNSPACE,
1871 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1872 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1873 TP_ACPI_HOTKEYSCAN_MUTE,
1874 TP_ACPI_HOTKEYSCAN_THINKPAD,
1875};
1876
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001877enum { /* Keys available through NVRAM polling */
1878 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1879 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1880};
1881
1882enum { /* Positions of some of the keys in hotkey masks */
1883 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1884 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1885 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1886 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1887 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1888 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1889 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1890 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1891 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1892 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1893 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1894};
1895
1896enum { /* NVRAM to ACPI HKEY group map */
1897 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1898 TP_ACPI_HKEY_ZOOM_MASK |
1899 TP_ACPI_HKEY_DISPSWTCH_MASK |
1900 TP_ACPI_HKEY_HIBERNATE_MASK,
1901 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1902 TP_ACPI_HKEY_BRGHTDWN_MASK,
1903 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1904 TP_ACPI_HKEY_VOLDWN_MASK |
1905 TP_ACPI_HKEY_MUTE_MASK,
1906};
1907
1908#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1909struct tp_nvram_state {
1910 u16 thinkpad_toggle:1;
1911 u16 zoom_toggle:1;
1912 u16 display_toggle:1;
1913 u16 thinklight_toggle:1;
1914 u16 hibernate_toggle:1;
1915 u16 displayexp_toggle:1;
1916 u16 display_state:1;
1917 u16 brightness_toggle:1;
1918 u16 volume_toggle:1;
1919 u16 mute:1;
1920
1921 u8 brightness_level;
1922 u8 volume_level;
1923};
1924
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001925/* kthread for the hotkey poller */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001926static struct task_struct *tpacpi_hotkey_task;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001927
1928/* Acquired while the poller kthread is running, use to sync start/stop */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001929static struct mutex hotkey_thread_mutex;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001930
1931/*
1932 * Acquire mutex to write poller control variables.
1933 * Increment hotkey_config_change when changing them.
1934 *
1935 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1936 */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001937static struct mutex hotkey_thread_data_mutex;
1938static unsigned int hotkey_config_change;
1939
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001940/*
1941 * hotkey poller control variables
1942 *
1943 * Must be atomic or readers will also need to acquire mutex
1944 */
1945static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1946static unsigned int hotkey_poll_freq = 10; /* Hz */
1947
1948#define HOTKEY_CONFIG_CRITICAL_START \
1949 do { \
1950 mutex_lock(&hotkey_thread_data_mutex); \
1951 hotkey_config_change++; \
1952 } while (0);
1953#define HOTKEY_CONFIG_CRITICAL_END \
1954 mutex_unlock(&hotkey_thread_data_mutex);
1955
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001956#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1957
1958#define hotkey_source_mask 0U
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001959#define HOTKEY_CONFIG_CRITICAL_START
1960#define HOTKEY_CONFIG_CRITICAL_END
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001961
1962#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1963
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001964static struct mutex hotkey_mutex;
1965
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02001966static enum { /* Reasons for waking up */
1967 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1968 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1969 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1970} hotkey_wakeup_reason;
1971
1972static int hotkey_autosleep_ack;
1973
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001974static u32 hotkey_orig_mask;
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001975static u32 hotkey_all_mask;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001976static u32 hotkey_reserved_mask;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001977static u32 hotkey_mask;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001978
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001979static unsigned int hotkey_report_mode;
1980
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001981static u16 *hotkey_keycode_map;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001982
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03001983static struct attribute_set *hotkey_dev_attributes;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001984
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001985/* HKEY.MHKG() return bits */
1986#define TP_HOTKEY_TABLET_MASK (1 << 3)
1987
Johannes Berg19d337d2009-06-02 13:01:37 +02001988static int hotkey_get_wlsw(void)
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001989{
Johannes Berg19d337d2009-06-02 13:01:37 +02001990 int status;
1991
1992 if (!tp_features.hotkey_wlsw)
1993 return -ENODEV;
1994
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001995#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
Johannes Berg19d337d2009-06-02 13:01:37 +02001996 if (dbg_wlswemul)
1997 return (tpacpi_wlsw_emulstate) ?
1998 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001999#endif
Johannes Berg19d337d2009-06-02 13:01:37 +02002000
2001 if (!acpi_evalf(hkey_handle, &status, "WLSW", "d"))
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002002 return -EIO;
Johannes Berg19d337d2009-06-02 13:01:37 +02002003
2004 return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002005}
2006
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002007static int hotkey_get_tablet_mode(int *status)
2008{
2009 int s;
2010
2011 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
2012 return -EIO;
2013
Henrique de Moraes Holschuhcee47f52008-03-04 14:29:21 -08002014 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
2015 return 0;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002016}
2017
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002018/*
2019 * Call with hotkey_mutex held
2020 */
2021static int hotkey_mask_get(void)
2022{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002023 u32 m = 0;
2024
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002025 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002026 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002027 return -EIO;
2028 }
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002029 HOTKEY_CONFIG_CRITICAL_START
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002030 hotkey_mask = m | (hotkey_source_mask & hotkey_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002031 HOTKEY_CONFIG_CRITICAL_END
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002032
2033 return 0;
2034}
2035
2036/*
2037 * Call with hotkey_mutex held
2038 */
2039static int hotkey_mask_set(u32 mask)
2040{
2041 int i;
2042 int rc = 0;
2043
2044 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -03002045 if (!tp_warned.hotkey_mask_ff &&
2046 (mask == 0xffff || mask == 0xffffff ||
2047 mask == 0xffffffff)) {
2048 tp_warned.hotkey_mask_ff = 1;
2049 printk(TPACPI_NOTICE
2050 "setting the hotkey mask to 0x%08x is likely "
2051 "not the best way to go about it\n", mask);
2052 printk(TPACPI_NOTICE
2053 "please consider using the driver defaults, "
2054 "and refer to up-to-date thinkpad-acpi "
2055 "documentation\n");
2056 }
2057
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002058 HOTKEY_CONFIG_CRITICAL_START
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002059 for (i = 0; i < 32; i++) {
2060 u32 m = 1 << i;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002061 /* enable in firmware mask only keys not in NVRAM
2062 * mode, but enable the key in the cached hotkey_mask
2063 * regardless of mode, or the key will end up
2064 * disabled by hotkey_mask_get() */
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002065 if (!acpi_evalf(hkey_handle,
2066 NULL, "MHKM", "vdd", i + 1,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002067 !!((mask & ~hotkey_source_mask) & m))) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002068 rc = -EIO;
2069 break;
2070 } else {
2071 hotkey_mask = (hotkey_mask & ~m) | (mask & m);
2072 }
2073 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002074 HOTKEY_CONFIG_CRITICAL_END
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002075
2076 /* hotkey_mask_get must be called unconditionally below */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002077 if (!hotkey_mask_get() && !rc &&
2078 (hotkey_mask & ~hotkey_source_mask) !=
2079 (mask & ~hotkey_source_mask)) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002080 printk(TPACPI_NOTICE
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002081 "requested hot key mask 0x%08x, but "
2082 "firmware forced it to 0x%08x\n",
2083 mask, hotkey_mask);
2084 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002085 } else {
2086#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2087 HOTKEY_CONFIG_CRITICAL_START
2088 hotkey_mask = mask & hotkey_source_mask;
2089 HOTKEY_CONFIG_CRITICAL_END
2090 hotkey_mask_get();
2091 if (hotkey_mask != mask) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002092 printk(TPACPI_NOTICE
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002093 "requested hot key mask 0x%08x, "
2094 "forced to 0x%08x (NVRAM poll mask is "
2095 "0x%08x): no firmware mask support\n",
2096 mask, hotkey_mask, hotkey_source_mask);
2097 }
2098#else
2099 hotkey_mask_get();
2100 rc = -ENXIO;
2101#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002102 }
2103
2104 return rc;
2105}
2106
2107static int hotkey_status_get(int *status)
2108{
2109 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
2110 return -EIO;
2111
2112 return 0;
2113}
2114
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002115static int hotkey_status_set(bool enable)
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002116{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002117 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002118 return -EIO;
2119
2120 return 0;
2121}
2122
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002123static void tpacpi_input_send_tabletsw(void)
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002124{
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002125 int state;
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002126
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002127 if (tp_features.hotkey_tablet &&
2128 !hotkey_get_tablet_mode(&state)) {
2129 mutex_lock(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002130
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002131 input_report_switch(tpacpi_inputdev,
2132 SW_TABLET_MODE, !!state);
2133 input_sync(tpacpi_inputdev);
2134
2135 mutex_unlock(&tpacpi_inputdev_send_mutex);
2136 }
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002137}
2138
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002139static void tpacpi_input_send_key(unsigned int scancode)
2140{
2141 unsigned int keycode;
2142
2143 keycode = hotkey_keycode_map[scancode];
2144
2145 if (keycode != KEY_RESERVED) {
2146 mutex_lock(&tpacpi_inputdev_send_mutex);
2147
2148 input_report_key(tpacpi_inputdev, keycode, 1);
2149 if (keycode == KEY_UNKNOWN)
2150 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
2151 scancode);
2152 input_sync(tpacpi_inputdev);
2153
2154 input_report_key(tpacpi_inputdev, keycode, 0);
2155 if (keycode == KEY_UNKNOWN)
2156 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
2157 scancode);
2158 input_sync(tpacpi_inputdev);
2159
2160 mutex_unlock(&tpacpi_inputdev_send_mutex);
2161 }
2162}
2163
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002164#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2165static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
2166
2167static void tpacpi_hotkey_send_key(unsigned int scancode)
2168{
2169 tpacpi_input_send_key(scancode);
2170 if (hotkey_report_mode < 2) {
2171 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
2172 0x80, 0x1001 + scancode);
2173 }
2174}
2175
2176static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
2177{
2178 u8 d;
2179
2180 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
2181 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
2182 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
2183 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
2184 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
2185 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
2186 }
2187 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
2188 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
2189 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
2190 }
2191 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
2192 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
2193 n->displayexp_toggle =
2194 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
2195 }
2196 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
2197 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
2198 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
2199 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
2200 n->brightness_toggle =
2201 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
2202 }
2203 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
2204 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
2205 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
2206 >> TP_NVRAM_POS_LEVEL_VOLUME;
2207 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2208 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2209 }
2210}
2211
2212#define TPACPI_COMPARE_KEY(__scancode, __member) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002213 do { \
2214 if ((mask & (1 << __scancode)) && \
2215 oldn->__member != newn->__member) \
2216 tpacpi_hotkey_send_key(__scancode); \
2217 } while (0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002218
2219#define TPACPI_MAY_SEND_KEY(__scancode) \
2220 do { if (mask & (1 << __scancode)) \
2221 tpacpi_hotkey_send_key(__scancode); } while (0)
2222
2223static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002224 struct tp_nvram_state *newn,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002225 u32 mask)
2226{
2227 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2228 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2229 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2230 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2231
2232 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2233
2234 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2235
2236 /* handle volume */
2237 if (oldn->volume_toggle != newn->volume_toggle) {
2238 if (oldn->mute != newn->mute) {
2239 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2240 }
2241 if (oldn->volume_level > newn->volume_level) {
2242 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2243 } else if (oldn->volume_level < newn->volume_level) {
2244 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2245 } else if (oldn->mute == newn->mute) {
2246 /* repeated key presses that didn't change state */
2247 if (newn->mute) {
2248 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2249 } else if (newn->volume_level != 0) {
2250 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2251 } else {
2252 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2253 }
2254 }
2255 }
2256
2257 /* handle brightness */
2258 if (oldn->brightness_toggle != newn->brightness_toggle) {
2259 if (oldn->brightness_level < newn->brightness_level) {
2260 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2261 } else if (oldn->brightness_level > newn->brightness_level) {
2262 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2263 } else {
2264 /* repeated key presses that didn't change state */
2265 if (newn->brightness_level != 0) {
2266 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2267 } else {
2268 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2269 }
2270 }
2271 }
2272}
2273
2274#undef TPACPI_COMPARE_KEY
2275#undef TPACPI_MAY_SEND_KEY
2276
2277static int hotkey_kthread(void *data)
2278{
2279 struct tp_nvram_state s[2];
2280 u32 mask;
2281 unsigned int si, so;
2282 unsigned long t;
2283 unsigned int change_detector, must_reset;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002284 unsigned int poll_freq;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002285
2286 mutex_lock(&hotkey_thread_mutex);
2287
2288 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2289 goto exit;
2290
2291 set_freezable();
2292
2293 so = 0;
2294 si = 1;
2295 t = 0;
2296
2297 /* Initial state for compares */
2298 mutex_lock(&hotkey_thread_data_mutex);
2299 change_detector = hotkey_config_change;
2300 mask = hotkey_source_mask & hotkey_mask;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002301 poll_freq = hotkey_poll_freq;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002302 mutex_unlock(&hotkey_thread_data_mutex);
2303 hotkey_read_nvram(&s[so], mask);
2304
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002305 while (!kthread_should_stop()) {
2306 if (t == 0) {
2307 if (likely(poll_freq))
2308 t = 1000/poll_freq;
2309 else
2310 t = 100; /* should never happen... */
2311 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002312 t = msleep_interruptible(t);
2313 if (unlikely(kthread_should_stop()))
2314 break;
2315 must_reset = try_to_freeze();
2316 if (t > 0 && !must_reset)
2317 continue;
2318
2319 mutex_lock(&hotkey_thread_data_mutex);
2320 if (must_reset || hotkey_config_change != change_detector) {
2321 /* forget old state on thaw or config change */
2322 si = so;
2323 t = 0;
2324 change_detector = hotkey_config_change;
2325 }
2326 mask = hotkey_source_mask & hotkey_mask;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002327 poll_freq = hotkey_poll_freq;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002328 mutex_unlock(&hotkey_thread_data_mutex);
2329
2330 if (likely(mask)) {
2331 hotkey_read_nvram(&s[si], mask);
2332 if (likely(si != so)) {
2333 hotkey_compare_and_issue_event(&s[so], &s[si],
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002334 mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002335 }
2336 }
2337
2338 so = si;
2339 si ^= 1;
2340 }
2341
2342exit:
2343 mutex_unlock(&hotkey_thread_mutex);
2344 return 0;
2345}
2346
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002347/* call with hotkey_mutex held */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002348static void hotkey_poll_stop_sync(void)
2349{
2350 if (tpacpi_hotkey_task) {
2351 if (frozen(tpacpi_hotkey_task) ||
2352 freezing(tpacpi_hotkey_task))
2353 thaw_process(tpacpi_hotkey_task);
2354
2355 kthread_stop(tpacpi_hotkey_task);
2356 tpacpi_hotkey_task = NULL;
2357 mutex_lock(&hotkey_thread_mutex);
2358 /* at this point, the thread did exit */
2359 mutex_unlock(&hotkey_thread_mutex);
2360 }
2361}
2362
2363/* call with hotkey_mutex held */
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002364static void hotkey_poll_setup(bool may_warn)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002365{
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002366 u32 hotkeys_to_poll = hotkey_source_mask & hotkey_mask;
2367
2368 if (hotkeys_to_poll != 0 && hotkey_poll_freq > 0 &&
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002369 (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
2370 if (!tpacpi_hotkey_task) {
2371 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03002372 NULL, TPACPI_NVRAM_KTHREAD_NAME);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002373 if (IS_ERR(tpacpi_hotkey_task)) {
2374 tpacpi_hotkey_task = NULL;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002375 printk(TPACPI_ERR
2376 "could not create kernel thread "
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002377 "for hotkey polling\n");
2378 }
2379 }
2380 } else {
2381 hotkey_poll_stop_sync();
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002382 if (may_warn && hotkeys_to_poll != 0 &&
2383 hotkey_poll_freq == 0) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002384 printk(TPACPI_NOTICE
2385 "hot keys 0x%08x require polling, "
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002386 "which is currently disabled\n",
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002387 hotkeys_to_poll);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002388 }
2389 }
2390}
2391
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002392static void hotkey_poll_setup_safe(bool may_warn)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002393{
2394 mutex_lock(&hotkey_mutex);
2395 hotkey_poll_setup(may_warn);
2396 mutex_unlock(&hotkey_mutex);
2397}
2398
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002399/* call with hotkey_mutex held */
2400static void hotkey_poll_set_freq(unsigned int freq)
2401{
2402 if (!freq)
2403 hotkey_poll_stop_sync();
2404
2405 HOTKEY_CONFIG_CRITICAL_START
2406 hotkey_poll_freq = freq;
2407 HOTKEY_CONFIG_CRITICAL_END
2408}
2409
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02002410#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2411
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002412static void hotkey_poll_setup_safe(bool __unused)
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02002413{
2414}
2415
2416#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2417
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002418static int hotkey_inputdev_open(struct input_dev *dev)
2419{
2420 switch (tpacpi_lifecycle) {
2421 case TPACPI_LIFE_INIT:
2422 /*
2423 * hotkey_init will call hotkey_poll_setup_safe
2424 * at the appropriate moment
2425 */
2426 return 0;
2427 case TPACPI_LIFE_EXITING:
2428 return -EBUSY;
2429 case TPACPI_LIFE_RUNNING:
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002430 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002431 return 0;
2432 }
2433
2434 /* Should only happen if tpacpi_lifecycle is corrupt */
2435 BUG();
2436 return -EBUSY;
2437}
2438
2439static void hotkey_inputdev_close(struct input_dev *dev)
2440{
2441 /* disable hotkey polling when possible */
2442 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002443 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002444}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002445
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002446/* sysfs hotkey enable ------------------------------------------------- */
2447static ssize_t hotkey_enable_show(struct device *dev,
2448 struct device_attribute *attr,
2449 char *buf)
2450{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002451 int res, status;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002452
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002453 printk_deprecated_attribute("hotkey_enable",
2454 "Hotkey reporting is always enabled");
2455
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002456 res = hotkey_status_get(&status);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002457 if (res)
2458 return res;
2459
2460 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2461}
2462
2463static ssize_t hotkey_enable_store(struct device *dev,
2464 struct device_attribute *attr,
2465 const char *buf, size_t count)
2466{
2467 unsigned long t;
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002468
2469 printk_deprecated_attribute("hotkey_enable",
2470 "Hotkeys can be disabled through hotkey_mask");
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002471
2472 if (parse_strtoul(buf, 1, &t))
2473 return -EINVAL;
2474
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002475 if (t == 0)
2476 return -EPERM;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002477
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002478 return count;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002479}
2480
2481static struct device_attribute dev_attr_hotkey_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002482 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002483 hotkey_enable_show, hotkey_enable_store);
2484
2485/* sysfs hotkey mask --------------------------------------------------- */
2486static ssize_t hotkey_mask_show(struct device *dev,
2487 struct device_attribute *attr,
2488 char *buf)
2489{
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002490 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002491
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002492 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002493 return -ERESTARTSYS;
2494 res = hotkey_mask_get();
2495 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002496
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002497 return (res)?
2498 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002499}
2500
2501static ssize_t hotkey_mask_store(struct device *dev,
2502 struct device_attribute *attr,
2503 const char *buf, size_t count)
2504{
2505 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002506 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002507
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002508 if (parse_strtoul(buf, 0xffffffffUL, &t))
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002509 return -EINVAL;
2510
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002511 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002512 return -ERESTARTSYS;
2513
2514 res = hotkey_mask_set(t);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002515
2516#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002517 hotkey_poll_setup(true);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002518#endif
2519
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002520 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002521
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002522 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2523
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002524 return (res) ? res : count;
2525}
2526
2527static struct device_attribute dev_attr_hotkey_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002528 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002529 hotkey_mask_show, hotkey_mask_store);
2530
2531/* sysfs hotkey bios_enabled ------------------------------------------- */
2532static ssize_t hotkey_bios_enabled_show(struct device *dev,
2533 struct device_attribute *attr,
2534 char *buf)
2535{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002536 return sprintf(buf, "0\n");
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002537}
2538
2539static struct device_attribute dev_attr_hotkey_bios_enabled =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002540 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002541
2542/* sysfs hotkey bios_mask ---------------------------------------------- */
2543static ssize_t hotkey_bios_mask_show(struct device *dev,
2544 struct device_attribute *attr,
2545 char *buf)
2546{
Henrique de Moraes Holschuh06777be2009-09-12 15:22:15 -03002547 printk_deprecated_attribute("hotkey_bios_mask",
2548 "This attribute is useless.");
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002549 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002550}
2551
2552static struct device_attribute dev_attr_hotkey_bios_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002553 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002554
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002555/* sysfs hotkey all_mask ----------------------------------------------- */
2556static ssize_t hotkey_all_mask_show(struct device *dev,
2557 struct device_attribute *attr,
2558 char *buf)
2559{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002560 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2561 hotkey_all_mask | hotkey_source_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002562}
2563
2564static struct device_attribute dev_attr_hotkey_all_mask =
2565 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
2566
2567/* sysfs hotkey recommended_mask --------------------------------------- */
2568static ssize_t hotkey_recommended_mask_show(struct device *dev,
2569 struct device_attribute *attr,
2570 char *buf)
2571{
2572 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002573 (hotkey_all_mask | hotkey_source_mask)
2574 & ~hotkey_reserved_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002575}
2576
2577static struct device_attribute dev_attr_hotkey_recommended_mask =
2578 __ATTR(hotkey_recommended_mask, S_IRUGO,
2579 hotkey_recommended_mask_show, NULL);
2580
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002581#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2582
2583/* sysfs hotkey hotkey_source_mask ------------------------------------- */
2584static ssize_t hotkey_source_mask_show(struct device *dev,
2585 struct device_attribute *attr,
2586 char *buf)
2587{
2588 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2589}
2590
2591static ssize_t hotkey_source_mask_store(struct device *dev,
2592 struct device_attribute *attr,
2593 const char *buf, size_t count)
2594{
2595 unsigned long t;
2596
2597 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2598 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2599 return -EINVAL;
2600
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002601 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002602 return -ERESTARTSYS;
2603
2604 HOTKEY_CONFIG_CRITICAL_START
2605 hotkey_source_mask = t;
2606 HOTKEY_CONFIG_CRITICAL_END
2607
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002608 hotkey_poll_setup(true);
2609 hotkey_mask_set(hotkey_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002610
2611 mutex_unlock(&hotkey_mutex);
2612
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002613 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2614
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002615 return count;
2616}
2617
2618static struct device_attribute dev_attr_hotkey_source_mask =
2619 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2620 hotkey_source_mask_show, hotkey_source_mask_store);
2621
2622/* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2623static ssize_t hotkey_poll_freq_show(struct device *dev,
2624 struct device_attribute *attr,
2625 char *buf)
2626{
2627 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2628}
2629
2630static ssize_t hotkey_poll_freq_store(struct device *dev,
2631 struct device_attribute *attr,
2632 const char *buf, size_t count)
2633{
2634 unsigned long t;
2635
2636 if (parse_strtoul(buf, 25, &t))
2637 return -EINVAL;
2638
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002639 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002640 return -ERESTARTSYS;
2641
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002642 hotkey_poll_set_freq(t);
2643 hotkey_poll_setup(true);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002644
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002645 mutex_unlock(&hotkey_mutex);
2646
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002647 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2648
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002649 return count;
2650}
2651
2652static struct device_attribute dev_attr_hotkey_poll_freq =
2653 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2654 hotkey_poll_freq_show, hotkey_poll_freq_store);
2655
2656#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2657
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002658/* sysfs hotkey radio_sw (pollable) ------------------------------------ */
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002659static ssize_t hotkey_radio_sw_show(struct device *dev,
2660 struct device_attribute *attr,
2661 char *buf)
2662{
Johannes Berg19d337d2009-06-02 13:01:37 +02002663 int res;
2664 res = hotkey_get_wlsw();
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002665 if (res < 0)
2666 return res;
2667
Johannes Berg19d337d2009-06-02 13:01:37 +02002668 /* Opportunistic update */
2669 tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
2670
2671 return snprintf(buf, PAGE_SIZE, "%d\n",
2672 (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002673}
2674
2675static struct device_attribute dev_attr_hotkey_radio_sw =
2676 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2677
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002678static void hotkey_radio_sw_notify_change(void)
2679{
2680 if (tp_features.hotkey_wlsw)
2681 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2682 "hotkey_radio_sw");
2683}
2684
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002685/* sysfs hotkey tablet mode (pollable) --------------------------------- */
2686static ssize_t hotkey_tablet_mode_show(struct device *dev,
2687 struct device_attribute *attr,
2688 char *buf)
2689{
2690 int res, s;
2691 res = hotkey_get_tablet_mode(&s);
2692 if (res < 0)
2693 return res;
2694
2695 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2696}
2697
2698static struct device_attribute dev_attr_hotkey_tablet_mode =
2699 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2700
2701static void hotkey_tablet_mode_notify_change(void)
2702{
2703 if (tp_features.hotkey_tablet)
2704 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2705 "hotkey_tablet_mode");
2706}
2707
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002708/* sysfs hotkey report_mode -------------------------------------------- */
2709static ssize_t hotkey_report_mode_show(struct device *dev,
2710 struct device_attribute *attr,
2711 char *buf)
2712{
2713 return snprintf(buf, PAGE_SIZE, "%d\n",
2714 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2715}
2716
2717static struct device_attribute dev_attr_hotkey_report_mode =
2718 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2719
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002720/* sysfs wakeup reason (pollable) -------------------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002721static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2722 struct device_attribute *attr,
2723 char *buf)
2724{
2725 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2726}
2727
2728static struct device_attribute dev_attr_hotkey_wakeup_reason =
2729 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2730
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02002731static void hotkey_wakeup_reason_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002732{
2733 if (tp_features.hotkey_mask)
2734 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2735 "wakeup_reason");
2736}
2737
2738/* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002739static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2740 struct device_attribute *attr,
2741 char *buf)
2742{
2743 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2744}
2745
2746static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2747 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2748 hotkey_wakeup_hotunplug_complete_show, NULL);
2749
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02002750static void hotkey_wakeup_hotunplug_complete_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002751{
2752 if (tp_features.hotkey_mask)
2753 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2754 "wakeup_hotunplug_complete");
2755}
2756
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002757/* --------------------------------------------------------------------- */
2758
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002759static struct attribute *hotkey_attributes[] __initdata = {
2760 &dev_attr_hotkey_enable.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002761 &dev_attr_hotkey_bios_enabled.attr,
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002762 &dev_attr_hotkey_report_mode.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002763#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2764 &dev_attr_hotkey_mask.attr,
2765 &dev_attr_hotkey_all_mask.attr,
2766 &dev_attr_hotkey_recommended_mask.attr,
2767 &dev_attr_hotkey_source_mask.attr,
2768 &dev_attr_hotkey_poll_freq.attr,
2769#endif
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002770};
2771
2772static struct attribute *hotkey_mask_attributes[] __initdata = {
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002773 &dev_attr_hotkey_bios_mask.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002774#ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2775 &dev_attr_hotkey_mask.attr,
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002776 &dev_attr_hotkey_all_mask.attr,
2777 &dev_attr_hotkey_recommended_mask.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002778#endif
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002779 &dev_attr_hotkey_wakeup_reason.attr,
2780 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002781};
2782
Johannes Berg19d337d2009-06-02 13:01:37 +02002783/*
2784 * Sync both the hw and sw blocking state of all switches
2785 */
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002786static void tpacpi_send_radiosw_update(void)
2787{
2788 int wlsw;
2789
Johannes Berg19d337d2009-06-02 13:01:37 +02002790 /*
2791 * We must sync all rfkill controllers *before* issuing any
2792 * rfkill input events, or we will race the rfkill core input
2793 * handler.
2794 *
2795 * tpacpi_inputdev_send_mutex works as a syncronization point
2796 * for the above.
2797 *
2798 * We optimize to avoid numerous calls to hotkey_get_wlsw.
2799 */
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002800
Johannes Berg19d337d2009-06-02 13:01:37 +02002801 wlsw = hotkey_get_wlsw();
2802
2803 /* Sync hw blocking state first if it is hw-blocked */
2804 if (wlsw == TPACPI_RFK_RADIO_OFF)
2805 tpacpi_rfk_update_hwblock_state(true);
2806
2807 /* Sync sw blocking state */
2808 tpacpi_rfk_update_swstate_all();
2809
2810 /* Sync hw blocking state last if it is hw-unblocked */
2811 if (wlsw == TPACPI_RFK_RADIO_ON)
2812 tpacpi_rfk_update_hwblock_state(false);
2813
2814 /* Issue rfkill input event for WLSW switch */
2815 if (!(wlsw < 0)) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002816 mutex_lock(&tpacpi_inputdev_send_mutex);
2817
2818 input_report_switch(tpacpi_inputdev,
Johannes Berg19d337d2009-06-02 13:01:37 +02002819 SW_RFKILL_ALL, (wlsw > 0));
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002820 input_sync(tpacpi_inputdev);
2821
2822 mutex_unlock(&tpacpi_inputdev_send_mutex);
2823 }
Johannes Berg19d337d2009-06-02 13:01:37 +02002824
2825 /*
2826 * this can be unconditional, as we will poll state again
2827 * if userspace uses the notify to read data
2828 */
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002829 hotkey_radio_sw_notify_change();
2830}
2831
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002832static void hotkey_exit(void)
2833{
2834#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002835 mutex_lock(&hotkey_mutex);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002836 hotkey_poll_stop_sync();
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002837 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002838#endif
2839
2840 if (hotkey_dev_attributes)
2841 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2842
2843 kfree(hotkey_keycode_map);
2844
2845 if (tp_features.hotkey) {
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002846 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002847 "restoring original hot key mask\n");
2848 /* no short-circuit boolean operator below! */
2849 if ((hotkey_mask_set(hotkey_orig_mask) |
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002850 hotkey_status_set(false)) != 0)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002851 printk(TPACPI_ERR
2852 "failed to restore hot key mask "
2853 "to BIOS defaults\n");
2854 }
2855}
2856
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002857static int __init hotkey_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002858{
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002859 /* Requirements for changing the default keymaps:
2860 *
2861 * 1. Many of the keys are mapped to KEY_RESERVED for very
2862 * good reasons. Do not change them unless you have deep
2863 * knowledge on the IBM and Lenovo ThinkPad firmware for
2864 * the various ThinkPad models. The driver behaves
2865 * differently for KEY_RESERVED: such keys have their
2866 * hot key mask *unset* in mask_recommended, and also
2867 * in the initial hot key mask programmed into the
2868 * firmware at driver load time, which means the firm-
2869 * ware may react very differently if you change them to
2870 * something else;
2871 *
2872 * 2. You must be subscribed to the linux-thinkpad and
2873 * ibm-acpi-devel mailing lists, and you should read the
2874 * list archives since 2007 if you want to change the
2875 * keymaps. This requirement exists so that you will
2876 * know the past history of problems with the thinkpad-
2877 * acpi driver keymaps, and also that you will be
2878 * listening to any bug reports;
2879 *
2880 * 3. Do not send thinkpad-acpi specific patches directly to
2881 * for merging, *ever*. Send them to the linux-acpi
2882 * mailinglist for comments. Merging is to be done only
2883 * through acpi-test and the ACPI maintainer.
2884 *
2885 * If the above is too much to ask, don't change the keymap.
2886 * Ask the thinkpad-acpi maintainer to do it, instead.
2887 */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002888 static u16 ibm_keycode_map[] __initdata = {
2889 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2890 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
2891 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2892 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002893
2894 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002895 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2896 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2897 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002898
2899 /* brightness: firmware always reacts to them, unless
2900 * X.org did some tricks in the radeon BIOS scratch
2901 * registers of *some* models */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02002902 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02002903 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002904
2905 /* Thinklight: firmware always react to it */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002906 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002907
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002908 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2909 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002910
2911 /* Volume: firmware always react to it and reprograms
2912 * the built-in *extra* mixer. Never map it to control
2913 * another mixer by default. */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02002914 KEY_RESERVED, /* 0x14: VOLUME UP */
2915 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2916 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002917
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002918 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002919
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002920 /* (assignments unknown, please report if found) */
2921 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2922 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2923 };
2924 static u16 lenovo_keycode_map[] __initdata = {
2925 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2926 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
2927 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2928 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002929
2930 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002931 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2932 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2933 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002934
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03002935 /* These either have to go through ACPI video, or
2936 * act like in the IBM ThinkPads, so don't ever
2937 * enable them by default */
Henrique de Moraes Holschuh56a185b2007-12-13 12:14:09 -02002938 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuh56a185b2007-12-13 12:14:09 -02002939 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002940
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002941 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002942
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002943 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2944 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002945
2946 /* Volume: z60/z61, T60 (BIOS version?): firmware always
2947 * react to it and reprograms the built-in *extra* mixer.
2948 * Never map it to control another mixer by default.
2949 *
2950 * T60?, T61, R60?, R61: firmware and EC tries to send
2951 * these over the regular keyboard, so these are no-ops,
2952 * but there are still weird bugs re. MUTE, so do not
2953 * change unless you get test reports from all Lenovo
2954 * models. May cause the BIOS to interfere with the
2955 * HDA mixer.
2956 */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02002957 KEY_RESERVED, /* 0x14: VOLUME UP */
2958 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2959 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002960
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002961 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002962
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002963 /* (assignments unknown, please report if found) */
2964 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2965 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2966 };
2967
2968#define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
2969#define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
2970#define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
2971
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002972 int res, i;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002973 int status;
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03002974 int hkeyv;
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03002975
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002976 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2977 "initializing hotkey subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002978
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002979 BUG_ON(!tpacpi_inputdev);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002980 BUG_ON(tpacpi_inputdev->open != NULL ||
2981 tpacpi_inputdev->close != NULL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002982
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002983 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002984 mutex_init(&hotkey_mutex);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002985
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002986#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2987 mutex_init(&hotkey_thread_mutex);
2988 mutex_init(&hotkey_thread_data_mutex);
2989#endif
2990
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002991 /* hotkey not supported on 570 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002992 tp_features.hotkey = hkey_handle != NULL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002993
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002994 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2995 "hotkeys are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002996 str_supported(tp_features.hotkey));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002997
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002998 if (!tp_features.hotkey)
2999 return 1;
3000
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03003001 tpacpi_disable_brightness_delay();
3002
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003003 hotkey_dev_attributes = create_attr_set(13, NULL);
3004 if (!hotkey_dev_attributes)
3005 return -ENOMEM;
3006 res = add_many_to_attr_set(hotkey_dev_attributes,
3007 hotkey_attributes,
3008 ARRAY_SIZE(hotkey_attributes));
3009 if (res)
3010 goto err_exit;
3011
3012 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3013 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
3014 for HKEY interface version 0x100 */
3015 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
3016 if ((hkeyv >> 8) != 1) {
3017 printk(TPACPI_ERR "unknown version of the "
3018 "HKEY interface: 0x%x\n", hkeyv);
3019 printk(TPACPI_ERR "please report this to %s\n",
3020 TPACPI_MAIL);
3021 } else {
3022 /*
3023 * MHKV 0x100 in A31, R40, R40e,
3024 * T4x, X31, and later
3025 */
3026 tp_features.hotkey_mask = 1;
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003027 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3028 "firmware HKEY interface version: 0x%x\n",
3029 hkeyv);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003030 }
3031 }
3032
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003033 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3034 "hotkey masks are %s\n",
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003035 str_supported(tp_features.hotkey_mask));
3036
3037 if (tp_features.hotkey_mask) {
3038 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3039 "MHKA", "qd")) {
3040 printk(TPACPI_ERR
3041 "missing MHKA handler, "
3042 "please report this to %s\n",
3043 TPACPI_MAIL);
3044 /* FN+F12, FN+F4, FN+F3 */
3045 hotkey_all_mask = 0x080cU;
3046 }
3047 }
3048
3049 /* hotkey_source_mask *must* be zero for
3050 * the first hotkey_mask_get */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003051 if (tp_features.hotkey_mask) {
3052 res = hotkey_mask_get();
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003053 if (res)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003054 goto err_exit;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003055
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003056 hotkey_orig_mask = hotkey_mask;
3057 res = add_many_to_attr_set(
3058 hotkey_dev_attributes,
3059 hotkey_mask_attributes,
3060 ARRAY_SIZE(hotkey_mask_attributes));
3061 if (res)
3062 goto err_exit;
3063 }
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003064
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003065#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003066 if (tp_features.hotkey_mask) {
3067 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3068 & ~hotkey_all_mask;
3069 } else {
3070 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK;
3071 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003072
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003073 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003074 "hotkey source mask 0x%08x, polling freq %u\n",
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003075 hotkey_source_mask, hotkey_poll_freq);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003076#endif
3077
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003078#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3079 if (dbg_wlswemul) {
3080 tp_features.hotkey_wlsw = 1;
3081 printk(TPACPI_INFO
3082 "radio switch emulation enabled\n");
3083 } else
3084#endif
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003085 /* Not all thinkpads have a hardware radio switch */
3086 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
3087 tp_features.hotkey_wlsw = 1;
3088 printk(TPACPI_INFO
3089 "radio switch found; radios are %s\n",
3090 enabled(status, 0));
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003091 }
3092 if (tp_features.hotkey_wlsw)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003093 res = add_to_attr_set(hotkey_dev_attributes,
3094 &dev_attr_hotkey_radio_sw.attr);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003095
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003096 /* For X41t, X60t, X61t Tablets... */
3097 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
3098 tp_features.hotkey_tablet = 1;
3099 printk(TPACPI_INFO
3100 "possible tablet mode switch found; "
3101 "ThinkPad in %s mode\n",
3102 (status & TP_HOTKEY_TABLET_MASK)?
3103 "tablet" : "laptop");
3104 res = add_to_attr_set(hotkey_dev_attributes,
3105 &dev_attr_hotkey_tablet_mode.attr);
3106 }
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02003107
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003108 if (!res)
3109 res = register_attr_set_with_sysfs(
3110 hotkey_dev_attributes,
3111 &tpacpi_pdev->dev.kobj);
3112 if (res)
3113 goto err_exit;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003114
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003115 /* Set up key map */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003116
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003117 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
3118 GFP_KERNEL);
3119 if (!hotkey_keycode_map) {
3120 printk(TPACPI_ERR
3121 "failed to allocate memory for key map\n");
3122 res = -ENOMEM;
3123 goto err_exit;
3124 }
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003125
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003126 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
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 Lenovo default hot key map\n");
3129 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
3130 TPACPI_HOTKEY_MAP_SIZE);
3131 } else {
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003132 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003133 "using IBM default hot key map\n");
3134 memcpy(hotkey_keycode_map, &ibm_keycode_map,
3135 TPACPI_HOTKEY_MAP_SIZE);
3136 }
3137
3138 set_bit(EV_KEY, tpacpi_inputdev->evbit);
3139 set_bit(EV_MSC, tpacpi_inputdev->evbit);
3140 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
3141 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
3142 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3143 tpacpi_inputdev->keycode = hotkey_keycode_map;
3144 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3145 if (hotkey_keycode_map[i] != KEY_RESERVED) {
3146 set_bit(hotkey_keycode_map[i],
3147 tpacpi_inputdev->keybit);
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003148 } else {
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003149 if (i < sizeof(hotkey_reserved_mask)*8)
3150 hotkey_reserved_mask |= 1 << i;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003151 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003152 }
3153
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003154 if (tp_features.hotkey_wlsw) {
3155 set_bit(EV_SW, tpacpi_inputdev->evbit);
3156 set_bit(SW_RFKILL_ALL, tpacpi_inputdev->swbit);
3157 }
3158 if (tp_features.hotkey_tablet) {
3159 set_bit(EV_SW, tpacpi_inputdev->evbit);
3160 set_bit(SW_TABLET_MODE, tpacpi_inputdev->swbit);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003161 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003162
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003163 /* Do not issue duplicate brightness change events to
3164 * userspace */
3165 if (!tp_features.bright_acpimode)
3166 /* update bright_acpimode... */
3167 tpacpi_check_std_acpi_brightness_support();
3168
Henrique de Moraes Holschuh8bf3d4c2009-05-30 13:25:09 -03003169 if (tp_features.bright_acpimode && acpi_video_backlight_support()) {
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003170 printk(TPACPI_INFO
3171 "This ThinkPad has standard ACPI backlight "
3172 "brightness control, supported by the ACPI "
3173 "video driver\n");
3174 printk(TPACPI_NOTICE
3175 "Disabling thinkpad-acpi brightness events "
3176 "by default...\n");
3177
3178 /* The hotkey_reserved_mask change below is not
3179 * necessary while the keys are at KEY_RESERVED in the
3180 * default map, but better safe than sorry, leave it
3181 * here as a marker of what we have to do, especially
3182 * when we finally become able to set this at runtime
3183 * on response to X.org requests */
3184 hotkey_reserved_mask |=
3185 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3186 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003187 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003188
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003189 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3190 "enabling firmware HKEY event interface...\n");
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003191 res = hotkey_status_set(true);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003192 if (res) {
3193 hotkey_exit();
3194 return res;
3195 }
3196 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
3197 & ~hotkey_reserved_mask)
3198 | hotkey_orig_mask);
3199 if (res < 0 && res != -ENXIO) {
3200 hotkey_exit();
3201 return res;
3202 }
3203
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003204 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3205 "legacy ibm/hotkey event reporting over procfs %s\n",
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003206 (hotkey_report_mode < 2) ?
3207 "enabled" : "disabled");
3208
3209 tpacpi_inputdev->open = &hotkey_inputdev_open;
3210 tpacpi_inputdev->close = &hotkey_inputdev_close;
3211
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003212 hotkey_poll_setup_safe(true);
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003213 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003214 tpacpi_input_send_tabletsw();
3215
3216 return 0;
3217
3218err_exit:
3219 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3220 hotkey_dev_attributes = NULL;
3221
3222 return (res < 0)? res : 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003223}
3224
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003225static bool hotkey_notify_hotkey(const u32 hkey,
3226 bool *send_acpi_ev,
3227 bool *ignore_acpi_ev)
3228{
3229 /* 0x1000-0x1FFF: key presses */
3230 unsigned int scancode = hkey & 0xfff;
3231 *send_acpi_ev = true;
3232 *ignore_acpi_ev = false;
3233
3234 if (scancode > 0 && scancode < 0x21) {
3235 scancode--;
3236 if (!(hotkey_source_mask & (1 << scancode))) {
3237 tpacpi_input_send_key(scancode);
3238 *send_acpi_ev = false;
3239 } else {
3240 *ignore_acpi_ev = true;
3241 }
3242 return true;
3243 }
3244 return false;
3245}
3246
3247static bool hotkey_notify_wakeup(const u32 hkey,
3248 bool *send_acpi_ev,
3249 bool *ignore_acpi_ev)
3250{
3251 /* 0x2000-0x2FFF: Wakeup reason */
3252 *send_acpi_ev = true;
3253 *ignore_acpi_ev = false;
3254
3255 switch (hkey) {
3256 case 0x2304: /* suspend, undock */
3257 case 0x2404: /* hibernation, undock */
3258 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3259 *ignore_acpi_ev = true;
3260 break;
3261
3262 case 0x2305: /* suspend, bay eject */
3263 case 0x2405: /* hibernation, bay eject */
3264 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3265 *ignore_acpi_ev = true;
3266 break;
3267
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003268 case 0x2313: /* Battery on critical low level (S3) */
3269 case 0x2413: /* Battery on critical low level (S4) */
3270 printk(TPACPI_ALERT
3271 "EMERGENCY WAKEUP: battery almost empty\n");
3272 /* how to auto-heal: */
3273 /* 2313: woke up from S3, go to S4/S5 */
3274 /* 2413: woke up from S4, go to S5 */
3275 break;
3276
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003277 default:
3278 return false;
3279 }
3280
3281 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
3282 printk(TPACPI_INFO
3283 "woke up due to a hot-unplug "
3284 "request...\n");
3285 hotkey_wakeup_reason_notify_change();
3286 }
3287 return true;
3288}
3289
3290static bool hotkey_notify_usrevent(const u32 hkey,
3291 bool *send_acpi_ev,
3292 bool *ignore_acpi_ev)
3293{
3294 /* 0x5000-0x5FFF: human interface helpers */
3295 *send_acpi_ev = true;
3296 *ignore_acpi_ev = false;
3297
3298 switch (hkey) {
3299 case 0x5010: /* Lenovo new BIOS: brightness changed */
3300 case 0x500b: /* X61t: tablet pen inserted into bay */
3301 case 0x500c: /* X61t: tablet pen removed from bay */
3302 return true;
3303
3304 case 0x5009: /* X41t-X61t: swivel up (tablet mode) */
3305 case 0x500a: /* X41t-X61t: swivel down (normal mode) */
3306 tpacpi_input_send_tabletsw();
3307 hotkey_tablet_mode_notify_change();
3308 *send_acpi_ev = false;
3309 return true;
3310
3311 case 0x5001:
3312 case 0x5002:
3313 /* LID switch events. Do not propagate */
3314 *ignore_acpi_ev = true;
3315 return true;
3316
3317 default:
3318 return false;
3319 }
3320}
3321
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003322static bool hotkey_notify_thermal(const u32 hkey,
3323 bool *send_acpi_ev,
3324 bool *ignore_acpi_ev)
3325{
3326 /* 0x6000-0x6FFF: thermal alarms */
3327 *send_acpi_ev = true;
3328 *ignore_acpi_ev = false;
3329
3330 switch (hkey) {
3331 case 0x6011:
3332 printk(TPACPI_CRIT
3333 "THERMAL ALARM: battery is too hot!\n");
3334 /* recommended action: warn user through gui */
3335 return true;
3336 case 0x6012:
3337 printk(TPACPI_ALERT
3338 "THERMAL EMERGENCY: battery is extremely hot!\n");
3339 /* recommended action: immediate sleep/hibernate */
3340 return true;
3341 case 0x6021:
3342 printk(TPACPI_CRIT
3343 "THERMAL ALARM: "
3344 "a sensor reports something is too hot!\n");
3345 /* recommended action: warn user through gui, that */
3346 /* some internal component is too hot */
3347 return true;
3348 case 0x6022:
3349 printk(TPACPI_ALERT
3350 "THERMAL EMERGENCY: "
3351 "a sensor reports something is extremely hot!\n");
3352 /* recommended action: immediate sleep/hibernate */
3353 return true;
Henrique de Moraes Holschuh54926ce2009-01-11 03:01:09 -02003354 case 0x6030:
3355 printk(TPACPI_INFO
3356 "EC reports that Thermal Table has changed\n");
3357 /* recommended action: do nothing, we don't have
3358 * Lenovo ATM information */
3359 return true;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003360 default:
3361 printk(TPACPI_ALERT
3362 "THERMAL ALERT: unknown thermal alarm received\n");
3363 return false;
3364 }
3365}
3366
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003367static void hotkey_notify(struct ibm_struct *ibm, u32 event)
3368{
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003369 u32 hkey;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003370 bool send_acpi_ev;
3371 bool ignore_acpi_ev;
3372 bool known_ev;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003373
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003374 if (event != 0x80) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003375 printk(TPACPI_ERR
3376 "unknown HKEY notification event %d\n", event);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003377 /* forward it to userspace, maybe it knows how to handle it */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003378 acpi_bus_generate_netlink_event(
3379 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02003380 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003381 event, 0);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003382 return;
3383 }
3384
3385 while (1) {
3386 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003387 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003388 return;
3389 }
3390
3391 if (hkey == 0) {
3392 /* queue empty */
3393 return;
3394 }
3395
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003396 send_acpi_ev = true;
3397 ignore_acpi_ev = false;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003398
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003399 switch (hkey >> 12) {
3400 case 1:
3401 /* 0x1000-0x1FFF: key presses */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003402 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
3403 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003404 break;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003405 case 2:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003406 /* 0x2000-0x2FFF: Wakeup reason */
3407 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
3408 &ignore_acpi_ev);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003409 break;
3410 case 3:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003411 /* 0x3000-0x3FFF: bay-related wakeups */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003412 if (hkey == 0x3003) {
3413 hotkey_autosleep_ack = 1;
3414 printk(TPACPI_INFO
3415 "bay ejected\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003416 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003417 known_ev = true;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003418 } else {
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003419 known_ev = false;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003420 }
3421 break;
3422 case 4:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003423 /* 0x4000-0x4FFF: dock-related wakeups */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003424 if (hkey == 0x4003) {
3425 hotkey_autosleep_ack = 1;
3426 printk(TPACPI_INFO
3427 "undocked\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003428 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003429 known_ev = true;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003430 } else {
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003431 known_ev = false;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003432 }
3433 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003434 case 5:
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02003435 /* 0x5000-0x5FFF: human interface helpers */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003436 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
3437 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003438 break;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003439 case 6:
3440 /* 0x6000-0x6FFF: thermal alarms */
3441 known_ev = hotkey_notify_thermal(hkey, &send_acpi_ev,
3442 &ignore_acpi_ev);
3443 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003444 case 7:
3445 /* 0x7000-0x7FFF: misc */
3446 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003447 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02003448 send_acpi_ev = 0;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003449 known_ev = true;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003450 break;
3451 }
3452 /* fallthrough to default */
3453 default:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003454 known_ev = false;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02003455 }
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003456 if (!known_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003457 printk(TPACPI_NOTICE
3458 "unhandled HKEY event 0x%04x\n", hkey);
Henrique de Moraes Holschuhcb429352009-01-11 03:01:07 -02003459 printk(TPACPI_NOTICE
3460 "please report the conditions when this "
3461 "event happened to %s\n", TPACPI_MAIL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003462 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003463
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003464 /* Legacy events */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003465 if (!ignore_acpi_ev &&
3466 (send_acpi_ev || hotkey_report_mode < 2)) {
3467 acpi_bus_generate_proc_event(ibm->acpi->device,
3468 event, hkey);
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03003469 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003470
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003471 /* netlink events */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03003472 if (!ignore_acpi_ev && send_acpi_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003473 acpi_bus_generate_netlink_event(
3474 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02003475 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003476 event, hkey);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003477 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003478 }
3479}
3480
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003481static void hotkey_suspend(pm_message_t state)
3482{
3483 /* Do these on suspend, we get the events on early resume! */
3484 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
3485 hotkey_autosleep_ack = 0;
3486}
3487
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03003488static void hotkey_resume(void)
3489{
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03003490 tpacpi_disable_brightness_delay();
3491
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02003492 if (hotkey_mask_get())
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003493 printk(TPACPI_ERR
3494 "error while trying to read hot key mask "
3495 "from firmware\n");
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003496 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02003497 hotkey_tablet_mode_notify_change();
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003498 hotkey_wakeup_reason_notify_change();
3499 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003500 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03003501}
3502
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003503/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003504static int hotkey_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03003506 int res, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507 int len = 0;
3508
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003509 if (!tp_features.hotkey) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003510 len += sprintf(p + len, "status:\t\tnot supported\n");
3511 return len;
3512 }
3513
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02003514 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02003515 return -ERESTARTSYS;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02003516 res = hotkey_status_get(&status);
3517 if (!res)
3518 res = hotkey_mask_get();
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003519 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03003520 if (res)
3521 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522
3523 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003524 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02003525 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526 len += sprintf(p + len,
3527 "commands:\tenable, disable, reset, <mask>\n");
3528 } else {
3529 len += sprintf(p + len, "mask:\t\tnot supported\n");
3530 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
3531 }
3532
3533 return len;
3534}
3535
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003536static void hotkey_enabledisable_warn(bool enable)
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003537{
3538 tpacpi_log_usertask("procfs hotkey enable/disable");
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003539 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
3540 TPACPI_WARN
3541 "hotkey enable/disable functionality has been "
3542 "removed from the driver. Hotkeys are always "
3543 "enabled\n"))
3544 printk(TPACPI_ERR
3545 "Please remove the hotkey=enable module "
3546 "parameter, it is deprecated. Hotkeys are always "
3547 "enabled\n");
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003548}
3549
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003550static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003552 int res;
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03003553 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003556 if (!tp_features.hotkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557 return -ENODEV;
3558
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02003559 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02003560 return -ERESTARTSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003561
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02003562 mask = hotkey_mask;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003563
3564 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565 while ((cmd = next_cmd(&buf))) {
3566 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003567 hotkey_enabledisable_warn(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003569 hotkey_enabledisable_warn(0);
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003570 res = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571 } else if (strlencmp(cmd, "reset") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003572 mask = hotkey_orig_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
3574 /* mask set */
3575 } else if (sscanf(cmd, "%x", &mask) == 1) {
3576 /* mask set */
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003577 } else {
3578 res = -EINVAL;
3579 goto errexit;
3580 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581 }
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003582
3583 if (!res)
3584 tpacpi_disclose_usertask("procfs hotkey",
3585 "set mask to 0x%08x\n", mask);
3586
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02003587 if (!res && mask != hotkey_mask)
3588 res = hotkey_mask_set(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003590errexit:
3591 mutex_unlock(&hotkey_mutex);
3592 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003593}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003595static const struct acpi_device_id ibm_htk_device_ids[] = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003596 {TPACPI_ACPI_HKEY_HID, 0},
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003597 {"", 0},
3598};
3599
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003600static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003601 .hid = ibm_htk_device_ids,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003602 .notify = hotkey_notify,
3603 .handle = &hkey_handle,
3604 .type = ACPI_DEVICE_NOTIFY,
3605};
3606
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003607static struct ibm_struct hotkey_driver_data = {
3608 .name = "hotkey",
3609 .read = hotkey_read,
3610 .write = hotkey_write,
3611 .exit = hotkey_exit,
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03003612 .resume = hotkey_resume,
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003613 .suspend = hotkey_suspend,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003614 .acpi = &ibm_hotkey_acpidriver,
3615};
3616
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003617/*************************************************************************
3618 * Bluetooth subdriver
3619 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003621enum {
3622 /* ACPI GBDC/SBDC bits */
3623 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
3624 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003625 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
3626 off / last state */
3627};
3628
3629enum {
3630 /* ACPI \BLTH commands */
3631 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
3632 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
3633 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
3634 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
3635 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003636};
3637
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003638#define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
3639
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003640static void bluetooth_suspend(pm_message_t state)
3641{
3642 /* Try to make sure radio will resume powered off */
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003643 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3644 TP_ACPI_BLTH_PWR_OFF_ON_RESUME))
3645 vdbg_printk(TPACPI_DBG_RFKILL,
3646 "bluetooth power down on resume request failed\n");
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003647}
3648
Johannes Berg19d337d2009-06-02 13:01:37 +02003649static int bluetooth_get_status(void)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003650{
3651 int status;
3652
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003653#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3654 if (dbg_bluetoothemul)
3655 return (tpacpi_bluetooth_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02003656 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003657#endif
3658
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003659 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
3660 return -EIO;
3661
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003662 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02003663 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003664}
3665
Johannes Berg19d337d2009-06-02 13:01:37 +02003666static int bluetooth_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003667{
3668 int status;
3669
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003670 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02003671 "will attempt to %s bluetooth\n",
3672 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003673
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003674#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3675 if (dbg_bluetoothemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02003676 tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003677 return 0;
3678 }
3679#endif
3680
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003681 /* We make sure to keep TP_ACPI_BLUETOOTH_RESUMECTRL off */
Johannes Berg19d337d2009-06-02 13:01:37 +02003682 if (state == TPACPI_RFK_RADIO_ON)
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003683 status = TP_ACPI_BLUETOOTH_RADIOSSW;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003684 else
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003685 status = 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02003686
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003687 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
3688 return -EIO;
3689
3690 return 0;
3691}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003692
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003693/* sysfs bluetooth enable ---------------------------------------------- */
3694static ssize_t bluetooth_enable_show(struct device *dev,
3695 struct device_attribute *attr,
3696 char *buf)
3697{
Johannes Berg19d337d2009-06-02 13:01:37 +02003698 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID,
3699 attr, buf);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003700}
3701
3702static ssize_t bluetooth_enable_store(struct device *dev,
3703 struct device_attribute *attr,
3704 const char *buf, size_t count)
3705{
Johannes Berg19d337d2009-06-02 13:01:37 +02003706 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID,
3707 attr, buf, count);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003708}
3709
3710static struct device_attribute dev_attr_bluetooth_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03003711 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003712 bluetooth_enable_show, bluetooth_enable_store);
3713
3714/* --------------------------------------------------------------------- */
3715
3716static struct attribute *bluetooth_attributes[] = {
3717 &dev_attr_bluetooth_enable.attr,
3718 NULL
3719};
3720
3721static const struct attribute_group bluetooth_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003722 .attrs = bluetooth_attributes,
3723};
3724
Johannes Berg19d337d2009-06-02 13:01:37 +02003725static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = {
3726 .get_status = bluetooth_get_status,
3727 .set_status = bluetooth_set_status,
3728};
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003729
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003730static void bluetooth_shutdown(void)
3731{
3732 /* Order firmware to save current state to NVRAM */
3733 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3734 TP_ACPI_BLTH_SAVE_STATE))
3735 printk(TPACPI_NOTICE
3736 "failed to save bluetooth state to NVRAM\n");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003737 else
3738 vdbg_printk(TPACPI_DBG_RFKILL,
3739 "bluestooth state saved to NVRAM\n");
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003740}
3741
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003742static void bluetooth_exit(void)
3743{
3744 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3745 &bluetooth_attr_group);
Johannes Berg19d337d2009-06-02 13:01:37 +02003746
3747 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
3748
3749 bluetooth_shutdown();
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003750}
3751
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003752static int __init bluetooth_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003754 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003755 int status = 0;
3756
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003757 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3758 "initializing bluetooth subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003759
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003760 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003761
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003762 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3763 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003764 tp_features.bluetooth = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003765 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003767 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3768 "bluetooth is %s, status 0x%02x\n",
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003769 str_supported(tp_features.bluetooth),
3770 status);
3771
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003772#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3773 if (dbg_bluetoothemul) {
3774 tp_features.bluetooth = 1;
3775 printk(TPACPI_INFO
3776 "bluetooth switch emulation enabled\n");
3777 } else
3778#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003779 if (tp_features.bluetooth &&
3780 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
3781 /* no bluetooth hardware present in system */
3782 tp_features.bluetooth = 0;
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003783 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003784 "bluetooth hardware not installed\n");
3785 }
3786
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003787 if (!tp_features.bluetooth)
3788 return 1;
3789
Johannes Berg19d337d2009-06-02 13:01:37 +02003790 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
3791 &bluetooth_tprfk_ops,
3792 RFKILL_TYPE_BLUETOOTH,
3793 TPACPI_RFK_BLUETOOTH_SW_NAME,
3794 true);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003795 if (res)
3796 return res;
3797
Johannes Berg19d337d2009-06-02 13:01:37 +02003798 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3799 &bluetooth_attr_group);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003800 if (res) {
Johannes Berg19d337d2009-06-02 13:01:37 +02003801 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003802 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003803 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003804
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003805 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806}
3807
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003808/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003809static int bluetooth_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810{
Johannes Berg19d337d2009-06-02 13:01:37 +02003811 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812}
3813
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003814static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815{
Johannes Berg19d337d2009-06-02 13:01:37 +02003816 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817}
3818
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003819static struct ibm_struct bluetooth_driver_data = {
3820 .name = "bluetooth",
3821 .read = bluetooth_read,
3822 .write = bluetooth_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003823 .exit = bluetooth_exit,
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003824 .suspend = bluetooth_suspend,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003825 .shutdown = bluetooth_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003826};
3827
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003828/*************************************************************************
3829 * Wan subdriver
3830 */
3831
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003832enum {
3833 /* ACPI GWAN/SWAN bits */
3834 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
3835 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003836 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
3837 off / last state */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003838};
3839
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003840#define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
3841
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003842static void wan_suspend(pm_message_t state)
3843{
3844 /* Try to make sure radio will resume powered off */
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003845 if (!acpi_evalf(NULL, NULL, "\\WGSV", "qvd",
3846 TP_ACPI_WGSV_PWR_OFF_ON_RESUME))
3847 vdbg_printk(TPACPI_DBG_RFKILL,
3848 "WWAN power down on resume request failed\n");
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003849}
3850
Johannes Berg19d337d2009-06-02 13:01:37 +02003851static int wan_get_status(void)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003852{
3853 int status;
3854
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003855#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3856 if (dbg_wwanemul)
3857 return (tpacpi_wwan_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02003858 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003859#endif
3860
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003861 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
3862 return -EIO;
3863
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003864 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02003865 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003866}
3867
Johannes Berg19d337d2009-06-02 13:01:37 +02003868static int wan_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003869{
3870 int status;
3871
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003872 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02003873 "will attempt to %s wwan\n",
3874 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003875
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003876#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3877 if (dbg_wwanemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02003878 tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003879 return 0;
3880 }
3881#endif
3882
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003883 /* We make sure to keep TP_ACPI_WANCARD_RESUMECTRL off */
Johannes Berg19d337d2009-06-02 13:01:37 +02003884 if (state == TPACPI_RFK_RADIO_ON)
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003885 status = TP_ACPI_WANCARD_RADIOSSW;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003886 else
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003887 status = 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02003888
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003889 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
3890 return -EIO;
3891
3892 return 0;
3893}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003894
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003895/* sysfs wan enable ---------------------------------------------------- */
3896static ssize_t wan_enable_show(struct device *dev,
3897 struct device_attribute *attr,
3898 char *buf)
3899{
Johannes Berg19d337d2009-06-02 13:01:37 +02003900 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
3901 attr, buf);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003902}
3903
3904static ssize_t wan_enable_store(struct device *dev,
3905 struct device_attribute *attr,
3906 const char *buf, size_t count)
3907{
Johannes Berg19d337d2009-06-02 13:01:37 +02003908 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
3909 attr, buf, count);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003910}
3911
3912static struct device_attribute dev_attr_wan_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03003913 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003914 wan_enable_show, wan_enable_store);
3915
3916/* --------------------------------------------------------------------- */
3917
3918static struct attribute *wan_attributes[] = {
3919 &dev_attr_wan_enable.attr,
3920 NULL
3921};
3922
3923static const struct attribute_group wan_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003924 .attrs = wan_attributes,
3925};
3926
Johannes Berg19d337d2009-06-02 13:01:37 +02003927static const struct tpacpi_rfk_ops wan_tprfk_ops = {
3928 .get_status = wan_get_status,
3929 .set_status = wan_set_status,
3930};
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003931
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003932static void wan_shutdown(void)
3933{
3934 /* Order firmware to save current state to NVRAM */
3935 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
3936 TP_ACPI_WGSV_SAVE_STATE))
3937 printk(TPACPI_NOTICE
3938 "failed to save WWAN state to NVRAM\n");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003939 else
3940 vdbg_printk(TPACPI_DBG_RFKILL,
3941 "WWAN state saved to NVRAM\n");
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003942}
3943
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003944static void wan_exit(void)
3945{
3946 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3947 &wan_attr_group);
Johannes Berg19d337d2009-06-02 13:01:37 +02003948
3949 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
3950
3951 wan_shutdown();
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003952}
3953
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003954static int __init wan_init(struct ibm_init_struct *iibm)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003955{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003956 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003957 int status = 0;
3958
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003959 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3960 "initializing wan subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003961
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003962 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003963
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003964 tp_features.wan = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003965 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003966
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003967 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3968 "wan is %s, status 0x%02x\n",
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003969 str_supported(tp_features.wan),
3970 status);
3971
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003972#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3973 if (dbg_wwanemul) {
3974 tp_features.wan = 1;
3975 printk(TPACPI_INFO
3976 "wwan switch emulation enabled\n");
3977 } else
3978#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003979 if (tp_features.wan &&
3980 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
3981 /* no wan hardware present in system */
3982 tp_features.wan = 0;
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003983 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003984 "wan hardware not installed\n");
3985 }
3986
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003987 if (!tp_features.wan)
3988 return 1;
3989
Johannes Berg19d337d2009-06-02 13:01:37 +02003990 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
3991 &wan_tprfk_ops,
3992 RFKILL_TYPE_WWAN,
3993 TPACPI_RFK_WWAN_SW_NAME,
3994 true);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003995 if (res)
3996 return res;
3997
Johannes Berg19d337d2009-06-02 13:01:37 +02003998 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3999 &wan_attr_group);
4000
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004001 if (res) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004002 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004003 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004004 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004005
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004006 return 0;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004007}
4008
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004009/* procfs -------------------------------------------------------------- */
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004010static int wan_read(char *p)
4011{
Johannes Berg19d337d2009-06-02 13:01:37 +02004012 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, p);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004013}
4014
4015static int wan_write(char *buf)
4016{
Johannes Berg19d337d2009-06-02 13:01:37 +02004017 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004018}
4019
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004020static struct ibm_struct wan_driver_data = {
4021 .name = "wan",
4022 .read = wan_read,
4023 .write = wan_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004024 .exit = wan_exit,
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02004025 .suspend = wan_suspend,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004026 .shutdown = wan_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004027};
4028
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004029/*************************************************************************
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004030 * UWB subdriver
4031 */
4032
4033enum {
4034 /* ACPI GUWB/SUWB bits */
4035 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4036 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4037};
4038
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004039#define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4040
Johannes Berg19d337d2009-06-02 13:01:37 +02004041static int uwb_get_status(void)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004042{
4043 int status;
4044
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004045#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4046 if (dbg_uwbemul)
4047 return (tpacpi_uwb_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004048 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004049#endif
4050
4051 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4052 return -EIO;
4053
4054 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004055 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004056}
4057
Johannes Berg19d337d2009-06-02 13:01:37 +02004058static int uwb_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004059{
4060 int status;
4061
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004062 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02004063 "will attempt to %s UWB\n",
4064 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004065
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004066#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4067 if (dbg_uwbemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004068 tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004069 return 0;
4070 }
4071#endif
4072
Johannes Berg19d337d2009-06-02 13:01:37 +02004073 if (state == TPACPI_RFK_RADIO_ON)
4074 status = TP_ACPI_UWB_RADIOSSW;
4075 else
4076 status = 0;
4077
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004078 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4079 return -EIO;
4080
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004081 return 0;
4082}
4083
4084/* --------------------------------------------------------------------- */
4085
Johannes Berg19d337d2009-06-02 13:01:37 +02004086static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
4087 .get_status = uwb_get_status,
4088 .set_status = uwb_set_status,
4089};
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004090
4091static void uwb_exit(void)
4092{
Johannes Berg19d337d2009-06-02 13:01:37 +02004093 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004094}
4095
4096static int __init uwb_init(struct ibm_init_struct *iibm)
4097{
4098 int res;
4099 int status = 0;
4100
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004101 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4102 "initializing uwb subdriver\n");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004103
4104 TPACPI_ACPIHANDLE_INIT(hkey);
4105
4106 tp_features.uwb = hkey_handle &&
4107 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4108
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004109 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4110 "uwb is %s, status 0x%02x\n",
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004111 str_supported(tp_features.uwb),
4112 status);
4113
4114#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4115 if (dbg_uwbemul) {
4116 tp_features.uwb = 1;
4117 printk(TPACPI_INFO
4118 "uwb switch emulation enabled\n");
4119 } else
4120#endif
4121 if (tp_features.uwb &&
4122 !(status & TP_ACPI_UWB_HWPRESENT)) {
4123 /* no uwb hardware present in system */
4124 tp_features.uwb = 0;
4125 dbg_printk(TPACPI_DBG_INIT,
4126 "uwb hardware not installed\n");
4127 }
4128
4129 if (!tp_features.uwb)
4130 return 1;
4131
4132 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
Johannes Berg19d337d2009-06-02 13:01:37 +02004133 &uwb_tprfk_ops,
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004134 RFKILL_TYPE_UWB,
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004135 TPACPI_RFK_UWB_SW_NAME,
Johannes Berg19d337d2009-06-02 13:01:37 +02004136 false);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004137 return res;
4138}
4139
4140static struct ibm_struct uwb_driver_data = {
4141 .name = "uwb",
4142 .exit = uwb_exit,
4143 .flags.experimental = 1,
4144};
4145
4146/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004147 * Video subdriver
4148 */
4149
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02004150#ifdef CONFIG_THINKPAD_ACPI_VIDEO
4151
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004152enum video_access_mode {
4153 TPACPI_VIDEO_NONE = 0,
4154 TPACPI_VIDEO_570, /* 570 */
4155 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4156 TPACPI_VIDEO_NEW, /* all others */
4157};
4158
4159enum { /* video status flags, based on VIDEO_570 */
4160 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4161 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4162 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4163};
4164
4165enum { /* TPACPI_VIDEO_570 constants */
4166 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4167 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4168 * video_status_flags */
4169 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4170 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4171};
4172
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02004173static enum video_access_mode video_supported;
4174static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004175
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004176static int video_autosw_get(void);
4177static int video_autosw_set(int enable);
4178
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004179TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004180
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004181static int __init video_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004183 int ivga;
4184
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004185 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4186
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004187 TPACPI_ACPIHANDLE_INIT(vid);
4188 TPACPI_ACPIHANDLE_INIT(vid2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004189
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004190 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4191 /* G41, assume IVGA doesn't change */
4192 vid_handle = vid2_handle;
4193
4194 if (!vid_handle)
4195 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004196 video_supported = TPACPI_VIDEO_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004197 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
4198 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004199 video_supported = TPACPI_VIDEO_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004200 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
4201 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004202 video_supported = TPACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004203 else
4204 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004205 video_supported = TPACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004207 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
4208 str_supported(video_supported != TPACPI_VIDEO_NONE),
4209 video_supported);
4210
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004211 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212}
4213
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004214static void video_exit(void)
4215{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004216 dbg_printk(TPACPI_DBG_EXIT,
4217 "restoring original video autoswitch mode\n");
4218 if (video_autosw_set(video_orig_autosw))
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004219 printk(TPACPI_ERR "error while trying to restore original "
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004220 "video autoswitch mode\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004221}
4222
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004223static int video_outputsw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224{
4225 int status = 0;
4226 int i;
4227
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004228 switch (video_supported) {
4229 case TPACPI_VIDEO_570:
4230 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
4231 TP_ACPI_VIDEO_570_PHSCMD))
4232 return -EIO;
4233 status = i & TP_ACPI_VIDEO_570_PHSMASK;
4234 break;
4235 case TPACPI_VIDEO_770:
4236 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4237 return -EIO;
4238 if (i)
4239 status |= TP_ACPI_VIDEO_S_LCD;
4240 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4241 return -EIO;
4242 if (i)
4243 status |= TP_ACPI_VIDEO_S_CRT;
4244 break;
4245 case TPACPI_VIDEO_NEW:
4246 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4247 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4248 return -EIO;
4249 if (i)
4250 status |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004252 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4253 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4254 return -EIO;
4255 if (i)
4256 status |= TP_ACPI_VIDEO_S_LCD;
4257 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4258 return -EIO;
4259 if (i)
4260 status |= TP_ACPI_VIDEO_S_DVI;
4261 break;
4262 default:
4263 return -ENOSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004264 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265
4266 return status;
4267}
4268
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004269static int video_outputsw_set(int status)
4270{
4271 int autosw;
4272 int res = 0;
4273
4274 switch (video_supported) {
4275 case TPACPI_VIDEO_570:
4276 res = acpi_evalf(NULL, NULL,
4277 "\\_SB.PHS2", "vdd",
4278 TP_ACPI_VIDEO_570_PHS2CMD,
4279 status | TP_ACPI_VIDEO_570_PHS2SET);
4280 break;
4281 case TPACPI_VIDEO_770:
4282 autosw = video_autosw_get();
4283 if (autosw < 0)
4284 return autosw;
4285
4286 res = video_autosw_set(1);
4287 if (res)
4288 return res;
4289 res = acpi_evalf(vid_handle, NULL,
4290 "ASWT", "vdd", status * 0x100, 0);
4291 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004292 printk(TPACPI_ERR
4293 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004294 return -EIO;
4295 }
4296 break;
4297 case TPACPI_VIDEO_NEW:
4298 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004299 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004300 break;
4301 default:
4302 return -ENOSYS;
4303 }
4304
4305 return (res)? 0 : -EIO;
4306}
4307
4308static int video_autosw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004310 int autosw = 0;
4311
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004312 switch (video_supported) {
4313 case TPACPI_VIDEO_570:
4314 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
4315 return -EIO;
4316 break;
4317 case TPACPI_VIDEO_770:
4318 case TPACPI_VIDEO_NEW:
4319 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4320 return -EIO;
4321 break;
4322 default:
4323 return -ENOSYS;
4324 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004325
4326 return autosw & 1;
4327}
4328
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004329static int video_autosw_set(int enable)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004330{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004331 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004332 return -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004333 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004334}
4335
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004336static int video_outputsw_cycle(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004337{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004338 int autosw = video_autosw_get();
4339 int res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004340
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004341 if (autosw < 0)
4342 return autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004343
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004344 switch (video_supported) {
4345 case TPACPI_VIDEO_570:
4346 res = video_autosw_set(1);
4347 if (res)
4348 return res;
4349 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4350 break;
4351 case TPACPI_VIDEO_770:
4352 case TPACPI_VIDEO_NEW:
4353 res = video_autosw_set(1);
4354 if (res)
4355 return res;
4356 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4357 break;
4358 default:
4359 return -ENOSYS;
4360 }
4361 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004362 printk(TPACPI_ERR
4363 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004364 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004365 }
4366
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004367 return (res)? 0 : -EIO;
4368}
4369
4370static int video_expand_toggle(void)
4371{
4372 switch (video_supported) {
4373 case TPACPI_VIDEO_570:
4374 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
4375 0 : -EIO;
4376 case TPACPI_VIDEO_770:
4377 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
4378 0 : -EIO;
4379 case TPACPI_VIDEO_NEW:
4380 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
4381 0 : -EIO;
4382 default:
4383 return -ENOSYS;
4384 }
4385 /* not reached */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004386}
4387
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004388static int video_read(char *p)
4389{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004390 int status, autosw;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004391 int len = 0;
4392
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004393 if (video_supported == TPACPI_VIDEO_NONE) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004394 len += sprintf(p + len, "status:\t\tnot supported\n");
4395 return len;
4396 }
4397
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004398 status = video_outputsw_get();
4399 if (status < 0)
4400 return status;
4401
4402 autosw = video_autosw_get();
4403 if (autosw < 0)
4404 return autosw;
4405
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004406 len += sprintf(p + len, "status:\t\tsupported\n");
4407 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
4408 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004409 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004410 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
4411 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
4412 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
4413 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004414 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004415 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
4416 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
4417 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
4418
4419 return len;
4420}
4421
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004422static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423{
4424 char *cmd;
4425 int enable, disable, status;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004426 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004428 if (video_supported == TPACPI_VIDEO_NONE)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004429 return -ENODEV;
4430
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004431 enable = 0;
4432 disable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433
4434 while ((cmd = next_cmd(&buf))) {
4435 if (strlencmp(cmd, "lcd_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004436 enable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437 } else if (strlencmp(cmd, "lcd_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004438 disable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439 } else if (strlencmp(cmd, "crt_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004440 enable |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441 } else if (strlencmp(cmd, "crt_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004442 disable |= TP_ACPI_VIDEO_S_CRT;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004443 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004444 strlencmp(cmd, "dvi_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004445 enable |= TP_ACPI_VIDEO_S_DVI;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004446 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004447 strlencmp(cmd, "dvi_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004448 disable |= TP_ACPI_VIDEO_S_DVI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449 } else if (strlencmp(cmd, "auto_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004450 res = video_autosw_set(1);
4451 if (res)
4452 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453 } else if (strlencmp(cmd, "auto_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004454 res = video_autosw_set(0);
4455 if (res)
4456 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457 } else if (strlencmp(cmd, "video_switch") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004458 res = video_outputsw_cycle();
4459 if (res)
4460 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004462 res = video_expand_toggle();
4463 if (res)
4464 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465 } else
4466 return -EINVAL;
4467 }
4468
4469 if (enable || disable) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004470 status = video_outputsw_get();
4471 if (status < 0)
4472 return status;
4473 res = video_outputsw_set((status & ~disable) | enable);
4474 if (res)
4475 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476 }
4477
4478 return 0;
4479}
4480
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004481static struct ibm_struct video_driver_data = {
4482 .name = "video",
4483 .read = video_read,
4484 .write = video_write,
4485 .exit = video_exit,
4486};
4487
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02004488#endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4489
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004490/*************************************************************************
4491 * Light (thinklight) subdriver
4492 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004494TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4495TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004496
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004497static int light_get_status(void)
4498{
4499 int status = 0;
4500
4501 if (tp_features.light_status) {
4502 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4503 return -EIO;
4504 return (!!status);
4505 }
4506
4507 return -ENXIO;
4508}
4509
4510static int light_set_status(int status)
4511{
4512 int rc;
4513
4514 if (tp_features.light) {
4515 if (cmos_handle) {
4516 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4517 (status)?
4518 TP_CMOS_THINKLIGHT_ON :
4519 TP_CMOS_THINKLIGHT_OFF);
4520 } else {
4521 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4522 (status)? 1 : 0);
4523 }
4524 return (rc)? 0 : -EIO;
4525 }
4526
4527 return -ENXIO;
4528}
4529
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004530static void light_set_status_worker(struct work_struct *work)
4531{
4532 struct tpacpi_led_classdev *data =
4533 container_of(work, struct tpacpi_led_classdev, work);
4534
4535 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00004536 light_set_status((data->new_state != TPACPI_LED_OFF));
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004537}
4538
4539static void light_sysfs_set(struct led_classdev *led_cdev,
4540 enum led_brightness brightness)
4541{
4542 struct tpacpi_led_classdev *data =
4543 container_of(led_cdev,
4544 struct tpacpi_led_classdev,
4545 led_classdev);
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00004546 data->new_state = (brightness != LED_OFF) ?
4547 TPACPI_LED_ON : TPACPI_LED_OFF;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004548 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004549}
4550
4551static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4552{
4553 return (light_get_status() == 1)? LED_FULL : LED_OFF;
4554}
4555
4556static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4557 .led_classdev = {
4558 .name = "tpacpi::thinklight",
4559 .brightness_set = &light_sysfs_set,
4560 .brightness_get = &light_sysfs_get,
4561 }
4562};
4563
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004564static int __init light_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565{
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004566 int rc;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004567
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004568 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4569
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004570 TPACPI_ACPIHANDLE_INIT(ledb);
4571 TPACPI_ACPIHANDLE_INIT(lght);
4572 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004573 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004574
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004575 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004576 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004577
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004578 if (tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004579 /* light status not supported on
4580 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004581 tp_features.light_status =
4582 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004584 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4585 str_supported(tp_features.light),
4586 str_supported(tp_features.light_status));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004587
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004588 if (!tp_features.light)
4589 return 1;
4590
4591 rc = led_classdev_register(&tpacpi_pdev->dev,
4592 &tpacpi_led_thinklight.led_classdev);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004593
4594 if (rc < 0) {
4595 tp_features.light = 0;
4596 tp_features.light_status = 0;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004597 } else {
4598 rc = 0;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004599 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004600
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004601 return rc;
4602}
4603
4604static void light_exit(void)
4605{
4606 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
4607 if (work_pending(&tpacpi_led_thinklight.work))
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004608 flush_workqueue(tpacpi_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609}
4610
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004611static int light_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612{
4613 int len = 0;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004614 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004616 if (!tp_features.light) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004617 len += sprintf(p + len, "status:\t\tnot supported\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004618 } else if (!tp_features.light_status) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004619 len += sprintf(p + len, "status:\t\tunknown\n");
4620 len += sprintf(p + len, "commands:\ton, off\n");
4621 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004622 status = light_get_status();
4623 if (status < 0)
4624 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004625 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004626 len += sprintf(p + len, "commands:\ton, off\n");
4627 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004628
4629 return len;
4630}
4631
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004632static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004635 int newstatus = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004636
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004637 if (!tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004638 return -ENODEV;
4639
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640 while ((cmd = next_cmd(&buf))) {
4641 if (strlencmp(cmd, "on") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004642 newstatus = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643 } else if (strlencmp(cmd, "off") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004644 newstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004645 } else
4646 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647 }
4648
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004649 return light_set_status(newstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650}
4651
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004652static struct ibm_struct light_driver_data = {
4653 .name = "light",
4654 .read = light_read,
4655 .write = light_write,
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004656 .exit = light_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004657};
4658
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004659/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004660 * CMOS subdriver
4661 */
4662
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004663/* sysfs cmos_command -------------------------------------------------- */
4664static ssize_t cmos_command_store(struct device *dev,
4665 struct device_attribute *attr,
4666 const char *buf, size_t count)
4667{
4668 unsigned long cmos_cmd;
4669 int res;
4670
4671 if (parse_strtoul(buf, 21, &cmos_cmd))
4672 return -EINVAL;
4673
4674 res = issue_thinkpad_cmos_command(cmos_cmd);
4675 return (res)? res : count;
4676}
4677
4678static struct device_attribute dev_attr_cmos_command =
4679 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4680
4681/* --------------------------------------------------------------------- */
4682
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004683static int __init cmos_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004684{
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004685 int res;
4686
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004687 vdbg_printk(TPACPI_DBG_INIT,
4688 "initializing cmos commands subdriver\n");
4689
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004690 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004691
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004692 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4693 str_supported(cmos_handle != NULL));
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004694
4695 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4696 if (res)
4697 return res;
4698
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004699 return (cmos_handle)? 0 : 1;
4700}
4701
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004702static void cmos_exit(void)
4703{
4704 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4705}
4706
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004707static int cmos_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708{
4709 int len = 0;
4710
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004711 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4712 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004713 if (!cmos_handle)
4714 len += sprintf(p + len, "status:\t\tnot supported\n");
4715 else {
4716 len += sprintf(p + len, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004717 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004718 }
4719
4720 return len;
4721}
4722
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004723static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004724{
4725 char *cmd;
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004726 int cmos_cmd, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004727
4728 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004729 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
4730 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004731 /* cmos_cmd set */
4732 } else
4733 return -EINVAL;
4734
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004735 res = issue_thinkpad_cmos_command(cmos_cmd);
4736 if (res)
4737 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004738 }
4739
4740 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004741}
4742
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004743static struct ibm_struct cmos_driver_data = {
4744 .name = "cmos",
4745 .read = cmos_read,
4746 .write = cmos_write,
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004747 .exit = cmos_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004748};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004749
4750/*************************************************************************
4751 * LED subdriver
4752 */
4753
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004754enum led_access_mode {
4755 TPACPI_LED_NONE = 0,
4756 TPACPI_LED_570, /* 570 */
4757 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4758 TPACPI_LED_NEW, /* all others */
4759};
4760
4761enum { /* For TPACPI_LED_OLD */
4762 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
4763 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
4764 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
4765};
4766
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02004767static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004768
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004769TPACPI_HANDLE(led, ec, "SLED", /* 570 */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004770 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
4771 /* T20-22, X20-21 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004772 "LED", /* all others */
4773 ); /* R30, R31 */
4774
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03004775#define TPACPI_LED_NUMLEDS 16
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004776static struct tpacpi_led_classdev *tpacpi_leds;
4777static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
Harvey Harrisone3aa51f2008-05-29 17:51:57 -07004778static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004779 /* there's a limit of 19 chars + NULL before 2.6.26 */
4780 "tpacpi::power",
4781 "tpacpi:orange:batt",
4782 "tpacpi:green:batt",
4783 "tpacpi::dock_active",
4784 "tpacpi::bay_active",
4785 "tpacpi::dock_batt",
4786 "tpacpi::unknown_led",
4787 "tpacpi::standby",
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03004788 "tpacpi::dock_status1",
4789 "tpacpi::dock_status2",
4790 "tpacpi::unknown_led2",
4791 "tpacpi::unknown_led3",
4792 "tpacpi::thinkvantage",
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004793};
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03004794#define TPACPI_SAFE_LEDS 0x1081U
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004795
4796static inline bool tpacpi_is_led_restricted(const unsigned int led)
4797{
4798#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
4799 return false;
4800#else
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03004801 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004802#endif
4803}
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004804
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004805static int led_get_status(const unsigned int led)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004806{
4807 int status;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004808 enum led_status_t led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004809
4810 switch (led_supported) {
4811 case TPACPI_LED_570:
4812 if (!acpi_evalf(ec_handle,
4813 &status, "GLED", "dd", 1 << led))
4814 return -EIO;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004815 led_s = (status == 0)?
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004816 TPACPI_LED_OFF :
4817 ((status == 1)?
4818 TPACPI_LED_ON :
4819 TPACPI_LED_BLINK);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004820 tpacpi_led_state_cache[led] = led_s;
4821 return led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004822 default:
4823 return -ENXIO;
4824 }
4825
4826 /* not reached */
4827}
4828
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004829static int led_set_status(const unsigned int led,
4830 const enum led_status_t ledstatus)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004831{
4832 /* off, on, blink. Index is led_status_t */
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004833 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
4834 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004835
4836 int rc = 0;
4837
4838 switch (led_supported) {
4839 case TPACPI_LED_570:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004840 /* 570 */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004841 if (unlikely(led > 7))
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004842 return -EINVAL;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004843 if (unlikely(tpacpi_is_led_restricted(led)))
4844 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004845 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4846 (1 << led), led_sled_arg1[ledstatus]))
4847 rc = -EIO;
4848 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004849 case TPACPI_LED_OLD:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004850 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
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 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
4856 if (rc >= 0)
4857 rc = ec_write(TPACPI_LED_EC_HLBL,
4858 (ledstatus == TPACPI_LED_BLINK) << led);
4859 if (rc >= 0)
4860 rc = ec_write(TPACPI_LED_EC_HLCL,
4861 (ledstatus != TPACPI_LED_OFF) << led);
4862 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004863 case TPACPI_LED_NEW:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004864 /* all others */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004865 if (unlikely(led >= TPACPI_LED_NUMLEDS))
4866 return -EINVAL;
4867 if (unlikely(tpacpi_is_led_restricted(led)))
4868 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004869 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4870 led, led_led_arg1[ledstatus]))
4871 rc = -EIO;
4872 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004873 default:
4874 rc = -ENXIO;
4875 }
4876
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004877 if (!rc)
4878 tpacpi_led_state_cache[led] = ledstatus;
4879
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004880 return rc;
4881}
4882
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004883static void led_set_status_worker(struct work_struct *work)
4884{
4885 struct tpacpi_led_classdev *data =
4886 container_of(work, struct tpacpi_led_classdev, work);
4887
4888 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00004889 led_set_status(data->led, data->new_state);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004890}
4891
4892static void led_sysfs_set(struct led_classdev *led_cdev,
4893 enum led_brightness brightness)
4894{
4895 struct tpacpi_led_classdev *data = container_of(led_cdev,
4896 struct tpacpi_led_classdev, led_classdev);
4897
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00004898 if (brightness == LED_OFF)
4899 data->new_state = TPACPI_LED_OFF;
4900 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
4901 data->new_state = TPACPI_LED_ON;
4902 else
4903 data->new_state = TPACPI_LED_BLINK;
4904
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004905 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004906}
4907
4908static int led_sysfs_blink_set(struct led_classdev *led_cdev,
4909 unsigned long *delay_on, unsigned long *delay_off)
4910{
4911 struct tpacpi_led_classdev *data = container_of(led_cdev,
4912 struct tpacpi_led_classdev, led_classdev);
4913
4914 /* Can we choose the flash rate? */
4915 if (*delay_on == 0 && *delay_off == 0) {
4916 /* yes. set them to the hardware blink rate (1 Hz) */
4917 *delay_on = 500; /* ms */
4918 *delay_off = 500; /* ms */
4919 } else if ((*delay_on != 500) || (*delay_off != 500))
4920 return -EINVAL;
4921
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00004922 data->new_state = TPACPI_LED_BLINK;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004923 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004924
4925 return 0;
4926}
4927
4928static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
4929{
4930 int rc;
4931
4932 struct tpacpi_led_classdev *data = container_of(led_cdev,
4933 struct tpacpi_led_classdev, led_classdev);
4934
4935 rc = led_get_status(data->led);
4936
4937 if (rc == TPACPI_LED_OFF || rc < 0)
4938 rc = LED_OFF; /* no error handling in led class :( */
4939 else
4940 rc = LED_FULL;
4941
4942 return rc;
4943}
4944
4945static void led_exit(void)
4946{
4947 unsigned int i;
4948
4949 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4950 if (tpacpi_leds[i].led_classdev.name)
4951 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
4952 }
4953
4954 kfree(tpacpi_leds);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004955}
4956
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004957static int __init tpacpi_init_led(unsigned int led)
4958{
4959 int rc;
4960
4961 tpacpi_leds[led].led = led;
4962
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03004963 /* LEDs with no name don't get registered */
4964 if (!tpacpi_led_names[led])
4965 return 0;
4966
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004967 tpacpi_leds[led].led_classdev.brightness_set = &led_sysfs_set;
4968 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
4969 if (led_supported == TPACPI_LED_570)
4970 tpacpi_leds[led].led_classdev.brightness_get =
4971 &led_sysfs_get;
4972
4973 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
4974
4975 INIT_WORK(&tpacpi_leds[led].work, led_set_status_worker);
4976
4977 rc = led_classdev_register(&tpacpi_pdev->dev,
4978 &tpacpi_leds[led].led_classdev);
4979 if (rc < 0)
4980 tpacpi_leds[led].led_classdev.name = NULL;
4981
4982 return rc;
4983}
4984
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03004985static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
4986 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
4987 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
4988 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
4989
4990 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
4991 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
4992 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
4993 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
4994 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
4995 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
4996 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
4997 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
4998
4999 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5000 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5001 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5002 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5003 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5004
5005 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5006 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5007 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5008 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5009
5010 /* (1) - may have excess leds enabled on MSB */
5011
5012 /* Defaults (order matters, keep last, don't reorder!) */
5013 { /* Lenovo */
5014 .vendor = PCI_VENDOR_ID_LENOVO,
5015 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5016 .quirks = 0x1fffU,
5017 },
5018 { /* IBM ThinkPads with no EC version string */
5019 .vendor = PCI_VENDOR_ID_IBM,
5020 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
5021 .quirks = 0x00ffU,
5022 },
5023 { /* IBM ThinkPads with EC version string */
5024 .vendor = PCI_VENDOR_ID_IBM,
5025 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5026 .quirks = 0x00bfU,
5027 },
5028};
5029
5030#undef TPACPI_LEDQ_IBM
5031#undef TPACPI_LEDQ_LNV
5032
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005033static int __init led_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005034{
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005035 unsigned int i;
5036 int rc;
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005037 unsigned long useful_leds;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005038
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005039 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
5040
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005041 TPACPI_ACPIHANDLE_INIT(led);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005042
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005043 if (!led_handle)
5044 /* led not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005045 led_supported = TPACPI_LED_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005046 else if (strlencmp(led_path, "SLED") == 0)
5047 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005048 led_supported = TPACPI_LED_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005049 else if (strlencmp(led_path, "SYSL") == 0)
5050 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005051 led_supported = TPACPI_LED_OLD;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005052 else
5053 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005054 led_supported = TPACPI_LED_NEW;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005055
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005056 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
5057 str_supported(led_supported), led_supported);
5058
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005059 if (led_supported == TPACPI_LED_NONE)
5060 return 1;
5061
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005062 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
5063 GFP_KERNEL);
5064 if (!tpacpi_leds) {
5065 printk(TPACPI_ERR "Out of memory for LED data\n");
5066 return -ENOMEM;
5067 }
5068
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005069 useful_leds = tpacpi_check_quirks(led_useful_qtable,
5070 ARRAY_SIZE(led_useful_qtable));
5071
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005072 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005073 if (!tpacpi_is_led_restricted(i) &&
5074 test_bit(i, &useful_leds)) {
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005075 rc = tpacpi_init_led(i);
5076 if (rc < 0) {
5077 led_exit();
5078 return rc;
5079 }
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005080 }
5081 }
5082
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005083#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005084 printk(TPACPI_NOTICE
5085 "warning: userspace override of important "
5086 "firmware LEDs is enabled\n");
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005087#endif
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005088 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005089}
5090
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005091#define str_led_status(s) \
5092 ((s) == TPACPI_LED_OFF ? "off" : \
5093 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005094
5095static int led_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005096{
5097 int len = 0;
5098
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005099 if (!led_supported) {
5100 len += sprintf(p + len, "status:\t\tnot supported\n");
5101 return len;
5102 }
5103 len += sprintf(p + len, "status:\t\tsupported\n");
5104
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005105 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005106 /* 570 */
5107 int i, status;
5108 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005109 status = led_get_status(i);
5110 if (status < 0)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005111 return -EIO;
5112 len += sprintf(p + len, "%d:\t\t%s\n",
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005113 i, str_led_status(status));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005114 }
5115 }
5116
Linus Torvalds1da177e2005-04-16 15:20:36 -07005117 len += sprintf(p + len, "commands:\t"
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005118 "<led> on, <led> off, <led> blink (<led> is 0-15)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005119
5120 return len;
5121}
5122
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005123static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005124{
5125 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005126 int led, rc;
5127 enum led_status_t s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005128
5129 if (!led_supported)
5130 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005131
5132 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005133 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 15)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005134 return -EINVAL;
5135
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005136 if (strstr(cmd, "off")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005137 s = TPACPI_LED_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005138 } else if (strstr(cmd, "on")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005139 s = TPACPI_LED_ON;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005140 } else if (strstr(cmd, "blink")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005141 s = TPACPI_LED_BLINK;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005142 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005143 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005144 }
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005145
5146 rc = led_set_status(led, s);
5147 if (rc < 0)
5148 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005149 }
5150
5151 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005152}
5153
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005154static struct ibm_struct led_driver_data = {
5155 .name = "led",
5156 .read = led_read,
5157 .write = led_write,
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005158 .exit = led_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005159};
5160
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005161/*************************************************************************
5162 * Beep subdriver
5163 */
5164
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005165TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005166
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005167#define TPACPI_BEEP_Q1 0x0001
5168
5169static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
5170 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
5171 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
5172};
5173
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005174static int __init beep_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005175{
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005176 unsigned long quirks;
5177
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005178 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
5179
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005180 TPACPI_ACPIHANDLE_INIT(beep);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005181
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005182 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
5183 str_supported(beep_handle != NULL));
5184
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005185 quirks = tpacpi_check_quirks(beep_quirk_table,
5186 ARRAY_SIZE(beep_quirk_table));
5187
5188 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
5189
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005190 return (beep_handle)? 0 : 1;
5191}
5192
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005193static int beep_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005194{
5195 int len = 0;
5196
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005197 if (!beep_handle)
5198 len += sprintf(p + len, "status:\t\tnot supported\n");
5199 else {
5200 len += sprintf(p + len, "status:\t\tsupported\n");
5201 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
5202 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005203
5204 return len;
5205}
5206
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005207static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005208{
5209 char *cmd;
5210 int beep_cmd;
5211
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005212 if (!beep_handle)
5213 return -ENODEV;
5214
Linus Torvalds1da177e2005-04-16 15:20:36 -07005215 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005216 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
5217 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005218 /* beep_cmd set */
5219 } else
5220 return -EINVAL;
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005221 if (tp_features.beep_needs_two_args) {
5222 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
5223 beep_cmd, 0))
5224 return -EIO;
5225 } else {
5226 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
5227 beep_cmd))
5228 return -EIO;
5229 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005230 }
5231
5232 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005233}
5234
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005235static struct ibm_struct beep_driver_data = {
5236 .name = "beep",
5237 .read = beep_read,
5238 .write = beep_write,
5239};
5240
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005241/*************************************************************************
5242 * Thermal subdriver
5243 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005244
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005245enum thermal_access_mode {
5246 TPACPI_THERMAL_NONE = 0, /* No thermal support */
5247 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
5248 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
5249 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
5250 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
5251};
5252
5253enum { /* TPACPI_THERMAL_TPEC_* */
5254 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
5255 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
5256 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
5257};
5258
5259#define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5260struct ibm_thermal_sensors_struct {
5261 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
5262};
5263
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005264static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005265
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005266/* idx is zero-based */
5267static int thermal_get_sensor(int idx, s32 *value)
5268{
5269 int t;
5270 s8 tmp;
5271 char tmpi[5];
5272
5273 t = TP_EC_THERMAL_TMP0;
5274
5275 switch (thermal_read_mode) {
5276#if TPACPI_MAX_THERMAL_SENSORS >= 16
5277 case TPACPI_THERMAL_TPEC_16:
5278 if (idx >= 8 && idx <= 15) {
5279 t = TP_EC_THERMAL_TMP8;
5280 idx -= 8;
5281 }
5282 /* fallthrough */
5283#endif
5284 case TPACPI_THERMAL_TPEC_8:
5285 if (idx <= 7) {
5286 if (!acpi_ec_read(t + idx, &tmp))
5287 return -EIO;
5288 *value = tmp * 1000;
5289 return 0;
5290 }
5291 break;
5292
5293 case TPACPI_THERMAL_ACPI_UPDT:
5294 if (idx <= 7) {
5295 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5296 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
5297 return -EIO;
5298 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5299 return -EIO;
5300 *value = (t - 2732) * 100;
5301 return 0;
5302 }
5303 break;
5304
5305 case TPACPI_THERMAL_ACPI_TMP07:
5306 if (idx <= 7) {
5307 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5308 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5309 return -EIO;
5310 if (t > 127 || t < -127)
5311 t = TP_EC_THERMAL_TMP_NA;
5312 *value = t * 1000;
5313 return 0;
5314 }
5315 break;
5316
5317 case TPACPI_THERMAL_NONE:
5318 default:
5319 return -ENOSYS;
5320 }
5321
5322 return -EINVAL;
5323}
5324
5325static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5326{
5327 int res, i;
5328 int n;
5329
5330 n = 8;
5331 i = 0;
5332
5333 if (!s)
5334 return -EINVAL;
5335
5336 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5337 n = 16;
5338
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005339 for (i = 0 ; i < n; i++) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005340 res = thermal_get_sensor(i, &s->temp[i]);
5341 if (res)
5342 return res;
5343 }
5344
5345 return n;
5346}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005347
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005348/* sysfs temp##_input -------------------------------------------------- */
5349
5350static ssize_t thermal_temp_input_show(struct device *dev,
5351 struct device_attribute *attr,
5352 char *buf)
5353{
5354 struct sensor_device_attribute *sensor_attr =
5355 to_sensor_dev_attr(attr);
5356 int idx = sensor_attr->index;
5357 s32 value;
5358 int res;
5359
5360 res = thermal_get_sensor(idx, &value);
5361 if (res)
5362 return res;
5363 if (value == TP_EC_THERMAL_TMP_NA * 1000)
5364 return -ENXIO;
5365
5366 return snprintf(buf, PAGE_SIZE, "%d\n", value);
5367}
5368
5369#define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005370 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5371 thermal_temp_input_show, NULL, _idxB)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005372
5373static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5374 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5375 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5376 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5377 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5378 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5379 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5380 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5381 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5382 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5383 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5384 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5385 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5386 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5387 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5388 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5389 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5390};
5391
5392#define THERMAL_ATTRS(X) \
5393 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5394
5395static struct attribute *thermal_temp_input_attr[] = {
5396 THERMAL_ATTRS(8),
5397 THERMAL_ATTRS(9),
5398 THERMAL_ATTRS(10),
5399 THERMAL_ATTRS(11),
5400 THERMAL_ATTRS(12),
5401 THERMAL_ATTRS(13),
5402 THERMAL_ATTRS(14),
5403 THERMAL_ATTRS(15),
5404 THERMAL_ATTRS(0),
5405 THERMAL_ATTRS(1),
5406 THERMAL_ATTRS(2),
5407 THERMAL_ATTRS(3),
5408 THERMAL_ATTRS(4),
5409 THERMAL_ATTRS(5),
5410 THERMAL_ATTRS(6),
5411 THERMAL_ATTRS(7),
5412 NULL
5413};
5414
5415static const struct attribute_group thermal_temp_input16_group = {
5416 .attrs = thermal_temp_input_attr
5417};
5418
5419static const struct attribute_group thermal_temp_input8_group = {
5420 .attrs = &thermal_temp_input_attr[8]
5421};
5422
5423#undef THERMAL_SENSOR_ATTR_TEMP
5424#undef THERMAL_ATTRS
5425
5426/* --------------------------------------------------------------------- */
5427
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005428static int __init thermal_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005429{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005430 u8 t, ta1, ta2;
5431 int i;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005432 int acpi_tmp7;
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005433 int res;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005434
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005435 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5436
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005437 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005438
Henrique de Moraes Holschuh3d6f99c2007-07-18 23:45:46 -03005439 if (thinkpad_id.ec_model) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005440 /*
5441 * Direct EC access mode: sensors at registers
5442 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5443 * non-implemented, thermal sensors return 0x80 when
5444 * not available
5445 */
5446
5447 ta1 = ta2 = 0;
5448 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03005449 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005450 ta1 |= t;
5451 } else {
5452 ta1 = 0;
5453 break;
5454 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03005455 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005456 ta2 |= t;
5457 } else {
5458 ta1 = 0;
5459 break;
5460 }
5461 }
5462 if (ta1 == 0) {
5463 /* This is sheer paranoia, but we handle it anyway */
5464 if (acpi_tmp7) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005465 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005466 "ThinkPad ACPI EC access misbehaving, "
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005467 "falling back to ACPI TMPx access "
5468 "mode\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005469 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005470 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005471 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005472 "ThinkPad ACPI EC access misbehaving, "
5473 "disabling thermal sensors access\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005474 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005475 }
5476 } else {
5477 thermal_read_mode =
5478 (ta2 != 0) ?
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005479 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005480 }
5481 } else if (acpi_tmp7) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005482 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
5483 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005484 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005485 } else {
5486 /* Standard ACPI TMPx access, max 8 sensors */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005487 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005488 }
5489 } else {
5490 /* temperatures not supported on 570, G4x, R30, R31, R32 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005491 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005492 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005493
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005494 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5495 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5496 thermal_read_mode);
5497
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005498 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005499 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005500 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005501 &thermal_temp_input16_group);
5502 if (res)
5503 return res;
5504 break;
5505 case TPACPI_THERMAL_TPEC_8:
5506 case TPACPI_THERMAL_ACPI_TMP07:
5507 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005508 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005509 &thermal_temp_input8_group);
5510 if (res)
5511 return res;
5512 break;
5513 case TPACPI_THERMAL_NONE:
5514 default:
5515 return 1;
5516 }
5517
5518 return 0;
5519}
5520
5521static void thermal_exit(void)
5522{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005523 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005524 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005525 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005526 &thermal_temp_input16_group);
5527 break;
5528 case TPACPI_THERMAL_TPEC_8:
5529 case TPACPI_THERMAL_ACPI_TMP07:
5530 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005531 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005532 &thermal_temp_input16_group);
5533 break;
5534 case TPACPI_THERMAL_NONE:
5535 default:
5536 break;
5537 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005538}
5539
5540static int thermal_read(char *p)
5541{
5542 int len = 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005543 int n, i;
5544 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005545
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005546 n = thermal_get_sensors(&t);
5547 if (unlikely(n < 0))
5548 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005549
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005550 len += sprintf(p + len, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005551
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005552 if (n > 0) {
5553 for (i = 0; i < (n - 1); i++)
5554 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
5555 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
5556 } else
5557 len += sprintf(p + len, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005558
5559 return len;
5560}
5561
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005562static struct ibm_struct thermal_driver_data = {
5563 .name = "thermal",
5564 .read = thermal_read,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005565 .exit = thermal_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005566};
5567
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005568/*************************************************************************
5569 * EC Dump subdriver
5570 */
5571
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005572static u8 ecdump_regs[256];
5573
5574static int ecdump_read(char *p)
5575{
5576 int len = 0;
5577 int i, j;
5578 u8 v;
5579
5580 len += sprintf(p + len, "EC "
5581 " +00 +01 +02 +03 +04 +05 +06 +07"
5582 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
5583 for (i = 0; i < 256; i += 16) {
5584 len += sprintf(p + len, "EC 0x%02x:", i);
5585 for (j = 0; j < 16; j++) {
5586 if (!acpi_ec_read(i + j, &v))
5587 break;
5588 if (v != ecdump_regs[i + j])
5589 len += sprintf(p + len, " *%02x", v);
5590 else
5591 len += sprintf(p + len, " %02x", v);
5592 ecdump_regs[i + j] = v;
5593 }
5594 len += sprintf(p + len, "\n");
5595 if (j != 16)
5596 break;
5597 }
5598
5599 /* These are way too dangerous to advertise openly... */
5600#if 0
5601 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
5602 " (<offset> is 00-ff, <value> is 00-ff)\n");
5603 len += sprintf(p + len, "commands:\t0x<offset> <value> "
5604 " (<offset> is 00-ff, <value> is 0-255)\n");
5605#endif
5606 return len;
5607}
5608
5609static int ecdump_write(char *buf)
5610{
5611 char *cmd;
5612 int i, v;
5613
5614 while ((cmd = next_cmd(&buf))) {
5615 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
5616 /* i and v set */
5617 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
5618 /* i and v set */
5619 } else
5620 return -EINVAL;
5621 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
5622 if (!acpi_ec_write(i, v))
5623 return -EIO;
5624 } else
5625 return -EINVAL;
5626 }
5627
5628 return 0;
5629}
5630
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005631static struct ibm_struct ecdump_driver_data = {
5632 .name = "ecdump",
5633 .read = ecdump_read,
5634 .write = ecdump_write,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03005635 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005636};
5637
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005638/*************************************************************************
5639 * Backlight/brightness subdriver
5640 */
Holger Macht8acb0252006-10-20 14:30:28 -07005641
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005642#define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5643
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005644/*
5645 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
5646 * CMOS NVRAM byte 0x5E, bits 0-3.
5647 *
5648 * EC HBRV (0x31) has the following layout
5649 * Bit 7: unknown function
5650 * Bit 6: unknown function
5651 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5652 * Bit 4: must be set to zero to avoid problems
5653 * Bit 3-0: backlight brightness level
5654 *
5655 * brightness_get_raw returns status data in the HBRV layout
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03005656 *
5657 * WARNING: The X61 has been verified to use HBRV for something else, so
5658 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
5659 * testing on the very early *60 Lenovo models...
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005660 */
5661
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005662enum {
5663 TP_EC_BACKLIGHT = 0x31,
5664
5665 /* TP_EC_BACKLIGHT bitmasks */
5666 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5667 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5668 TP_EC_BACKLIGHT_MAPSW = 0x20,
5669};
5670
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005671enum tpacpi_brightness_access_mode {
5672 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
5673 TPACPI_BRGHT_MODE_EC, /* EC control */
5674 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
5675 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
5676 TPACPI_BRGHT_MODE_MAX
5677};
5678
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03005679static struct backlight_device *ibm_backlight_device;
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005680
5681static enum tpacpi_brightness_access_mode brightness_mode =
5682 TPACPI_BRGHT_MODE_MAX;
5683
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005684static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5685
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005686static struct mutex brightness_mutex;
5687
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005688/* NVRAM brightness access,
5689 * call with brightness_mutex held! */
5690static unsigned int tpacpi_brightness_nvram_get(void)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005691{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005692 u8 lnvram;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005693
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005694 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5695 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5696 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
5697 lnvram &= (tp_features.bright_16levels) ? 0x0f : 0x07;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005698
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005699 return lnvram;
5700}
5701
5702static void tpacpi_brightness_checkpoint_nvram(void)
5703{
5704 u8 lec = 0;
5705 u8 b_nvram;
5706
5707 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
5708 return;
5709
5710 vdbg_printk(TPACPI_DBG_BRGHT,
5711 "trying to checkpoint backlight level to NVRAM...\n");
5712
5713 if (mutex_lock_killable(&brightness_mutex) < 0)
5714 return;
5715
5716 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5717 goto unlock;
5718 lec &= TP_EC_BACKLIGHT_LVLMSK;
5719 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
5720
5721 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5722 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
5723 /* NVRAM needs update */
5724 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
5725 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
5726 b_nvram |= lec;
5727 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
5728 dbg_printk(TPACPI_DBG_BRGHT,
5729 "updated NVRAM backlight level to %u (0x%02x)\n",
5730 (unsigned int) lec, (unsigned int) b_nvram);
5731 } else
5732 vdbg_printk(TPACPI_DBG_BRGHT,
5733 "NVRAM backlight level already is %u (0x%02x)\n",
5734 (unsigned int) lec, (unsigned int) b_nvram);
5735
5736unlock:
5737 mutex_unlock(&brightness_mutex);
5738}
5739
5740
5741/* call with brightness_mutex held! */
5742static int tpacpi_brightness_get_raw(int *status)
5743{
5744 u8 lec = 0;
5745
5746 switch (brightness_mode) {
5747 case TPACPI_BRGHT_MODE_UCMS_STEP:
5748 *status = tpacpi_brightness_nvram_get();
5749 return 0;
5750 case TPACPI_BRGHT_MODE_EC:
5751 case TPACPI_BRGHT_MODE_ECNVRAM:
5752 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
Henrique de Moraes Holschuh2d5e94d2008-04-26 01:02:20 -03005753 return -EIO;
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005754 *status = lec;
5755 return 0;
5756 default:
5757 return -ENXIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005758 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005759}
5760
5761/* call with brightness_mutex held! */
5762/* do NOT call with illegal backlight level value */
5763static int tpacpi_brightness_set_ec(unsigned int value)
5764{
5765 u8 lec = 0;
5766
5767 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5768 return -EIO;
5769
5770 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
5771 (lec & TP_EC_BACKLIGHT_CMDMSK) |
5772 (value & TP_EC_BACKLIGHT_LVLMSK))))
5773 return -EIO;
5774
5775 return 0;
5776}
5777
5778/* call with brightness_mutex held! */
5779static int tpacpi_brightness_set_ucmsstep(unsigned int value)
5780{
5781 int cmos_cmd, inc;
5782 unsigned int current_value, i;
5783
5784 current_value = tpacpi_brightness_nvram_get();
5785
5786 if (value == current_value)
5787 return 0;
5788
5789 cmos_cmd = (value > current_value) ?
5790 TP_CMOS_BRIGHTNESS_UP :
5791 TP_CMOS_BRIGHTNESS_DOWN;
5792 inc = (value > current_value) ? 1 : -1;
5793
5794 for (i = current_value; i != value; i += inc)
5795 if (issue_thinkpad_cmos_command(cmos_cmd))
5796 return -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005797
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005798 return 0;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005799}
5800
5801/* May return EINTR which can always be mapped to ERESTARTSYS */
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005802static int brightness_set(unsigned int value)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005803{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005804 int res;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005805
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005806 if (value > ((tp_features.bright_16levels)? 15 : 7) ||
5807 value < 0)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005808 return -EINVAL;
5809
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005810 vdbg_printk(TPACPI_DBG_BRGHT,
5811 "set backlight level to %d\n", value);
5812
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02005813 res = mutex_lock_killable(&brightness_mutex);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005814 if (res < 0)
5815 return res;
5816
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005817 switch (brightness_mode) {
5818 case TPACPI_BRGHT_MODE_EC:
5819 case TPACPI_BRGHT_MODE_ECNVRAM:
5820 res = tpacpi_brightness_set_ec(value);
5821 break;
5822 case TPACPI_BRGHT_MODE_UCMS_STEP:
5823 res = tpacpi_brightness_set_ucmsstep(value);
5824 break;
5825 default:
5826 res = -ENXIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005827 }
5828
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005829 mutex_unlock(&brightness_mutex);
5830 return res;
5831}
5832
5833/* sysfs backlight class ----------------------------------------------- */
5834
5835static int brightness_update_status(struct backlight_device *bd)
5836{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005837 unsigned int level =
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005838 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
5839 bd->props.power == FB_BLANK_UNBLANK) ?
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005840 bd->props.brightness : 0;
5841
5842 dbg_printk(TPACPI_DBG_BRGHT,
5843 "backlight: attempt to set level to %d\n",
5844 level);
5845
5846 /* it is the backlight class's job (caller) to handle
5847 * EINTR and other errors properly */
5848 return brightness_set(level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005849}
Holger Macht8acb0252006-10-20 14:30:28 -07005850
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005851static int brightness_get(struct backlight_device *bd)
5852{
5853 int status, res;
5854
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005855 res = mutex_lock_killable(&brightness_mutex);
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005856 if (res < 0)
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005857 return 0;
5858
5859 res = tpacpi_brightness_get_raw(&status);
5860
5861 mutex_unlock(&brightness_mutex);
5862
5863 if (res < 0)
5864 return 0;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005865
5866 return status & TP_EC_BACKLIGHT_LVLMSK;
5867}
5868
Richard Purdie599a52d2007-02-10 23:07:48 +00005869static struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005870 .get_brightness = brightness_get,
5871 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005872};
5873
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005874/* --------------------------------------------------------------------- */
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03005875
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03005876/*
5877 * These are only useful for models that have only one possibility
5878 * of GPU. If the BIOS model handles both ATI and Intel, don't use
5879 * these quirks.
5880 */
5881#define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
5882#define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
5883#define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
5884
5885static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
5886 /* Models with ATI GPUs known to require ECNVRAM mode */
5887 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
5888
Henrique de Moraes Holschuh6da25bf2009-09-12 15:22:11 -03005889 /* Models with ATI GPUs that can use ECNVRAM */
5890 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC),
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03005891 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
5892 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
5893 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
5894
Henrique de Moraes Holschuh6da25bf2009-09-12 15:22:11 -03005895 /* Models with Intel Extreme Graphics 2 */
5896 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC),
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03005897 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_NOEC),
5898 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_NOEC),
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03005899
5900 /* Models with Intel GMA900 */
5901 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
5902 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
5903 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
5904};
5905
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005906static int __init brightness_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005907{
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02005908 int b;
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03005909 unsigned long quirks;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02005910
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005911 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
5912
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03005913 mutex_init(&brightness_mutex);
5914
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03005915 quirks = tpacpi_check_quirks(brightness_quirk_table,
5916 ARRAY_SIZE(brightness_quirk_table));
5917
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03005918 /*
5919 * We always attempt to detect acpi support, so as to switch
5920 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
5921 * going to publish a backlight interface
5922 */
5923 b = tpacpi_check_std_acpi_brightness_support();
5924 if (b > 0) {
Thomas Renninger2dba1b52008-08-01 17:38:03 +02005925
5926 if (acpi_video_backlight_support()) {
5927 if (brightness_enable > 1) {
5928 printk(TPACPI_NOTICE
5929 "Standard ACPI backlight interface "
5930 "available, not loading native one.\n");
5931 return 1;
5932 } else if (brightness_enable == 1) {
5933 printk(TPACPI_NOTICE
5934 "Backlight control force enabled, even if standard "
5935 "ACPI backlight interface is available\n");
5936 }
5937 } else {
5938 if (brightness_enable > 1) {
5939 printk(TPACPI_NOTICE
5940 "Standard ACPI backlight interface not "
5941 "available, thinkpad_acpi native "
5942 "brightness control enabled\n");
5943 }
Henrique de Moraes Holschuhe11e2112007-10-30 17:46:22 -02005944 }
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02005945 }
5946
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03005947 if (!brightness_enable) {
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005948 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03005949 "brightness support disabled by "
5950 "module parameter\n");
5951 return 1;
5952 }
5953
5954 if (b > 16) {
5955 printk(TPACPI_ERR
5956 "Unsupported brightness interface, "
5957 "please contact %s\n", TPACPI_MAIL);
5958 return 1;
5959 }
5960 if (b == 16)
5961 tp_features.bright_16levels = 1;
5962
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005963 /*
5964 * Check for module parameter bogosity, note that we
5965 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
5966 * able to detect "unspecified"
5967 */
5968 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03005969 return -EINVAL;
5970
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005971 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
5972 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
5973 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03005974 if (quirks & TPACPI_BRGHT_Q_EC)
5975 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
5976 else
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005977 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
5978
5979 dbg_printk(TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03005980 "driver auto-selected brightness_mode=%d\n",
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005981 brightness_mode);
5982 }
5983
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03005984 /* Safety */
5985 if (thinkpad_id.vendor != PCI_VENDOR_ID_IBM &&
5986 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
5987 brightness_mode == TPACPI_BRGHT_MODE_EC))
5988 return -EINVAL;
5989
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005990 if (tpacpi_brightness_get_raw(&b) < 0)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03005991 return 1;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02005992
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02005993 if (tp_features.bright_16levels)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005994 printk(TPACPI_INFO
5995 "detected a 16-level brightness capable ThinkPad\n");
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02005996
Henrique de Moraes Holschuh7d5a0152007-04-24 11:48:20 -03005997 ibm_backlight_device = backlight_device_register(
5998 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
5999 &ibm_backlight_data);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006000 if (IS_ERR(ibm_backlight_device)) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006001 printk(TPACPI_ERR "Could not register backlight device\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006002 return PTR_ERR(ibm_backlight_device);
6003 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006004 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6005 "brightness is supported\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006006
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006007 if (quirks & TPACPI_BRGHT_Q_ASK) {
6008 printk(TPACPI_NOTICE
6009 "brightness: will use unverified default: "
6010 "brightness_mode=%d\n", brightness_mode);
6011 printk(TPACPI_NOTICE
6012 "brightness: please report to %s whether it works well "
6013 "or not on your ThinkPad\n", TPACPI_MAIL);
6014 }
6015
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02006016 ibm_backlight_device->props.max_brightness =
6017 (tp_features.bright_16levels)? 15 : 7;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006018 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006019 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00006020
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006021 return 0;
6022}
6023
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006024static void brightness_suspend(pm_message_t state)
6025{
6026 tpacpi_brightness_checkpoint_nvram();
6027}
6028
6029static void brightness_shutdown(void)
6030{
6031 tpacpi_brightness_checkpoint_nvram();
6032}
6033
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006034static void brightness_exit(void)
6035{
6036 if (ibm_backlight_device) {
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006037 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006038 "calling backlight_device_unregister()\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006039 backlight_device_unregister(ibm_backlight_device);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006040 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006041
6042 tpacpi_brightness_checkpoint_nvram();
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006043}
6044
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006045static int brightness_read(char *p)
6046{
6047 int len = 0;
6048 int level;
6049
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006050 level = brightness_get(NULL);
6051 if (level < 0) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006052 len += sprintf(p + len, "level:\t\tunreadable\n");
6053 } else {
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02006054 len += sprintf(p + len, "level:\t\t%d\n", level);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006055 len += sprintf(p + len, "commands:\tup, down\n");
6056 len += sprintf(p + len, "commands:\tlevel <level>"
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02006057 " (<level> is 0-%d)\n",
6058 (tp_features.bright_16levels) ? 15 : 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006059 }
6060
6061 return len;
6062}
6063
6064static int brightness_write(char *buf)
6065{
6066 int level;
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006067 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006068 char *cmd;
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02006069 int max_level = (tp_features.bright_16levels) ? 15 : 7;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006070
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006071 level = brightness_get(NULL);
6072 if (level < 0)
6073 return level;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006074
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006075 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006076 if (strlencmp(cmd, "up") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006077 if (level < max_level)
6078 level++;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006079 } else if (strlencmp(cmd, "down") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006080 if (level > 0)
6081 level--;
6082 } else if (sscanf(cmd, "level %d", &level) == 1 &&
6083 level >= 0 && level <= max_level) {
6084 /* new level set */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006085 } else
6086 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006087 }
6088
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006089 tpacpi_disclose_usertask("procfs brightness",
6090 "set level to %d\n", level);
6091
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006092 /*
6093 * Now we know what the final level should be, so we try to set it.
6094 * Doing it this way makes the syscall restartable in case of EINTR
6095 */
6096 rc = brightness_set(level);
6097 return (rc == -EINTR)? ERESTARTSYS : rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006098}
6099
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006100static struct ibm_struct brightness_driver_data = {
6101 .name = "brightness",
6102 .read = brightness_read,
6103 .write = brightness_write,
6104 .exit = brightness_exit,
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006105 .suspend = brightness_suspend,
6106 .shutdown = brightness_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006107};
6108
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006109/*************************************************************************
6110 * Volume subdriver
6111 */
6112
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006113static int volume_offset = 0x30;
6114
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006115static int volume_read(char *p)
6116{
6117 int len = 0;
6118 u8 level;
6119
6120 if (!acpi_ec_read(volume_offset, &level)) {
6121 len += sprintf(p + len, "level:\t\tunreadable\n");
6122 } else {
6123 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
6124 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
6125 len += sprintf(p + len, "commands:\tup, down, mute\n");
6126 len += sprintf(p + len, "commands:\tlevel <level>"
6127 " (<level> is 0-15)\n");
6128 }
6129
6130 return len;
6131}
6132
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006133static int volume_write(char *buf)
6134{
6135 int cmos_cmd, inc, i;
6136 u8 level, mute;
6137 int new_level, new_mute;
6138 char *cmd;
6139
6140 while ((cmd = next_cmd(&buf))) {
6141 if (!acpi_ec_read(volume_offset, &level))
6142 return -EIO;
6143 new_mute = mute = level & 0x40;
6144 new_level = level = level & 0xf;
6145
6146 if (strlencmp(cmd, "up") == 0) {
6147 if (mute)
6148 new_mute = 0;
6149 else
6150 new_level = level == 15 ? 15 : level + 1;
6151 } else if (strlencmp(cmd, "down") == 0) {
6152 if (mute)
6153 new_mute = 0;
6154 else
6155 new_level = level == 0 ? 0 : level - 1;
6156 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
6157 new_level >= 0 && new_level <= 15) {
6158 /* new_level set */
6159 } else if (strlencmp(cmd, "mute") == 0) {
6160 new_mute = 0x40;
6161 } else
6162 return -EINVAL;
6163
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006164 if (new_level != level) {
6165 /* mute doesn't change */
6166
6167 cmos_cmd = (new_level > level) ?
6168 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006169 inc = new_level > level ? 1 : -1;
6170
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03006171 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006172 !acpi_ec_write(volume_offset, level)))
6173 return -EIO;
6174
6175 for (i = level; i != new_level; i += inc)
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03006176 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006177 !acpi_ec_write(volume_offset, i + inc))
6178 return -EIO;
6179
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006180 if (mute &&
6181 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
6182 !acpi_ec_write(volume_offset, new_level + mute))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006183 return -EIO;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006184 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006185 }
6186
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006187 if (new_mute != mute) {
6188 /* level doesn't change */
6189
6190 cmos_cmd = (new_mute) ?
6191 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006192
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03006193 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006194 !acpi_ec_write(volume_offset, level + new_mute))
6195 return -EIO;
6196 }
6197 }
6198
6199 return 0;
6200}
6201
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006202static struct ibm_struct volume_driver_data = {
6203 .name = "volume",
6204 .read = volume_read,
6205 .write = volume_write,
6206};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006207
6208/*************************************************************************
6209 * Fan subdriver
6210 */
6211
6212/*
6213 * FAN ACCESS MODES
6214 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006215 * TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006216 * ACPI GFAN method: returns fan level
6217 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006218 * see TPACPI_FAN_WR_ACPI_SFAN
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03006219 * EC 0x2f (HFSP) not available if GFAN exists
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006220 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006221 * TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006222 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
6223 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03006224 * EC 0x2f (HFSP) might be available *for reading*, but do not use
6225 * it for writing.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006226 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006227 * TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03006228 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
6229 * Supported on almost all ThinkPads
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006230 *
6231 * Fan speed changes of any sort (including those caused by the
6232 * disengaged mode) are usually done slowly by the firmware as the
6233 * maximum ammount of fan duty cycle change per second seems to be
6234 * limited.
6235 *
6236 * Reading is not available if GFAN exists.
6237 * Writing is not available if SFAN exists.
6238 *
6239 * Bits
6240 * 7 automatic mode engaged;
6241 * (default operation mode of the ThinkPad)
6242 * fan level is ignored in this mode.
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03006243 * 6 full speed mode (takes precedence over bit 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006244 * not available on all thinkpads. May disable
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03006245 * the tachometer while the fan controller ramps up
6246 * the speed (which can take up to a few *minutes*).
6247 * Speeds up fan to 100% duty-cycle, which is far above
6248 * the standard RPM levels. It is not impossible that
6249 * it could cause hardware damage.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006250 * 5-3 unused in some models. Extra bits for fan level
6251 * in others, but still useless as all values above
6252 * 7 map to the same speed as level 7 in these models.
6253 * 2-0 fan level (0..7 usually)
6254 * 0x00 = stop
6255 * 0x07 = max (set when temperatures critical)
6256 * Some ThinkPads may have other levels, see
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006257 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006258 *
6259 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
6260 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
6261 * does so, its initial value is meaningless (0x07).
6262 *
6263 * For firmware bugs, refer to:
6264 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
6265 *
6266 * ----
6267 *
6268 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
6269 * Main fan tachometer reading (in RPM)
6270 *
6271 * This register is present on all ThinkPads with a new-style EC, and
6272 * it is known not to be present on the A21m/e, and T22, as there is
6273 * something else in offset 0x84 according to the ACPI DSDT. Other
6274 * ThinkPads from this same time period (and earlier) probably lack the
6275 * tachometer as well.
6276 *
Nick Andrew877d0312009-01-26 11:06:57 +01006277 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006278 * was never fixed by IBM to report the EC firmware version string
6279 * probably support the tachometer (like the early X models), so
6280 * detecting it is quite hard. We need more data to know for sure.
6281 *
6282 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
6283 * might result.
6284 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03006285 * FIRMWARE BUG: may go stale while the EC is switching to full speed
6286 * mode.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006287 *
6288 * For firmware bugs, refer to:
6289 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
6290 *
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03006291 * ----
6292 *
6293 * ThinkPad EC register 0x31 bit 0 (only on select models)
6294 *
6295 * When bit 0 of EC register 0x31 is zero, the tachometer registers
6296 * show the speed of the main fan. When bit 0 of EC register 0x31
6297 * is one, the tachometer registers show the speed of the auxiliary
6298 * fan.
6299 *
6300 * Fan control seems to affect both fans, regardless of the state
6301 * of this bit.
6302 *
6303 * So far, only the firmware for the X60/X61 non-tablet versions
6304 * seem to support this (firmware TP-7M).
6305 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006306 * TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006307 * ThinkPad X31, X40, X41. Not available in the X60.
6308 *
6309 * FANS ACPI handle: takes three arguments: low speed, medium speed,
6310 * high speed. ACPI DSDT seems to map these three speeds to levels
6311 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
6312 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
6313 *
6314 * The speeds are stored on handles
6315 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
6316 *
6317 * There are three default speed sets, acessible as handles:
6318 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
6319 *
6320 * ACPI DSDT switches which set is in use depending on various
6321 * factors.
6322 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006323 * TPACPI_FAN_WR_TPEC is also available and should be used to
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006324 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
6325 * but the ACPI tables just mention level 7.
6326 */
6327
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006328enum { /* Fan control constants */
6329 fan_status_offset = 0x2f, /* EC register 0x2f */
6330 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
6331 * 0x84 must be read before 0x85 */
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03006332 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
6333 bit 0 selects which fan is active */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006334
6335 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
6336 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
6337
6338 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
6339};
6340
6341enum fan_status_access_mode {
6342 TPACPI_FAN_NONE = 0, /* No fan status or control */
6343 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
6344 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
6345};
6346
6347enum fan_control_access_mode {
6348 TPACPI_FAN_WR_NONE = 0, /* No fan control */
6349 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
6350 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
6351 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
6352};
6353
6354enum fan_control_commands {
6355 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
6356 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
6357 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
6358 * and also watchdog cmd */
6359};
6360
6361static int fan_control_allowed;
6362
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006363static enum fan_status_access_mode fan_status_access_mode;
6364static enum fan_control_access_mode fan_control_access_mode;
6365static enum fan_control_commands fan_control_commands;
6366
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02006367static u8 fan_control_initial_status;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006368static u8 fan_control_desired_level;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006369static u8 fan_control_resume_level;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02006370static int fan_watchdog_maxinterval;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006371
6372static struct mutex fan_mutex;
6373
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006374static void fan_watchdog_fire(struct work_struct *ignored);
Len Brown25c68a32006-12-08 04:43:41 -05006375static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02006376
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006377TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
6378TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006379 "\\FSPD", /* 600e/x, 770e, 770x */
6380 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006381TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006382 "JFNS", /* 770x-JL */
6383 ); /* all others */
6384
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006385/*
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006386 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
6387 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
6388 * be in auto mode (0x80).
6389 *
6390 * This is corrected by any write to HFSP either by the driver, or
6391 * by the firmware.
6392 *
6393 * We assume 0x07 really means auto mode while this quirk is active,
6394 * as this is far more likely than the ThinkPad being in level 7,
6395 * which is only used by the firmware during thermal emergencies.
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03006396 *
6397 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
6398 * TP-70 (T43, R52), which are known to be buggy.
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006399 */
6400
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03006401static void fan_quirk1_setup(void)
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006402{
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006403 if (fan_control_initial_status == 0x07) {
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03006404 printk(TPACPI_NOTICE
6405 "fan_init: initial fan status is unknown, "
6406 "assuming it is in auto mode\n");
6407 tp_features.fan_ctrl_status_undef = 1;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006408 }
6409}
6410
6411static void fan_quirk1_handle(u8 *fan_status)
6412{
6413 if (unlikely(tp_features.fan_ctrl_status_undef)) {
6414 if (*fan_status != fan_control_initial_status) {
6415 /* something changed the HFSP regisnter since
6416 * driver init time, so it is not undefined
6417 * anymore */
6418 tp_features.fan_ctrl_status_undef = 0;
6419 } else {
6420 /* Return most likely status. In fact, it
6421 * might be the only possible status */
6422 *fan_status = TP_EC_FAN_AUTO;
6423 }
6424 }
6425}
6426
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03006427/* Select main fan on X60/X61, NOOP on others */
6428static bool fan_select_fan1(void)
6429{
6430 if (tp_features.second_fan) {
6431 u8 val;
6432
6433 if (ec_read(fan_select_offset, &val) < 0)
6434 return false;
6435 val &= 0xFEU;
6436 if (ec_write(fan_select_offset, val) < 0)
6437 return false;
6438 }
6439 return true;
6440}
6441
6442/* Select secondary fan on X60/X61 */
6443static bool fan_select_fan2(void)
6444{
6445 u8 val;
6446
6447 if (!tp_features.second_fan)
6448 return false;
6449
6450 if (ec_read(fan_select_offset, &val) < 0)
6451 return false;
6452 val |= 0x01U;
6453 if (ec_write(fan_select_offset, val) < 0)
6454 return false;
6455
6456 return true;
6457}
6458
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006459/*
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006460 * Call with fan_mutex held
6461 */
6462static void fan_update_desired_level(u8 status)
6463{
6464 if ((status &
6465 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
6466 if (status > 7)
6467 fan_control_desired_level = 7;
6468 else
6469 fan_control_desired_level = status;
6470 }
6471}
6472
6473static int fan_get_status(u8 *status)
6474{
6475 u8 s;
6476
6477 /* TODO:
6478 * Add TPACPI_FAN_RD_ACPI_FANS ? */
6479
6480 switch (fan_status_access_mode) {
6481 case TPACPI_FAN_RD_ACPI_GFAN:
6482 /* 570, 600e/x, 770e, 770x */
6483
6484 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
6485 return -EIO;
6486
6487 if (likely(status))
6488 *status = s & 0x07;
6489
6490 break;
6491
6492 case TPACPI_FAN_RD_TPEC:
6493 /* all except 570, 600e/x, 770e, 770x */
6494 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
6495 return -EIO;
6496
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006497 if (likely(status)) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006498 *status = s;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006499 fan_quirk1_handle(status);
6500 }
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006501
6502 break;
6503
6504 default:
6505 return -ENXIO;
6506 }
6507
6508 return 0;
6509}
6510
6511static int fan_get_status_safe(u8 *status)
6512{
6513 int rc;
6514 u8 s;
6515
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006516 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006517 return -ERESTARTSYS;
6518 rc = fan_get_status(&s);
6519 if (!rc)
6520 fan_update_desired_level(s);
6521 mutex_unlock(&fan_mutex);
6522
6523 if (status)
6524 *status = s;
6525
6526 return rc;
6527}
6528
6529static int fan_get_speed(unsigned int *speed)
6530{
6531 u8 hi, lo;
6532
6533 switch (fan_status_access_mode) {
6534 case TPACPI_FAN_RD_TPEC:
6535 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03006536 if (unlikely(!fan_select_fan1()))
6537 return -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006538 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
6539 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
6540 return -EIO;
6541
6542 if (likely(speed))
6543 *speed = (hi << 8) | lo;
6544
6545 break;
6546
6547 default:
6548 return -ENXIO;
6549 }
6550
6551 return 0;
6552}
6553
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03006554static int fan2_get_speed(unsigned int *speed)
6555{
6556 u8 hi, lo;
6557 bool rc;
6558
6559 switch (fan_status_access_mode) {
6560 case TPACPI_FAN_RD_TPEC:
6561 /* all except 570, 600e/x, 770e, 770x */
6562 if (unlikely(!fan_select_fan2()))
6563 return -EIO;
6564 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
6565 !acpi_ec_read(fan_rpm_offset + 1, &hi);
6566 fan_select_fan1(); /* play it safe */
6567 if (rc)
6568 return -EIO;
6569
6570 if (likely(speed))
6571 *speed = (hi << 8) | lo;
6572
6573 break;
6574
6575 default:
6576 return -ENXIO;
6577 }
6578
6579 return 0;
6580}
6581
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006582static int fan_set_level(int level)
6583{
6584 if (!fan_control_allowed)
6585 return -EPERM;
6586
6587 switch (fan_control_access_mode) {
6588 case TPACPI_FAN_WR_ACPI_SFAN:
6589 if (level >= 0 && level <= 7) {
6590 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
6591 return -EIO;
6592 } else
6593 return -EINVAL;
6594 break;
6595
6596 case TPACPI_FAN_WR_ACPI_FANS:
6597 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006598 if (!(level & TP_EC_FAN_AUTO) &&
6599 !(level & TP_EC_FAN_FULLSPEED) &&
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006600 ((level < 0) || (level > 7)))
6601 return -EINVAL;
6602
6603 /* safety net should the EC not support AUTO
6604 * or FULLSPEED mode bits and just ignore them */
6605 if (level & TP_EC_FAN_FULLSPEED)
6606 level |= 7; /* safety min speed 7 */
Roel Kluin547266e2008-02-05 00:24:56 +01006607 else if (level & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006608 level |= 4; /* safety min speed 4 */
6609
6610 if (!acpi_ec_write(fan_status_offset, level))
6611 return -EIO;
6612 else
6613 tp_features.fan_ctrl_status_undef = 0;
6614 break;
6615
6616 default:
6617 return -ENXIO;
6618 }
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00006619
6620 vdbg_printk(TPACPI_DBG_FAN,
6621 "fan control: set fan control register to 0x%02x\n", level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006622 return 0;
6623}
6624
6625static int fan_set_level_safe(int level)
6626{
6627 int rc;
6628
6629 if (!fan_control_allowed)
6630 return -EPERM;
6631
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006632 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006633 return -ERESTARTSYS;
6634
6635 if (level == TPACPI_FAN_LAST_LEVEL)
6636 level = fan_control_desired_level;
6637
6638 rc = fan_set_level(level);
6639 if (!rc)
6640 fan_update_desired_level(level);
6641
6642 mutex_unlock(&fan_mutex);
6643 return rc;
6644}
6645
6646static int fan_set_enable(void)
6647{
6648 u8 s;
6649 int rc;
6650
6651 if (!fan_control_allowed)
6652 return -EPERM;
6653
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006654 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006655 return -ERESTARTSYS;
6656
6657 switch (fan_control_access_mode) {
6658 case TPACPI_FAN_WR_ACPI_FANS:
6659 case TPACPI_FAN_WR_TPEC:
6660 rc = fan_get_status(&s);
6661 if (rc < 0)
6662 break;
6663
6664 /* Don't go out of emergency fan mode */
6665 if (s != 7) {
6666 s &= 0x07;
6667 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
6668 }
6669
6670 if (!acpi_ec_write(fan_status_offset, s))
6671 rc = -EIO;
6672 else {
6673 tp_features.fan_ctrl_status_undef = 0;
6674 rc = 0;
6675 }
6676 break;
6677
6678 case TPACPI_FAN_WR_ACPI_SFAN:
6679 rc = fan_get_status(&s);
6680 if (rc < 0)
6681 break;
6682
6683 s &= 0x07;
6684
6685 /* Set fan to at least level 4 */
6686 s |= 4;
6687
6688 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006689 rc = -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006690 else
6691 rc = 0;
6692 break;
6693
6694 default:
6695 rc = -ENXIO;
6696 }
6697
6698 mutex_unlock(&fan_mutex);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00006699
6700 if (!rc)
6701 vdbg_printk(TPACPI_DBG_FAN,
6702 "fan control: set fan control register to 0x%02x\n",
6703 s);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006704 return rc;
6705}
6706
6707static int fan_set_disable(void)
6708{
6709 int rc;
6710
6711 if (!fan_control_allowed)
6712 return -EPERM;
6713
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006714 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006715 return -ERESTARTSYS;
6716
6717 rc = 0;
6718 switch (fan_control_access_mode) {
6719 case TPACPI_FAN_WR_ACPI_FANS:
6720 case TPACPI_FAN_WR_TPEC:
6721 if (!acpi_ec_write(fan_status_offset, 0x00))
6722 rc = -EIO;
6723 else {
6724 fan_control_desired_level = 0;
6725 tp_features.fan_ctrl_status_undef = 0;
6726 }
6727 break;
6728
6729 case TPACPI_FAN_WR_ACPI_SFAN:
6730 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
6731 rc = -EIO;
6732 else
6733 fan_control_desired_level = 0;
6734 break;
6735
6736 default:
6737 rc = -ENXIO;
6738 }
6739
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00006740 if (!rc)
6741 vdbg_printk(TPACPI_DBG_FAN,
6742 "fan control: set fan control register to 0\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006743
6744 mutex_unlock(&fan_mutex);
6745 return rc;
6746}
6747
6748static int fan_set_speed(int speed)
6749{
6750 int rc;
6751
6752 if (!fan_control_allowed)
6753 return -EPERM;
6754
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006755 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006756 return -ERESTARTSYS;
6757
6758 rc = 0;
6759 switch (fan_control_access_mode) {
6760 case TPACPI_FAN_WR_ACPI_FANS:
6761 if (speed >= 0 && speed <= 65535) {
6762 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
6763 speed, speed, speed))
6764 rc = -EIO;
6765 } else
6766 rc = -EINVAL;
6767 break;
6768
6769 default:
6770 rc = -ENXIO;
6771 }
6772
6773 mutex_unlock(&fan_mutex);
6774 return rc;
6775}
6776
6777static void fan_watchdog_reset(void)
6778{
6779 static int fan_watchdog_active;
6780
6781 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
6782 return;
6783
6784 if (fan_watchdog_active)
6785 cancel_delayed_work(&fan_watchdog_task);
6786
6787 if (fan_watchdog_maxinterval > 0 &&
6788 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
6789 fan_watchdog_active = 1;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03006790 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006791 msecs_to_jiffies(fan_watchdog_maxinterval
6792 * 1000))) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006793 printk(TPACPI_ERR
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03006794 "failed to queue the fan watchdog, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006795 "watchdog will not trigger\n");
6796 }
6797 } else
6798 fan_watchdog_active = 0;
6799}
6800
6801static void fan_watchdog_fire(struct work_struct *ignored)
6802{
6803 int rc;
6804
6805 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
6806 return;
6807
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006808 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006809 rc = fan_set_enable();
6810 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006811 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006812 "will try again later...\n", -rc);
6813 /* reschedule for later */
6814 fan_watchdog_reset();
6815 }
6816}
6817
6818/*
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006819 * SYSFS fan layout: hwmon compatible (device)
6820 *
6821 * pwm*_enable:
6822 * 0: "disengaged" mode
6823 * 1: manual mode
6824 * 2: native EC "auto" mode (recommended, hardware default)
6825 *
6826 * pwm*: set speed in manual mode, ignored otherwise.
6827 * 0 is level 0; 255 is level 7. Intermediate points done with linear
6828 * interpolation.
6829 *
6830 * fan*_input: tachometer reading, RPM
6831 *
6832 *
6833 * SYSFS fan layout: extensions
6834 *
6835 * fan_watchdog (driver):
6836 * fan watchdog interval in seconds, 0 disables (default), max 120
6837 */
6838
6839/* sysfs fan pwm1_enable ----------------------------------------------- */
6840static ssize_t fan_pwm1_enable_show(struct device *dev,
6841 struct device_attribute *attr,
6842 char *buf)
6843{
6844 int res, mode;
6845 u8 status;
6846
6847 res = fan_get_status_safe(&status);
6848 if (res)
6849 return res;
6850
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006851 if (status & TP_EC_FAN_FULLSPEED) {
6852 mode = 0;
6853 } else if (status & TP_EC_FAN_AUTO) {
6854 mode = 2;
6855 } else
6856 mode = 1;
6857
6858 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
6859}
6860
6861static ssize_t fan_pwm1_enable_store(struct device *dev,
6862 struct device_attribute *attr,
6863 const char *buf, size_t count)
6864{
6865 unsigned long t;
6866 int res, level;
6867
6868 if (parse_strtoul(buf, 2, &t))
6869 return -EINVAL;
6870
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00006871 tpacpi_disclose_usertask("hwmon pwm1_enable",
6872 "set fan mode to %lu\n", t);
6873
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006874 switch (t) {
6875 case 0:
6876 level = TP_EC_FAN_FULLSPEED;
6877 break;
6878 case 1:
6879 level = TPACPI_FAN_LAST_LEVEL;
6880 break;
6881 case 2:
6882 level = TP_EC_FAN_AUTO;
6883 break;
6884 case 3:
6885 /* reserved for software-controlled auto mode */
6886 return -ENOSYS;
6887 default:
6888 return -EINVAL;
6889 }
6890
6891 res = fan_set_level_safe(level);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03006892 if (res == -ENXIO)
6893 return -EINVAL;
6894 else if (res < 0)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006895 return res;
6896
6897 fan_watchdog_reset();
6898
6899 return count;
6900}
6901
6902static struct device_attribute dev_attr_fan_pwm1_enable =
6903 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
6904 fan_pwm1_enable_show, fan_pwm1_enable_store);
6905
6906/* sysfs fan pwm1 ------------------------------------------------------ */
6907static ssize_t fan_pwm1_show(struct device *dev,
6908 struct device_attribute *attr,
6909 char *buf)
6910{
6911 int res;
6912 u8 status;
6913
6914 res = fan_get_status_safe(&status);
6915 if (res)
6916 return res;
6917
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006918 if ((status &
6919 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
6920 status = fan_control_desired_level;
6921
6922 if (status > 7)
6923 status = 7;
6924
6925 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
6926}
6927
6928static ssize_t fan_pwm1_store(struct device *dev,
6929 struct device_attribute *attr,
6930 const char *buf, size_t count)
6931{
6932 unsigned long s;
6933 int rc;
6934 u8 status, newlevel;
6935
6936 if (parse_strtoul(buf, 255, &s))
6937 return -EINVAL;
6938
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00006939 tpacpi_disclose_usertask("hwmon pwm1",
6940 "set fan speed to %lu\n", s);
6941
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006942 /* scale down from 0-255 to 0-7 */
6943 newlevel = (s >> 5) & 0x07;
6944
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006945 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02006946 return -ERESTARTSYS;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006947
6948 rc = fan_get_status(&status);
6949 if (!rc && (status &
6950 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
6951 rc = fan_set_level(newlevel);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03006952 if (rc == -ENXIO)
6953 rc = -EINVAL;
6954 else if (!rc) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006955 fan_update_desired_level(newlevel);
Henrique de Moraes Holschuhca4ac2f2007-04-27 22:00:11 -03006956 fan_watchdog_reset();
6957 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006958 }
6959
6960 mutex_unlock(&fan_mutex);
6961 return (rc)? rc : count;
6962}
6963
6964static struct device_attribute dev_attr_fan_pwm1 =
6965 __ATTR(pwm1, S_IWUSR | S_IRUGO,
6966 fan_pwm1_show, fan_pwm1_store);
6967
6968/* sysfs fan fan1_input ------------------------------------------------ */
6969static ssize_t fan_fan1_input_show(struct device *dev,
6970 struct device_attribute *attr,
6971 char *buf)
6972{
6973 int res;
6974 unsigned int speed;
6975
6976 res = fan_get_speed(&speed);
6977 if (res < 0)
6978 return res;
6979
6980 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
6981}
6982
6983static struct device_attribute dev_attr_fan_fan1_input =
6984 __ATTR(fan1_input, S_IRUGO,
6985 fan_fan1_input_show, NULL);
6986
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03006987/* sysfs fan fan2_input ------------------------------------------------ */
6988static ssize_t fan_fan2_input_show(struct device *dev,
6989 struct device_attribute *attr,
6990 char *buf)
6991{
6992 int res;
6993 unsigned int speed;
6994
6995 res = fan2_get_speed(&speed);
6996 if (res < 0)
6997 return res;
6998
6999 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7000}
7001
7002static struct device_attribute dev_attr_fan_fan2_input =
7003 __ATTR(fan2_input, S_IRUGO,
7004 fan_fan2_input_show, NULL);
7005
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007006/* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007007static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
7008 char *buf)
7009{
7010 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
7011}
7012
7013static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
7014 const char *buf, size_t count)
7015{
7016 unsigned long t;
7017
7018 if (parse_strtoul(buf, 120, &t))
7019 return -EINVAL;
7020
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03007021 if (!fan_control_allowed)
7022 return -EPERM;
7023
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007024 fan_watchdog_maxinterval = t;
7025 fan_watchdog_reset();
7026
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007027 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
7028
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007029 return count;
7030}
7031
7032static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
7033 fan_fan_watchdog_show, fan_fan_watchdog_store);
7034
7035/* --------------------------------------------------------------------- */
7036static struct attribute *fan_attributes[] = {
7037 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
7038 &dev_attr_fan_fan1_input.attr,
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007039 NULL, /* for fan2_input */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007040 NULL
7041};
7042
7043static const struct attribute_group fan_attr_group = {
7044 .attrs = fan_attributes,
7045};
7046
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007047#define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
7048#define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007049
7050#define TPACPI_FAN_QI(__id1, __id2, __quirks) \
7051 { .vendor = PCI_VENDOR_ID_IBM, \
7052 .bios = TPACPI_MATCH_ANY, \
7053 .ec = TPID(__id1, __id2), \
7054 .quirks = __quirks }
7055
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007056#define TPACPI_FAN_QL(__id1, __id2, __quirks) \
7057 { .vendor = PCI_VENDOR_ID_LENOVO, \
7058 .bios = TPACPI_MATCH_ANY, \
7059 .ec = TPID(__id1, __id2), \
7060 .quirks = __quirks }
7061
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007062static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
7063 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
7064 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
7065 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
7066 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007067 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007068};
7069
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007070#undef TPACPI_FAN_QL
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007071#undef TPACPI_FAN_QI
7072
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007073static int __init fan_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007074{
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007075 int rc;
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007076 unsigned long quirks;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007077
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007078 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7079 "initializing fan subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007080
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03007081 mutex_init(&fan_mutex);
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007082 fan_status_access_mode = TPACPI_FAN_NONE;
7083 fan_control_access_mode = TPACPI_FAN_WR_NONE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007084 fan_control_commands = 0;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007085 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03007086 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007087 tp_features.second_fan = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007088 fan_control_desired_level = 7;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007089
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007090 TPACPI_ACPIHANDLE_INIT(fans);
7091 TPACPI_ACPIHANDLE_INIT(gfan);
7092 TPACPI_ACPIHANDLE_INIT(sfan);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007093
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007094 quirks = tpacpi_check_quirks(fan_quirk_table,
7095 ARRAY_SIZE(fan_quirk_table));
7096
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007097 if (gfan_handle) {
7098 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007099 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007100 } else {
7101 /* all other ThinkPads: note that even old-style
7102 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02007103 if (likely(acpi_ec_read(fan_status_offset,
7104 &fan_control_initial_status))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007105 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007106 if (quirks & TPACPI_FAN_Q1)
7107 fan_quirk1_setup();
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007108 if (quirks & TPACPI_FAN_2FAN) {
7109 tp_features.second_fan = 1;
7110 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7111 "secondary fan support enabled\n");
7112 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007113 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007114 printk(TPACPI_ERR
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007115 "ThinkPad ACPI EC access misbehaving, "
7116 "fan status and control unavailable\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007117 return 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007118 }
7119 }
7120
7121 if (sfan_handle) {
7122 /* 570, 770x-JL */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007123 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02007124 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007125 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007126 } else {
7127 if (!gfan_handle) {
7128 /* gfan without sfan means no fan control */
7129 /* all other models implement TP EC 0x2f control */
7130
7131 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02007132 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007133 fan_control_access_mode =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007134 TPACPI_FAN_WR_ACPI_FANS;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007135 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007136 TPACPI_FAN_CMD_SPEED |
7137 TPACPI_FAN_CMD_LEVEL |
7138 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007139 } else {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007140 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02007141 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007142 TPACPI_FAN_CMD_LEVEL |
7143 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007144 }
7145 }
7146 }
7147
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007148 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7149 "fan is %s, modes %d, %d\n",
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007150 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
7151 fan_control_access_mode != TPACPI_FAN_WR_NONE),
7152 fan_status_access_mode, fan_control_access_mode);
7153
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03007154 /* fan control master switch */
7155 if (!fan_control_allowed) {
7156 fan_control_access_mode = TPACPI_FAN_WR_NONE;
7157 fan_control_commands = 0;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007158 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03007159 "fan control features disabled by parameter\n");
7160 }
7161
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007162 /* update fan_control_desired_level */
7163 if (fan_status_access_mode != TPACPI_FAN_NONE)
7164 fan_get_status_safe(NULL);
7165
7166 if (fan_status_access_mode != TPACPI_FAN_NONE ||
7167 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007168 if (tp_features.second_fan) {
7169 /* attach second fan tachometer */
7170 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
7171 &dev_attr_fan_fan2_input.attr;
7172 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007173 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007174 &fan_attr_group);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007175 if (rc < 0)
7176 return rc;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03007177
7178 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
7179 &driver_attr_fan_watchdog);
7180 if (rc < 0) {
7181 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
7182 &fan_attr_group);
7183 return rc;
7184 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007185 return 0;
7186 } else
7187 return 1;
7188}
7189
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007190static void fan_exit(void)
7191{
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007192 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007193 "cancelling any pending fan watchdog tasks\n");
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007194
7195 /* FIXME: can we really do this unconditionally? */
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007196 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007197 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
7198 &driver_attr_fan_watchdog);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007199
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007200 cancel_delayed_work(&fan_watchdog_task);
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03007201 flush_workqueue(tpacpi_wq);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007202}
7203
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007204static void fan_suspend(pm_message_t state)
7205{
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007206 int rc;
7207
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007208 if (!fan_control_allowed)
7209 return;
7210
7211 /* Store fan status in cache */
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007212 fan_control_resume_level = 0;
7213 rc = fan_get_status_safe(&fan_control_resume_level);
7214 if (rc < 0)
7215 printk(TPACPI_NOTICE
7216 "failed to read fan level for later "
7217 "restore during resume: %d\n", rc);
7218
7219 /* if it is undefined, don't attempt to restore it.
7220 * KEEP THIS LAST */
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007221 if (tp_features.fan_ctrl_status_undef)
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007222 fan_control_resume_level = 0;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007223}
7224
7225static void fan_resume(void)
7226{
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007227 u8 current_level = 7;
7228 bool do_set = false;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007229 int rc;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007230
7231 /* DSDT *always* updates status on resume */
7232 tp_features.fan_ctrl_status_undef = 0;
7233
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007234 if (!fan_control_allowed ||
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007235 !fan_control_resume_level ||
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007236 (fan_get_status_safe(&current_level) < 0))
7237 return;
7238
7239 switch (fan_control_access_mode) {
7240 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007241 /* never decrease fan level */
7242 do_set = (fan_control_resume_level > current_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007243 break;
7244 case TPACPI_FAN_WR_ACPI_FANS:
7245 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007246 /* never decrease fan level, scale is:
7247 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
7248 *
7249 * We expect the firmware to set either 7 or AUTO, but we
7250 * handle FULLSPEED out of paranoia.
7251 *
7252 * So, we can safely only restore FULLSPEED or 7, anything
7253 * else could slow the fan. Restoring AUTO is useless, at
7254 * best that's exactly what the DSDT already set (it is the
7255 * slower it uses).
7256 *
7257 * Always keep in mind that the DSDT *will* have set the
7258 * fans to what the vendor supposes is the best level. We
7259 * muck with it only to speed the fan up.
7260 */
7261 if (fan_control_resume_level != 7 &&
7262 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
7263 return;
7264 else
7265 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
7266 (current_level != fan_control_resume_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007267 break;
7268 default:
7269 return;
7270 }
7271 if (do_set) {
7272 printk(TPACPI_NOTICE
7273 "restoring fan level to 0x%02x\n",
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007274 fan_control_resume_level);
7275 rc = fan_set_level_safe(fan_control_resume_level);
7276 if (rc < 0)
7277 printk(TPACPI_NOTICE
7278 "failed to restore fan level: %d\n", rc);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007279 }
7280}
7281
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007282static int fan_read(char *p)
7283{
7284 int len = 0;
7285 int rc;
7286 u8 status;
7287 unsigned int speed = 0;
7288
7289 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007290 case TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007291 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007292 rc = fan_get_status_safe(&status);
7293 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007294 return rc;
7295
7296 len += sprintf(p + len, "status:\t\t%s\n"
7297 "level:\t\t%d\n",
7298 (status != 0) ? "enabled" : "disabled", status);
7299 break;
7300
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007301 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007302 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007303 rc = fan_get_status_safe(&status);
7304 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007305 return rc;
7306
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007307 len += sprintf(p + len, "status:\t\t%s\n",
7308 (status != 0) ? "enabled" : "disabled");
7309
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007310 rc = fan_get_speed(&speed);
7311 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007312 return rc;
7313
7314 len += sprintf(p + len, "speed:\t\t%d\n", speed);
7315
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007316 if (status & TP_EC_FAN_FULLSPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007317 /* Disengaged mode takes precedence */
7318 len += sprintf(p + len, "level:\t\tdisengaged\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007319 else if (status & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007320 len += sprintf(p + len, "level:\t\tauto\n");
7321 else
7322 len += sprintf(p + len, "level:\t\t%d\n", status);
7323 break;
7324
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007325 case TPACPI_FAN_NONE:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007326 default:
7327 len += sprintf(p + len, "status:\t\tnot supported\n");
7328 }
7329
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007330 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007331 len += sprintf(p + len, "commands:\tlevel <level>");
7332
7333 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007334 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007335 len += sprintf(p + len, " (<level> is 0-7)\n");
7336 break;
7337
7338 default:
7339 len += sprintf(p + len, " (<level> is 0-7, "
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007340 "auto, disengaged, full-speed)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007341 break;
7342 }
7343 }
7344
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007345 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007346 len += sprintf(p + len, "commands:\tenable, disable\n"
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007347 "commands:\twatchdog <timeout> (<timeout> "
7348 "is 0 (off), 1-120 (seconds))\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007349
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007350 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007351 len += sprintf(p + len, "commands:\tspeed <speed>"
7352 " (<speed> is 0-65535)\n");
7353
7354 return len;
7355}
7356
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007357static int fan_write_cmd_level(const char *cmd, int *rc)
7358{
7359 int level;
7360
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02007361 if (strlencmp(cmd, "level auto") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007362 level = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007363 else if ((strlencmp(cmd, "level disengaged") == 0) |
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007364 (strlencmp(cmd, "level full-speed") == 0))
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007365 level = TP_EC_FAN_FULLSPEED;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02007366 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007367 return 0;
7368
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007369 *rc = fan_set_level_safe(level);
7370 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007371 printk(TPACPI_ERR "level command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007372 "access mode %d", fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007373 else if (!*rc)
7374 tpacpi_disclose_usertask("procfs fan",
7375 "set level to %d\n", level);
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007376
7377 return 1;
7378}
7379
7380static int fan_write_cmd_enable(const char *cmd, int *rc)
7381{
7382 if (strlencmp(cmd, "enable") != 0)
7383 return 0;
7384
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007385 *rc = fan_set_enable();
7386 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007387 printk(TPACPI_ERR "enable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007388 "access mode %d", fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007389 else if (!*rc)
7390 tpacpi_disclose_usertask("procfs fan", "enable\n");
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007391
7392 return 1;
7393}
7394
7395static int fan_write_cmd_disable(const char *cmd, int *rc)
7396{
7397 if (strlencmp(cmd, "disable") != 0)
7398 return 0;
7399
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007400 *rc = fan_set_disable();
7401 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007402 printk(TPACPI_ERR "disable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007403 "access mode %d", fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007404 else if (!*rc)
7405 tpacpi_disclose_usertask("procfs fan", "disable\n");
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007406
7407 return 1;
7408}
7409
7410static int fan_write_cmd_speed(const char *cmd, int *rc)
7411{
7412 int speed;
7413
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02007414 /* TODO:
7415 * Support speed <low> <medium> <high> ? */
7416
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007417 if (sscanf(cmd, "speed %d", &speed) != 1)
7418 return 0;
7419
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007420 *rc = fan_set_speed(speed);
7421 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007422 printk(TPACPI_ERR "speed command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007423 "access mode %d", fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007424 else if (!*rc)
7425 tpacpi_disclose_usertask("procfs fan",
7426 "set speed to %d\n", speed);
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007427
7428 return 1;
7429}
7430
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007431static int fan_write_cmd_watchdog(const char *cmd, int *rc)
7432{
7433 int interval;
7434
7435 if (sscanf(cmd, "watchdog %d", &interval) != 1)
7436 return 0;
7437
7438 if (interval < 0 || interval > 120)
7439 *rc = -EINVAL;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007440 else {
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007441 fan_watchdog_maxinterval = interval;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007442 tpacpi_disclose_usertask("procfs fan",
7443 "set watchdog timer to %d\n",
7444 interval);
7445 }
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007446
7447 return 1;
7448}
7449
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007450static int fan_write(char *buf)
7451{
7452 char *cmd;
7453 int rc = 0;
7454
7455 while (!rc && (cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007456 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007457 fan_write_cmd_level(cmd, &rc)) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007458 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007459 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007460 fan_write_cmd_disable(cmd, &rc) ||
7461 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007462 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007463 fan_write_cmd_speed(cmd, &rc))
7464 )
7465 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007466 else if (!rc)
7467 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007468 }
7469
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007470 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007471}
7472
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007473static struct ibm_struct fan_driver_data = {
7474 .name = "fan",
7475 .read = fan_read,
7476 .write = fan_write,
7477 .exit = fan_exit,
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007478 .suspend = fan_suspend,
7479 .resume = fan_resume,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007480};
7481
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007482/****************************************************************************
7483 ****************************************************************************
7484 *
7485 * Infrastructure
7486 *
7487 ****************************************************************************
7488 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07007489
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007490/* sysfs name ---------------------------------------------------------- */
7491static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
7492 struct device_attribute *attr,
7493 char *buf)
7494{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007495 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007496}
7497
7498static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
7499 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
7500
7501/* --------------------------------------------------------------------- */
7502
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007503/* /proc support */
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03007504static struct proc_dir_entry *proc_dir;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007505
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007506/*
7507 * Module and infrastructure proble, init and exit handling
7508 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007509
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007510static int force_load;
7511
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007512#ifdef CONFIG_THINKPAD_ACPI_DEBUG
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007513static const char * __init str_supported(int is_supported)
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007514{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007515 static char text_unsupported[] __initdata = "not supported";
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007516
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007517 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007518}
7519#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
7520
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007521static void ibm_exit(struct ibm_struct *ibm)
7522{
7523 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
7524
7525 list_del_init(&ibm->all_drivers);
7526
7527 if (ibm->flags.acpi_notify_installed) {
7528 dbg_printk(TPACPI_DBG_EXIT,
7529 "%s: acpi_remove_notify_handler\n", ibm->name);
7530 BUG_ON(!ibm->acpi);
7531 acpi_remove_notify_handler(*ibm->acpi->handle,
7532 ibm->acpi->type,
7533 dispatch_acpi_notify);
7534 ibm->flags.acpi_notify_installed = 0;
7535 ibm->flags.acpi_notify_installed = 0;
7536 }
7537
7538 if (ibm->flags.proc_created) {
7539 dbg_printk(TPACPI_DBG_EXIT,
7540 "%s: remove_proc_entry\n", ibm->name);
7541 remove_proc_entry(ibm->name, proc_dir);
7542 ibm->flags.proc_created = 0;
7543 }
7544
7545 if (ibm->flags.acpi_driver_registered) {
7546 dbg_printk(TPACPI_DBG_EXIT,
7547 "%s: acpi_bus_unregister_driver\n", ibm->name);
7548 BUG_ON(!ibm->acpi);
7549 acpi_bus_unregister_driver(ibm->acpi->driver);
7550 kfree(ibm->acpi->driver);
7551 ibm->acpi->driver = NULL;
7552 ibm->flags.acpi_driver_registered = 0;
7553 }
7554
7555 if (ibm->flags.init_called && ibm->exit) {
7556 ibm->exit();
7557 ibm->flags.init_called = 0;
7558 }
7559
7560 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
7561}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007562
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007563static int __init ibm_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007564{
7565 int ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007566 struct ibm_struct *ibm = iibm->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007567 struct proc_dir_entry *entry;
7568
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007569 BUG_ON(ibm == NULL);
7570
7571 INIT_LIST_HEAD(&ibm->all_drivers);
7572
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03007573 if (ibm->flags.experimental && !experimental)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007574 return 0;
7575
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007576 dbg_printk(TPACPI_DBG_INIT,
7577 "probing for %s\n", ibm->name);
7578
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007579 if (iibm->init) {
7580 ret = iibm->init(iibm);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007581 if (ret > 0)
7582 return 0; /* probe failed */
7583 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007584 return ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007585
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03007586 ibm->flags.init_called = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007587 }
7588
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03007589 if (ibm->acpi) {
7590 if (ibm->acpi->hid) {
7591 ret = register_tpacpi_subdriver(ibm);
7592 if (ret)
7593 goto err_out;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007594 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03007595
7596 if (ibm->acpi->notify) {
7597 ret = setup_acpi_notify(ibm);
7598 if (ret == -ENODEV) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007599 printk(TPACPI_NOTICE "disabling subdriver %s\n",
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03007600 ibm->name);
7601 ret = 0;
7602 goto err_out;
7603 }
7604 if (ret < 0)
7605 goto err_out;
7606 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007607 }
7608
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007609 dbg_printk(TPACPI_DBG_INIT,
7610 "%s installed\n", ibm->name);
7611
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007612 if (ibm->read) {
7613 entry = create_proc_entry(ibm->name,
7614 S_IFREG | S_IRUGO | S_IWUSR,
7615 proc_dir);
7616 if (!entry) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007617 printk(TPACPI_ERR "unable to create proc entry %s\n",
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007618 ibm->name);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007619 ret = -ENODEV;
7620 goto err_out;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007621 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007622 entry->data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03007623 entry->read_proc = &dispatch_procfs_read;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007624 if (ibm->write)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03007625 entry->write_proc = &dispatch_procfs_write;
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03007626 ibm->flags.proc_created = 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007627 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007628
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007629 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
7630
Linus Torvalds1da177e2005-04-16 15:20:36 -07007631 return 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007632
7633err_out:
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007634 dbg_printk(TPACPI_DBG_INIT,
7635 "%s: at error exit path with result %d\n",
7636 ibm->name, ret);
7637
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007638 ibm_exit(ibm);
7639 return (ret < 0)? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007640}
7641
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007642/* Probing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007643
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03007644static bool __pure __init tpacpi_is_fw_digit(const char c)
7645{
7646 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
7647}
7648
7649/* Most models: xxyTkkWW (#.##c); Ancient 570/600 and -SL lacks (#.##c) */
7650static bool __pure __init tpacpi_is_valid_fw_id(const char* const s,
7651 const char t)
7652{
7653 return s && strlen(s) >= 8 &&
7654 tpacpi_is_fw_digit(s[0]) &&
7655 tpacpi_is_fw_digit(s[1]) &&
7656 s[2] == t && s[3] == 'T' &&
7657 tpacpi_is_fw_digit(s[4]) &&
7658 tpacpi_is_fw_digit(s[5]) &&
7659 s[6] == 'W' && s[7] == 'W';
7660}
7661
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007662/* returns 0 - probe ok, or < 0 - probe error.
7663 * Probe ok doesn't mean thinkpad found.
7664 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
7665static int __must_check __init get_thinkpad_model_data(
7666 struct thinkpad_id_data *tp)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02007667{
Jeff Garzik18552562007-10-03 15:15:40 -04007668 const struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02007669 char ec_fw_string[18];
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007670 char const *s;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02007671
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007672 if (!tp)
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007673 return -EINVAL;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007674
7675 memset(tp, 0, sizeof(*tp));
7676
7677 if (dmi_name_in_vendors("IBM"))
7678 tp->vendor = PCI_VENDOR_ID_IBM;
7679 else if (dmi_name_in_vendors("LENOVO"))
7680 tp->vendor = PCI_VENDOR_ID_LENOVO;
7681 else
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007682 return 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007683
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007684 s = dmi_get_system_info(DMI_BIOS_VERSION);
7685 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
7686 if (s && !tp->bios_version_str)
7687 return -ENOMEM;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03007688
7689 /* Really ancient ThinkPad 240X will fail this, which is fine */
7690 if (!tpacpi_is_valid_fw_id(tp->bios_version_str, 'E'))
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007691 return 0;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03007692
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007693 tp->bios_model = tp->bios_version_str[0]
7694 | (tp->bios_version_str[1] << 8);
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03007695 tp->bios_release = (tp->bios_version_str[4] << 8)
7696 | tp->bios_version_str[5];
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007697
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02007698 /*
7699 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
7700 * X32 or newer, all Z series; Some models must have an
7701 * up-to-date BIOS or they will not be detected.
7702 *
7703 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
7704 */
7705 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02007706 if (sscanf(dev->name,
7707 "IBM ThinkPad Embedded Controller -[%17c",
7708 ec_fw_string) == 1) {
7709 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
7710 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007711
7712 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007713 if (!tp->ec_version_str)
7714 return -ENOMEM;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03007715
7716 if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) {
7717 tp->ec_model = ec_fw_string[0]
7718 | (ec_fw_string[1] << 8);
7719 tp->ec_release = (ec_fw_string[4] << 8)
7720 | ec_fw_string[5];
7721 } else {
7722 printk(TPACPI_NOTICE
7723 "ThinkPad firmware release %s "
7724 "doesn't match the known patterns\n",
7725 ec_fw_string);
7726 printk(TPACPI_NOTICE
7727 "please report this to %s\n",
7728 TPACPI_MAIL);
7729 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007730 break;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02007731 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02007732 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007733
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007734 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
7735 if (s && !strnicmp(s, "ThinkPad", 8)) {
7736 tp->model_str = kstrdup(s, GFP_KERNEL);
7737 if (!tp->model_str)
7738 return -ENOMEM;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007739 }
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03007740
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007741 s = dmi_get_system_info(DMI_PRODUCT_NAME);
7742 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
7743 if (s && !tp->nummodel_str)
7744 return -ENOMEM;
7745
7746 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007747}
7748
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007749static int __init probe_for_thinkpad(void)
7750{
7751 int is_thinkpad;
7752
7753 if (acpi_disabled)
7754 return -ENODEV;
7755
7756 /*
7757 * Non-ancient models have better DMI tagging, but very old models
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03007758 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007759 */
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03007760 is_thinkpad = (thinkpad_id.model_str != NULL) ||
7761 (thinkpad_id.ec_model != 0) ||
7762 tpacpi_is_fw_known();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007763
7764 /* ec is required because many other handles are relative to it */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007765 TPACPI_ACPIHANDLE_INIT(ec);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007766 if (!ec_handle) {
7767 if (is_thinkpad)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007768 printk(TPACPI_ERR
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007769 "Not yet supported ThinkPad detected!\n");
7770 return -ENODEV;
7771 }
7772
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03007773 if (!is_thinkpad && !force_load)
7774 return -ENODEV;
7775
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007776 return 0;
7777}
7778
7779
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007780/* Module init, exit, parameters */
7781
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007782static struct ibm_init_struct ibms_init[] __initdata = {
7783 {
7784 .init = thinkpad_acpi_driver_init,
7785 .data = &thinkpad_acpi_driver_data,
7786 },
7787 {
7788 .init = hotkey_init,
7789 .data = &hotkey_driver_data,
7790 },
7791 {
7792 .init = bluetooth_init,
7793 .data = &bluetooth_driver_data,
7794 },
7795 {
7796 .init = wan_init,
7797 .data = &wan_driver_data,
7798 },
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02007799 {
7800 .init = uwb_init,
7801 .data = &uwb_driver_data,
7802 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02007803#ifdef CONFIG_THINKPAD_ACPI_VIDEO
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007804 {
7805 .init = video_init,
7806 .data = &video_driver_data,
7807 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02007808#endif
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007809 {
7810 .init = light_init,
7811 .data = &light_driver_data,
7812 },
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007813 {
7814 .init = cmos_init,
7815 .data = &cmos_driver_data,
7816 },
7817 {
7818 .init = led_init,
7819 .data = &led_driver_data,
7820 },
7821 {
7822 .init = beep_init,
7823 .data = &beep_driver_data,
7824 },
7825 {
7826 .init = thermal_init,
7827 .data = &thermal_driver_data,
7828 },
7829 {
7830 .data = &ecdump_driver_data,
7831 },
7832 {
7833 .init = brightness_init,
7834 .data = &brightness_driver_data,
7835 },
7836 {
7837 .data = &volume_driver_data,
7838 },
7839 {
7840 .init = fan_init,
7841 .data = &fan_driver_data,
7842 },
7843};
7844
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007845static int __init set_ibm_param(const char *val, struct kernel_param *kp)
7846{
7847 unsigned int i;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007848 struct ibm_struct *ibm;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007849
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02007850 if (!kp || !kp->name || !val)
7851 return -EINVAL;
7852
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007853 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
7854 ibm = ibms_init[i].data;
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02007855 WARN_ON(ibm == NULL);
7856
7857 if (!ibm || !ibm->name)
7858 continue;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007859
7860 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
7861 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007862 return -ENOSPC;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007863 strcpy(ibms_init[i].param, val);
7864 strcat(ibms_init[i].param, ",");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007865 return 0;
7866 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007867 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007868
7869 return -EINVAL;
7870}
7871
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007872module_param(experimental, int, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007873MODULE_PARM_DESC(experimental,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007874 "Enables experimental features when non-zero");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007875
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03007876module_param_named(debug, dbg_level, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007877MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03007878
Henrique de Moraes Holschuh86cc9442007-07-18 23:45:41 -03007879module_param(force_load, bool, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007880MODULE_PARM_DESC(force_load,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007881 "Attempts to load the driver even on a "
7882 "mis-identified ThinkPad when true");
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03007883
Henrique de Moraes Holschuh86cc9442007-07-18 23:45:41 -03007884module_param_named(fan_control, fan_control_allowed, bool, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007885MODULE_PARM_DESC(fan_control,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007886 "Enables setting fan parameters features when true");
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03007887
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00007888module_param_named(brightness_mode, brightness_mode, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007889MODULE_PARM_DESC(brightness_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007890 "Selects brightness control strategy: "
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00007891 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03007892
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02007893module_param(brightness_enable, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007894MODULE_PARM_DESC(brightness_enable,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007895 "Enables backlight control when 1, disables when 0");
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02007896
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03007897module_param(hotkey_report_mode, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007898MODULE_PARM_DESC(hotkey_report_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007899 "used for backwards compatibility with userspace, "
7900 "see documentation");
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03007901
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007902#define TPACPI_PARAM(feature) \
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007903 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
Henrique de Moraes Holschuhcbb14842008-02-16 02:17:50 -02007904 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007905 "at module load, see documentation")
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007906
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007907TPACPI_PARAM(hotkey);
7908TPACPI_PARAM(bluetooth);
7909TPACPI_PARAM(video);
7910TPACPI_PARAM(light);
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007911TPACPI_PARAM(cmos);
7912TPACPI_PARAM(led);
7913TPACPI_PARAM(beep);
7914TPACPI_PARAM(ecdump);
7915TPACPI_PARAM(brightness);
7916TPACPI_PARAM(volume);
7917TPACPI_PARAM(fan);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007918
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02007919#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
7920module_param(dbg_wlswemul, uint, 0);
7921MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
7922module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
7923MODULE_PARM_DESC(wlsw_state,
7924 "Initial state of the emulated WLSW switch");
7925
7926module_param(dbg_bluetoothemul, uint, 0);
7927MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
7928module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
7929MODULE_PARM_DESC(bluetooth_state,
7930 "Initial state of the emulated bluetooth switch");
7931
7932module_param(dbg_wwanemul, uint, 0);
7933MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
7934module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
7935MODULE_PARM_DESC(wwan_state,
7936 "Initial state of the emulated WWAN switch");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02007937
7938module_param(dbg_uwbemul, uint, 0);
7939MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
7940module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
7941MODULE_PARM_DESC(uwb_state,
7942 "Initial state of the emulated UWB switch");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02007943#endif
7944
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007945static void thinkpad_acpi_module_exit(void)
7946{
7947 struct ibm_struct *ibm, *itmp;
7948
7949 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
7950
7951 list_for_each_entry_safe_reverse(ibm, itmp,
7952 &tpacpi_all_drivers,
7953 all_drivers) {
7954 ibm_exit(ibm);
7955 }
7956
7957 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
7958
7959 if (tpacpi_inputdev) {
7960 if (tp_features.input_device_registered)
7961 input_unregister_device(tpacpi_inputdev);
7962 else
7963 input_free_device(tpacpi_inputdev);
7964 }
7965
7966 if (tpacpi_hwmon)
7967 hwmon_device_unregister(tpacpi_hwmon);
7968
7969 if (tp_features.sensors_pdev_attrs_registered)
7970 device_remove_file(&tpacpi_sensors_pdev->dev,
7971 &dev_attr_thinkpad_acpi_pdev_name);
7972 if (tpacpi_sensors_pdev)
7973 platform_device_unregister(tpacpi_sensors_pdev);
7974 if (tpacpi_pdev)
7975 platform_device_unregister(tpacpi_pdev);
7976
7977 if (tp_features.sensors_pdrv_attrs_registered)
7978 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
7979 if (tp_features.platform_drv_attrs_registered)
7980 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
7981
7982 if (tp_features.sensors_pdrv_registered)
7983 platform_driver_unregister(&tpacpi_hwmon_pdriver);
7984
7985 if (tp_features.platform_drv_registered)
7986 platform_driver_unregister(&tpacpi_pdriver);
7987
7988 if (proc_dir)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007989 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007990
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03007991 if (tpacpi_wq)
7992 destroy_workqueue(tpacpi_wq);
7993
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007994 kfree(thinkpad_id.bios_version_str);
7995 kfree(thinkpad_id.ec_version_str);
7996 kfree(thinkpad_id.model_str);
7997}
7998
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007999
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03008000static int __init thinkpad_acpi_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008001{
8002 int ret, i;
8003
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03008004 tpacpi_lifecycle = TPACPI_LIFE_INIT;
8005
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03008006 /* Parameter checking */
8007 if (hotkey_report_mode > 2)
8008 return -EINVAL;
8009
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008010 /* Driver-level probe */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008011
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008012 ret = get_thinkpad_model_data(&thinkpad_id);
8013 if (ret) {
8014 printk(TPACPI_ERR
8015 "unable to get DMI data: %d\n", ret);
8016 thinkpad_acpi_module_exit();
8017 return ret;
8018 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008019 ret = probe_for_thinkpad();
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008020 if (ret) {
8021 thinkpad_acpi_module_exit();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008022 return ret;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008023 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008024
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008025 /* Driver initialization */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008026
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008027 TPACPI_ACPIHANDLE_INIT(ecrd);
8028 TPACPI_ACPIHANDLE_INIT(ecwr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008029
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03008030 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
8031 if (!tpacpi_wq) {
8032 thinkpad_acpi_module_exit();
8033 return -ENOMEM;
8034 }
8035
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008036 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008037 if (!proc_dir) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008038 printk(TPACPI_ERR
8039 "unable to create proc dir " TPACPI_PROC_DIR);
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03008040 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008041 return -ENODEV;
8042 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008043
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008044 ret = platform_driver_register(&tpacpi_pdriver);
8045 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008046 printk(TPACPI_ERR
8047 "unable to register main platform driver\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008048 thinkpad_acpi_module_exit();
8049 return ret;
8050 }
Henrique de Moraes Holschuhac363932007-07-27 17:04:40 -03008051 tp_features.platform_drv_registered = 1;
8052
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008053 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
8054 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008055 printk(TPACPI_ERR
8056 "unable to register hwmon platform driver\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008057 thinkpad_acpi_module_exit();
8058 return ret;
8059 }
8060 tp_features.sensors_pdrv_registered = 1;
8061
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03008062 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03008063 if (!ret) {
8064 tp_features.platform_drv_attrs_registered = 1;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008065 ret = tpacpi_create_driver_attributes(
8066 &tpacpi_hwmon_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03008067 }
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03008068 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008069 printk(TPACPI_ERR
8070 "unable to create sysfs driver attributes\n");
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03008071 thinkpad_acpi_module_exit();
8072 return ret;
8073 }
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03008074 tp_features.sensors_pdrv_attrs_registered = 1;
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03008075
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008076
8077 /* Device initialization */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008078 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008079 NULL, 0);
8080 if (IS_ERR(tpacpi_pdev)) {
8081 ret = PTR_ERR(tpacpi_pdev);
8082 tpacpi_pdev = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008083 printk(TPACPI_ERR "unable to register platform device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008084 thinkpad_acpi_module_exit();
8085 return ret;
8086 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008087 tpacpi_sensors_pdev = platform_device_register_simple(
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008088 TPACPI_HWMON_DRVR_NAME,
8089 -1, NULL, 0);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008090 if (IS_ERR(tpacpi_sensors_pdev)) {
8091 ret = PTR_ERR(tpacpi_sensors_pdev);
8092 tpacpi_sensors_pdev = NULL;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008093 printk(TPACPI_ERR
8094 "unable to register hwmon platform device\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008095 thinkpad_acpi_module_exit();
8096 return ret;
8097 }
8098 ret = device_create_file(&tpacpi_sensors_pdev->dev,
8099 &dev_attr_thinkpad_acpi_pdev_name);
8100 if (ret) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008101 printk(TPACPI_ERR
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008102 "unable to create sysfs hwmon device attributes\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008103 thinkpad_acpi_module_exit();
8104 return ret;
8105 }
8106 tp_features.sensors_pdev_attrs_registered = 1;
8107 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008108 if (IS_ERR(tpacpi_hwmon)) {
8109 ret = PTR_ERR(tpacpi_hwmon);
8110 tpacpi_hwmon = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008111 printk(TPACPI_ERR "unable to register hwmon device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008112 thinkpad_acpi_module_exit();
8113 return ret;
8114 }
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -03008115 mutex_init(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03008116 tpacpi_inputdev = input_allocate_device();
8117 if (!tpacpi_inputdev) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008118 printk(TPACPI_ERR "unable to allocate input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03008119 thinkpad_acpi_module_exit();
8120 return -ENOMEM;
8121 } else {
8122 /* Prepare input device, but don't register */
8123 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008124 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03008125 tpacpi_inputdev->id.bustype = BUS_HOST;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03008126 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
8127 thinkpad_id.vendor :
8128 PCI_VENDOR_ID_IBM;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03008129 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
8130 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
8131 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008132 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
8133 ret = ibm_init(&ibms_init[i]);
8134 if (ret >= 0 && *ibms_init[i].param)
8135 ret = ibms_init[i].data->write(ibms_init[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008136 if (ret < 0) {
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03008137 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008138 return ret;
8139 }
8140 }
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03008141 ret = input_register_device(tpacpi_inputdev);
8142 if (ret < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008143 printk(TPACPI_ERR "unable to register input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03008144 thinkpad_acpi_module_exit();
8145 return ret;
8146 } else {
8147 tp_features.input_device_registered = 1;
8148 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008149
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03008150 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008151 return 0;
8152}
8153
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03008154MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
8155
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008156/*
Henrique de Moraes Holschuh922fe092009-04-14 02:44:13 +00008157 * This will autoload the driver in almost every ThinkPad
8158 * in widespread use.
8159 *
8160 * Only _VERY_ old models, like the 240, 240x and 570 lack
8161 * the HKEY event interface.
8162 */
8163MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
8164
8165/*
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008166 * DMI matching for module autoloading
8167 *
8168 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
8169 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
8170 *
8171 * Only models listed in thinkwiki will be supported, so add yours
8172 * if it is not there yet.
8173 */
8174#define IBM_BIOS_MODULE_ALIAS(__type) \
Mathieu Chouquet-Stringerb36a50f2009-03-14 16:35:26 +01008175 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008176
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008177/* Ancient thinkpad BIOSes have to be identified by
8178 * BIOS type or model number, and there are far less
8179 * BIOS types than model numbers... */
Henrique de Moraes Holschuh922fe092009-04-14 02:44:13 +00008180IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008181
Henrique de Moraes Holschuhf68f53a2009-04-14 02:44:12 +00008182MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
8183MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008184MODULE_DESCRIPTION(TPACPI_DESC);
8185MODULE_VERSION(TPACPI_VERSION);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008186MODULE_LICENSE("GPL");
8187
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03008188module_init(thinkpad_acpi_module_init);
8189module_exit(thinkpad_acpi_module_exit);