blob: cc4155c3620e7698056888e25cff3569121ce2ec [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -03002 * thinkpad_acpi.c - ThinkPad ACPI Extras
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 *
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005 * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
Henrique de Moraes Holschuh1c762ca2009-04-04 04:25:42 +00006 * Copyright (C) 2006-2009 Henrique de Moraes Holschuh <hmh@hmh.eng.br>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
Henrique de Moraes Holschuha62bc912007-03-23 17:33:58 -030020 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301, USA.
Borislav Deianov78f81cc2005-08-17 00:00:00 -040022 */
23
Henrique de Moraes Holschuhb57f7e72009-04-14 02:44:14 +000024#define TPACPI_VERSION "0.23"
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -030025#define TPACPI_SYSFS_VERSION 0x020400
Borislav Deianov78f81cc2005-08-17 00:00:00 -040026
27/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 * Changelog:
Henrique de Moraes Holschuh4b45cc02008-01-08 13:02:46 -020029 * 2007-10-20 changelog trimmed down
30 *
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -030031 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
32 * drivers/misc.
Henrique de Moraes Holschuhf9ff43a2006-11-25 16:37:38 -020033 *
34 * 2006-11-22 0.13 new maintainer
35 * changelog now lives in git commit history, and will
36 * not be updated further in-file.
Henrique de Moraes Holschuh837ca6d2007-03-23 17:33:54 -030037 *
Borislav Deianov78f81cc2005-08-17 00:00:00 -040038 * 2005-03-17 0.11 support for 600e, 770x
39 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
Henrique de Moraes Holschuh4b45cc02008-01-08 13:02:46 -020040 *
41 * 2005-01-16 0.9 use MODULE_VERSION
Borislav Deianov78f81cc2005-08-17 00:00:00 -040042 * thanks to Henrik Brix Andersen <brix@gentoo.org>
43 * fix parameter passing on module loading
44 * thanks to Rusty Russell <rusty@rustcorp.com.au>
45 * thanks to Jim Radford <radford@blackbean.org>
46 * 2004-11-08 0.8 fix init error case, don't return from a macro
47 * thanks to Chris Wright <chrisw@osdl.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 */
49
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020050#include <linux/kernel.h>
51#include <linux/module.h>
52#include <linux/init.h>
53#include <linux/types.h>
54#include <linux/string.h>
55#include <linux/list.h>
56#include <linux/mutex.h>
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +000057#include <linux/sched.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020058#include <linux/kthread.h>
59#include <linux/freezer.h>
60#include <linux/delay.h>
61
62#include <linux/nvram.h>
63#include <linux/proc_fs.h>
64#include <linux/sysfs.h>
65#include <linux/backlight.h>
66#include <linux/fb.h>
67#include <linux/platform_device.h>
68#include <linux/hwmon.h>
69#include <linux/hwmon-sysfs.h>
70#include <linux/input.h>
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -030071#include <linux/leds.h>
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -030072#include <linux/rfkill.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020073#include <asm/uaccess.h>
74
75#include <linux/dmi.h>
76#include <linux/jiffies.h>
77#include <linux/workqueue.h>
78
79#include <acpi/acpi_drivers.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020080
81#include <linux/pci_ids.h>
82
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020083
84/* ThinkPad CMOS commands */
85#define TP_CMOS_VOLUME_DOWN 0
86#define TP_CMOS_VOLUME_UP 1
87#define TP_CMOS_VOLUME_MUTE 2
88#define TP_CMOS_BRIGHTNESS_UP 4
89#define TP_CMOS_BRIGHTNESS_DOWN 5
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -030090#define TP_CMOS_THINKLIGHT_ON 12
91#define TP_CMOS_THINKLIGHT_OFF 13
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020092
93/* NVRAM Addresses */
94enum tp_nvram_addr {
95 TP_NVRAM_ADDR_HK2 = 0x57,
96 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
97 TP_NVRAM_ADDR_VIDEO = 0x59,
98 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
99 TP_NVRAM_ADDR_MIXER = 0x60,
100};
101
102/* NVRAM bit masks */
103enum {
104 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
105 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
106 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
107 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
108 TP_NVRAM_MASK_THINKLIGHT = 0x10,
109 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
110 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
111 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
112 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
113 TP_NVRAM_MASK_MUTE = 0x40,
114 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
115 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
116 TP_NVRAM_POS_LEVEL_VOLUME = 0,
117};
118
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200119/* ACPI HIDs */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200120#define TPACPI_ACPI_HKEY_HID "IBM0068"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200121
122/* Input IDs */
123#define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
124#define TPACPI_HKEY_INPUT_VERSION 0x4101
125
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -0200126/* ACPI \WGSV commands */
127enum {
128 TP_ACPI_WGSV_GET_STATE = 0x01, /* Get state information */
129 TP_ACPI_WGSV_PWR_ON_ON_RESUME = 0x02, /* Resume WWAN powered on */
130 TP_ACPI_WGSV_PWR_OFF_ON_RESUME = 0x03, /* Resume WWAN powered off */
131 TP_ACPI_WGSV_SAVE_STATE = 0x04, /* Save state for S4/S5 */
132};
133
134/* TP_ACPI_WGSV_GET_STATE bits */
135enum {
136 TP_ACPI_WGSV_STATE_WWANEXIST = 0x0001, /* WWAN hw available */
137 TP_ACPI_WGSV_STATE_WWANPWR = 0x0002, /* WWAN radio enabled */
138 TP_ACPI_WGSV_STATE_WWANPWRRES = 0x0004, /* WWAN state at resume */
139 TP_ACPI_WGSV_STATE_WWANBIOSOFF = 0x0008, /* WWAN disabled in BIOS */
140 TP_ACPI_WGSV_STATE_BLTHEXIST = 0x0001, /* BLTH hw available */
141 TP_ACPI_WGSV_STATE_BLTHPWR = 0x0002, /* BLTH radio enabled */
142 TP_ACPI_WGSV_STATE_BLTHPWRRES = 0x0004, /* BLTH state at resume */
143 TP_ACPI_WGSV_STATE_BLTHBIOSOFF = 0x0008, /* BLTH disabled in BIOS */
144 TP_ACPI_WGSV_STATE_UWBEXIST = 0x0010, /* UWB hw available */
145 TP_ACPI_WGSV_STATE_UWBPWR = 0x0020, /* UWB radio enabled */
146};
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200147
148/****************************************************************************
149 * Main driver
150 */
151
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200152#define TPACPI_NAME "thinkpad"
153#define TPACPI_DESC "ThinkPad ACPI Extras"
154#define TPACPI_FILE TPACPI_NAME "_acpi"
155#define TPACPI_URL "http://ibm-acpi.sf.net/"
156#define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200157
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200158#define TPACPI_PROC_DIR "ibm"
159#define TPACPI_ACPI_EVENT_PREFIX "ibm"
160#define TPACPI_DRVR_NAME TPACPI_FILE
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300161#define TPACPI_DRVR_SHORTNAME "tpacpi"
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200162#define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200163
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300164#define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -0300165#define TPACPI_WORKQUEUE_NAME "ktpacpid"
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300166
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200167#define TPACPI_MAX_ACPI_ARGS 3
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200168
Henrique de Moraes Holschuh7ff8d622009-04-04 04:25:46 +0000169/* printk headers */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200170#define TPACPI_LOG TPACPI_FILE ": "
Henrique de Moraes Holschuh7ff8d622009-04-04 04:25:46 +0000171#define TPACPI_EMERG KERN_EMERG TPACPI_LOG
172#define TPACPI_ALERT KERN_ALERT TPACPI_LOG
173#define TPACPI_CRIT KERN_CRIT TPACPI_LOG
174#define TPACPI_ERR KERN_ERR TPACPI_LOG
175#define TPACPI_WARN KERN_WARNING TPACPI_LOG
176#define TPACPI_NOTICE KERN_NOTICE TPACPI_LOG
177#define TPACPI_INFO KERN_INFO TPACPI_LOG
178#define TPACPI_DEBUG KERN_DEBUG TPACPI_LOG
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200179
Henrique de Moraes Holschuh7ff8d622009-04-04 04:25:46 +0000180/* Debugging printk groups */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200181#define TPACPI_DBG_ALL 0xffff
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +0000182#define TPACPI_DBG_DISCLOSETASK 0x8000
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200183#define TPACPI_DBG_INIT 0x0001
184#define TPACPI_DBG_EXIT 0x0002
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +0000185#define TPACPI_DBG_RFKILL 0x0004
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +0000186#define TPACPI_DBG_HKEY 0x0008
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +0000187#define TPACPI_DBG_FAN 0x0010
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +0000188#define TPACPI_DBG_BRGHT 0x0020
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200189
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200190#define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
191#define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
192#define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200193
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200194
195/****************************************************************************
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200196 * Driver-wide structs and misc. variables
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200197 */
198
199struct ibm_struct;
200
201struct tp_acpi_drv_struct {
202 const struct acpi_device_id *hid;
203 struct acpi_driver *driver;
204
205 void (*notify) (struct ibm_struct *, u32);
206 acpi_handle *handle;
207 u32 type;
208 struct acpi_device *device;
209};
210
211struct ibm_struct {
212 char *name;
213
214 int (*read) (char *);
215 int (*write) (char *);
216 void (*exit) (void);
217 void (*resume) (void);
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200218 void (*suspend) (pm_message_t state);
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200219 void (*shutdown) (void);
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200220
221 struct list_head all_drivers;
222
223 struct tp_acpi_drv_struct *acpi;
224
225 struct {
226 u8 acpi_driver_registered:1;
227 u8 acpi_notify_installed:1;
228 u8 proc_created:1;
229 u8 init_called:1;
230 u8 experimental:1;
231 } flags;
232};
233
234struct ibm_init_struct {
235 char param[32];
236
237 int (*init) (struct ibm_init_struct *);
238 struct ibm_struct *data;
239};
240
241static struct {
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200242 u32 bluetooth:1;
243 u32 hotkey:1;
244 u32 hotkey_mask:1;
245 u32 hotkey_wlsw:1;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -0200246 u32 hotkey_tablet:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200247 u32 light:1;
248 u32 light_status:1;
249 u32 bright_16levels:1;
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -0300250 u32 bright_acpimode:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200251 u32 wan:1;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -0200252 u32 uwb:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200253 u32 fan_ctrl_status_undef:1;
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -0300254 u32 second_fan:1;
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -0300255 u32 beep_needs_two_args:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200256 u32 input_device_registered:1;
257 u32 platform_drv_registered:1;
258 u32 platform_drv_attrs_registered:1;
259 u32 sensors_pdrv_registered:1;
260 u32 sensors_pdrv_attrs_registered:1;
261 u32 sensors_pdev_attrs_registered:1;
262 u32 hotkey_poll_active:1;
263} tp_features;
264
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -0300265static struct {
266 u16 hotkey_mask_ff:1;
267} tp_warned;
268
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200269struct thinkpad_id_data {
270 unsigned int vendor; /* ThinkPad vendor:
271 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
272
273 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
274 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
275
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -0300276 u16 bios_model; /* 1Y = 0x5931, 0 = unknown */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200277 u16 ec_model;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -0300278 u16 bios_release; /* 1ZETK1WW = 0x314b, 0 = unknown */
279 u16 ec_release;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200280
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -0300281 char *model_str; /* ThinkPad T43 */
282 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200283};
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200284static struct thinkpad_id_data thinkpad_id;
285
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300286static enum {
287 TPACPI_LIFE_INIT = 0,
288 TPACPI_LIFE_RUNNING,
289 TPACPI_LIFE_EXITING,
290} tpacpi_lifecycle;
291
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200292static int experimental;
293static u32 dbg_level;
294
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -0300295static struct workqueue_struct *tpacpi_wq;
296
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +0000297enum led_status_t {
298 TPACPI_LED_OFF = 0,
299 TPACPI_LED_ON,
300 TPACPI_LED_BLINK,
301};
302
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -0300303/* Special LED class that can defer work */
304struct tpacpi_led_classdev {
305 struct led_classdev led_classdev;
306 struct work_struct work;
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +0000307 enum led_status_t new_state;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -0300308 unsigned int led;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -0300309};
310
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200311#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
312static int dbg_wlswemul;
313static int tpacpi_wlsw_emulstate;
314static int dbg_bluetoothemul;
315static int tpacpi_bluetooth_emulstate;
316static int dbg_wwanemul;
317static int tpacpi_wwan_emulstate;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -0200318static int dbg_uwbemul;
319static int tpacpi_uwb_emulstate;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200320#endif
321
322
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000323/*************************************************************************
324 * Debugging helpers
325 */
326
327#define dbg_printk(a_dbg_level, format, arg...) \
328 do { if (dbg_level & (a_dbg_level)) \
329 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
330 } while (0)
331
332#ifdef CONFIG_THINKPAD_ACPI_DEBUG
333#define vdbg_printk dbg_printk
334static const char *str_supported(int is_supported);
335#else
336#define vdbg_printk(a_dbg_level, format, arg...) \
337 do { } while (0)
338#endif
339
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +0000340static void tpacpi_log_usertask(const char * const what)
341{
342 printk(TPACPI_DEBUG "%s: access by process with PID %d\n",
343 what, task_tgid_vnr(current));
344}
345
346#define tpacpi_disclose_usertask(what, format, arg...) \
347 do { \
348 if (unlikely( \
349 (dbg_level & TPACPI_DBG_DISCLOSETASK) && \
350 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
351 printk(TPACPI_DEBUG "%s: PID %d: " format, \
352 what, task_tgid_vnr(current), ## arg); \
353 } \
354 } while (0)
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000355
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -0300356/*
357 * Quirk handling helpers
358 *
359 * ThinkPad IDs and versions seen in the field so far
360 * are two-characters from the set [0-9A-Z], i.e. base 36.
361 *
362 * We use values well outside that range as specials.
363 */
364
365#define TPACPI_MATCH_ANY 0xffffU
366#define TPACPI_MATCH_UNKNOWN 0U
367
368/* TPID('1', 'Y') == 0x5931 */
369#define TPID(__c1, __c2) (((__c2) << 8) | (__c1))
370
371#define TPACPI_Q_IBM(__id1, __id2, __quirk) \
372 { .vendor = PCI_VENDOR_ID_IBM, \
373 .bios = TPID(__id1, __id2), \
374 .ec = TPACPI_MATCH_ANY, \
375 .quirks = (__quirk) }
376
377#define TPACPI_Q_LNV(__id1, __id2, __quirk) \
378 { .vendor = PCI_VENDOR_ID_LENOVO, \
379 .bios = TPID(__id1, __id2), \
380 .ec = TPACPI_MATCH_ANY, \
381 .quirks = (__quirk) }
382
383struct tpacpi_quirk {
384 unsigned int vendor;
385 u16 bios;
386 u16 ec;
387 unsigned long quirks;
388};
389
390/**
391 * tpacpi_check_quirks() - search BIOS/EC version on a list
392 * @qlist: array of &struct tpacpi_quirk
393 * @qlist_size: number of elements in @qlist
394 *
395 * Iterates over a quirks list until one is found that matches the
396 * ThinkPad's vendor, BIOS and EC model.
397 *
398 * Returns 0 if nothing matches, otherwise returns the quirks field of
399 * the matching &struct tpacpi_quirk entry.
400 *
401 * The match criteria is: vendor, ec and bios much match.
402 */
403static unsigned long __init tpacpi_check_quirks(
404 const struct tpacpi_quirk *qlist,
405 unsigned int qlist_size)
406{
407 while (qlist_size) {
408 if ((qlist->vendor == thinkpad_id.vendor ||
409 qlist->vendor == TPACPI_MATCH_ANY) &&
410 (qlist->bios == thinkpad_id.bios_model ||
411 qlist->bios == TPACPI_MATCH_ANY) &&
412 (qlist->ec == thinkpad_id.ec_model ||
413 qlist->ec == TPACPI_MATCH_ANY))
414 return qlist->quirks;
415
416 qlist_size--;
417 qlist++;
418 }
419 return 0;
420}
421
422
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300423/****************************************************************************
424 ****************************************************************************
425 *
426 * ACPI Helpers and device model
427 *
428 ****************************************************************************
429 ****************************************************************************/
430
431/*************************************************************************
432 * ACPI basic handles
433 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -0300435static acpi_handle root_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200437#define TPACPI_HANDLE(object, parent, paths...) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 static acpi_handle object##_handle; \
439 static acpi_handle *object##_parent = &parent##_handle; \
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400440 static char *object##_path; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 static char *object##_paths[] = { paths }
442
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200443TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400444 "\\_SB.PCI.ISA.EC", /* 570 */
445 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
446 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
447 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
448 "\\_SB.PCI0.ICH3.EC0", /* R31 */
449 "\\_SB.PCI0.LPC.EC", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300450 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200452TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
453TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200455TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
456 /* T4x, X31, X40 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400457 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
458 "\\CMS", /* R40, R40e */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300459 ); /* all others */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400460
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200461TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400462 "^HKEY", /* R30, R31 */
463 "HKEY", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300464 ); /* 570 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400465
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -0200466TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
467 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
468 "\\_SB.PCI0.VID0", /* 770e */
469 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
470 "\\_SB.PCI0.AGP.VID", /* all others */
471 ); /* R30, R31 */
472
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400473
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300474/*************************************************************************
475 * ACPI helpers
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -0200476 */
477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478static int acpi_evalf(acpi_handle handle,
479 void *res, char *method, char *fmt, ...)
480{
481 char *fmt0 = fmt;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400482 struct acpi_object_list params;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200483 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400484 struct acpi_buffer result, *resultp;
485 union acpi_object out_obj;
486 acpi_status status;
487 va_list ap;
488 char res_type;
489 int success;
490 int quiet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
492 if (!*fmt) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200493 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 return 0;
495 }
496
497 if (*fmt == 'q') {
498 quiet = 1;
499 fmt++;
500 } else
501 quiet = 0;
502
503 res_type = *(fmt++);
504
505 params.count = 0;
506 params.pointer = &in_objs[0];
507
508 va_start(ap, fmt);
509 while (*fmt) {
510 char c = *(fmt++);
511 switch (c) {
512 case 'd': /* int */
513 in_objs[params.count].integer.value = va_arg(ap, int);
514 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
515 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400516 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 default:
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200518 printk(TPACPI_ERR "acpi_evalf() called "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 "with invalid format character '%c'\n", c);
520 return 0;
521 }
522 }
523 va_end(ap);
524
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400525 if (res_type != 'v') {
526 result.length = sizeof(out_obj);
527 result.pointer = &out_obj;
528 resultp = &result;
529 } else
530 resultp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400532 status = acpi_evaluate_object(handle, method, &params, resultp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
534 switch (res_type) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400535 case 'd': /* int */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 if (res)
537 *(int *)res = out_obj.integer.value;
538 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
539 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400540 case 'v': /* void */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 success = status == AE_OK;
542 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400543 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 default:
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200545 printk(TPACPI_ERR "acpi_evalf() called "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 "with invalid format character '%c'\n", res_type);
547 return 0;
548 }
549
550 if (!success && !quiet)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200551 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 method, fmt0, status);
553
554 return success;
555}
556
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200557static int acpi_ec_read(int i, u8 *p)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300558{
559 int v;
560
561 if (ecrd_handle) {
562 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
563 return 0;
564 *p = v;
565 } else {
566 if (ec_read(i, p) < 0)
567 return 0;
568 }
569
570 return 1;
571}
572
573static int acpi_ec_write(int i, u8 v)
574{
575 if (ecwr_handle) {
576 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
577 return 0;
578 } else {
579 if (ec_write(i, v) < 0)
580 return 0;
581 }
582
583 return 1;
584}
585
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -0300586static int issue_thinkpad_cmos_command(int cmos_cmd)
587{
588 if (!cmos_handle)
589 return -ENXIO;
590
591 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
592 return -EIO;
593
594 return 0;
595}
596
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300597/*************************************************************************
598 * ACPI device model
599 */
600
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200601#define TPACPI_ACPIHANDLE_INIT(object) \
602 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200603 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
604
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300605static void drv_acpi_handle_init(char *name,
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300606 acpi_handle *handle, acpi_handle parent,
607 char **paths, int num_paths, char **path)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300608{
609 int i;
610 acpi_status status;
611
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300612 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
613 name);
614
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300615 for (i = 0; i < num_paths; i++) {
616 status = acpi_get_handle(parent, paths[i], handle);
617 if (ACPI_SUCCESS(status)) {
618 *path = paths[i];
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300619 dbg_printk(TPACPI_DBG_INIT,
620 "Found ACPI handle %s for %s\n",
621 *path, name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300622 return;
623 }
624 }
625
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300626 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
627 name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300628 *handle = NULL;
629}
630
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300631static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300632{
633 struct ibm_struct *ibm = data;
634
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300635 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
636 return;
637
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300638 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300639 return;
640
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300641 ibm->acpi->notify(ibm, event);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300642}
643
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300644static int __init setup_acpi_notify(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300645{
646 acpi_status status;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300647 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300648
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300649 BUG_ON(!ibm->acpi);
650
651 if (!*ibm->acpi->handle)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300652 return 0;
653
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300654 vdbg_printk(TPACPI_DBG_INIT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300655 "setting up ACPI notify for %s\n", ibm->name);
656
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300657 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
658 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200659 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300660 ibm->name, rc);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300661 return -ENODEV;
662 }
663
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700664 ibm->acpi->device->driver_data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300665 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200666 TPACPI_ACPI_EVENT_PREFIX,
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -0300667 ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300668
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300669 status = acpi_install_notify_handler(*ibm->acpi->handle,
670 ibm->acpi->type, dispatch_acpi_notify, ibm);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300671 if (ACPI_FAILURE(status)) {
672 if (status == AE_ALREADY_EXISTS) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200673 printk(TPACPI_NOTICE
674 "another device driver is already "
675 "handling %s events\n", ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300676 } else {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200677 printk(TPACPI_ERR
678 "acpi_install_notify_handler(%s) failed: %d\n",
679 ibm->name, status);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300680 }
681 return -ENODEV;
682 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300683 ibm->flags.acpi_notify_installed = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300684 return 0;
685}
686
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300687static int __init tpacpi_device_add(struct acpi_device *device)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300688{
689 return 0;
690}
691
Henrique de Moraes Holschuh67001212007-04-21 11:08:25 -0300692static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300693{
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300694 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300695
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300696 dbg_printk(TPACPI_DBG_INIT,
697 "registering %s as an ACPI driver\n", ibm->name);
698
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300699 BUG_ON(!ibm->acpi);
700
701 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
702 if (!ibm->acpi->driver) {
Mariusz Kozlowski4f778b92008-10-18 14:23:53 -0300703 printk(TPACPI_ERR
704 "failed to allocate memory for ibm->acpi->driver\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300705 return -ENOMEM;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300706 }
707
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200708 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300709 ibm->acpi->driver->ids = ibm->acpi->hid;
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200710
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300711 ibm->acpi->driver->ops.add = &tpacpi_device_add;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300712
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300713 rc = acpi_bus_register_driver(ibm->acpi->driver);
714 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200715 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200716 ibm->name, rc);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300717 kfree(ibm->acpi->driver);
718 ibm->acpi->driver = NULL;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300719 } else if (!rc)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300720 ibm->flags.acpi_driver_registered = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300721
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300722 return rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300723}
724
725
726/****************************************************************************
727 ****************************************************************************
728 *
729 * Procfs Helpers
730 *
731 ****************************************************************************
732 ****************************************************************************/
733
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300734static int dispatch_procfs_read(char *page, char **start, off_t off,
735 int count, int *eof, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300736{
737 struct ibm_struct *ibm = data;
738 int len;
739
740 if (!ibm || !ibm->read)
741 return -EINVAL;
742
743 len = ibm->read(page);
744 if (len < 0)
745 return len;
746
747 if (len <= off + count)
748 *eof = 1;
749 *start = page + off;
750 len -= off;
751 if (len > count)
752 len = count;
753 if (len < 0)
754 len = 0;
755
756 return len;
757}
758
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300759static int dispatch_procfs_write(struct file *file,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200760 const char __user *userbuf,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300761 unsigned long count, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300762{
763 struct ibm_struct *ibm = data;
764 char *kernbuf;
765 int ret;
766
767 if (!ibm || !ibm->write)
768 return -EINVAL;
Michael Buesch5b05d462009-08-01 12:04:19 -0300769 if (count > PAGE_SIZE - 2)
770 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300771
772 kernbuf = kmalloc(count + 2, GFP_KERNEL);
773 if (!kernbuf)
774 return -ENOMEM;
775
776 if (copy_from_user(kernbuf, userbuf, count)) {
777 kfree(kernbuf);
778 return -EFAULT;
779 }
780
781 kernbuf[count] = 0;
782 strcat(kernbuf, ",");
783 ret = ibm->write(kernbuf);
784 if (ret == 0)
785 ret = count;
786
787 kfree(kernbuf);
788
789 return ret;
790}
791
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792static char *next_cmd(char **cmds)
793{
794 char *start = *cmds;
795 char *end;
796
797 while ((end = strchr(start, ',')) && end == start)
798 start = end + 1;
799
800 if (!end)
801 return NULL;
802
803 *end = 0;
804 *cmds = end + 1;
805 return start;
806}
807
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300808
809/****************************************************************************
810 ****************************************************************************
811 *
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300812 * Device model: input, hwmon and platform
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300813 *
814 ****************************************************************************
815 ****************************************************************************/
816
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -0300817static struct platform_device *tpacpi_pdev;
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300818static struct platform_device *tpacpi_sensors_pdev;
Tony Jones1beeffe2007-08-20 13:46:20 -0700819static struct device *tpacpi_hwmon;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300820static struct input_dev *tpacpi_inputdev;
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -0300821static struct mutex tpacpi_inputdev_send_mutex;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200822static LIST_HEAD(tpacpi_all_drivers);
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300823
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200824static int tpacpi_suspend_handler(struct platform_device *pdev,
825 pm_message_t state)
826{
827 struct ibm_struct *ibm, *itmp;
828
829 list_for_each_entry_safe(ibm, itmp,
830 &tpacpi_all_drivers,
831 all_drivers) {
832 if (ibm->suspend)
833 (ibm->suspend)(state);
834 }
835
836 return 0;
837}
838
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300839static int tpacpi_resume_handler(struct platform_device *pdev)
840{
841 struct ibm_struct *ibm, *itmp;
842
843 list_for_each_entry_safe(ibm, itmp,
844 &tpacpi_all_drivers,
845 all_drivers) {
846 if (ibm->resume)
847 (ibm->resume)();
848 }
849
850 return 0;
851}
852
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200853static void tpacpi_shutdown_handler(struct platform_device *pdev)
854{
855 struct ibm_struct *ibm, *itmp;
856
857 list_for_each_entry_safe(ibm, itmp,
858 &tpacpi_all_drivers,
859 all_drivers) {
860 if (ibm->shutdown)
861 (ibm->shutdown)();
862 }
863}
864
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300865static struct platform_driver tpacpi_pdriver = {
866 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200867 .name = TPACPI_DRVR_NAME,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300868 .owner = THIS_MODULE,
869 },
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200870 .suspend = tpacpi_suspend_handler,
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300871 .resume = tpacpi_resume_handler,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200872 .shutdown = tpacpi_shutdown_handler,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300873};
874
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300875static struct platform_driver tpacpi_hwmon_pdriver = {
876 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200877 .name = TPACPI_HWMON_DRVR_NAME,
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300878 .owner = THIS_MODULE,
879 },
880};
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300881
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -0300882/*************************************************************************
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300883 * sysfs support helpers
884 */
885
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200886struct attribute_set {
887 unsigned int members, max_members;
888 struct attribute_group group;
889};
890
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300891struct attribute_set_obj {
892 struct attribute_set s;
893 struct attribute *a;
894} __attribute__((packed));
895
896static struct attribute_set *create_attr_set(unsigned int max_members,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200897 const char *name)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300898{
899 struct attribute_set_obj *sobj;
900
901 if (max_members == 0)
902 return NULL;
903
904 /* Allocates space for implicit NULL at the end too */
905 sobj = kzalloc(sizeof(struct attribute_set_obj) +
906 max_members * sizeof(struct attribute *),
907 GFP_KERNEL);
908 if (!sobj)
909 return NULL;
910 sobj->s.max_members = max_members;
911 sobj->s.group.attrs = &sobj->a;
912 sobj->s.group.name = name;
913
914 return &sobj->s;
915}
916
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200917#define destroy_attr_set(_set) \
918 kfree(_set);
919
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300920/* not multi-threaded safe, use it in a single thread per set */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200921static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300922{
923 if (!s || !attr)
924 return -EINVAL;
925
926 if (s->members >= s->max_members)
927 return -ENOMEM;
928
929 s->group.attrs[s->members] = attr;
930 s->members++;
931
932 return 0;
933}
934
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200935static int add_many_to_attr_set(struct attribute_set *s,
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300936 struct attribute **attr,
937 unsigned int count)
938{
939 int i, res;
940
941 for (i = 0; i < count; i++) {
942 res = add_to_attr_set(s, attr[i]);
943 if (res)
944 return res;
945 }
946
947 return 0;
948}
949
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200950static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300951{
952 sysfs_remove_group(kobj, &s->group);
953 destroy_attr_set(s);
954}
955
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200956#define register_attr_set_with_sysfs(_attr_set, _kobj) \
957 sysfs_create_group(_kobj, &_attr_set->group)
958
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300959static int parse_strtoul(const char *buf,
960 unsigned long max, unsigned long *value)
961{
962 char *endp;
963
Henrique de Moraes Holschuh32afbf02007-10-08 10:12:56 -0300964 while (*buf && isspace(*buf))
965 buf++;
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300966 *value = simple_strtoul(buf, &endp, 0);
967 while (*endp && isspace(*endp))
968 endp++;
969 if (*endp || *value > max)
970 return -EINVAL;
971
972 return 0;
973}
974
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -0300975static void tpacpi_disable_brightness_delay(void)
976{
977 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
978 printk(TPACPI_NOTICE
979 "ACPI backlight control delay disabled\n");
980}
981
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -0300982static int __init tpacpi_query_bcl_levels(acpi_handle handle)
983{
984 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
985 union acpi_object *obj;
986 int rc;
987
988 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
989 obj = (union acpi_object *)buffer.pointer;
990 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
991 printk(TPACPI_ERR "Unknown _BCL data, "
992 "please report this to %s\n", TPACPI_MAIL);
993 rc = 0;
994 } else {
995 rc = obj->package.count;
996 }
997 } else {
998 return 0;
999 }
1000
1001 kfree(buffer.pointer);
1002 return rc;
1003}
1004
1005static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
1006 u32 lvl, void *context, void **rv)
1007{
1008 char name[ACPI_PATH_SEGMENT_LENGTH];
1009 struct acpi_buffer buffer = { sizeof(name), &name };
1010
1011 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
1012 !strncmp("_BCL", name, sizeof(name) - 1)) {
1013 BUG_ON(!rv || !*rv);
1014 **(int **)rv = tpacpi_query_bcl_levels(handle);
1015 return AE_CTRL_TERMINATE;
1016 } else {
1017 return AE_OK;
1018 }
1019}
1020
1021/*
1022 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
1023 */
1024static int __init tpacpi_check_std_acpi_brightness_support(void)
1025{
1026 int status;
1027 int bcl_levels = 0;
1028 void *bcl_ptr = &bcl_levels;
1029
1030 if (!vid_handle) {
1031 TPACPI_ACPIHANDLE_INIT(vid);
1032 }
1033 if (!vid_handle)
1034 return 0;
1035
1036 /*
1037 * Search for a _BCL method, and execute it. This is safe on all
1038 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
1039 * BIOS in ACPI backlight control mode. We do NOT have to care
1040 * about calling the _BCL method in an enabled video device, any
1041 * will do for our purposes.
1042 */
1043
1044 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
1045 tpacpi_acpi_walk_find_bcl, NULL,
1046 &bcl_ptr);
1047
1048 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
1049 tp_features.bright_acpimode = 1;
1050 return (bcl_levels - 2);
1051 }
1052
1053 return 0;
1054}
1055
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00001056static void printk_deprecated_attribute(const char * const what,
1057 const char * const details)
1058{
1059 tpacpi_log_usertask("deprecated sysfs attribute");
1060 printk(TPACPI_WARN "WARNING: sysfs attribute %s is deprecated and "
1061 "will be removed. %s\n",
1062 what, details);
1063}
1064
Johannes Berg19d337d2009-06-02 13:01:37 +02001065/*************************************************************************
1066 * rfkill and radio control support helpers
1067 */
1068
1069/*
1070 * ThinkPad-ACPI firmware handling model:
1071 *
1072 * WLSW (master wireless switch) is event-driven, and is common to all
1073 * firmware-controlled radios. It cannot be controlled, just monitored,
1074 * as expected. It overrides all radio state in firmware
1075 *
1076 * The kernel, a masked-off hotkey, and WLSW can change the radio state
1077 * (TODO: verify how WLSW interacts with the returned radio state).
1078 *
1079 * The only time there are shadow radio state changes, is when
1080 * masked-off hotkeys are used.
1081 */
1082
1083/*
1084 * Internal driver API for radio state:
1085 *
1086 * int: < 0 = error, otherwise enum tpacpi_rfkill_state
1087 * bool: true means radio blocked (off)
1088 */
1089enum tpacpi_rfkill_state {
1090 TPACPI_RFK_RADIO_OFF = 0,
1091 TPACPI_RFK_RADIO_ON
1092};
1093
1094/* rfkill switches */
1095enum tpacpi_rfk_id {
1096 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
1097 TPACPI_RFK_WWAN_SW_ID,
1098 TPACPI_RFK_UWB_SW_ID,
1099 TPACPI_RFK_SW_MAX
1100};
1101
1102static const char *tpacpi_rfkill_names[] = {
1103 [TPACPI_RFK_BLUETOOTH_SW_ID] = "bluetooth",
1104 [TPACPI_RFK_WWAN_SW_ID] = "wwan",
1105 [TPACPI_RFK_UWB_SW_ID] = "uwb",
1106 [TPACPI_RFK_SW_MAX] = NULL
1107};
1108
1109/* ThinkPad-ACPI rfkill subdriver */
1110struct tpacpi_rfk {
1111 struct rfkill *rfkill;
1112 enum tpacpi_rfk_id id;
1113 const struct tpacpi_rfk_ops *ops;
1114};
1115
1116struct tpacpi_rfk_ops {
1117 /* firmware interface */
1118 int (*get_status)(void);
1119 int (*set_status)(const enum tpacpi_rfkill_state);
1120};
1121
1122static struct tpacpi_rfk *tpacpi_rfkill_switches[TPACPI_RFK_SW_MAX];
1123
1124/* Query FW and update rfkill sw state for a given rfkill switch */
1125static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk *tp_rfk)
1126{
1127 int status;
1128
1129 if (!tp_rfk)
1130 return -ENODEV;
1131
1132 status = (tp_rfk->ops->get_status)();
1133 if (status < 0)
1134 return status;
1135
1136 rfkill_set_sw_state(tp_rfk->rfkill,
1137 (status == TPACPI_RFK_RADIO_OFF));
1138
1139 return status;
1140}
1141
1142/* Query FW and update rfkill sw state for all rfkill switches */
1143static void tpacpi_rfk_update_swstate_all(void)
1144{
1145 unsigned int i;
1146
1147 for (i = 0; i < TPACPI_RFK_SW_MAX; i++)
1148 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[i]);
1149}
1150
1151/*
1152 * Sync the HW-blocking state of all rfkill switches,
1153 * do notice it causes the rfkill core to schedule uevents
1154 */
1155static void tpacpi_rfk_update_hwblock_state(bool blocked)
1156{
1157 unsigned int i;
1158 struct tpacpi_rfk *tp_rfk;
1159
1160 for (i = 0; i < TPACPI_RFK_SW_MAX; i++) {
1161 tp_rfk = tpacpi_rfkill_switches[i];
1162 if (tp_rfk) {
1163 if (rfkill_set_hw_state(tp_rfk->rfkill,
1164 blocked)) {
1165 /* ignore -- we track sw block */
1166 }
1167 }
1168 }
1169}
1170
1171/* Call to get the WLSW state from the firmware */
1172static int hotkey_get_wlsw(void);
1173
1174/* Call to query WLSW state and update all rfkill switches */
1175static bool tpacpi_rfk_check_hwblock_state(void)
1176{
1177 int res = hotkey_get_wlsw();
1178 int hw_blocked;
1179
1180 /* When unknown or unsupported, we have to assume it is unblocked */
1181 if (res < 0)
1182 return false;
1183
1184 hw_blocked = (res == TPACPI_RFK_RADIO_OFF);
1185 tpacpi_rfk_update_hwblock_state(hw_blocked);
1186
1187 return hw_blocked;
1188}
1189
1190static int tpacpi_rfk_hook_set_block(void *data, bool blocked)
1191{
1192 struct tpacpi_rfk *tp_rfk = data;
1193 int res;
1194
1195 dbg_printk(TPACPI_DBG_RFKILL,
1196 "request to change radio state to %s\n",
1197 blocked ? "blocked" : "unblocked");
1198
1199 /* try to set radio state */
1200 res = (tp_rfk->ops->set_status)(blocked ?
1201 TPACPI_RFK_RADIO_OFF : TPACPI_RFK_RADIO_ON);
1202
1203 /* and update the rfkill core with whatever the FW really did */
1204 tpacpi_rfk_update_swstate(tp_rfk);
1205
1206 return (res < 0) ? res : 0;
1207}
1208
1209static const struct rfkill_ops tpacpi_rfk_rfkill_ops = {
1210 .set_block = tpacpi_rfk_hook_set_block,
1211};
1212
1213static int __init tpacpi_new_rfkill(const enum tpacpi_rfk_id id,
1214 const struct tpacpi_rfk_ops *tp_rfkops,
1215 const enum rfkill_type rfktype,
1216 const char *name,
1217 const bool set_default)
1218{
1219 struct tpacpi_rfk *atp_rfk;
1220 int res;
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001221 bool sw_state = false;
1222 int sw_status;
Johannes Berg19d337d2009-06-02 13:01:37 +02001223
1224 BUG_ON(id >= TPACPI_RFK_SW_MAX || tpacpi_rfkill_switches[id]);
1225
Johannes Berg19d337d2009-06-02 13:01:37 +02001226 atp_rfk = kzalloc(sizeof(struct tpacpi_rfk), GFP_KERNEL);
1227 if (atp_rfk)
1228 atp_rfk->rfkill = rfkill_alloc(name,
1229 &tpacpi_pdev->dev,
1230 rfktype,
1231 &tpacpi_rfk_rfkill_ops,
1232 atp_rfk);
1233 if (!atp_rfk || !atp_rfk->rfkill) {
1234 printk(TPACPI_ERR
1235 "failed to allocate memory for rfkill class\n");
1236 kfree(atp_rfk);
1237 return -ENOMEM;
1238 }
1239
1240 atp_rfk->id = id;
1241 atp_rfk->ops = tp_rfkops;
1242
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001243 sw_status = (tp_rfkops->get_status)();
1244 if (sw_status < 0) {
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001245 printk(TPACPI_ERR
1246 "failed to read initial state for %s, error %d\n",
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001247 name, sw_status);
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001248 } else {
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001249 sw_state = (sw_status == TPACPI_RFK_RADIO_OFF);
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001250 if (set_default) {
1251 /* try to keep the initial state, since we ask the
1252 * firmware to preserve it across S5 in NVRAM */
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001253 rfkill_init_sw_state(atp_rfk->rfkill, sw_state);
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001254 }
1255 }
1256 rfkill_set_hw_state(atp_rfk->rfkill, tpacpi_rfk_check_hwblock_state());
Johannes Berg19d337d2009-06-02 13:01:37 +02001257
1258 res = rfkill_register(atp_rfk->rfkill);
1259 if (res < 0) {
1260 printk(TPACPI_ERR
1261 "failed to register %s rfkill switch: %d\n",
1262 name, res);
1263 rfkill_destroy(atp_rfk->rfkill);
1264 kfree(atp_rfk);
1265 return res;
1266 }
1267
1268 tpacpi_rfkill_switches[id] = atp_rfk;
1269 return 0;
1270}
1271
1272static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id)
1273{
1274 struct tpacpi_rfk *tp_rfk;
1275
1276 BUG_ON(id >= TPACPI_RFK_SW_MAX);
1277
1278 tp_rfk = tpacpi_rfkill_switches[id];
1279 if (tp_rfk) {
1280 rfkill_unregister(tp_rfk->rfkill);
1281 tpacpi_rfkill_switches[id] = NULL;
1282 kfree(tp_rfk);
1283 }
1284}
1285
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00001286static void printk_deprecated_rfkill_attribute(const char * const what)
1287{
1288 printk_deprecated_attribute(what,
1289 "Please switch to generic rfkill before year 2010");
1290}
1291
Johannes Berg19d337d2009-06-02 13:01:37 +02001292/* sysfs <radio> enable ------------------------------------------------ */
1293static ssize_t tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id,
1294 struct device_attribute *attr,
1295 char *buf)
1296{
1297 int status;
1298
1299 printk_deprecated_rfkill_attribute(attr->attr.name);
1300
1301 /* This is in the ABI... */
1302 if (tpacpi_rfk_check_hwblock_state()) {
1303 status = TPACPI_RFK_RADIO_OFF;
1304 } else {
1305 status = tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1306 if (status < 0)
1307 return status;
1308 }
1309
1310 return snprintf(buf, PAGE_SIZE, "%d\n",
1311 (status == TPACPI_RFK_RADIO_ON) ? 1 : 0);
1312}
1313
1314static ssize_t tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id,
1315 struct device_attribute *attr,
1316 const char *buf, size_t count)
1317{
1318 unsigned long t;
1319 int res;
1320
1321 printk_deprecated_rfkill_attribute(attr->attr.name);
1322
1323 if (parse_strtoul(buf, 1, &t))
1324 return -EINVAL;
1325
1326 tpacpi_disclose_usertask(attr->attr.name, "set to %ld\n", t);
1327
1328 /* This is in the ABI... */
1329 if (tpacpi_rfk_check_hwblock_state() && !!t)
1330 return -EPERM;
1331
1332 res = tpacpi_rfkill_switches[id]->ops->set_status((!!t) ?
1333 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF);
1334 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1335
1336 return (res < 0) ? res : count;
1337}
1338
1339/* procfs -------------------------------------------------------------- */
1340static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id, char *p)
1341{
1342 int len = 0;
1343
1344 if (id >= TPACPI_RFK_SW_MAX)
1345 len += sprintf(p + len, "status:\t\tnot supported\n");
1346 else {
1347 int status;
1348
1349 /* This is in the ABI... */
1350 if (tpacpi_rfk_check_hwblock_state()) {
1351 status = TPACPI_RFK_RADIO_OFF;
1352 } else {
1353 status = tpacpi_rfk_update_swstate(
1354 tpacpi_rfkill_switches[id]);
1355 if (status < 0)
1356 return status;
1357 }
1358
1359 len += sprintf(p + len, "status:\t\t%s\n",
1360 (status == TPACPI_RFK_RADIO_ON) ?
1361 "enabled" : "disabled");
1362 len += sprintf(p + len, "commands:\tenable, disable\n");
1363 }
1364
1365 return len;
1366}
1367
1368static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id, char *buf)
1369{
1370 char *cmd;
1371 int status = -1;
1372 int res = 0;
1373
1374 if (id >= TPACPI_RFK_SW_MAX)
1375 return -ENODEV;
1376
1377 while ((cmd = next_cmd(&buf))) {
1378 if (strlencmp(cmd, "enable") == 0)
1379 status = TPACPI_RFK_RADIO_ON;
1380 else if (strlencmp(cmd, "disable") == 0)
1381 status = TPACPI_RFK_RADIO_OFF;
1382 else
1383 return -EINVAL;
1384 }
1385
1386 if (status != -1) {
1387 tpacpi_disclose_usertask("procfs", "attempt to %s %s\n",
1388 (status == TPACPI_RFK_RADIO_ON) ?
1389 "enable" : "disable",
1390 tpacpi_rfkill_names[id]);
1391 res = (tpacpi_rfkill_switches[id]->ops->set_status)(status);
1392 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1393 }
1394
1395 return res;
1396}
1397
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001398/*************************************************************************
1399 * thinkpad-acpi driver attributes
1400 */
1401
1402/* interface_version --------------------------------------------------- */
1403static ssize_t tpacpi_driver_interface_version_show(
1404 struct device_driver *drv,
1405 char *buf)
1406{
1407 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1408}
1409
1410static DRIVER_ATTR(interface_version, S_IRUGO,
1411 tpacpi_driver_interface_version_show, NULL);
1412
1413/* debug_level --------------------------------------------------------- */
1414static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1415 char *buf)
1416{
1417 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1418}
1419
1420static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1421 const char *buf, size_t count)
1422{
1423 unsigned long t;
1424
1425 if (parse_strtoul(buf, 0xffff, &t))
1426 return -EINVAL;
1427
1428 dbg_level = t;
1429
1430 return count;
1431}
1432
1433static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1434 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1435
1436/* version ------------------------------------------------------------- */
1437static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1438 char *buf)
1439{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001440 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1441 TPACPI_DESC, TPACPI_VERSION);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001442}
1443
1444static DRIVER_ATTR(version, S_IRUGO,
1445 tpacpi_driver_version_show, NULL);
1446
1447/* --------------------------------------------------------------------- */
1448
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001449#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1450
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001451/* wlsw_emulstate ------------------------------------------------------ */
1452static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1453 char *buf)
1454{
1455 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1456}
1457
1458static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1459 const char *buf, size_t count)
1460{
1461 unsigned long t;
1462
1463 if (parse_strtoul(buf, 1, &t))
1464 return -EINVAL;
1465
Johannes Berg19d337d2009-06-02 13:01:37 +02001466 if (tpacpi_wlsw_emulstate != !!t) {
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001467 tpacpi_wlsw_emulstate = !!t;
Johannes Berg19d337d2009-06-02 13:01:37 +02001468 tpacpi_rfk_update_hwblock_state(!t); /* negative logic */
1469 }
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001470
1471 return count;
1472}
1473
1474static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1475 tpacpi_driver_wlsw_emulstate_show,
1476 tpacpi_driver_wlsw_emulstate_store);
1477
1478/* bluetooth_emulstate ------------------------------------------------- */
1479static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1480 struct device_driver *drv,
1481 char *buf)
1482{
1483 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1484}
1485
1486static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1487 struct device_driver *drv,
1488 const char *buf, size_t count)
1489{
1490 unsigned long t;
1491
1492 if (parse_strtoul(buf, 1, &t))
1493 return -EINVAL;
1494
1495 tpacpi_bluetooth_emulstate = !!t;
1496
1497 return count;
1498}
1499
1500static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1501 tpacpi_driver_bluetooth_emulstate_show,
1502 tpacpi_driver_bluetooth_emulstate_store);
1503
1504/* wwan_emulstate ------------------------------------------------- */
1505static ssize_t tpacpi_driver_wwan_emulstate_show(
1506 struct device_driver *drv,
1507 char *buf)
1508{
1509 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1510}
1511
1512static ssize_t tpacpi_driver_wwan_emulstate_store(
1513 struct device_driver *drv,
1514 const char *buf, size_t count)
1515{
1516 unsigned long t;
1517
1518 if (parse_strtoul(buf, 1, &t))
1519 return -EINVAL;
1520
1521 tpacpi_wwan_emulstate = !!t;
1522
1523 return count;
1524}
1525
1526static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1527 tpacpi_driver_wwan_emulstate_show,
1528 tpacpi_driver_wwan_emulstate_store);
1529
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001530/* uwb_emulstate ------------------------------------------------- */
1531static ssize_t tpacpi_driver_uwb_emulstate_show(
1532 struct device_driver *drv,
1533 char *buf)
1534{
1535 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1536}
1537
1538static ssize_t tpacpi_driver_uwb_emulstate_store(
1539 struct device_driver *drv,
1540 const char *buf, size_t count)
1541{
1542 unsigned long t;
1543
1544 if (parse_strtoul(buf, 1, &t))
1545 return -EINVAL;
1546
1547 tpacpi_uwb_emulstate = !!t;
1548
1549 return count;
1550}
1551
1552static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
1553 tpacpi_driver_uwb_emulstate_show,
1554 tpacpi_driver_uwb_emulstate_store);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001555#endif
1556
1557/* --------------------------------------------------------------------- */
1558
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001559static struct driver_attribute *tpacpi_driver_attributes[] = {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001560 &driver_attr_debug_level, &driver_attr_version,
1561 &driver_attr_interface_version,
1562};
1563
1564static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1565{
1566 int i, res;
1567
1568 i = 0;
1569 res = 0;
1570 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1571 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1572 i++;
1573 }
1574
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001575#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1576 if (!res && dbg_wlswemul)
1577 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1578 if (!res && dbg_bluetoothemul)
1579 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1580 if (!res && dbg_wwanemul)
1581 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001582 if (!res && dbg_uwbemul)
1583 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001584#endif
1585
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001586 return res;
1587}
1588
1589static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1590{
1591 int i;
1592
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001593 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001594 driver_remove_file(drv, tpacpi_driver_attributes[i]);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001595
1596#ifdef THINKPAD_ACPI_DEBUGFACILITIES
1597 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1598 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1599 driver_remove_file(drv, &driver_attr_wwan_emulstate);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001600 driver_remove_file(drv, &driver_attr_uwb_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001601#endif
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001602}
1603
Henrique de Moraes 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
1624 */
1625
1626#define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1627 { .vendor = (__v), \
1628 .bios = TPID(__id1, __id2), \
1629 .ec = TPACPI_MATCH_ANY, \
1630 .quirks = TPACPI_MATCH_ANY << 16 \
1631 | (__bv1) << 8 | (__bv2) }
1632
1633#define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
1634 __eid1, __eid2, __ev1, __ev2) \
1635 { .vendor = (__v), \
1636 .bios = TPID(__bid1, __bid2), \
1637 .ec = TPID(__eid1, __eid2), \
1638 .quirks = (__ev1) << 24 | (__ev2) << 16 \
1639 | (__bv1) << 8 | (__bv2) }
1640
1641#define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1642 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1643
1644#define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1645 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1646 __bv1, __bv2, __id1, __id2, __ev1, __ev2)
1647
1648#define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1649 __eid1, __eid2, __ev1, __ev2) \
1650 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1651 __bv1, __bv2, __eid1, __eid2, __ev1, __ev2)
1652
1653#define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1654 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1655
1656#define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1657 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
1658 __bv1, __bv2, __id1, __id2, __ev1, __ev2)
1659
1660#define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1661 __eid1, __eid2, __ev1, __ev2) \
1662 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
1663 __bv1, __bv2, __eid1, __eid2, __ev1, __ev2)
1664
1665static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
1666 /* Numeric models ------------------ */
1667 /* FW MODEL BIOS VERS */
1668 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1669 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1670 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1671 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1672 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1673 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1674 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1675 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1676 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1677
1678 /* A-series ------------------------- */
1679 /* FW MODEL BIOS VERS EC VERS */
1680 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1681 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1682 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1683 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1684 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1685 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1686 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1687 TPV_QI0('1', '3', '2', '0'), /* A22m */
1688 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1689 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1690 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1691
1692 /* G-series ------------------------- */
1693 /* FW MODEL BIOS VERS */
1694 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1695 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1696
1697 /* R-series, T-series --------------- */
1698 /* FW MODEL BIOS VERS EC VERS */
1699 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1700 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1701 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1702 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1703 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1704 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1705 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1706 T40/p, T41/p, T42/p (1) */
1707 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1708 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1709 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1710 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1711
1712 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1713 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1714 TPV_QI0('1', '6', '3', '2'), /* T22 */
1715 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1716 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1717 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1718
1719 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1720 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
1721 TPV_QL0('7', 'E', 'D', '0'), /* R60e, R60i */
1722
1723 /* BIOS FW BIOS VERS EC FW EC VERS */
1724 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1725 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1726
1727 /* X-series ------------------------- */
1728 /* FW MODEL BIOS VERS EC VERS */
1729 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1730 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1731 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1732 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1733 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1734 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1735 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1736
1737 TPV_QL0('7', 'B', 'D', '7'), /* X60/s */
1738 TPV_QL0('7', 'J', '3', '0'), /* X60t */
1739
1740 /* (0) - older versions lack DMI EC fw string and functionality */
1741 /* (1) - older versions known to lack functionality */
1742};
1743
1744#undef TPV_QL1
1745#undef TPV_QL0
1746#undef TPV_QI2
1747#undef TPV_QI1
1748#undef TPV_QI0
1749#undef TPV_Q_X
1750#undef TPV_Q
1751
1752static void __init tpacpi_check_outdated_fw(void)
1753{
1754 unsigned long fwvers;
1755 u16 ec_version, bios_version;
1756
1757 fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
1758 ARRAY_SIZE(tpacpi_bios_version_qtable));
1759
1760 if (!fwvers)
1761 return;
1762
1763 bios_version = fwvers & 0xffffU;
1764 ec_version = (fwvers >> 16) & 0xffffU;
1765
1766 /* note that unknown versions are set to 0x0000 and we use that */
1767 if ((bios_version > thinkpad_id.bios_release) ||
1768 (ec_version > thinkpad_id.ec_release &&
1769 ec_version != TPACPI_MATCH_ANY)) {
1770 /*
1771 * The changelogs would let us track down the exact
1772 * reason, but it is just too much of a pain to track
1773 * it. We only list BIOSes that are either really
1774 * broken, or really stable to begin with, so it is
1775 * best if the user upgrades the firmware anyway.
1776 */
1777 printk(TPACPI_WARN
1778 "WARNING: Outdated ThinkPad BIOS/EC firmware\n");
1779 printk(TPACPI_WARN
1780 "WARNING: This firmware may be missing critical bug "
1781 "fixes and/or important features\n");
1782 }
1783}
1784
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03001785/****************************************************************************
1786 ****************************************************************************
1787 *
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001788 * Subdrivers
1789 *
1790 ****************************************************************************
1791 ****************************************************************************/
1792
1793/*************************************************************************
Henrique de Moraes Holschuh142cfc92007-04-21 11:08:26 -03001794 * thinkpad-acpi init subdriver
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001795 */
1796
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001797static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001799 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
1800 printk(TPACPI_INFO "%s\n", TPACPI_URL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001802 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03001803 (thinkpad_id.bios_version_str) ?
1804 thinkpad_id.bios_version_str : "unknown",
1805 (thinkpad_id.ec_version_str) ?
1806 thinkpad_id.ec_version_str : "unknown");
1807
1808 if (thinkpad_id.vendor && thinkpad_id.model_str)
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03001809 printk(TPACPI_INFO "%s %s, model %s\n",
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03001810 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
1811 "IBM" : ((thinkpad_id.vendor ==
1812 PCI_VENDOR_ID_LENOVO) ?
1813 "Lenovo" : "Unknown vendor"),
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03001814 thinkpad_id.model_str,
1815 (thinkpad_id.nummodel_str) ?
1816 thinkpad_id.nummodel_str : "unknown");
Henrique de Moraes Holschuh3945ac32007-02-06 19:13:44 -02001817
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001818 tpacpi_check_outdated_fw();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 return 0;
1820}
1821
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -03001822static int thinkpad_acpi_driver_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823{
1824 int len = 0;
1825
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001826 len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
1827 len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828
1829 return len;
1830}
1831
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001832static struct ibm_struct thinkpad_acpi_driver_data = {
1833 .name = "driver",
1834 .read = thinkpad_acpi_driver_read,
1835};
1836
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001837/*************************************************************************
1838 * Hotkey subdriver
1839 */
1840
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001841enum { /* hot key scan codes (derived from ACPI DSDT) */
1842 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1843 TP_ACPI_HOTKEYSCAN_FNF2,
1844 TP_ACPI_HOTKEYSCAN_FNF3,
1845 TP_ACPI_HOTKEYSCAN_FNF4,
1846 TP_ACPI_HOTKEYSCAN_FNF5,
1847 TP_ACPI_HOTKEYSCAN_FNF6,
1848 TP_ACPI_HOTKEYSCAN_FNF7,
1849 TP_ACPI_HOTKEYSCAN_FNF8,
1850 TP_ACPI_HOTKEYSCAN_FNF9,
1851 TP_ACPI_HOTKEYSCAN_FNF10,
1852 TP_ACPI_HOTKEYSCAN_FNF11,
1853 TP_ACPI_HOTKEYSCAN_FNF12,
1854 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1855 TP_ACPI_HOTKEYSCAN_FNINSERT,
1856 TP_ACPI_HOTKEYSCAN_FNDELETE,
1857 TP_ACPI_HOTKEYSCAN_FNHOME,
1858 TP_ACPI_HOTKEYSCAN_FNEND,
1859 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1860 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1861 TP_ACPI_HOTKEYSCAN_FNSPACE,
1862 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1863 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1864 TP_ACPI_HOTKEYSCAN_MUTE,
1865 TP_ACPI_HOTKEYSCAN_THINKPAD,
1866};
1867
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001868enum { /* Keys available through NVRAM polling */
1869 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1870 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1871};
1872
1873enum { /* Positions of some of the keys in hotkey masks */
1874 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1875 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1876 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1877 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1878 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1879 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1880 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1881 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1882 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1883 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1884 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1885};
1886
1887enum { /* NVRAM to ACPI HKEY group map */
1888 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1889 TP_ACPI_HKEY_ZOOM_MASK |
1890 TP_ACPI_HKEY_DISPSWTCH_MASK |
1891 TP_ACPI_HKEY_HIBERNATE_MASK,
1892 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1893 TP_ACPI_HKEY_BRGHTDWN_MASK,
1894 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1895 TP_ACPI_HKEY_VOLDWN_MASK |
1896 TP_ACPI_HKEY_MUTE_MASK,
1897};
1898
1899#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1900struct tp_nvram_state {
1901 u16 thinkpad_toggle:1;
1902 u16 zoom_toggle:1;
1903 u16 display_toggle:1;
1904 u16 thinklight_toggle:1;
1905 u16 hibernate_toggle:1;
1906 u16 displayexp_toggle:1;
1907 u16 display_state:1;
1908 u16 brightness_toggle:1;
1909 u16 volume_toggle:1;
1910 u16 mute:1;
1911
1912 u8 brightness_level;
1913 u8 volume_level;
1914};
1915
1916static struct task_struct *tpacpi_hotkey_task;
1917static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1918static int hotkey_poll_freq = 10; /* Hz */
1919static struct mutex hotkey_thread_mutex;
1920static struct mutex hotkey_thread_data_mutex;
1921static unsigned int hotkey_config_change;
1922
1923#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1924
1925#define hotkey_source_mask 0U
1926
1927#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1928
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001929static struct mutex hotkey_mutex;
1930
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02001931static enum { /* Reasons for waking up */
1932 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1933 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1934 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1935} hotkey_wakeup_reason;
1936
1937static int hotkey_autosleep_ack;
1938
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001939static u32 hotkey_orig_mask;
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001940static u32 hotkey_all_mask;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001941static u32 hotkey_reserved_mask;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001942static u32 hotkey_mask;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001943
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001944static unsigned int hotkey_report_mode;
1945
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001946static u16 *hotkey_keycode_map;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001947
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -03001948static struct attribute_set *hotkey_dev_attributes;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001949
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001950#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1951#define HOTKEY_CONFIG_CRITICAL_START \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001952 do { \
1953 mutex_lock(&hotkey_thread_data_mutex); \
1954 hotkey_config_change++; \
1955 } while (0);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001956#define HOTKEY_CONFIG_CRITICAL_END \
1957 mutex_unlock(&hotkey_thread_data_mutex);
1958#else
1959#define HOTKEY_CONFIG_CRITICAL_START
1960#define HOTKEY_CONFIG_CRITICAL_END
1961#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1962
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001963/* HKEY.MHKG() return bits */
1964#define TP_HOTKEY_TABLET_MASK (1 << 3)
1965
Johannes Berg19d337d2009-06-02 13:01:37 +02001966static int hotkey_get_wlsw(void)
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001967{
Johannes Berg19d337d2009-06-02 13:01:37 +02001968 int status;
1969
1970 if (!tp_features.hotkey_wlsw)
1971 return -ENODEV;
1972
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001973#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
Johannes Berg19d337d2009-06-02 13:01:37 +02001974 if (dbg_wlswemul)
1975 return (tpacpi_wlsw_emulstate) ?
1976 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001977#endif
Johannes Berg19d337d2009-06-02 13:01:37 +02001978
1979 if (!acpi_evalf(hkey_handle, &status, "WLSW", "d"))
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001980 return -EIO;
Johannes Berg19d337d2009-06-02 13:01:37 +02001981
1982 return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001983}
1984
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001985static int hotkey_get_tablet_mode(int *status)
1986{
1987 int s;
1988
1989 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
1990 return -EIO;
1991
Henrique de Moraes Holschuhcee47f52008-03-04 14:29:21 -08001992 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
1993 return 0;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001994}
1995
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001996/*
1997 * Call with hotkey_mutex held
1998 */
1999static int hotkey_mask_get(void)
2000{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002001 u32 m = 0;
2002
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002003 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002004 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002005 return -EIO;
2006 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002007 hotkey_mask = m | (hotkey_source_mask & hotkey_mask);
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002008
2009 return 0;
2010}
2011
2012/*
2013 * Call with hotkey_mutex held
2014 */
2015static int hotkey_mask_set(u32 mask)
2016{
2017 int i;
2018 int rc = 0;
2019
2020 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -03002021 if (!tp_warned.hotkey_mask_ff &&
2022 (mask == 0xffff || mask == 0xffffff ||
2023 mask == 0xffffffff)) {
2024 tp_warned.hotkey_mask_ff = 1;
2025 printk(TPACPI_NOTICE
2026 "setting the hotkey mask to 0x%08x is likely "
2027 "not the best way to go about it\n", mask);
2028 printk(TPACPI_NOTICE
2029 "please consider using the driver defaults, "
2030 "and refer to up-to-date thinkpad-acpi "
2031 "documentation\n");
2032 }
2033
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002034 HOTKEY_CONFIG_CRITICAL_START
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002035 for (i = 0; i < 32; i++) {
2036 u32 m = 1 << i;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002037 /* enable in firmware mask only keys not in NVRAM
2038 * mode, but enable the key in the cached hotkey_mask
2039 * regardless of mode, or the key will end up
2040 * disabled by hotkey_mask_get() */
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002041 if (!acpi_evalf(hkey_handle,
2042 NULL, "MHKM", "vdd", i + 1,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002043 !!((mask & ~hotkey_source_mask) & m))) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002044 rc = -EIO;
2045 break;
2046 } else {
2047 hotkey_mask = (hotkey_mask & ~m) | (mask & m);
2048 }
2049 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002050 HOTKEY_CONFIG_CRITICAL_END
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002051
2052 /* hotkey_mask_get must be called unconditionally below */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002053 if (!hotkey_mask_get() && !rc &&
2054 (hotkey_mask & ~hotkey_source_mask) !=
2055 (mask & ~hotkey_source_mask)) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002056 printk(TPACPI_NOTICE
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002057 "requested hot key mask 0x%08x, but "
2058 "firmware forced it to 0x%08x\n",
2059 mask, hotkey_mask);
2060 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002061 } else {
2062#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2063 HOTKEY_CONFIG_CRITICAL_START
2064 hotkey_mask = mask & hotkey_source_mask;
2065 HOTKEY_CONFIG_CRITICAL_END
2066 hotkey_mask_get();
2067 if (hotkey_mask != mask) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002068 printk(TPACPI_NOTICE
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002069 "requested hot key mask 0x%08x, "
2070 "forced to 0x%08x (NVRAM poll mask is "
2071 "0x%08x): no firmware mask support\n",
2072 mask, hotkey_mask, hotkey_source_mask);
2073 }
2074#else
2075 hotkey_mask_get();
2076 rc = -ENXIO;
2077#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002078 }
2079
2080 return rc;
2081}
2082
2083static int hotkey_status_get(int *status)
2084{
2085 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
2086 return -EIO;
2087
2088 return 0;
2089}
2090
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002091static int hotkey_status_set(bool enable)
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002092{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002093 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002094 return -EIO;
2095
2096 return 0;
2097}
2098
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002099static void tpacpi_input_send_tabletsw(void)
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002100{
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002101 int state;
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002102
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002103 if (tp_features.hotkey_tablet &&
2104 !hotkey_get_tablet_mode(&state)) {
2105 mutex_lock(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002106
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002107 input_report_switch(tpacpi_inputdev,
2108 SW_TABLET_MODE, !!state);
2109 input_sync(tpacpi_inputdev);
2110
2111 mutex_unlock(&tpacpi_inputdev_send_mutex);
2112 }
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002113}
2114
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002115static void tpacpi_input_send_key(unsigned int scancode)
2116{
2117 unsigned int keycode;
2118
2119 keycode = hotkey_keycode_map[scancode];
2120
2121 if (keycode != KEY_RESERVED) {
2122 mutex_lock(&tpacpi_inputdev_send_mutex);
2123
2124 input_report_key(tpacpi_inputdev, keycode, 1);
2125 if (keycode == KEY_UNKNOWN)
2126 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
2127 scancode);
2128 input_sync(tpacpi_inputdev);
2129
2130 input_report_key(tpacpi_inputdev, keycode, 0);
2131 if (keycode == KEY_UNKNOWN)
2132 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
2133 scancode);
2134 input_sync(tpacpi_inputdev);
2135
2136 mutex_unlock(&tpacpi_inputdev_send_mutex);
2137 }
2138}
2139
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002140#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2141static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
2142
2143static void tpacpi_hotkey_send_key(unsigned int scancode)
2144{
2145 tpacpi_input_send_key(scancode);
2146 if (hotkey_report_mode < 2) {
2147 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
2148 0x80, 0x1001 + scancode);
2149 }
2150}
2151
2152static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
2153{
2154 u8 d;
2155
2156 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
2157 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
2158 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
2159 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
2160 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
2161 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
2162 }
2163 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
2164 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
2165 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
2166 }
2167 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
2168 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
2169 n->displayexp_toggle =
2170 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
2171 }
2172 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
2173 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
2174 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
2175 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
2176 n->brightness_toggle =
2177 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
2178 }
2179 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
2180 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
2181 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
2182 >> TP_NVRAM_POS_LEVEL_VOLUME;
2183 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2184 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2185 }
2186}
2187
2188#define TPACPI_COMPARE_KEY(__scancode, __member) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002189 do { \
2190 if ((mask & (1 << __scancode)) && \
2191 oldn->__member != newn->__member) \
2192 tpacpi_hotkey_send_key(__scancode); \
2193 } while (0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002194
2195#define TPACPI_MAY_SEND_KEY(__scancode) \
2196 do { if (mask & (1 << __scancode)) \
2197 tpacpi_hotkey_send_key(__scancode); } while (0)
2198
2199static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002200 struct tp_nvram_state *newn,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002201 u32 mask)
2202{
2203 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2204 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2205 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2206 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2207
2208 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2209
2210 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2211
2212 /* handle volume */
2213 if (oldn->volume_toggle != newn->volume_toggle) {
2214 if (oldn->mute != newn->mute) {
2215 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2216 }
2217 if (oldn->volume_level > newn->volume_level) {
2218 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2219 } else if (oldn->volume_level < newn->volume_level) {
2220 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2221 } else if (oldn->mute == newn->mute) {
2222 /* repeated key presses that didn't change state */
2223 if (newn->mute) {
2224 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2225 } else if (newn->volume_level != 0) {
2226 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2227 } else {
2228 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2229 }
2230 }
2231 }
2232
2233 /* handle brightness */
2234 if (oldn->brightness_toggle != newn->brightness_toggle) {
2235 if (oldn->brightness_level < newn->brightness_level) {
2236 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2237 } else if (oldn->brightness_level > newn->brightness_level) {
2238 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2239 } else {
2240 /* repeated key presses that didn't change state */
2241 if (newn->brightness_level != 0) {
2242 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2243 } else {
2244 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2245 }
2246 }
2247 }
2248}
2249
2250#undef TPACPI_COMPARE_KEY
2251#undef TPACPI_MAY_SEND_KEY
2252
2253static int hotkey_kthread(void *data)
2254{
2255 struct tp_nvram_state s[2];
2256 u32 mask;
2257 unsigned int si, so;
2258 unsigned long t;
2259 unsigned int change_detector, must_reset;
2260
2261 mutex_lock(&hotkey_thread_mutex);
2262
2263 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2264 goto exit;
2265
2266 set_freezable();
2267
2268 so = 0;
2269 si = 1;
2270 t = 0;
2271
2272 /* Initial state for compares */
2273 mutex_lock(&hotkey_thread_data_mutex);
2274 change_detector = hotkey_config_change;
2275 mask = hotkey_source_mask & hotkey_mask;
2276 mutex_unlock(&hotkey_thread_data_mutex);
2277 hotkey_read_nvram(&s[so], mask);
2278
2279 while (!kthread_should_stop() && hotkey_poll_freq) {
2280 if (t == 0)
2281 t = 1000/hotkey_poll_freq;
2282 t = msleep_interruptible(t);
2283 if (unlikely(kthread_should_stop()))
2284 break;
2285 must_reset = try_to_freeze();
2286 if (t > 0 && !must_reset)
2287 continue;
2288
2289 mutex_lock(&hotkey_thread_data_mutex);
2290 if (must_reset || hotkey_config_change != change_detector) {
2291 /* forget old state on thaw or config change */
2292 si = so;
2293 t = 0;
2294 change_detector = hotkey_config_change;
2295 }
2296 mask = hotkey_source_mask & hotkey_mask;
2297 mutex_unlock(&hotkey_thread_data_mutex);
2298
2299 if (likely(mask)) {
2300 hotkey_read_nvram(&s[si], mask);
2301 if (likely(si != so)) {
2302 hotkey_compare_and_issue_event(&s[so], &s[si],
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002303 mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002304 }
2305 }
2306
2307 so = si;
2308 si ^= 1;
2309 }
2310
2311exit:
2312 mutex_unlock(&hotkey_thread_mutex);
2313 return 0;
2314}
2315
2316static void hotkey_poll_stop_sync(void)
2317{
2318 if (tpacpi_hotkey_task) {
2319 if (frozen(tpacpi_hotkey_task) ||
2320 freezing(tpacpi_hotkey_task))
2321 thaw_process(tpacpi_hotkey_task);
2322
2323 kthread_stop(tpacpi_hotkey_task);
2324 tpacpi_hotkey_task = NULL;
2325 mutex_lock(&hotkey_thread_mutex);
2326 /* at this point, the thread did exit */
2327 mutex_unlock(&hotkey_thread_mutex);
2328 }
2329}
2330
2331/* call with hotkey_mutex held */
2332static void hotkey_poll_setup(int may_warn)
2333{
2334 if ((hotkey_source_mask & hotkey_mask) != 0 &&
2335 hotkey_poll_freq > 0 &&
2336 (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
2337 if (!tpacpi_hotkey_task) {
2338 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03002339 NULL, TPACPI_NVRAM_KTHREAD_NAME);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002340 if (IS_ERR(tpacpi_hotkey_task)) {
2341 tpacpi_hotkey_task = NULL;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002342 printk(TPACPI_ERR
2343 "could not create kernel thread "
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002344 "for hotkey polling\n");
2345 }
2346 }
2347 } else {
2348 hotkey_poll_stop_sync();
2349 if (may_warn &&
2350 hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002351 printk(TPACPI_NOTICE
2352 "hot keys 0x%08x require polling, "
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002353 "which is currently disabled\n",
2354 hotkey_source_mask);
2355 }
2356 }
2357}
2358
2359static void hotkey_poll_setup_safe(int may_warn)
2360{
2361 mutex_lock(&hotkey_mutex);
2362 hotkey_poll_setup(may_warn);
2363 mutex_unlock(&hotkey_mutex);
2364}
2365
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02002366#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2367
2368static void hotkey_poll_setup_safe(int __unused)
2369{
2370}
2371
2372#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2373
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002374static int hotkey_inputdev_open(struct input_dev *dev)
2375{
2376 switch (tpacpi_lifecycle) {
2377 case TPACPI_LIFE_INIT:
2378 /*
2379 * hotkey_init will call hotkey_poll_setup_safe
2380 * at the appropriate moment
2381 */
2382 return 0;
2383 case TPACPI_LIFE_EXITING:
2384 return -EBUSY;
2385 case TPACPI_LIFE_RUNNING:
2386 hotkey_poll_setup_safe(0);
2387 return 0;
2388 }
2389
2390 /* Should only happen if tpacpi_lifecycle is corrupt */
2391 BUG();
2392 return -EBUSY;
2393}
2394
2395static void hotkey_inputdev_close(struct input_dev *dev)
2396{
2397 /* disable hotkey polling when possible */
2398 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
2399 hotkey_poll_setup_safe(0);
2400}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002401
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002402/* sysfs hotkey enable ------------------------------------------------- */
2403static ssize_t hotkey_enable_show(struct device *dev,
2404 struct device_attribute *attr,
2405 char *buf)
2406{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002407 int res, status;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002408
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002409 printk_deprecated_attribute("hotkey_enable",
2410 "Hotkey reporting is always enabled");
2411
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002412 res = hotkey_status_get(&status);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002413 if (res)
2414 return res;
2415
2416 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2417}
2418
2419static ssize_t hotkey_enable_store(struct device *dev,
2420 struct device_attribute *attr,
2421 const char *buf, size_t count)
2422{
2423 unsigned long t;
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002424
2425 printk_deprecated_attribute("hotkey_enable",
2426 "Hotkeys can be disabled through hotkey_mask");
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002427
2428 if (parse_strtoul(buf, 1, &t))
2429 return -EINVAL;
2430
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002431 if (t == 0)
2432 return -EPERM;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002433
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002434 return count;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002435}
2436
2437static struct device_attribute dev_attr_hotkey_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002438 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002439 hotkey_enable_show, hotkey_enable_store);
2440
2441/* sysfs hotkey mask --------------------------------------------------- */
2442static ssize_t hotkey_mask_show(struct device *dev,
2443 struct device_attribute *attr,
2444 char *buf)
2445{
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002446 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002447
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002448 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002449 return -ERESTARTSYS;
2450 res = hotkey_mask_get();
2451 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002452
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002453 return (res)?
2454 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002455}
2456
2457static ssize_t hotkey_mask_store(struct device *dev,
2458 struct device_attribute *attr,
2459 const char *buf, size_t count)
2460{
2461 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002462 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002463
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002464 if (parse_strtoul(buf, 0xffffffffUL, &t))
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002465 return -EINVAL;
2466
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002467 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002468 return -ERESTARTSYS;
2469
2470 res = hotkey_mask_set(t);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002471
2472#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2473 hotkey_poll_setup(1);
2474#endif
2475
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002476 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002477
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002478 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2479
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002480 return (res) ? res : count;
2481}
2482
2483static struct device_attribute dev_attr_hotkey_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002484 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002485 hotkey_mask_show, hotkey_mask_store);
2486
2487/* sysfs hotkey bios_enabled ------------------------------------------- */
2488static ssize_t hotkey_bios_enabled_show(struct device *dev,
2489 struct device_attribute *attr,
2490 char *buf)
2491{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002492 return sprintf(buf, "0\n");
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002493}
2494
2495static struct device_attribute dev_attr_hotkey_bios_enabled =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002496 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002497
2498/* sysfs hotkey bios_mask ---------------------------------------------- */
2499static ssize_t hotkey_bios_mask_show(struct device *dev,
2500 struct device_attribute *attr,
2501 char *buf)
2502{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002503 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002504}
2505
2506static struct device_attribute dev_attr_hotkey_bios_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002507 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002508
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002509/* sysfs hotkey all_mask ----------------------------------------------- */
2510static ssize_t hotkey_all_mask_show(struct device *dev,
2511 struct device_attribute *attr,
2512 char *buf)
2513{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002514 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2515 hotkey_all_mask | hotkey_source_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002516}
2517
2518static struct device_attribute dev_attr_hotkey_all_mask =
2519 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
2520
2521/* sysfs hotkey recommended_mask --------------------------------------- */
2522static ssize_t hotkey_recommended_mask_show(struct device *dev,
2523 struct device_attribute *attr,
2524 char *buf)
2525{
2526 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002527 (hotkey_all_mask | hotkey_source_mask)
2528 & ~hotkey_reserved_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002529}
2530
2531static struct device_attribute dev_attr_hotkey_recommended_mask =
2532 __ATTR(hotkey_recommended_mask, S_IRUGO,
2533 hotkey_recommended_mask_show, NULL);
2534
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002535#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2536
2537/* sysfs hotkey hotkey_source_mask ------------------------------------- */
2538static ssize_t hotkey_source_mask_show(struct device *dev,
2539 struct device_attribute *attr,
2540 char *buf)
2541{
2542 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2543}
2544
2545static ssize_t hotkey_source_mask_store(struct device *dev,
2546 struct device_attribute *attr,
2547 const char *buf, size_t count)
2548{
2549 unsigned long t;
2550
2551 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2552 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2553 return -EINVAL;
2554
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002555 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002556 return -ERESTARTSYS;
2557
2558 HOTKEY_CONFIG_CRITICAL_START
2559 hotkey_source_mask = t;
2560 HOTKEY_CONFIG_CRITICAL_END
2561
2562 hotkey_poll_setup(1);
2563
2564 mutex_unlock(&hotkey_mutex);
2565
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002566 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2567
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002568 return count;
2569}
2570
2571static struct device_attribute dev_attr_hotkey_source_mask =
2572 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2573 hotkey_source_mask_show, hotkey_source_mask_store);
2574
2575/* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2576static ssize_t hotkey_poll_freq_show(struct device *dev,
2577 struct device_attribute *attr,
2578 char *buf)
2579{
2580 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2581}
2582
2583static ssize_t hotkey_poll_freq_store(struct device *dev,
2584 struct device_attribute *attr,
2585 const char *buf, size_t count)
2586{
2587 unsigned long t;
2588
2589 if (parse_strtoul(buf, 25, &t))
2590 return -EINVAL;
2591
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002592 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002593 return -ERESTARTSYS;
2594
2595 hotkey_poll_freq = t;
2596
2597 hotkey_poll_setup(1);
2598 mutex_unlock(&hotkey_mutex);
2599
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002600 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2601
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002602 return count;
2603}
2604
2605static struct device_attribute dev_attr_hotkey_poll_freq =
2606 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2607 hotkey_poll_freq_show, hotkey_poll_freq_store);
2608
2609#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2610
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002611/* sysfs hotkey radio_sw (pollable) ------------------------------------ */
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002612static ssize_t hotkey_radio_sw_show(struct device *dev,
2613 struct device_attribute *attr,
2614 char *buf)
2615{
Johannes Berg19d337d2009-06-02 13:01:37 +02002616 int res;
2617 res = hotkey_get_wlsw();
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002618 if (res < 0)
2619 return res;
2620
Johannes Berg19d337d2009-06-02 13:01:37 +02002621 /* Opportunistic update */
2622 tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
2623
2624 return snprintf(buf, PAGE_SIZE, "%d\n",
2625 (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002626}
2627
2628static struct device_attribute dev_attr_hotkey_radio_sw =
2629 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2630
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002631static void hotkey_radio_sw_notify_change(void)
2632{
2633 if (tp_features.hotkey_wlsw)
2634 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2635 "hotkey_radio_sw");
2636}
2637
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002638/* sysfs hotkey tablet mode (pollable) --------------------------------- */
2639static ssize_t hotkey_tablet_mode_show(struct device *dev,
2640 struct device_attribute *attr,
2641 char *buf)
2642{
2643 int res, s;
2644 res = hotkey_get_tablet_mode(&s);
2645 if (res < 0)
2646 return res;
2647
2648 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2649}
2650
2651static struct device_attribute dev_attr_hotkey_tablet_mode =
2652 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2653
2654static void hotkey_tablet_mode_notify_change(void)
2655{
2656 if (tp_features.hotkey_tablet)
2657 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2658 "hotkey_tablet_mode");
2659}
2660
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002661/* sysfs hotkey report_mode -------------------------------------------- */
2662static ssize_t hotkey_report_mode_show(struct device *dev,
2663 struct device_attribute *attr,
2664 char *buf)
2665{
2666 return snprintf(buf, PAGE_SIZE, "%d\n",
2667 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2668}
2669
2670static struct device_attribute dev_attr_hotkey_report_mode =
2671 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2672
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002673/* sysfs wakeup reason (pollable) -------------------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002674static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2675 struct device_attribute *attr,
2676 char *buf)
2677{
2678 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2679}
2680
2681static struct device_attribute dev_attr_hotkey_wakeup_reason =
2682 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2683
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02002684static void hotkey_wakeup_reason_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002685{
2686 if (tp_features.hotkey_mask)
2687 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2688 "wakeup_reason");
2689}
2690
2691/* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002692static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2693 struct device_attribute *attr,
2694 char *buf)
2695{
2696 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2697}
2698
2699static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2700 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2701 hotkey_wakeup_hotunplug_complete_show, NULL);
2702
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02002703static void hotkey_wakeup_hotunplug_complete_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002704{
2705 if (tp_features.hotkey_mask)
2706 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2707 "wakeup_hotunplug_complete");
2708}
2709
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002710/* --------------------------------------------------------------------- */
2711
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002712static struct attribute *hotkey_attributes[] __initdata = {
2713 &dev_attr_hotkey_enable.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002714 &dev_attr_hotkey_bios_enabled.attr,
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002715 &dev_attr_hotkey_report_mode.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002716#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2717 &dev_attr_hotkey_mask.attr,
2718 &dev_attr_hotkey_all_mask.attr,
2719 &dev_attr_hotkey_recommended_mask.attr,
2720 &dev_attr_hotkey_source_mask.attr,
2721 &dev_attr_hotkey_poll_freq.attr,
2722#endif
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002723};
2724
2725static struct attribute *hotkey_mask_attributes[] __initdata = {
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002726 &dev_attr_hotkey_bios_mask.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002727#ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2728 &dev_attr_hotkey_mask.attr,
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002729 &dev_attr_hotkey_all_mask.attr,
2730 &dev_attr_hotkey_recommended_mask.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002731#endif
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002732 &dev_attr_hotkey_wakeup_reason.attr,
2733 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002734};
2735
Johannes Berg19d337d2009-06-02 13:01:37 +02002736/*
2737 * Sync both the hw and sw blocking state of all switches
2738 */
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002739static void tpacpi_send_radiosw_update(void)
2740{
2741 int wlsw;
2742
Johannes Berg19d337d2009-06-02 13:01:37 +02002743 /*
2744 * We must sync all rfkill controllers *before* issuing any
2745 * rfkill input events, or we will race the rfkill core input
2746 * handler.
2747 *
2748 * tpacpi_inputdev_send_mutex works as a syncronization point
2749 * for the above.
2750 *
2751 * We optimize to avoid numerous calls to hotkey_get_wlsw.
2752 */
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002753
Johannes Berg19d337d2009-06-02 13:01:37 +02002754 wlsw = hotkey_get_wlsw();
2755
2756 /* Sync hw blocking state first if it is hw-blocked */
2757 if (wlsw == TPACPI_RFK_RADIO_OFF)
2758 tpacpi_rfk_update_hwblock_state(true);
2759
2760 /* Sync sw blocking state */
2761 tpacpi_rfk_update_swstate_all();
2762
2763 /* Sync hw blocking state last if it is hw-unblocked */
2764 if (wlsw == TPACPI_RFK_RADIO_ON)
2765 tpacpi_rfk_update_hwblock_state(false);
2766
2767 /* Issue rfkill input event for WLSW switch */
2768 if (!(wlsw < 0)) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002769 mutex_lock(&tpacpi_inputdev_send_mutex);
2770
2771 input_report_switch(tpacpi_inputdev,
Johannes Berg19d337d2009-06-02 13:01:37 +02002772 SW_RFKILL_ALL, (wlsw > 0));
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002773 input_sync(tpacpi_inputdev);
2774
2775 mutex_unlock(&tpacpi_inputdev_send_mutex);
2776 }
Johannes Berg19d337d2009-06-02 13:01:37 +02002777
2778 /*
2779 * this can be unconditional, as we will poll state again
2780 * if userspace uses the notify to read data
2781 */
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002782 hotkey_radio_sw_notify_change();
2783}
2784
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002785static void hotkey_exit(void)
2786{
2787#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2788 hotkey_poll_stop_sync();
2789#endif
2790
2791 if (hotkey_dev_attributes)
2792 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2793
2794 kfree(hotkey_keycode_map);
2795
2796 if (tp_features.hotkey) {
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002797 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002798 "restoring original hot key mask\n");
2799 /* no short-circuit boolean operator below! */
2800 if ((hotkey_mask_set(hotkey_orig_mask) |
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002801 hotkey_status_set(false)) != 0)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002802 printk(TPACPI_ERR
2803 "failed to restore hot key mask "
2804 "to BIOS defaults\n");
2805 }
2806}
2807
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002808static int __init hotkey_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002809{
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002810 /* Requirements for changing the default keymaps:
2811 *
2812 * 1. Many of the keys are mapped to KEY_RESERVED for very
2813 * good reasons. Do not change them unless you have deep
2814 * knowledge on the IBM and Lenovo ThinkPad firmware for
2815 * the various ThinkPad models. The driver behaves
2816 * differently for KEY_RESERVED: such keys have their
2817 * hot key mask *unset* in mask_recommended, and also
2818 * in the initial hot key mask programmed into the
2819 * firmware at driver load time, which means the firm-
2820 * ware may react very differently if you change them to
2821 * something else;
2822 *
2823 * 2. You must be subscribed to the linux-thinkpad and
2824 * ibm-acpi-devel mailing lists, and you should read the
2825 * list archives since 2007 if you want to change the
2826 * keymaps. This requirement exists so that you will
2827 * know the past history of problems with the thinkpad-
2828 * acpi driver keymaps, and also that you will be
2829 * listening to any bug reports;
2830 *
2831 * 3. Do not send thinkpad-acpi specific patches directly to
2832 * for merging, *ever*. Send them to the linux-acpi
2833 * mailinglist for comments. Merging is to be done only
2834 * through acpi-test and the ACPI maintainer.
2835 *
2836 * If the above is too much to ask, don't change the keymap.
2837 * Ask the thinkpad-acpi maintainer to do it, instead.
2838 */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002839 static u16 ibm_keycode_map[] __initdata = {
2840 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2841 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
2842 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2843 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002844
2845 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002846 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2847 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2848 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002849
2850 /* brightness: firmware always reacts to them, unless
2851 * X.org did some tricks in the radeon BIOS scratch
2852 * registers of *some* models */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02002853 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02002854 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002855
2856 /* Thinklight: firmware always react to it */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002857 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002858
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002859 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2860 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002861
2862 /* Volume: firmware always react to it and reprograms
2863 * the built-in *extra* mixer. Never map it to control
2864 * another mixer by default. */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02002865 KEY_RESERVED, /* 0x14: VOLUME UP */
2866 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2867 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002868
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002869 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002870
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002871 /* (assignments unknown, please report if found) */
2872 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2873 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2874 };
2875 static u16 lenovo_keycode_map[] __initdata = {
2876 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2877 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
2878 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2879 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002880
2881 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002882 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2883 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2884 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002885
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03002886 /* These either have to go through ACPI video, or
2887 * act like in the IBM ThinkPads, so don't ever
2888 * enable them by default */
Henrique de Moraes Holschuh56a185b2007-12-13 12:14:09 -02002889 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuh56a185b2007-12-13 12:14:09 -02002890 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002891
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002892 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002893
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002894 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2895 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002896
2897 /* Volume: z60/z61, T60 (BIOS version?): firmware always
2898 * react to it and reprograms the built-in *extra* mixer.
2899 * Never map it to control another mixer by default.
2900 *
2901 * T60?, T61, R60?, R61: firmware and EC tries to send
2902 * these over the regular keyboard, so these are no-ops,
2903 * but there are still weird bugs re. MUTE, so do not
2904 * change unless you get test reports from all Lenovo
2905 * models. May cause the BIOS to interfere with the
2906 * HDA mixer.
2907 */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02002908 KEY_RESERVED, /* 0x14: VOLUME UP */
2909 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2910 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002911
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002912 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002913
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002914 /* (assignments unknown, please report if found) */
2915 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2916 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2917 };
2918
2919#define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
2920#define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
2921#define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
2922
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002923 int res, i;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002924 int status;
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03002925 int hkeyv;
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03002926
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002927 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2928 "initializing hotkey subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002929
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002930 BUG_ON(!tpacpi_inputdev);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002931 BUG_ON(tpacpi_inputdev->open != NULL ||
2932 tpacpi_inputdev->close != NULL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002933
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002934 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002935 mutex_init(&hotkey_mutex);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002936
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002937#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2938 mutex_init(&hotkey_thread_mutex);
2939 mutex_init(&hotkey_thread_data_mutex);
2940#endif
2941
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002942 /* hotkey not supported on 570 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002943 tp_features.hotkey = hkey_handle != NULL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002944
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002945 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2946 "hotkeys are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002947 str_supported(tp_features.hotkey));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002948
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002949 if (!tp_features.hotkey)
2950 return 1;
2951
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03002952 tpacpi_disable_brightness_delay();
2953
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002954 hotkey_dev_attributes = create_attr_set(13, NULL);
2955 if (!hotkey_dev_attributes)
2956 return -ENOMEM;
2957 res = add_many_to_attr_set(hotkey_dev_attributes,
2958 hotkey_attributes,
2959 ARRAY_SIZE(hotkey_attributes));
2960 if (res)
2961 goto err_exit;
2962
2963 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2964 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
2965 for HKEY interface version 0x100 */
2966 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
2967 if ((hkeyv >> 8) != 1) {
2968 printk(TPACPI_ERR "unknown version of the "
2969 "HKEY interface: 0x%x\n", hkeyv);
2970 printk(TPACPI_ERR "please report this to %s\n",
2971 TPACPI_MAIL);
2972 } else {
2973 /*
2974 * MHKV 0x100 in A31, R40, R40e,
2975 * T4x, X31, and later
2976 */
2977 tp_features.hotkey_mask = 1;
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002978 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2979 "firmware HKEY interface version: 0x%x\n",
2980 hkeyv);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002981 }
2982 }
2983
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002984 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2985 "hotkey masks are %s\n",
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002986 str_supported(tp_features.hotkey_mask));
2987
2988 if (tp_features.hotkey_mask) {
2989 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
2990 "MHKA", "qd")) {
2991 printk(TPACPI_ERR
2992 "missing MHKA handler, "
2993 "please report this to %s\n",
2994 TPACPI_MAIL);
2995 /* FN+F12, FN+F4, FN+F3 */
2996 hotkey_all_mask = 0x080cU;
2997 }
2998 }
2999
3000 /* hotkey_source_mask *must* be zero for
3001 * the first hotkey_mask_get */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003002 if (tp_features.hotkey_mask) {
3003 res = hotkey_mask_get();
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003004 if (res)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003005 goto err_exit;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003006
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003007 hotkey_orig_mask = hotkey_mask;
3008 res = add_many_to_attr_set(
3009 hotkey_dev_attributes,
3010 hotkey_mask_attributes,
3011 ARRAY_SIZE(hotkey_mask_attributes));
3012 if (res)
3013 goto err_exit;
3014 }
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003015
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003016#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003017 if (tp_features.hotkey_mask) {
3018 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3019 & ~hotkey_all_mask;
3020 } else {
3021 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK;
3022 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003023
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003024 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003025 "hotkey source mask 0x%08x, polling freq %d\n",
3026 hotkey_source_mask, hotkey_poll_freq);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003027#endif
3028
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003029#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3030 if (dbg_wlswemul) {
3031 tp_features.hotkey_wlsw = 1;
3032 printk(TPACPI_INFO
3033 "radio switch emulation enabled\n");
3034 } else
3035#endif
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003036 /* Not all thinkpads have a hardware radio switch */
3037 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
3038 tp_features.hotkey_wlsw = 1;
3039 printk(TPACPI_INFO
3040 "radio switch found; radios are %s\n",
3041 enabled(status, 0));
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003042 }
3043 if (tp_features.hotkey_wlsw)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003044 res = add_to_attr_set(hotkey_dev_attributes,
3045 &dev_attr_hotkey_radio_sw.attr);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003046
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003047 /* For X41t, X60t, X61t Tablets... */
3048 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
3049 tp_features.hotkey_tablet = 1;
3050 printk(TPACPI_INFO
3051 "possible tablet mode switch found; "
3052 "ThinkPad in %s mode\n",
3053 (status & TP_HOTKEY_TABLET_MASK)?
3054 "tablet" : "laptop");
3055 res = add_to_attr_set(hotkey_dev_attributes,
3056 &dev_attr_hotkey_tablet_mode.attr);
3057 }
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02003058
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003059 if (!res)
3060 res = register_attr_set_with_sysfs(
3061 hotkey_dev_attributes,
3062 &tpacpi_pdev->dev.kobj);
3063 if (res)
3064 goto err_exit;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003065
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003066 /* Set up key map */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003067
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003068 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
3069 GFP_KERNEL);
3070 if (!hotkey_keycode_map) {
3071 printk(TPACPI_ERR
3072 "failed to allocate memory for key map\n");
3073 res = -ENOMEM;
3074 goto err_exit;
3075 }
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003076
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003077 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003078 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003079 "using Lenovo default hot key map\n");
3080 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
3081 TPACPI_HOTKEY_MAP_SIZE);
3082 } else {
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003083 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003084 "using IBM default hot key map\n");
3085 memcpy(hotkey_keycode_map, &ibm_keycode_map,
3086 TPACPI_HOTKEY_MAP_SIZE);
3087 }
3088
3089 set_bit(EV_KEY, tpacpi_inputdev->evbit);
3090 set_bit(EV_MSC, tpacpi_inputdev->evbit);
3091 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
3092 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
3093 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3094 tpacpi_inputdev->keycode = hotkey_keycode_map;
3095 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3096 if (hotkey_keycode_map[i] != KEY_RESERVED) {
3097 set_bit(hotkey_keycode_map[i],
3098 tpacpi_inputdev->keybit);
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003099 } else {
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003100 if (i < sizeof(hotkey_reserved_mask)*8)
3101 hotkey_reserved_mask |= 1 << i;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003102 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003103 }
3104
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003105 if (tp_features.hotkey_wlsw) {
3106 set_bit(EV_SW, tpacpi_inputdev->evbit);
3107 set_bit(SW_RFKILL_ALL, tpacpi_inputdev->swbit);
3108 }
3109 if (tp_features.hotkey_tablet) {
3110 set_bit(EV_SW, tpacpi_inputdev->evbit);
3111 set_bit(SW_TABLET_MODE, tpacpi_inputdev->swbit);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003112 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003113
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003114 /* Do not issue duplicate brightness change events to
3115 * userspace */
3116 if (!tp_features.bright_acpimode)
3117 /* update bright_acpimode... */
3118 tpacpi_check_std_acpi_brightness_support();
3119
Henrique de Moraes Holschuh8bf3d4c2009-05-30 13:25:09 -03003120 if (tp_features.bright_acpimode && acpi_video_backlight_support()) {
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003121 printk(TPACPI_INFO
3122 "This ThinkPad has standard ACPI backlight "
3123 "brightness control, supported by the ACPI "
3124 "video driver\n");
3125 printk(TPACPI_NOTICE
3126 "Disabling thinkpad-acpi brightness events "
3127 "by default...\n");
3128
3129 /* The hotkey_reserved_mask change below is not
3130 * necessary while the keys are at KEY_RESERVED in the
3131 * default map, but better safe than sorry, leave it
3132 * here as a marker of what we have to do, especially
3133 * when we finally become able to set this at runtime
3134 * on response to X.org requests */
3135 hotkey_reserved_mask |=
3136 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3137 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003138 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003139
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003140 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3141 "enabling firmware HKEY event interface...\n");
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003142 res = hotkey_status_set(true);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003143 if (res) {
3144 hotkey_exit();
3145 return res;
3146 }
3147 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
3148 & ~hotkey_reserved_mask)
3149 | hotkey_orig_mask);
3150 if (res < 0 && res != -ENXIO) {
3151 hotkey_exit();
3152 return res;
3153 }
3154
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003155 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3156 "legacy ibm/hotkey event reporting over procfs %s\n",
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003157 (hotkey_report_mode < 2) ?
3158 "enabled" : "disabled");
3159
3160 tpacpi_inputdev->open = &hotkey_inputdev_open;
3161 tpacpi_inputdev->close = &hotkey_inputdev_close;
3162
3163 hotkey_poll_setup_safe(1);
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003164 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003165 tpacpi_input_send_tabletsw();
3166
3167 return 0;
3168
3169err_exit:
3170 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3171 hotkey_dev_attributes = NULL;
3172
3173 return (res < 0)? res : 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003174}
3175
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003176static bool hotkey_notify_hotkey(const u32 hkey,
3177 bool *send_acpi_ev,
3178 bool *ignore_acpi_ev)
3179{
3180 /* 0x1000-0x1FFF: key presses */
3181 unsigned int scancode = hkey & 0xfff;
3182 *send_acpi_ev = true;
3183 *ignore_acpi_ev = false;
3184
3185 if (scancode > 0 && scancode < 0x21) {
3186 scancode--;
3187 if (!(hotkey_source_mask & (1 << scancode))) {
3188 tpacpi_input_send_key(scancode);
3189 *send_acpi_ev = false;
3190 } else {
3191 *ignore_acpi_ev = true;
3192 }
3193 return true;
3194 }
3195 return false;
3196}
3197
3198static bool hotkey_notify_wakeup(const u32 hkey,
3199 bool *send_acpi_ev,
3200 bool *ignore_acpi_ev)
3201{
3202 /* 0x2000-0x2FFF: Wakeup reason */
3203 *send_acpi_ev = true;
3204 *ignore_acpi_ev = false;
3205
3206 switch (hkey) {
3207 case 0x2304: /* suspend, undock */
3208 case 0x2404: /* hibernation, undock */
3209 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3210 *ignore_acpi_ev = true;
3211 break;
3212
3213 case 0x2305: /* suspend, bay eject */
3214 case 0x2405: /* hibernation, bay eject */
3215 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3216 *ignore_acpi_ev = true;
3217 break;
3218
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003219 case 0x2313: /* Battery on critical low level (S3) */
3220 case 0x2413: /* Battery on critical low level (S4) */
3221 printk(TPACPI_ALERT
3222 "EMERGENCY WAKEUP: battery almost empty\n");
3223 /* how to auto-heal: */
3224 /* 2313: woke up from S3, go to S4/S5 */
3225 /* 2413: woke up from S4, go to S5 */
3226 break;
3227
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003228 default:
3229 return false;
3230 }
3231
3232 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
3233 printk(TPACPI_INFO
3234 "woke up due to a hot-unplug "
3235 "request...\n");
3236 hotkey_wakeup_reason_notify_change();
3237 }
3238 return true;
3239}
3240
3241static bool hotkey_notify_usrevent(const u32 hkey,
3242 bool *send_acpi_ev,
3243 bool *ignore_acpi_ev)
3244{
3245 /* 0x5000-0x5FFF: human interface helpers */
3246 *send_acpi_ev = true;
3247 *ignore_acpi_ev = false;
3248
3249 switch (hkey) {
3250 case 0x5010: /* Lenovo new BIOS: brightness changed */
3251 case 0x500b: /* X61t: tablet pen inserted into bay */
3252 case 0x500c: /* X61t: tablet pen removed from bay */
3253 return true;
3254
3255 case 0x5009: /* X41t-X61t: swivel up (tablet mode) */
3256 case 0x500a: /* X41t-X61t: swivel down (normal mode) */
3257 tpacpi_input_send_tabletsw();
3258 hotkey_tablet_mode_notify_change();
3259 *send_acpi_ev = false;
3260 return true;
3261
3262 case 0x5001:
3263 case 0x5002:
3264 /* LID switch events. Do not propagate */
3265 *ignore_acpi_ev = true;
3266 return true;
3267
3268 default:
3269 return false;
3270 }
3271}
3272
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003273static bool hotkey_notify_thermal(const u32 hkey,
3274 bool *send_acpi_ev,
3275 bool *ignore_acpi_ev)
3276{
3277 /* 0x6000-0x6FFF: thermal alarms */
3278 *send_acpi_ev = true;
3279 *ignore_acpi_ev = false;
3280
3281 switch (hkey) {
3282 case 0x6011:
3283 printk(TPACPI_CRIT
3284 "THERMAL ALARM: battery is too hot!\n");
3285 /* recommended action: warn user through gui */
3286 return true;
3287 case 0x6012:
3288 printk(TPACPI_ALERT
3289 "THERMAL EMERGENCY: battery is extremely hot!\n");
3290 /* recommended action: immediate sleep/hibernate */
3291 return true;
3292 case 0x6021:
3293 printk(TPACPI_CRIT
3294 "THERMAL ALARM: "
3295 "a sensor reports something is too hot!\n");
3296 /* recommended action: warn user through gui, that */
3297 /* some internal component is too hot */
3298 return true;
3299 case 0x6022:
3300 printk(TPACPI_ALERT
3301 "THERMAL EMERGENCY: "
3302 "a sensor reports something is extremely hot!\n");
3303 /* recommended action: immediate sleep/hibernate */
3304 return true;
Henrique de Moraes Holschuh54926ce2009-01-11 03:01:09 -02003305 case 0x6030:
3306 printk(TPACPI_INFO
3307 "EC reports that Thermal Table has changed\n");
3308 /* recommended action: do nothing, we don't have
3309 * Lenovo ATM information */
3310 return true;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003311 default:
3312 printk(TPACPI_ALERT
3313 "THERMAL ALERT: unknown thermal alarm received\n");
3314 return false;
3315 }
3316}
3317
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003318static void hotkey_notify(struct ibm_struct *ibm, u32 event)
3319{
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003320 u32 hkey;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003321 bool send_acpi_ev;
3322 bool ignore_acpi_ev;
3323 bool known_ev;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003324
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003325 if (event != 0x80) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003326 printk(TPACPI_ERR
3327 "unknown HKEY notification event %d\n", event);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003328 /* forward it to userspace, maybe it knows how to handle it */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003329 acpi_bus_generate_netlink_event(
3330 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02003331 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003332 event, 0);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003333 return;
3334 }
3335
3336 while (1) {
3337 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003338 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003339 return;
3340 }
3341
3342 if (hkey == 0) {
3343 /* queue empty */
3344 return;
3345 }
3346
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003347 send_acpi_ev = true;
3348 ignore_acpi_ev = false;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003349
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003350 switch (hkey >> 12) {
3351 case 1:
3352 /* 0x1000-0x1FFF: key presses */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003353 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
3354 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003355 break;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003356 case 2:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003357 /* 0x2000-0x2FFF: Wakeup reason */
3358 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
3359 &ignore_acpi_ev);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003360 break;
3361 case 3:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003362 /* 0x3000-0x3FFF: bay-related wakeups */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003363 if (hkey == 0x3003) {
3364 hotkey_autosleep_ack = 1;
3365 printk(TPACPI_INFO
3366 "bay ejected\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003367 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003368 known_ev = true;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003369 } else {
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003370 known_ev = false;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003371 }
3372 break;
3373 case 4:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003374 /* 0x4000-0x4FFF: dock-related wakeups */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003375 if (hkey == 0x4003) {
3376 hotkey_autosleep_ack = 1;
3377 printk(TPACPI_INFO
3378 "undocked\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003379 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003380 known_ev = true;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003381 } else {
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003382 known_ev = false;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003383 }
3384 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003385 case 5:
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02003386 /* 0x5000-0x5FFF: human interface helpers */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003387 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
3388 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003389 break;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003390 case 6:
3391 /* 0x6000-0x6FFF: thermal alarms */
3392 known_ev = hotkey_notify_thermal(hkey, &send_acpi_ev,
3393 &ignore_acpi_ev);
3394 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003395 case 7:
3396 /* 0x7000-0x7FFF: misc */
3397 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003398 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02003399 send_acpi_ev = 0;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003400 known_ev = true;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003401 break;
3402 }
3403 /* fallthrough to default */
3404 default:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003405 known_ev = false;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02003406 }
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003407 if (!known_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003408 printk(TPACPI_NOTICE
3409 "unhandled HKEY event 0x%04x\n", hkey);
Henrique de Moraes Holschuhcb429352009-01-11 03:01:07 -02003410 printk(TPACPI_NOTICE
3411 "please report the conditions when this "
3412 "event happened to %s\n", TPACPI_MAIL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003413 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003414
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003415 /* Legacy events */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003416 if (!ignore_acpi_ev &&
3417 (send_acpi_ev || hotkey_report_mode < 2)) {
3418 acpi_bus_generate_proc_event(ibm->acpi->device,
3419 event, hkey);
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03003420 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003421
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003422 /* netlink events */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03003423 if (!ignore_acpi_ev && send_acpi_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003424 acpi_bus_generate_netlink_event(
3425 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02003426 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003427 event, hkey);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003428 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003429 }
3430}
3431
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003432static void hotkey_suspend(pm_message_t state)
3433{
3434 /* Do these on suspend, we get the events on early resume! */
3435 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
3436 hotkey_autosleep_ack = 0;
3437}
3438
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03003439static void hotkey_resume(void)
3440{
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03003441 tpacpi_disable_brightness_delay();
3442
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02003443 if (hotkey_mask_get())
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003444 printk(TPACPI_ERR
3445 "error while trying to read hot key mask "
3446 "from firmware\n");
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003447 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02003448 hotkey_tablet_mode_notify_change();
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003449 hotkey_wakeup_reason_notify_change();
3450 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003451 hotkey_poll_setup_safe(0);
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03003452}
3453
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003454/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003455static int hotkey_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03003457 int res, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458 int len = 0;
3459
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003460 if (!tp_features.hotkey) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003461 len += sprintf(p + len, "status:\t\tnot supported\n");
3462 return len;
3463 }
3464
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02003465 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02003466 return -ERESTARTSYS;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02003467 res = hotkey_status_get(&status);
3468 if (!res)
3469 res = hotkey_mask_get();
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003470 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03003471 if (res)
3472 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473
3474 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003475 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02003476 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477 len += sprintf(p + len,
3478 "commands:\tenable, disable, reset, <mask>\n");
3479 } else {
3480 len += sprintf(p + len, "mask:\t\tnot supported\n");
3481 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
3482 }
3483
3484 return len;
3485}
3486
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003487static void hotkey_enabledisable_warn(bool enable)
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003488{
3489 tpacpi_log_usertask("procfs hotkey enable/disable");
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003490 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
3491 TPACPI_WARN
3492 "hotkey enable/disable functionality has been "
3493 "removed from the driver. Hotkeys are always "
3494 "enabled\n"))
3495 printk(TPACPI_ERR
3496 "Please remove the hotkey=enable module "
3497 "parameter, it is deprecated. Hotkeys are always "
3498 "enabled\n");
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003499}
3500
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003501static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003503 int res;
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03003504 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003507 if (!tp_features.hotkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508 return -ENODEV;
3509
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02003510 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02003511 return -ERESTARTSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003512
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02003513 mask = hotkey_mask;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003514
3515 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516 while ((cmd = next_cmd(&buf))) {
3517 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003518 hotkey_enabledisable_warn(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003520 hotkey_enabledisable_warn(0);
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003521 res = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522 } else if (strlencmp(cmd, "reset") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003523 mask = hotkey_orig_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
3525 /* mask set */
3526 } else if (sscanf(cmd, "%x", &mask) == 1) {
3527 /* mask set */
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003528 } else {
3529 res = -EINVAL;
3530 goto errexit;
3531 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532 }
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003533
3534 if (!res)
3535 tpacpi_disclose_usertask("procfs hotkey",
3536 "set mask to 0x%08x\n", mask);
3537
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02003538 if (!res && mask != hotkey_mask)
3539 res = hotkey_mask_set(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003541errexit:
3542 mutex_unlock(&hotkey_mutex);
3543 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003544}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003546static const struct acpi_device_id ibm_htk_device_ids[] = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003547 {TPACPI_ACPI_HKEY_HID, 0},
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003548 {"", 0},
3549};
3550
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003551static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003552 .hid = ibm_htk_device_ids,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003553 .notify = hotkey_notify,
3554 .handle = &hkey_handle,
3555 .type = ACPI_DEVICE_NOTIFY,
3556};
3557
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003558static struct ibm_struct hotkey_driver_data = {
3559 .name = "hotkey",
3560 .read = hotkey_read,
3561 .write = hotkey_write,
3562 .exit = hotkey_exit,
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03003563 .resume = hotkey_resume,
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003564 .suspend = hotkey_suspend,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003565 .acpi = &ibm_hotkey_acpidriver,
3566};
3567
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003568/*************************************************************************
3569 * Bluetooth subdriver
3570 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003572enum {
3573 /* ACPI GBDC/SBDC bits */
3574 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
3575 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003576 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
3577 off / last state */
3578};
3579
3580enum {
3581 /* ACPI \BLTH commands */
3582 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
3583 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
3584 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
3585 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
3586 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003587};
3588
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003589#define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
3590
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003591static void bluetooth_suspend(pm_message_t state)
3592{
3593 /* Try to make sure radio will resume powered off */
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003594 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3595 TP_ACPI_BLTH_PWR_OFF_ON_RESUME))
3596 vdbg_printk(TPACPI_DBG_RFKILL,
3597 "bluetooth power down on resume request failed\n");
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003598}
3599
Johannes Berg19d337d2009-06-02 13:01:37 +02003600static int bluetooth_get_status(void)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003601{
3602 int status;
3603
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003604#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3605 if (dbg_bluetoothemul)
3606 return (tpacpi_bluetooth_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02003607 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003608#endif
3609
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003610 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
3611 return -EIO;
3612
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003613 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02003614 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003615}
3616
Johannes Berg19d337d2009-06-02 13:01:37 +02003617static int bluetooth_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003618{
3619 int status;
3620
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003621 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02003622 "will attempt to %s bluetooth\n",
3623 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003624
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003625#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3626 if (dbg_bluetoothemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02003627 tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003628 return 0;
3629 }
3630#endif
3631
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003632 /* We make sure to keep TP_ACPI_BLUETOOTH_RESUMECTRL off */
Johannes Berg19d337d2009-06-02 13:01:37 +02003633 if (state == TPACPI_RFK_RADIO_ON)
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003634 status = TP_ACPI_BLUETOOTH_RADIOSSW;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003635 else
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003636 status = 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02003637
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003638 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
3639 return -EIO;
3640
3641 return 0;
3642}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003643
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003644/* sysfs bluetooth enable ---------------------------------------------- */
3645static ssize_t bluetooth_enable_show(struct device *dev,
3646 struct device_attribute *attr,
3647 char *buf)
3648{
Johannes Berg19d337d2009-06-02 13:01:37 +02003649 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID,
3650 attr, buf);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003651}
3652
3653static ssize_t bluetooth_enable_store(struct device *dev,
3654 struct device_attribute *attr,
3655 const char *buf, size_t count)
3656{
Johannes Berg19d337d2009-06-02 13:01:37 +02003657 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID,
3658 attr, buf, count);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003659}
3660
3661static struct device_attribute dev_attr_bluetooth_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03003662 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003663 bluetooth_enable_show, bluetooth_enable_store);
3664
3665/* --------------------------------------------------------------------- */
3666
3667static struct attribute *bluetooth_attributes[] = {
3668 &dev_attr_bluetooth_enable.attr,
3669 NULL
3670};
3671
3672static const struct attribute_group bluetooth_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003673 .attrs = bluetooth_attributes,
3674};
3675
Johannes Berg19d337d2009-06-02 13:01:37 +02003676static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = {
3677 .get_status = bluetooth_get_status,
3678 .set_status = bluetooth_set_status,
3679};
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003680
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003681static void bluetooth_shutdown(void)
3682{
3683 /* Order firmware to save current state to NVRAM */
3684 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3685 TP_ACPI_BLTH_SAVE_STATE))
3686 printk(TPACPI_NOTICE
3687 "failed to save bluetooth state to NVRAM\n");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003688 else
3689 vdbg_printk(TPACPI_DBG_RFKILL,
3690 "bluestooth state saved to NVRAM\n");
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003691}
3692
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003693static void bluetooth_exit(void)
3694{
3695 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3696 &bluetooth_attr_group);
Johannes Berg19d337d2009-06-02 13:01:37 +02003697
3698 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
3699
3700 bluetooth_shutdown();
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003701}
3702
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003703static int __init bluetooth_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003705 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003706 int status = 0;
3707
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003708 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3709 "initializing bluetooth subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003710
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003711 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003712
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003713 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3714 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003715 tp_features.bluetooth = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003716 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003718 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3719 "bluetooth is %s, status 0x%02x\n",
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003720 str_supported(tp_features.bluetooth),
3721 status);
3722
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003723#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3724 if (dbg_bluetoothemul) {
3725 tp_features.bluetooth = 1;
3726 printk(TPACPI_INFO
3727 "bluetooth switch emulation enabled\n");
3728 } else
3729#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003730 if (tp_features.bluetooth &&
3731 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
3732 /* no bluetooth hardware present in system */
3733 tp_features.bluetooth = 0;
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003734 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003735 "bluetooth hardware not installed\n");
3736 }
3737
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003738 if (!tp_features.bluetooth)
3739 return 1;
3740
Johannes Berg19d337d2009-06-02 13:01:37 +02003741 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
3742 &bluetooth_tprfk_ops,
3743 RFKILL_TYPE_BLUETOOTH,
3744 TPACPI_RFK_BLUETOOTH_SW_NAME,
3745 true);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003746 if (res)
3747 return res;
3748
Johannes Berg19d337d2009-06-02 13:01:37 +02003749 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3750 &bluetooth_attr_group);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003751 if (res) {
Johannes Berg19d337d2009-06-02 13:01:37 +02003752 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003753 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003754 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003755
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003756 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757}
3758
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003759/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003760static int bluetooth_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761{
Johannes Berg19d337d2009-06-02 13:01:37 +02003762 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763}
3764
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003765static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766{
Johannes Berg19d337d2009-06-02 13:01:37 +02003767 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768}
3769
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003770static struct ibm_struct bluetooth_driver_data = {
3771 .name = "bluetooth",
3772 .read = bluetooth_read,
3773 .write = bluetooth_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003774 .exit = bluetooth_exit,
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003775 .suspend = bluetooth_suspend,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003776 .shutdown = bluetooth_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003777};
3778
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003779/*************************************************************************
3780 * Wan subdriver
3781 */
3782
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003783enum {
3784 /* ACPI GWAN/SWAN bits */
3785 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
3786 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003787 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
3788 off / last state */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003789};
3790
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003791#define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
3792
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003793static void wan_suspend(pm_message_t state)
3794{
3795 /* Try to make sure radio will resume powered off */
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003796 if (!acpi_evalf(NULL, NULL, "\\WGSV", "qvd",
3797 TP_ACPI_WGSV_PWR_OFF_ON_RESUME))
3798 vdbg_printk(TPACPI_DBG_RFKILL,
3799 "WWAN power down on resume request failed\n");
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003800}
3801
Johannes Berg19d337d2009-06-02 13:01:37 +02003802static int wan_get_status(void)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003803{
3804 int status;
3805
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003806#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3807 if (dbg_wwanemul)
3808 return (tpacpi_wwan_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02003809 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003810#endif
3811
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003812 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
3813 return -EIO;
3814
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003815 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02003816 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003817}
3818
Johannes Berg19d337d2009-06-02 13:01:37 +02003819static int wan_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003820{
3821 int status;
3822
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003823 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02003824 "will attempt to %s wwan\n",
3825 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003826
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003827#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3828 if (dbg_wwanemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02003829 tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003830 return 0;
3831 }
3832#endif
3833
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003834 /* We make sure to keep TP_ACPI_WANCARD_RESUMECTRL off */
Johannes Berg19d337d2009-06-02 13:01:37 +02003835 if (state == TPACPI_RFK_RADIO_ON)
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003836 status = TP_ACPI_WANCARD_RADIOSSW;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003837 else
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003838 status = 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02003839
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003840 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
3841 return -EIO;
3842
3843 return 0;
3844}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003845
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003846/* sysfs wan enable ---------------------------------------------------- */
3847static ssize_t wan_enable_show(struct device *dev,
3848 struct device_attribute *attr,
3849 char *buf)
3850{
Johannes Berg19d337d2009-06-02 13:01:37 +02003851 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
3852 attr, buf);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003853}
3854
3855static ssize_t wan_enable_store(struct device *dev,
3856 struct device_attribute *attr,
3857 const char *buf, size_t count)
3858{
Johannes Berg19d337d2009-06-02 13:01:37 +02003859 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
3860 attr, buf, count);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003861}
3862
3863static struct device_attribute dev_attr_wan_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03003864 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003865 wan_enable_show, wan_enable_store);
3866
3867/* --------------------------------------------------------------------- */
3868
3869static struct attribute *wan_attributes[] = {
3870 &dev_attr_wan_enable.attr,
3871 NULL
3872};
3873
3874static const struct attribute_group wan_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003875 .attrs = wan_attributes,
3876};
3877
Johannes Berg19d337d2009-06-02 13:01:37 +02003878static const struct tpacpi_rfk_ops wan_tprfk_ops = {
3879 .get_status = wan_get_status,
3880 .set_status = wan_set_status,
3881};
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003882
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003883static void wan_shutdown(void)
3884{
3885 /* Order firmware to save current state to NVRAM */
3886 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
3887 TP_ACPI_WGSV_SAVE_STATE))
3888 printk(TPACPI_NOTICE
3889 "failed to save WWAN state to NVRAM\n");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003890 else
3891 vdbg_printk(TPACPI_DBG_RFKILL,
3892 "WWAN state saved to NVRAM\n");
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003893}
3894
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003895static void wan_exit(void)
3896{
3897 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3898 &wan_attr_group);
Johannes Berg19d337d2009-06-02 13:01:37 +02003899
3900 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
3901
3902 wan_shutdown();
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003903}
3904
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003905static int __init wan_init(struct ibm_init_struct *iibm)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003906{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003907 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003908 int status = 0;
3909
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003910 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3911 "initializing wan subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003912
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003913 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003914
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003915 tp_features.wan = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003916 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003917
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003918 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3919 "wan is %s, status 0x%02x\n",
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003920 str_supported(tp_features.wan),
3921 status);
3922
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003923#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3924 if (dbg_wwanemul) {
3925 tp_features.wan = 1;
3926 printk(TPACPI_INFO
3927 "wwan switch emulation enabled\n");
3928 } else
3929#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003930 if (tp_features.wan &&
3931 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
3932 /* no wan hardware present in system */
3933 tp_features.wan = 0;
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003934 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003935 "wan hardware not installed\n");
3936 }
3937
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003938 if (!tp_features.wan)
3939 return 1;
3940
Johannes Berg19d337d2009-06-02 13:01:37 +02003941 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
3942 &wan_tprfk_ops,
3943 RFKILL_TYPE_WWAN,
3944 TPACPI_RFK_WWAN_SW_NAME,
3945 true);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003946 if (res)
3947 return res;
3948
Johannes Berg19d337d2009-06-02 13:01:37 +02003949 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3950 &wan_attr_group);
3951
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003952 if (res) {
Johannes Berg19d337d2009-06-02 13:01:37 +02003953 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003954 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003955 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003956
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003957 return 0;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003958}
3959
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003960/* procfs -------------------------------------------------------------- */
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003961static int wan_read(char *p)
3962{
Johannes Berg19d337d2009-06-02 13:01:37 +02003963 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, p);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003964}
3965
3966static int wan_write(char *buf)
3967{
Johannes Berg19d337d2009-06-02 13:01:37 +02003968 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003969}
3970
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003971static struct ibm_struct wan_driver_data = {
3972 .name = "wan",
3973 .read = wan_read,
3974 .write = wan_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003975 .exit = wan_exit,
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003976 .suspend = wan_suspend,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003977 .shutdown = wan_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003978};
3979
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003980/*************************************************************************
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02003981 * UWB subdriver
3982 */
3983
3984enum {
3985 /* ACPI GUWB/SUWB bits */
3986 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
3987 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
3988};
3989
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003990#define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
3991
Johannes Berg19d337d2009-06-02 13:01:37 +02003992static int uwb_get_status(void)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02003993{
3994 int status;
3995
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02003996#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3997 if (dbg_uwbemul)
3998 return (tpacpi_uwb_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02003999 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004000#endif
4001
4002 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4003 return -EIO;
4004
4005 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004006 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004007}
4008
Johannes Berg19d337d2009-06-02 13:01:37 +02004009static int uwb_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004010{
4011 int status;
4012
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004013 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02004014 "will attempt to %s UWB\n",
4015 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004016
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004017#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4018 if (dbg_uwbemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004019 tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004020 return 0;
4021 }
4022#endif
4023
Johannes Berg19d337d2009-06-02 13:01:37 +02004024 if (state == TPACPI_RFK_RADIO_ON)
4025 status = TP_ACPI_UWB_RADIOSSW;
4026 else
4027 status = 0;
4028
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004029 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4030 return -EIO;
4031
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004032 return 0;
4033}
4034
4035/* --------------------------------------------------------------------- */
4036
Johannes Berg19d337d2009-06-02 13:01:37 +02004037static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
4038 .get_status = uwb_get_status,
4039 .set_status = uwb_set_status,
4040};
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004041
4042static void uwb_exit(void)
4043{
Johannes Berg19d337d2009-06-02 13:01:37 +02004044 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004045}
4046
4047static int __init uwb_init(struct ibm_init_struct *iibm)
4048{
4049 int res;
4050 int status = 0;
4051
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004052 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4053 "initializing uwb subdriver\n");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004054
4055 TPACPI_ACPIHANDLE_INIT(hkey);
4056
4057 tp_features.uwb = hkey_handle &&
4058 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4059
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004060 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4061 "uwb is %s, status 0x%02x\n",
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004062 str_supported(tp_features.uwb),
4063 status);
4064
4065#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4066 if (dbg_uwbemul) {
4067 tp_features.uwb = 1;
4068 printk(TPACPI_INFO
4069 "uwb switch emulation enabled\n");
4070 } else
4071#endif
4072 if (tp_features.uwb &&
4073 !(status & TP_ACPI_UWB_HWPRESENT)) {
4074 /* no uwb hardware present in system */
4075 tp_features.uwb = 0;
4076 dbg_printk(TPACPI_DBG_INIT,
4077 "uwb hardware not installed\n");
4078 }
4079
4080 if (!tp_features.uwb)
4081 return 1;
4082
4083 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
Johannes Berg19d337d2009-06-02 13:01:37 +02004084 &uwb_tprfk_ops,
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004085 RFKILL_TYPE_UWB,
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004086 TPACPI_RFK_UWB_SW_NAME,
Johannes Berg19d337d2009-06-02 13:01:37 +02004087 false);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004088 return res;
4089}
4090
4091static struct ibm_struct uwb_driver_data = {
4092 .name = "uwb",
4093 .exit = uwb_exit,
4094 .flags.experimental = 1,
4095};
4096
4097/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004098 * Video subdriver
4099 */
4100
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02004101#ifdef CONFIG_THINKPAD_ACPI_VIDEO
4102
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004103enum video_access_mode {
4104 TPACPI_VIDEO_NONE = 0,
4105 TPACPI_VIDEO_570, /* 570 */
4106 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4107 TPACPI_VIDEO_NEW, /* all others */
4108};
4109
4110enum { /* video status flags, based on VIDEO_570 */
4111 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4112 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4113 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4114};
4115
4116enum { /* TPACPI_VIDEO_570 constants */
4117 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4118 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4119 * video_status_flags */
4120 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4121 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4122};
4123
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02004124static enum video_access_mode video_supported;
4125static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004126
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004127static int video_autosw_get(void);
4128static int video_autosw_set(int enable);
4129
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004130TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004131
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004132static int __init video_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004134 int ivga;
4135
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004136 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4137
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004138 TPACPI_ACPIHANDLE_INIT(vid);
4139 TPACPI_ACPIHANDLE_INIT(vid2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004140
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004141 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4142 /* G41, assume IVGA doesn't change */
4143 vid_handle = vid2_handle;
4144
4145 if (!vid_handle)
4146 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004147 video_supported = TPACPI_VIDEO_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004148 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
4149 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004150 video_supported = TPACPI_VIDEO_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004151 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
4152 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004153 video_supported = TPACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004154 else
4155 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004156 video_supported = TPACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004158 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
4159 str_supported(video_supported != TPACPI_VIDEO_NONE),
4160 video_supported);
4161
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004162 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163}
4164
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004165static void video_exit(void)
4166{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004167 dbg_printk(TPACPI_DBG_EXIT,
4168 "restoring original video autoswitch mode\n");
4169 if (video_autosw_set(video_orig_autosw))
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004170 printk(TPACPI_ERR "error while trying to restore original "
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004171 "video autoswitch mode\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004172}
4173
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004174static int video_outputsw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175{
4176 int status = 0;
4177 int i;
4178
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004179 switch (video_supported) {
4180 case TPACPI_VIDEO_570:
4181 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
4182 TP_ACPI_VIDEO_570_PHSCMD))
4183 return -EIO;
4184 status = i & TP_ACPI_VIDEO_570_PHSMASK;
4185 break;
4186 case TPACPI_VIDEO_770:
4187 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4188 return -EIO;
4189 if (i)
4190 status |= TP_ACPI_VIDEO_S_LCD;
4191 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4192 return -EIO;
4193 if (i)
4194 status |= TP_ACPI_VIDEO_S_CRT;
4195 break;
4196 case TPACPI_VIDEO_NEW:
4197 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4198 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4199 return -EIO;
4200 if (i)
4201 status |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004202
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004203 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4204 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4205 return -EIO;
4206 if (i)
4207 status |= TP_ACPI_VIDEO_S_LCD;
4208 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4209 return -EIO;
4210 if (i)
4211 status |= TP_ACPI_VIDEO_S_DVI;
4212 break;
4213 default:
4214 return -ENOSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004215 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216
4217 return status;
4218}
4219
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004220static int video_outputsw_set(int status)
4221{
4222 int autosw;
4223 int res = 0;
4224
4225 switch (video_supported) {
4226 case TPACPI_VIDEO_570:
4227 res = acpi_evalf(NULL, NULL,
4228 "\\_SB.PHS2", "vdd",
4229 TP_ACPI_VIDEO_570_PHS2CMD,
4230 status | TP_ACPI_VIDEO_570_PHS2SET);
4231 break;
4232 case TPACPI_VIDEO_770:
4233 autosw = video_autosw_get();
4234 if (autosw < 0)
4235 return autosw;
4236
4237 res = video_autosw_set(1);
4238 if (res)
4239 return res;
4240 res = acpi_evalf(vid_handle, NULL,
4241 "ASWT", "vdd", status * 0x100, 0);
4242 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004243 printk(TPACPI_ERR
4244 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004245 return -EIO;
4246 }
4247 break;
4248 case TPACPI_VIDEO_NEW:
4249 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004250 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004251 break;
4252 default:
4253 return -ENOSYS;
4254 }
4255
4256 return (res)? 0 : -EIO;
4257}
4258
4259static int video_autosw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004261 int autosw = 0;
4262
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004263 switch (video_supported) {
4264 case TPACPI_VIDEO_570:
4265 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
4266 return -EIO;
4267 break;
4268 case TPACPI_VIDEO_770:
4269 case TPACPI_VIDEO_NEW:
4270 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4271 return -EIO;
4272 break;
4273 default:
4274 return -ENOSYS;
4275 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004276
4277 return autosw & 1;
4278}
4279
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004280static int video_autosw_set(int enable)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004281{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004282 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004283 return -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004284 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004285}
4286
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004287static int video_outputsw_cycle(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004288{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004289 int autosw = video_autosw_get();
4290 int res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004291
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004292 if (autosw < 0)
4293 return autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004294
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004295 switch (video_supported) {
4296 case TPACPI_VIDEO_570:
4297 res = video_autosw_set(1);
4298 if (res)
4299 return res;
4300 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4301 break;
4302 case TPACPI_VIDEO_770:
4303 case TPACPI_VIDEO_NEW:
4304 res = video_autosw_set(1);
4305 if (res)
4306 return res;
4307 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4308 break;
4309 default:
4310 return -ENOSYS;
4311 }
4312 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004313 printk(TPACPI_ERR
4314 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004315 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004316 }
4317
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004318 return (res)? 0 : -EIO;
4319}
4320
4321static int video_expand_toggle(void)
4322{
4323 switch (video_supported) {
4324 case TPACPI_VIDEO_570:
4325 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
4326 0 : -EIO;
4327 case TPACPI_VIDEO_770:
4328 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
4329 0 : -EIO;
4330 case TPACPI_VIDEO_NEW:
4331 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
4332 0 : -EIO;
4333 default:
4334 return -ENOSYS;
4335 }
4336 /* not reached */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004337}
4338
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004339static int video_read(char *p)
4340{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004341 int status, autosw;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004342 int len = 0;
4343
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004344 if (video_supported == TPACPI_VIDEO_NONE) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004345 len += sprintf(p + len, "status:\t\tnot supported\n");
4346 return len;
4347 }
4348
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004349 status = video_outputsw_get();
4350 if (status < 0)
4351 return status;
4352
4353 autosw = video_autosw_get();
4354 if (autosw < 0)
4355 return autosw;
4356
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004357 len += sprintf(p + len, "status:\t\tsupported\n");
4358 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
4359 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004360 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004361 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
4362 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
4363 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
4364 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004365 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004366 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
4367 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
4368 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
4369
4370 return len;
4371}
4372
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004373static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374{
4375 char *cmd;
4376 int enable, disable, status;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004377 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004379 if (video_supported == TPACPI_VIDEO_NONE)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004380 return -ENODEV;
4381
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004382 enable = 0;
4383 disable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384
4385 while ((cmd = next_cmd(&buf))) {
4386 if (strlencmp(cmd, "lcd_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004387 enable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388 } else if (strlencmp(cmd, "lcd_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004389 disable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390 } else if (strlencmp(cmd, "crt_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004391 enable |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392 } else if (strlencmp(cmd, "crt_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004393 disable |= TP_ACPI_VIDEO_S_CRT;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004394 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004395 strlencmp(cmd, "dvi_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004396 enable |= TP_ACPI_VIDEO_S_DVI;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004397 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004398 strlencmp(cmd, "dvi_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004399 disable |= TP_ACPI_VIDEO_S_DVI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400 } else if (strlencmp(cmd, "auto_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004401 res = video_autosw_set(1);
4402 if (res)
4403 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404 } else if (strlencmp(cmd, "auto_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004405 res = video_autosw_set(0);
4406 if (res)
4407 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408 } else if (strlencmp(cmd, "video_switch") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004409 res = video_outputsw_cycle();
4410 if (res)
4411 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004412 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004413 res = video_expand_toggle();
4414 if (res)
4415 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416 } else
4417 return -EINVAL;
4418 }
4419
4420 if (enable || disable) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004421 status = video_outputsw_get();
4422 if (status < 0)
4423 return status;
4424 res = video_outputsw_set((status & ~disable) | enable);
4425 if (res)
4426 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427 }
4428
4429 return 0;
4430}
4431
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004432static struct ibm_struct video_driver_data = {
4433 .name = "video",
4434 .read = video_read,
4435 .write = video_write,
4436 .exit = video_exit,
4437};
4438
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02004439#endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4440
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004441/*************************************************************************
4442 * Light (thinklight) subdriver
4443 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004445TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4446TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004447
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004448static int light_get_status(void)
4449{
4450 int status = 0;
4451
4452 if (tp_features.light_status) {
4453 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4454 return -EIO;
4455 return (!!status);
4456 }
4457
4458 return -ENXIO;
4459}
4460
4461static int light_set_status(int status)
4462{
4463 int rc;
4464
4465 if (tp_features.light) {
4466 if (cmos_handle) {
4467 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4468 (status)?
4469 TP_CMOS_THINKLIGHT_ON :
4470 TP_CMOS_THINKLIGHT_OFF);
4471 } else {
4472 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4473 (status)? 1 : 0);
4474 }
4475 return (rc)? 0 : -EIO;
4476 }
4477
4478 return -ENXIO;
4479}
4480
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004481static void light_set_status_worker(struct work_struct *work)
4482{
4483 struct tpacpi_led_classdev *data =
4484 container_of(work, struct tpacpi_led_classdev, work);
4485
4486 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00004487 light_set_status((data->new_state != TPACPI_LED_OFF));
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004488}
4489
4490static void light_sysfs_set(struct led_classdev *led_cdev,
4491 enum led_brightness brightness)
4492{
4493 struct tpacpi_led_classdev *data =
4494 container_of(led_cdev,
4495 struct tpacpi_led_classdev,
4496 led_classdev);
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00004497 data->new_state = (brightness != LED_OFF) ?
4498 TPACPI_LED_ON : TPACPI_LED_OFF;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004499 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004500}
4501
4502static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4503{
4504 return (light_get_status() == 1)? LED_FULL : LED_OFF;
4505}
4506
4507static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4508 .led_classdev = {
4509 .name = "tpacpi::thinklight",
4510 .brightness_set = &light_sysfs_set,
4511 .brightness_get = &light_sysfs_get,
4512 }
4513};
4514
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004515static int __init light_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516{
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004517 int rc;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004518
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004519 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4520
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004521 TPACPI_ACPIHANDLE_INIT(ledb);
4522 TPACPI_ACPIHANDLE_INIT(lght);
4523 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004524 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004525
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004526 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004527 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004528
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004529 if (tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004530 /* light status not supported on
4531 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004532 tp_features.light_status =
4533 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004534
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004535 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4536 str_supported(tp_features.light),
4537 str_supported(tp_features.light_status));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004538
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004539 if (!tp_features.light)
4540 return 1;
4541
4542 rc = led_classdev_register(&tpacpi_pdev->dev,
4543 &tpacpi_led_thinklight.led_classdev);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004544
4545 if (rc < 0) {
4546 tp_features.light = 0;
4547 tp_features.light_status = 0;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004548 } else {
4549 rc = 0;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004550 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004551
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004552 return rc;
4553}
4554
4555static void light_exit(void)
4556{
4557 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
4558 if (work_pending(&tpacpi_led_thinklight.work))
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004559 flush_workqueue(tpacpi_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560}
4561
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004562static int light_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563{
4564 int len = 0;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004565 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004567 if (!tp_features.light) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004568 len += sprintf(p + len, "status:\t\tnot supported\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004569 } else if (!tp_features.light_status) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004570 len += sprintf(p + len, "status:\t\tunknown\n");
4571 len += sprintf(p + len, "commands:\ton, off\n");
4572 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004573 status = light_get_status();
4574 if (status < 0)
4575 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004577 len += sprintf(p + len, "commands:\ton, off\n");
4578 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579
4580 return len;
4581}
4582
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004583static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004586 int newstatus = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004587
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004588 if (!tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004589 return -ENODEV;
4590
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591 while ((cmd = next_cmd(&buf))) {
4592 if (strlencmp(cmd, "on") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004593 newstatus = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004594 } else if (strlencmp(cmd, "off") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004595 newstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596 } else
4597 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598 }
4599
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004600 return light_set_status(newstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004601}
4602
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004603static struct ibm_struct light_driver_data = {
4604 .name = "light",
4605 .read = light_read,
4606 .write = light_write,
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004607 .exit = light_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004608};
4609
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004610/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004611 * CMOS subdriver
4612 */
4613
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004614/* sysfs cmos_command -------------------------------------------------- */
4615static ssize_t cmos_command_store(struct device *dev,
4616 struct device_attribute *attr,
4617 const char *buf, size_t count)
4618{
4619 unsigned long cmos_cmd;
4620 int res;
4621
4622 if (parse_strtoul(buf, 21, &cmos_cmd))
4623 return -EINVAL;
4624
4625 res = issue_thinkpad_cmos_command(cmos_cmd);
4626 return (res)? res : count;
4627}
4628
4629static struct device_attribute dev_attr_cmos_command =
4630 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4631
4632/* --------------------------------------------------------------------- */
4633
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004634static int __init cmos_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004635{
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004636 int res;
4637
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004638 vdbg_printk(TPACPI_DBG_INIT,
4639 "initializing cmos commands subdriver\n");
4640
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004641 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004642
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004643 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4644 str_supported(cmos_handle != NULL));
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004645
4646 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4647 if (res)
4648 return res;
4649
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004650 return (cmos_handle)? 0 : 1;
4651}
4652
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004653static void cmos_exit(void)
4654{
4655 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4656}
4657
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004658static int cmos_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004659{
4660 int len = 0;
4661
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004662 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4663 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664 if (!cmos_handle)
4665 len += sprintf(p + len, "status:\t\tnot supported\n");
4666 else {
4667 len += sprintf(p + len, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004668 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669 }
4670
4671 return len;
4672}
4673
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004674static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004675{
4676 char *cmd;
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004677 int cmos_cmd, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678
4679 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004680 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
4681 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682 /* cmos_cmd set */
4683 } else
4684 return -EINVAL;
4685
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004686 res = issue_thinkpad_cmos_command(cmos_cmd);
4687 if (res)
4688 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689 }
4690
4691 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004692}
4693
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004694static struct ibm_struct cmos_driver_data = {
4695 .name = "cmos",
4696 .read = cmos_read,
4697 .write = cmos_write,
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004698 .exit = cmos_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004699};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004700
4701/*************************************************************************
4702 * LED subdriver
4703 */
4704
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004705enum led_access_mode {
4706 TPACPI_LED_NONE = 0,
4707 TPACPI_LED_570, /* 570 */
4708 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4709 TPACPI_LED_NEW, /* all others */
4710};
4711
4712enum { /* For TPACPI_LED_OLD */
4713 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
4714 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
4715 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
4716};
4717
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02004718static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004719
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004720TPACPI_HANDLE(led, ec, "SLED", /* 570 */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004721 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
4722 /* T20-22, X20-21 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004723 "LED", /* all others */
4724 ); /* R30, R31 */
4725
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03004726#define TPACPI_LED_NUMLEDS 16
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004727static struct tpacpi_led_classdev *tpacpi_leds;
4728static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
Harvey Harrisone3aa51f2008-05-29 17:51:57 -07004729static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004730 /* there's a limit of 19 chars + NULL before 2.6.26 */
4731 "tpacpi::power",
4732 "tpacpi:orange:batt",
4733 "tpacpi:green:batt",
4734 "tpacpi::dock_active",
4735 "tpacpi::bay_active",
4736 "tpacpi::dock_batt",
4737 "tpacpi::unknown_led",
4738 "tpacpi::standby",
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03004739 "tpacpi::dock_status1",
4740 "tpacpi::dock_status2",
4741 "tpacpi::unknown_led2",
4742 "tpacpi::unknown_led3",
4743 "tpacpi::thinkvantage",
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004744};
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03004745#define TPACPI_SAFE_LEDS 0x1081U
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004746
4747static inline bool tpacpi_is_led_restricted(const unsigned int led)
4748{
4749#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
4750 return false;
4751#else
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03004752 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004753#endif
4754}
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004755
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004756static int led_get_status(const unsigned int led)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004757{
4758 int status;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004759 enum led_status_t led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004760
4761 switch (led_supported) {
4762 case TPACPI_LED_570:
4763 if (!acpi_evalf(ec_handle,
4764 &status, "GLED", "dd", 1 << led))
4765 return -EIO;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004766 led_s = (status == 0)?
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004767 TPACPI_LED_OFF :
4768 ((status == 1)?
4769 TPACPI_LED_ON :
4770 TPACPI_LED_BLINK);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004771 tpacpi_led_state_cache[led] = led_s;
4772 return led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004773 default:
4774 return -ENXIO;
4775 }
4776
4777 /* not reached */
4778}
4779
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004780static int led_set_status(const unsigned int led,
4781 const enum led_status_t ledstatus)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004782{
4783 /* off, on, blink. Index is led_status_t */
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004784 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
4785 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004786
4787 int rc = 0;
4788
4789 switch (led_supported) {
4790 case TPACPI_LED_570:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004791 /* 570 */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004792 if (unlikely(led > 7))
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004793 return -EINVAL;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004794 if (unlikely(tpacpi_is_led_restricted(led)))
4795 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004796 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4797 (1 << led), led_sled_arg1[ledstatus]))
4798 rc = -EIO;
4799 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004800 case TPACPI_LED_OLD:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004801 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004802 if (unlikely(led > 7))
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004803 return -EINVAL;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004804 if (unlikely(tpacpi_is_led_restricted(led)))
4805 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004806 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
4807 if (rc >= 0)
4808 rc = ec_write(TPACPI_LED_EC_HLBL,
4809 (ledstatus == TPACPI_LED_BLINK) << led);
4810 if (rc >= 0)
4811 rc = ec_write(TPACPI_LED_EC_HLCL,
4812 (ledstatus != TPACPI_LED_OFF) << led);
4813 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004814 case TPACPI_LED_NEW:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004815 /* all others */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004816 if (unlikely(led >= TPACPI_LED_NUMLEDS))
4817 return -EINVAL;
4818 if (unlikely(tpacpi_is_led_restricted(led)))
4819 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004820 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4821 led, led_led_arg1[ledstatus]))
4822 rc = -EIO;
4823 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004824 default:
4825 rc = -ENXIO;
4826 }
4827
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004828 if (!rc)
4829 tpacpi_led_state_cache[led] = ledstatus;
4830
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004831 return rc;
4832}
4833
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004834static void led_set_status_worker(struct work_struct *work)
4835{
4836 struct tpacpi_led_classdev *data =
4837 container_of(work, struct tpacpi_led_classdev, work);
4838
4839 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00004840 led_set_status(data->led, data->new_state);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004841}
4842
4843static void led_sysfs_set(struct led_classdev *led_cdev,
4844 enum led_brightness brightness)
4845{
4846 struct tpacpi_led_classdev *data = container_of(led_cdev,
4847 struct tpacpi_led_classdev, led_classdev);
4848
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00004849 if (brightness == LED_OFF)
4850 data->new_state = TPACPI_LED_OFF;
4851 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
4852 data->new_state = TPACPI_LED_ON;
4853 else
4854 data->new_state = TPACPI_LED_BLINK;
4855
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004856 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004857}
4858
4859static int led_sysfs_blink_set(struct led_classdev *led_cdev,
4860 unsigned long *delay_on, unsigned long *delay_off)
4861{
4862 struct tpacpi_led_classdev *data = container_of(led_cdev,
4863 struct tpacpi_led_classdev, led_classdev);
4864
4865 /* Can we choose the flash rate? */
4866 if (*delay_on == 0 && *delay_off == 0) {
4867 /* yes. set them to the hardware blink rate (1 Hz) */
4868 *delay_on = 500; /* ms */
4869 *delay_off = 500; /* ms */
4870 } else if ((*delay_on != 500) || (*delay_off != 500))
4871 return -EINVAL;
4872
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00004873 data->new_state = TPACPI_LED_BLINK;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004874 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004875
4876 return 0;
4877}
4878
4879static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
4880{
4881 int rc;
4882
4883 struct tpacpi_led_classdev *data = container_of(led_cdev,
4884 struct tpacpi_led_classdev, led_classdev);
4885
4886 rc = led_get_status(data->led);
4887
4888 if (rc == TPACPI_LED_OFF || rc < 0)
4889 rc = LED_OFF; /* no error handling in led class :( */
4890 else
4891 rc = LED_FULL;
4892
4893 return rc;
4894}
4895
4896static void led_exit(void)
4897{
4898 unsigned int i;
4899
4900 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4901 if (tpacpi_leds[i].led_classdev.name)
4902 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
4903 }
4904
4905 kfree(tpacpi_leds);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004906}
4907
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004908static int __init tpacpi_init_led(unsigned int led)
4909{
4910 int rc;
4911
4912 tpacpi_leds[led].led = led;
4913
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03004914 /* LEDs with no name don't get registered */
4915 if (!tpacpi_led_names[led])
4916 return 0;
4917
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004918 tpacpi_leds[led].led_classdev.brightness_set = &led_sysfs_set;
4919 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
4920 if (led_supported == TPACPI_LED_570)
4921 tpacpi_leds[led].led_classdev.brightness_get =
4922 &led_sysfs_get;
4923
4924 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
4925
4926 INIT_WORK(&tpacpi_leds[led].work, led_set_status_worker);
4927
4928 rc = led_classdev_register(&tpacpi_pdev->dev,
4929 &tpacpi_leds[led].led_classdev);
4930 if (rc < 0)
4931 tpacpi_leds[led].led_classdev.name = NULL;
4932
4933 return rc;
4934}
4935
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03004936static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
4937 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
4938 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
4939 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
4940
4941 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
4942 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
4943 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
4944 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
4945 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
4946 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
4947 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
4948 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
4949
4950 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
4951 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
4952 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
4953 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
4954 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
4955
4956 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
4957 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
4958 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
4959 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
4960
4961 /* (1) - may have excess leds enabled on MSB */
4962
4963 /* Defaults (order matters, keep last, don't reorder!) */
4964 { /* Lenovo */
4965 .vendor = PCI_VENDOR_ID_LENOVO,
4966 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
4967 .quirks = 0x1fffU,
4968 },
4969 { /* IBM ThinkPads with no EC version string */
4970 .vendor = PCI_VENDOR_ID_IBM,
4971 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
4972 .quirks = 0x00ffU,
4973 },
4974 { /* IBM ThinkPads with EC version string */
4975 .vendor = PCI_VENDOR_ID_IBM,
4976 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
4977 .quirks = 0x00bfU,
4978 },
4979};
4980
4981#undef TPACPI_LEDQ_IBM
4982#undef TPACPI_LEDQ_LNV
4983
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004984static int __init led_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004985{
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004986 unsigned int i;
4987 int rc;
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03004988 unsigned long useful_leds;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004989
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004990 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
4991
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004992 TPACPI_ACPIHANDLE_INIT(led);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004993
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004994 if (!led_handle)
4995 /* led not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004996 led_supported = TPACPI_LED_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004997 else if (strlencmp(led_path, "SLED") == 0)
4998 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004999 led_supported = TPACPI_LED_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005000 else if (strlencmp(led_path, "SYSL") == 0)
5001 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005002 led_supported = TPACPI_LED_OLD;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005003 else
5004 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005005 led_supported = TPACPI_LED_NEW;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005006
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005007 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
5008 str_supported(led_supported), led_supported);
5009
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005010 if (led_supported == TPACPI_LED_NONE)
5011 return 1;
5012
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005013 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
5014 GFP_KERNEL);
5015 if (!tpacpi_leds) {
5016 printk(TPACPI_ERR "Out of memory for LED data\n");
5017 return -ENOMEM;
5018 }
5019
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005020 useful_leds = tpacpi_check_quirks(led_useful_qtable,
5021 ARRAY_SIZE(led_useful_qtable));
5022
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005023 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005024 if (!tpacpi_is_led_restricted(i) &&
5025 test_bit(i, &useful_leds)) {
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005026 rc = tpacpi_init_led(i);
5027 if (rc < 0) {
5028 led_exit();
5029 return rc;
5030 }
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005031 }
5032 }
5033
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005034#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005035 printk(TPACPI_NOTICE
5036 "warning: userspace override of important "
5037 "firmware LEDs is enabled\n");
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005038#endif
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005039 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005040}
5041
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005042#define str_led_status(s) \
5043 ((s) == TPACPI_LED_OFF ? "off" : \
5044 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005045
5046static int led_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005047{
5048 int len = 0;
5049
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005050 if (!led_supported) {
5051 len += sprintf(p + len, "status:\t\tnot supported\n");
5052 return len;
5053 }
5054 len += sprintf(p + len, "status:\t\tsupported\n");
5055
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005056 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005057 /* 570 */
5058 int i, status;
5059 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005060 status = led_get_status(i);
5061 if (status < 0)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005062 return -EIO;
5063 len += sprintf(p + len, "%d:\t\t%s\n",
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005064 i, str_led_status(status));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005065 }
5066 }
5067
Linus Torvalds1da177e2005-04-16 15:20:36 -07005068 len += sprintf(p + len, "commands:\t"
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005069 "<led> on, <led> off, <led> blink (<led> is 0-15)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005070
5071 return len;
5072}
5073
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005074static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005075{
5076 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005077 int led, rc;
5078 enum led_status_t s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005079
5080 if (!led_supported)
5081 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005082
5083 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005084 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 15)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005085 return -EINVAL;
5086
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005087 if (strstr(cmd, "off")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005088 s = TPACPI_LED_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005089 } else if (strstr(cmd, "on")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005090 s = TPACPI_LED_ON;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005091 } else if (strstr(cmd, "blink")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005092 s = TPACPI_LED_BLINK;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005093 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005094 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005095 }
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005096
5097 rc = led_set_status(led, s);
5098 if (rc < 0)
5099 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005100 }
5101
5102 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005103}
5104
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005105static struct ibm_struct led_driver_data = {
5106 .name = "led",
5107 .read = led_read,
5108 .write = led_write,
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005109 .exit = led_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005110};
5111
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005112/*************************************************************************
5113 * Beep subdriver
5114 */
5115
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005116TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005117
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005118#define TPACPI_BEEP_Q1 0x0001
5119
5120static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
5121 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
5122 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
5123};
5124
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005125static int __init beep_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005126{
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005127 unsigned long quirks;
5128
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005129 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
5130
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005131 TPACPI_ACPIHANDLE_INIT(beep);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005132
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005133 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
5134 str_supported(beep_handle != NULL));
5135
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005136 quirks = tpacpi_check_quirks(beep_quirk_table,
5137 ARRAY_SIZE(beep_quirk_table));
5138
5139 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
5140
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005141 return (beep_handle)? 0 : 1;
5142}
5143
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005144static int beep_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005145{
5146 int len = 0;
5147
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005148 if (!beep_handle)
5149 len += sprintf(p + len, "status:\t\tnot supported\n");
5150 else {
5151 len += sprintf(p + len, "status:\t\tsupported\n");
5152 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
5153 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005154
5155 return len;
5156}
5157
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005158static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005159{
5160 char *cmd;
5161 int beep_cmd;
5162
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005163 if (!beep_handle)
5164 return -ENODEV;
5165
Linus Torvalds1da177e2005-04-16 15:20:36 -07005166 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005167 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
5168 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005169 /* beep_cmd set */
5170 } else
5171 return -EINVAL;
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005172 if (tp_features.beep_needs_two_args) {
5173 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
5174 beep_cmd, 0))
5175 return -EIO;
5176 } else {
5177 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
5178 beep_cmd))
5179 return -EIO;
5180 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005181 }
5182
5183 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005184}
5185
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005186static struct ibm_struct beep_driver_data = {
5187 .name = "beep",
5188 .read = beep_read,
5189 .write = beep_write,
5190};
5191
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005192/*************************************************************************
5193 * Thermal subdriver
5194 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005195
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005196enum thermal_access_mode {
5197 TPACPI_THERMAL_NONE = 0, /* No thermal support */
5198 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
5199 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
5200 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
5201 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
5202};
5203
5204enum { /* TPACPI_THERMAL_TPEC_* */
5205 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
5206 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
5207 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
5208};
5209
5210#define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5211struct ibm_thermal_sensors_struct {
5212 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
5213};
5214
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005215static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005216
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005217/* idx is zero-based */
5218static int thermal_get_sensor(int idx, s32 *value)
5219{
5220 int t;
5221 s8 tmp;
5222 char tmpi[5];
5223
5224 t = TP_EC_THERMAL_TMP0;
5225
5226 switch (thermal_read_mode) {
5227#if TPACPI_MAX_THERMAL_SENSORS >= 16
5228 case TPACPI_THERMAL_TPEC_16:
5229 if (idx >= 8 && idx <= 15) {
5230 t = TP_EC_THERMAL_TMP8;
5231 idx -= 8;
5232 }
5233 /* fallthrough */
5234#endif
5235 case TPACPI_THERMAL_TPEC_8:
5236 if (idx <= 7) {
5237 if (!acpi_ec_read(t + idx, &tmp))
5238 return -EIO;
5239 *value = tmp * 1000;
5240 return 0;
5241 }
5242 break;
5243
5244 case TPACPI_THERMAL_ACPI_UPDT:
5245 if (idx <= 7) {
5246 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5247 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
5248 return -EIO;
5249 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5250 return -EIO;
5251 *value = (t - 2732) * 100;
5252 return 0;
5253 }
5254 break;
5255
5256 case TPACPI_THERMAL_ACPI_TMP07:
5257 if (idx <= 7) {
5258 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5259 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5260 return -EIO;
5261 if (t > 127 || t < -127)
5262 t = TP_EC_THERMAL_TMP_NA;
5263 *value = t * 1000;
5264 return 0;
5265 }
5266 break;
5267
5268 case TPACPI_THERMAL_NONE:
5269 default:
5270 return -ENOSYS;
5271 }
5272
5273 return -EINVAL;
5274}
5275
5276static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5277{
5278 int res, i;
5279 int n;
5280
5281 n = 8;
5282 i = 0;
5283
5284 if (!s)
5285 return -EINVAL;
5286
5287 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5288 n = 16;
5289
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005290 for (i = 0 ; i < n; i++) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005291 res = thermal_get_sensor(i, &s->temp[i]);
5292 if (res)
5293 return res;
5294 }
5295
5296 return n;
5297}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005298
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005299/* sysfs temp##_input -------------------------------------------------- */
5300
5301static ssize_t thermal_temp_input_show(struct device *dev,
5302 struct device_attribute *attr,
5303 char *buf)
5304{
5305 struct sensor_device_attribute *sensor_attr =
5306 to_sensor_dev_attr(attr);
5307 int idx = sensor_attr->index;
5308 s32 value;
5309 int res;
5310
5311 res = thermal_get_sensor(idx, &value);
5312 if (res)
5313 return res;
5314 if (value == TP_EC_THERMAL_TMP_NA * 1000)
5315 return -ENXIO;
5316
5317 return snprintf(buf, PAGE_SIZE, "%d\n", value);
5318}
5319
5320#define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005321 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5322 thermal_temp_input_show, NULL, _idxB)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005323
5324static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5325 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5326 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5327 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5328 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5329 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5330 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5331 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5332 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5333 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5334 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5335 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5336 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5337 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5338 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5339 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5340 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5341};
5342
5343#define THERMAL_ATTRS(X) \
5344 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5345
5346static struct attribute *thermal_temp_input_attr[] = {
5347 THERMAL_ATTRS(8),
5348 THERMAL_ATTRS(9),
5349 THERMAL_ATTRS(10),
5350 THERMAL_ATTRS(11),
5351 THERMAL_ATTRS(12),
5352 THERMAL_ATTRS(13),
5353 THERMAL_ATTRS(14),
5354 THERMAL_ATTRS(15),
5355 THERMAL_ATTRS(0),
5356 THERMAL_ATTRS(1),
5357 THERMAL_ATTRS(2),
5358 THERMAL_ATTRS(3),
5359 THERMAL_ATTRS(4),
5360 THERMAL_ATTRS(5),
5361 THERMAL_ATTRS(6),
5362 THERMAL_ATTRS(7),
5363 NULL
5364};
5365
5366static const struct attribute_group thermal_temp_input16_group = {
5367 .attrs = thermal_temp_input_attr
5368};
5369
5370static const struct attribute_group thermal_temp_input8_group = {
5371 .attrs = &thermal_temp_input_attr[8]
5372};
5373
5374#undef THERMAL_SENSOR_ATTR_TEMP
5375#undef THERMAL_ATTRS
5376
5377/* --------------------------------------------------------------------- */
5378
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005379static int __init thermal_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005380{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005381 u8 t, ta1, ta2;
5382 int i;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005383 int acpi_tmp7;
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005384 int res;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005385
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005386 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5387
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005388 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005389
Henrique de Moraes Holschuh3d6f99c2007-07-18 23:45:46 -03005390 if (thinkpad_id.ec_model) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005391 /*
5392 * Direct EC access mode: sensors at registers
5393 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5394 * non-implemented, thermal sensors return 0x80 when
5395 * not available
5396 */
5397
5398 ta1 = ta2 = 0;
5399 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03005400 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005401 ta1 |= t;
5402 } else {
5403 ta1 = 0;
5404 break;
5405 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03005406 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005407 ta2 |= t;
5408 } else {
5409 ta1 = 0;
5410 break;
5411 }
5412 }
5413 if (ta1 == 0) {
5414 /* This is sheer paranoia, but we handle it anyway */
5415 if (acpi_tmp7) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005416 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005417 "ThinkPad ACPI EC access misbehaving, "
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005418 "falling back to ACPI TMPx access "
5419 "mode\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005420 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005421 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005422 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005423 "ThinkPad ACPI EC access misbehaving, "
5424 "disabling thermal sensors access\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005425 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005426 }
5427 } else {
5428 thermal_read_mode =
5429 (ta2 != 0) ?
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005430 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005431 }
5432 } else if (acpi_tmp7) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005433 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
5434 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005435 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005436 } else {
5437 /* Standard ACPI TMPx access, max 8 sensors */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005438 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005439 }
5440 } else {
5441 /* temperatures not supported on 570, G4x, R30, R31, R32 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005442 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005443 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005444
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005445 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5446 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5447 thermal_read_mode);
5448
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005449 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005450 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005451 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005452 &thermal_temp_input16_group);
5453 if (res)
5454 return res;
5455 break;
5456 case TPACPI_THERMAL_TPEC_8:
5457 case TPACPI_THERMAL_ACPI_TMP07:
5458 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005459 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005460 &thermal_temp_input8_group);
5461 if (res)
5462 return res;
5463 break;
5464 case TPACPI_THERMAL_NONE:
5465 default:
5466 return 1;
5467 }
5468
5469 return 0;
5470}
5471
5472static void thermal_exit(void)
5473{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005474 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005475 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005476 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005477 &thermal_temp_input16_group);
5478 break;
5479 case TPACPI_THERMAL_TPEC_8:
5480 case TPACPI_THERMAL_ACPI_TMP07:
5481 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005482 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005483 &thermal_temp_input16_group);
5484 break;
5485 case TPACPI_THERMAL_NONE:
5486 default:
5487 break;
5488 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005489}
5490
5491static int thermal_read(char *p)
5492{
5493 int len = 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005494 int n, i;
5495 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005496
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005497 n = thermal_get_sensors(&t);
5498 if (unlikely(n < 0))
5499 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005500
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005501 len += sprintf(p + len, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005502
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005503 if (n > 0) {
5504 for (i = 0; i < (n - 1); i++)
5505 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
5506 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
5507 } else
5508 len += sprintf(p + len, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005509
5510 return len;
5511}
5512
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005513static struct ibm_struct thermal_driver_data = {
5514 .name = "thermal",
5515 .read = thermal_read,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005516 .exit = thermal_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005517};
5518
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005519/*************************************************************************
5520 * EC Dump subdriver
5521 */
5522
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005523static u8 ecdump_regs[256];
5524
5525static int ecdump_read(char *p)
5526{
5527 int len = 0;
5528 int i, j;
5529 u8 v;
5530
5531 len += sprintf(p + len, "EC "
5532 " +00 +01 +02 +03 +04 +05 +06 +07"
5533 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
5534 for (i = 0; i < 256; i += 16) {
5535 len += sprintf(p + len, "EC 0x%02x:", i);
5536 for (j = 0; j < 16; j++) {
5537 if (!acpi_ec_read(i + j, &v))
5538 break;
5539 if (v != ecdump_regs[i + j])
5540 len += sprintf(p + len, " *%02x", v);
5541 else
5542 len += sprintf(p + len, " %02x", v);
5543 ecdump_regs[i + j] = v;
5544 }
5545 len += sprintf(p + len, "\n");
5546 if (j != 16)
5547 break;
5548 }
5549
5550 /* These are way too dangerous to advertise openly... */
5551#if 0
5552 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
5553 " (<offset> is 00-ff, <value> is 00-ff)\n");
5554 len += sprintf(p + len, "commands:\t0x<offset> <value> "
5555 " (<offset> is 00-ff, <value> is 0-255)\n");
5556#endif
5557 return len;
5558}
5559
5560static int ecdump_write(char *buf)
5561{
5562 char *cmd;
5563 int i, v;
5564
5565 while ((cmd = next_cmd(&buf))) {
5566 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
5567 /* i and v set */
5568 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
5569 /* i and v set */
5570 } else
5571 return -EINVAL;
5572 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
5573 if (!acpi_ec_write(i, v))
5574 return -EIO;
5575 } else
5576 return -EINVAL;
5577 }
5578
5579 return 0;
5580}
5581
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005582static struct ibm_struct ecdump_driver_data = {
5583 .name = "ecdump",
5584 .read = ecdump_read,
5585 .write = ecdump_write,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03005586 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005587};
5588
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005589/*************************************************************************
5590 * Backlight/brightness subdriver
5591 */
Holger Macht8acb0252006-10-20 14:30:28 -07005592
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005593#define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5594
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005595/*
5596 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
5597 * CMOS NVRAM byte 0x5E, bits 0-3.
5598 *
5599 * EC HBRV (0x31) has the following layout
5600 * Bit 7: unknown function
5601 * Bit 6: unknown function
5602 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5603 * Bit 4: must be set to zero to avoid problems
5604 * Bit 3-0: backlight brightness level
5605 *
5606 * brightness_get_raw returns status data in the HBRV layout
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03005607 *
5608 * WARNING: The X61 has been verified to use HBRV for something else, so
5609 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
5610 * testing on the very early *60 Lenovo models...
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005611 */
5612
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005613enum {
5614 TP_EC_BACKLIGHT = 0x31,
5615
5616 /* TP_EC_BACKLIGHT bitmasks */
5617 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5618 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5619 TP_EC_BACKLIGHT_MAPSW = 0x20,
5620};
5621
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005622enum tpacpi_brightness_access_mode {
5623 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
5624 TPACPI_BRGHT_MODE_EC, /* EC control */
5625 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
5626 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
5627 TPACPI_BRGHT_MODE_MAX
5628};
5629
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -03005630static struct backlight_device *ibm_backlight_device;
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005631
5632static enum tpacpi_brightness_access_mode brightness_mode =
5633 TPACPI_BRGHT_MODE_MAX;
5634
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005635static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5636
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005637static struct mutex brightness_mutex;
5638
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005639/* NVRAM brightness access,
5640 * call with brightness_mutex held! */
5641static unsigned int tpacpi_brightness_nvram_get(void)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005642{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005643 u8 lnvram;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005644
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005645 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5646 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5647 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
5648 lnvram &= (tp_features.bright_16levels) ? 0x0f : 0x07;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005649
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005650 return lnvram;
5651}
5652
5653static void tpacpi_brightness_checkpoint_nvram(void)
5654{
5655 u8 lec = 0;
5656 u8 b_nvram;
5657
5658 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
5659 return;
5660
5661 vdbg_printk(TPACPI_DBG_BRGHT,
5662 "trying to checkpoint backlight level to NVRAM...\n");
5663
5664 if (mutex_lock_killable(&brightness_mutex) < 0)
5665 return;
5666
5667 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5668 goto unlock;
5669 lec &= TP_EC_BACKLIGHT_LVLMSK;
5670 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
5671
5672 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5673 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
5674 /* NVRAM needs update */
5675 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
5676 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
5677 b_nvram |= lec;
5678 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
5679 dbg_printk(TPACPI_DBG_BRGHT,
5680 "updated NVRAM backlight level to %u (0x%02x)\n",
5681 (unsigned int) lec, (unsigned int) b_nvram);
5682 } else
5683 vdbg_printk(TPACPI_DBG_BRGHT,
5684 "NVRAM backlight level already is %u (0x%02x)\n",
5685 (unsigned int) lec, (unsigned int) b_nvram);
5686
5687unlock:
5688 mutex_unlock(&brightness_mutex);
5689}
5690
5691
5692/* call with brightness_mutex held! */
5693static int tpacpi_brightness_get_raw(int *status)
5694{
5695 u8 lec = 0;
5696
5697 switch (brightness_mode) {
5698 case TPACPI_BRGHT_MODE_UCMS_STEP:
5699 *status = tpacpi_brightness_nvram_get();
5700 return 0;
5701 case TPACPI_BRGHT_MODE_EC:
5702 case TPACPI_BRGHT_MODE_ECNVRAM:
5703 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
Henrique de Moraes Holschuh2d5e94d2008-04-26 01:02:20 -03005704 return -EIO;
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005705 *status = lec;
5706 return 0;
5707 default:
5708 return -ENXIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005709 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005710}
5711
5712/* call with brightness_mutex held! */
5713/* do NOT call with illegal backlight level value */
5714static int tpacpi_brightness_set_ec(unsigned int value)
5715{
5716 u8 lec = 0;
5717
5718 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5719 return -EIO;
5720
5721 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
5722 (lec & TP_EC_BACKLIGHT_CMDMSK) |
5723 (value & TP_EC_BACKLIGHT_LVLMSK))))
5724 return -EIO;
5725
5726 return 0;
5727}
5728
5729/* call with brightness_mutex held! */
5730static int tpacpi_brightness_set_ucmsstep(unsigned int value)
5731{
5732 int cmos_cmd, inc;
5733 unsigned int current_value, i;
5734
5735 current_value = tpacpi_brightness_nvram_get();
5736
5737 if (value == current_value)
5738 return 0;
5739
5740 cmos_cmd = (value > current_value) ?
5741 TP_CMOS_BRIGHTNESS_UP :
5742 TP_CMOS_BRIGHTNESS_DOWN;
5743 inc = (value > current_value) ? 1 : -1;
5744
5745 for (i = current_value; i != value; i += inc)
5746 if (issue_thinkpad_cmos_command(cmos_cmd))
5747 return -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005748
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005749 return 0;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005750}
5751
5752/* May return EINTR which can always be mapped to ERESTARTSYS */
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005753static int brightness_set(unsigned int value)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005754{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005755 int res;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005756
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005757 if (value > ((tp_features.bright_16levels)? 15 : 7) ||
5758 value < 0)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005759 return -EINVAL;
5760
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005761 vdbg_printk(TPACPI_DBG_BRGHT,
5762 "set backlight level to %d\n", value);
5763
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02005764 res = mutex_lock_killable(&brightness_mutex);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005765 if (res < 0)
5766 return res;
5767
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005768 switch (brightness_mode) {
5769 case TPACPI_BRGHT_MODE_EC:
5770 case TPACPI_BRGHT_MODE_ECNVRAM:
5771 res = tpacpi_brightness_set_ec(value);
5772 break;
5773 case TPACPI_BRGHT_MODE_UCMS_STEP:
5774 res = tpacpi_brightness_set_ucmsstep(value);
5775 break;
5776 default:
5777 res = -ENXIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005778 }
5779
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005780 mutex_unlock(&brightness_mutex);
5781 return res;
5782}
5783
5784/* sysfs backlight class ----------------------------------------------- */
5785
5786static int brightness_update_status(struct backlight_device *bd)
5787{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005788 unsigned int level =
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005789 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
5790 bd->props.power == FB_BLANK_UNBLANK) ?
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005791 bd->props.brightness : 0;
5792
5793 dbg_printk(TPACPI_DBG_BRGHT,
5794 "backlight: attempt to set level to %d\n",
5795 level);
5796
5797 /* it is the backlight class's job (caller) to handle
5798 * EINTR and other errors properly */
5799 return brightness_set(level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005800}
Holger Macht8acb0252006-10-20 14:30:28 -07005801
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005802static int brightness_get(struct backlight_device *bd)
5803{
5804 int status, res;
5805
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005806 res = mutex_lock_killable(&brightness_mutex);
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005807 if (res < 0)
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005808 return 0;
5809
5810 res = tpacpi_brightness_get_raw(&status);
5811
5812 mutex_unlock(&brightness_mutex);
5813
5814 if (res < 0)
5815 return 0;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005816
5817 return status & TP_EC_BACKLIGHT_LVLMSK;
5818}
5819
Richard Purdie599a52d2007-02-10 23:07:48 +00005820static struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005821 .get_brightness = brightness_get,
5822 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005823};
5824
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005825/* --------------------------------------------------------------------- */
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03005826
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03005827/*
5828 * These are only useful for models that have only one possibility
5829 * of GPU. If the BIOS model handles both ATI and Intel, don't use
5830 * these quirks.
5831 */
5832#define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
5833#define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
5834#define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
5835
5836static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
5837 /* Models with ATI GPUs known to require ECNVRAM mode */
5838 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
5839
Henrique de Moraes Holschuh6da25bf2009-09-12 15:22:11 -03005840 /* Models with ATI GPUs that can use ECNVRAM */
5841 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC),
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03005842 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
5843 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
5844 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
5845
Henrique de Moraes Holschuh6da25bf2009-09-12 15:22:11 -03005846 /* Models with Intel Extreme Graphics 2 */
5847 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC),
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03005848 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_NOEC),
5849 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_NOEC),
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03005850
5851 /* Models with Intel GMA900 */
5852 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
5853 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
5854 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
5855};
5856
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005857static int __init brightness_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005858{
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02005859 int b;
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03005860 unsigned long quirks;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02005861
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005862 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
5863
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03005864 mutex_init(&brightness_mutex);
5865
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03005866 quirks = tpacpi_check_quirks(brightness_quirk_table,
5867 ARRAY_SIZE(brightness_quirk_table));
5868
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03005869 /*
5870 * We always attempt to detect acpi support, so as to switch
5871 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
5872 * going to publish a backlight interface
5873 */
5874 b = tpacpi_check_std_acpi_brightness_support();
5875 if (b > 0) {
Thomas Renninger2dba1b52008-08-01 17:38:03 +02005876
5877 if (acpi_video_backlight_support()) {
5878 if (brightness_enable > 1) {
5879 printk(TPACPI_NOTICE
5880 "Standard ACPI backlight interface "
5881 "available, not loading native one.\n");
5882 return 1;
5883 } else if (brightness_enable == 1) {
5884 printk(TPACPI_NOTICE
5885 "Backlight control force enabled, even if standard "
5886 "ACPI backlight interface is available\n");
5887 }
5888 } else {
5889 if (brightness_enable > 1) {
5890 printk(TPACPI_NOTICE
5891 "Standard ACPI backlight interface not "
5892 "available, thinkpad_acpi native "
5893 "brightness control enabled\n");
5894 }
Henrique de Moraes Holschuhe11e2112007-10-30 17:46:22 -02005895 }
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02005896 }
5897
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03005898 if (!brightness_enable) {
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005899 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03005900 "brightness support disabled by "
5901 "module parameter\n");
5902 return 1;
5903 }
5904
5905 if (b > 16) {
5906 printk(TPACPI_ERR
5907 "Unsupported brightness interface, "
5908 "please contact %s\n", TPACPI_MAIL);
5909 return 1;
5910 }
5911 if (b == 16)
5912 tp_features.bright_16levels = 1;
5913
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005914 /*
5915 * Check for module parameter bogosity, note that we
5916 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
5917 * able to detect "unspecified"
5918 */
5919 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03005920 return -EINVAL;
5921
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005922 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
5923 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
5924 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03005925 if (quirks & TPACPI_BRGHT_Q_EC)
5926 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
5927 else
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005928 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
5929
5930 dbg_printk(TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03005931 "driver auto-selected brightness_mode=%d\n",
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005932 brightness_mode);
5933 }
5934
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03005935 /* Safety */
5936 if (thinkpad_id.vendor != PCI_VENDOR_ID_IBM &&
5937 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
5938 brightness_mode == TPACPI_BRGHT_MODE_EC))
5939 return -EINVAL;
5940
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005941 if (tpacpi_brightness_get_raw(&b) < 0)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03005942 return 1;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02005943
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02005944 if (tp_features.bright_16levels)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005945 printk(TPACPI_INFO
5946 "detected a 16-level brightness capable ThinkPad\n");
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02005947
Henrique de Moraes Holschuh7d5a0152007-04-24 11:48:20 -03005948 ibm_backlight_device = backlight_device_register(
5949 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
5950 &ibm_backlight_data);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005951 if (IS_ERR(ibm_backlight_device)) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005952 printk(TPACPI_ERR "Could not register backlight device\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005953 return PTR_ERR(ibm_backlight_device);
5954 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005955 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
5956 "brightness is supported\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005957
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03005958 if (quirks & TPACPI_BRGHT_Q_ASK) {
5959 printk(TPACPI_NOTICE
5960 "brightness: will use unverified default: "
5961 "brightness_mode=%d\n", brightness_mode);
5962 printk(TPACPI_NOTICE
5963 "brightness: please report to %s whether it works well "
5964 "or not on your ThinkPad\n", TPACPI_MAIL);
5965 }
5966
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02005967 ibm_backlight_device->props.max_brightness =
5968 (tp_features.bright_16levels)? 15 : 7;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005969 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02005970 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00005971
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005972 return 0;
5973}
5974
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005975static void brightness_suspend(pm_message_t state)
5976{
5977 tpacpi_brightness_checkpoint_nvram();
5978}
5979
5980static void brightness_shutdown(void)
5981{
5982 tpacpi_brightness_checkpoint_nvram();
5983}
5984
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005985static void brightness_exit(void)
5986{
5987 if (ibm_backlight_device) {
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005988 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005989 "calling backlight_device_unregister()\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005990 backlight_device_unregister(ibm_backlight_device);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005991 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005992
5993 tpacpi_brightness_checkpoint_nvram();
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005994}
5995
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005996static int brightness_read(char *p)
5997{
5998 int len = 0;
5999 int level;
6000
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006001 level = brightness_get(NULL);
6002 if (level < 0) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006003 len += sprintf(p + len, "level:\t\tunreadable\n");
6004 } else {
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02006005 len += sprintf(p + len, "level:\t\t%d\n", level);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006006 len += sprintf(p + len, "commands:\tup, down\n");
6007 len += sprintf(p + len, "commands:\tlevel <level>"
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02006008 " (<level> is 0-%d)\n",
6009 (tp_features.bright_16levels) ? 15 : 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006010 }
6011
6012 return len;
6013}
6014
6015static int brightness_write(char *buf)
6016{
6017 int level;
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006018 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006019 char *cmd;
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02006020 int max_level = (tp_features.bright_16levels) ? 15 : 7;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006021
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006022 level = brightness_get(NULL);
6023 if (level < 0)
6024 return level;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006025
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006026 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006027 if (strlencmp(cmd, "up") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006028 if (level < max_level)
6029 level++;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006030 } else if (strlencmp(cmd, "down") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006031 if (level > 0)
6032 level--;
6033 } else if (sscanf(cmd, "level %d", &level) == 1 &&
6034 level >= 0 && level <= max_level) {
6035 /* new level set */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006036 } else
6037 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006038 }
6039
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006040 tpacpi_disclose_usertask("procfs brightness",
6041 "set level to %d\n", level);
6042
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006043 /*
6044 * Now we know what the final level should be, so we try to set it.
6045 * Doing it this way makes the syscall restartable in case of EINTR
6046 */
6047 rc = brightness_set(level);
6048 return (rc == -EINTR)? ERESTARTSYS : rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006049}
6050
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006051static struct ibm_struct brightness_driver_data = {
6052 .name = "brightness",
6053 .read = brightness_read,
6054 .write = brightness_write,
6055 .exit = brightness_exit,
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006056 .suspend = brightness_suspend,
6057 .shutdown = brightness_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006058};
6059
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006060/*************************************************************************
6061 * Volume subdriver
6062 */
6063
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006064static int volume_offset = 0x30;
6065
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006066static int volume_read(char *p)
6067{
6068 int len = 0;
6069 u8 level;
6070
6071 if (!acpi_ec_read(volume_offset, &level)) {
6072 len += sprintf(p + len, "level:\t\tunreadable\n");
6073 } else {
6074 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
6075 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
6076 len += sprintf(p + len, "commands:\tup, down, mute\n");
6077 len += sprintf(p + len, "commands:\tlevel <level>"
6078 " (<level> is 0-15)\n");
6079 }
6080
6081 return len;
6082}
6083
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006084static int volume_write(char *buf)
6085{
6086 int cmos_cmd, inc, i;
6087 u8 level, mute;
6088 int new_level, new_mute;
6089 char *cmd;
6090
6091 while ((cmd = next_cmd(&buf))) {
6092 if (!acpi_ec_read(volume_offset, &level))
6093 return -EIO;
6094 new_mute = mute = level & 0x40;
6095 new_level = level = level & 0xf;
6096
6097 if (strlencmp(cmd, "up") == 0) {
6098 if (mute)
6099 new_mute = 0;
6100 else
6101 new_level = level == 15 ? 15 : level + 1;
6102 } else if (strlencmp(cmd, "down") == 0) {
6103 if (mute)
6104 new_mute = 0;
6105 else
6106 new_level = level == 0 ? 0 : level - 1;
6107 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
6108 new_level >= 0 && new_level <= 15) {
6109 /* new_level set */
6110 } else if (strlencmp(cmd, "mute") == 0) {
6111 new_mute = 0x40;
6112 } else
6113 return -EINVAL;
6114
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006115 if (new_level != level) {
6116 /* mute doesn't change */
6117
6118 cmos_cmd = (new_level > level) ?
6119 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006120 inc = new_level > level ? 1 : -1;
6121
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03006122 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006123 !acpi_ec_write(volume_offset, level)))
6124 return -EIO;
6125
6126 for (i = level; i != new_level; i += inc)
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03006127 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006128 !acpi_ec_write(volume_offset, i + inc))
6129 return -EIO;
6130
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006131 if (mute &&
6132 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
6133 !acpi_ec_write(volume_offset, new_level + mute))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006134 return -EIO;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006135 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006136 }
6137
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006138 if (new_mute != mute) {
6139 /* level doesn't change */
6140
6141 cmos_cmd = (new_mute) ?
6142 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006143
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03006144 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006145 !acpi_ec_write(volume_offset, level + new_mute))
6146 return -EIO;
6147 }
6148 }
6149
6150 return 0;
6151}
6152
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006153static struct ibm_struct volume_driver_data = {
6154 .name = "volume",
6155 .read = volume_read,
6156 .write = volume_write,
6157};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006158
6159/*************************************************************************
6160 * Fan subdriver
6161 */
6162
6163/*
6164 * FAN ACCESS MODES
6165 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006166 * TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006167 * ACPI GFAN method: returns fan level
6168 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006169 * see TPACPI_FAN_WR_ACPI_SFAN
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03006170 * EC 0x2f (HFSP) not available if GFAN exists
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006171 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006172 * TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006173 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
6174 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03006175 * EC 0x2f (HFSP) might be available *for reading*, but do not use
6176 * it for writing.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006177 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006178 * TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03006179 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
6180 * Supported on almost all ThinkPads
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006181 *
6182 * Fan speed changes of any sort (including those caused by the
6183 * disengaged mode) are usually done slowly by the firmware as the
6184 * maximum ammount of fan duty cycle change per second seems to be
6185 * limited.
6186 *
6187 * Reading is not available if GFAN exists.
6188 * Writing is not available if SFAN exists.
6189 *
6190 * Bits
6191 * 7 automatic mode engaged;
6192 * (default operation mode of the ThinkPad)
6193 * fan level is ignored in this mode.
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03006194 * 6 full speed mode (takes precedence over bit 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006195 * not available on all thinkpads. May disable
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03006196 * the tachometer while the fan controller ramps up
6197 * the speed (which can take up to a few *minutes*).
6198 * Speeds up fan to 100% duty-cycle, which is far above
6199 * the standard RPM levels. It is not impossible that
6200 * it could cause hardware damage.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006201 * 5-3 unused in some models. Extra bits for fan level
6202 * in others, but still useless as all values above
6203 * 7 map to the same speed as level 7 in these models.
6204 * 2-0 fan level (0..7 usually)
6205 * 0x00 = stop
6206 * 0x07 = max (set when temperatures critical)
6207 * Some ThinkPads may have other levels, see
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006208 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006209 *
6210 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
6211 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
6212 * does so, its initial value is meaningless (0x07).
6213 *
6214 * For firmware bugs, refer to:
6215 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
6216 *
6217 * ----
6218 *
6219 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
6220 * Main fan tachometer reading (in RPM)
6221 *
6222 * This register is present on all ThinkPads with a new-style EC, and
6223 * it is known not to be present on the A21m/e, and T22, as there is
6224 * something else in offset 0x84 according to the ACPI DSDT. Other
6225 * ThinkPads from this same time period (and earlier) probably lack the
6226 * tachometer as well.
6227 *
Nick Andrew877d0312009-01-26 11:06:57 +01006228 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006229 * was never fixed by IBM to report the EC firmware version string
6230 * probably support the tachometer (like the early X models), so
6231 * detecting it is quite hard. We need more data to know for sure.
6232 *
6233 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
6234 * might result.
6235 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03006236 * FIRMWARE BUG: may go stale while the EC is switching to full speed
6237 * mode.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006238 *
6239 * For firmware bugs, refer to:
6240 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
6241 *
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03006242 * ----
6243 *
6244 * ThinkPad EC register 0x31 bit 0 (only on select models)
6245 *
6246 * When bit 0 of EC register 0x31 is zero, the tachometer registers
6247 * show the speed of the main fan. When bit 0 of EC register 0x31
6248 * is one, the tachometer registers show the speed of the auxiliary
6249 * fan.
6250 *
6251 * Fan control seems to affect both fans, regardless of the state
6252 * of this bit.
6253 *
6254 * So far, only the firmware for the X60/X61 non-tablet versions
6255 * seem to support this (firmware TP-7M).
6256 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006257 * TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006258 * ThinkPad X31, X40, X41. Not available in the X60.
6259 *
6260 * FANS ACPI handle: takes three arguments: low speed, medium speed,
6261 * high speed. ACPI DSDT seems to map these three speeds to levels
6262 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
6263 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
6264 *
6265 * The speeds are stored on handles
6266 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
6267 *
6268 * There are three default speed sets, acessible as handles:
6269 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
6270 *
6271 * ACPI DSDT switches which set is in use depending on various
6272 * factors.
6273 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006274 * TPACPI_FAN_WR_TPEC is also available and should be used to
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006275 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
6276 * but the ACPI tables just mention level 7.
6277 */
6278
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006279enum { /* Fan control constants */
6280 fan_status_offset = 0x2f, /* EC register 0x2f */
6281 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
6282 * 0x84 must be read before 0x85 */
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03006283 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
6284 bit 0 selects which fan is active */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006285
6286 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
6287 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
6288
6289 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
6290};
6291
6292enum fan_status_access_mode {
6293 TPACPI_FAN_NONE = 0, /* No fan status or control */
6294 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
6295 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
6296};
6297
6298enum fan_control_access_mode {
6299 TPACPI_FAN_WR_NONE = 0, /* No fan control */
6300 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
6301 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
6302 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
6303};
6304
6305enum fan_control_commands {
6306 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
6307 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
6308 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
6309 * and also watchdog cmd */
6310};
6311
6312static int fan_control_allowed;
6313
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006314static enum fan_status_access_mode fan_status_access_mode;
6315static enum fan_control_access_mode fan_control_access_mode;
6316static enum fan_control_commands fan_control_commands;
6317
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02006318static u8 fan_control_initial_status;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006319static u8 fan_control_desired_level;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006320static u8 fan_control_resume_level;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02006321static int fan_watchdog_maxinterval;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006322
6323static struct mutex fan_mutex;
6324
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006325static void fan_watchdog_fire(struct work_struct *ignored);
Len Brown25c68a32006-12-08 04:43:41 -05006326static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02006327
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006328TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
6329TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006330 "\\FSPD", /* 600e/x, 770e, 770x */
6331 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006332TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006333 "JFNS", /* 770x-JL */
6334 ); /* all others */
6335
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006336/*
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006337 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
6338 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
6339 * be in auto mode (0x80).
6340 *
6341 * This is corrected by any write to HFSP either by the driver, or
6342 * by the firmware.
6343 *
6344 * We assume 0x07 really means auto mode while this quirk is active,
6345 * as this is far more likely than the ThinkPad being in level 7,
6346 * which is only used by the firmware during thermal emergencies.
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03006347 *
6348 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
6349 * TP-70 (T43, R52), which are known to be buggy.
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006350 */
6351
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03006352static void fan_quirk1_setup(void)
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006353{
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006354 if (fan_control_initial_status == 0x07) {
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03006355 printk(TPACPI_NOTICE
6356 "fan_init: initial fan status is unknown, "
6357 "assuming it is in auto mode\n");
6358 tp_features.fan_ctrl_status_undef = 1;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006359 }
6360}
6361
6362static void fan_quirk1_handle(u8 *fan_status)
6363{
6364 if (unlikely(tp_features.fan_ctrl_status_undef)) {
6365 if (*fan_status != fan_control_initial_status) {
6366 /* something changed the HFSP regisnter since
6367 * driver init time, so it is not undefined
6368 * anymore */
6369 tp_features.fan_ctrl_status_undef = 0;
6370 } else {
6371 /* Return most likely status. In fact, it
6372 * might be the only possible status */
6373 *fan_status = TP_EC_FAN_AUTO;
6374 }
6375 }
6376}
6377
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03006378/* Select main fan on X60/X61, NOOP on others */
6379static bool fan_select_fan1(void)
6380{
6381 if (tp_features.second_fan) {
6382 u8 val;
6383
6384 if (ec_read(fan_select_offset, &val) < 0)
6385 return false;
6386 val &= 0xFEU;
6387 if (ec_write(fan_select_offset, val) < 0)
6388 return false;
6389 }
6390 return true;
6391}
6392
6393/* Select secondary fan on X60/X61 */
6394static bool fan_select_fan2(void)
6395{
6396 u8 val;
6397
6398 if (!tp_features.second_fan)
6399 return false;
6400
6401 if (ec_read(fan_select_offset, &val) < 0)
6402 return false;
6403 val |= 0x01U;
6404 if (ec_write(fan_select_offset, val) < 0)
6405 return false;
6406
6407 return true;
6408}
6409
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006410/*
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006411 * Call with fan_mutex held
6412 */
6413static void fan_update_desired_level(u8 status)
6414{
6415 if ((status &
6416 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
6417 if (status > 7)
6418 fan_control_desired_level = 7;
6419 else
6420 fan_control_desired_level = status;
6421 }
6422}
6423
6424static int fan_get_status(u8 *status)
6425{
6426 u8 s;
6427
6428 /* TODO:
6429 * Add TPACPI_FAN_RD_ACPI_FANS ? */
6430
6431 switch (fan_status_access_mode) {
6432 case TPACPI_FAN_RD_ACPI_GFAN:
6433 /* 570, 600e/x, 770e, 770x */
6434
6435 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
6436 return -EIO;
6437
6438 if (likely(status))
6439 *status = s & 0x07;
6440
6441 break;
6442
6443 case TPACPI_FAN_RD_TPEC:
6444 /* all except 570, 600e/x, 770e, 770x */
6445 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
6446 return -EIO;
6447
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006448 if (likely(status)) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006449 *status = s;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006450 fan_quirk1_handle(status);
6451 }
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006452
6453 break;
6454
6455 default:
6456 return -ENXIO;
6457 }
6458
6459 return 0;
6460}
6461
6462static int fan_get_status_safe(u8 *status)
6463{
6464 int rc;
6465 u8 s;
6466
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006467 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006468 return -ERESTARTSYS;
6469 rc = fan_get_status(&s);
6470 if (!rc)
6471 fan_update_desired_level(s);
6472 mutex_unlock(&fan_mutex);
6473
6474 if (status)
6475 *status = s;
6476
6477 return rc;
6478}
6479
6480static int fan_get_speed(unsigned int *speed)
6481{
6482 u8 hi, lo;
6483
6484 switch (fan_status_access_mode) {
6485 case TPACPI_FAN_RD_TPEC:
6486 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03006487 if (unlikely(!fan_select_fan1()))
6488 return -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006489 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
6490 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
6491 return -EIO;
6492
6493 if (likely(speed))
6494 *speed = (hi << 8) | lo;
6495
6496 break;
6497
6498 default:
6499 return -ENXIO;
6500 }
6501
6502 return 0;
6503}
6504
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03006505static int fan2_get_speed(unsigned int *speed)
6506{
6507 u8 hi, lo;
6508 bool rc;
6509
6510 switch (fan_status_access_mode) {
6511 case TPACPI_FAN_RD_TPEC:
6512 /* all except 570, 600e/x, 770e, 770x */
6513 if (unlikely(!fan_select_fan2()))
6514 return -EIO;
6515 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
6516 !acpi_ec_read(fan_rpm_offset + 1, &hi);
6517 fan_select_fan1(); /* play it safe */
6518 if (rc)
6519 return -EIO;
6520
6521 if (likely(speed))
6522 *speed = (hi << 8) | lo;
6523
6524 break;
6525
6526 default:
6527 return -ENXIO;
6528 }
6529
6530 return 0;
6531}
6532
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006533static int fan_set_level(int level)
6534{
6535 if (!fan_control_allowed)
6536 return -EPERM;
6537
6538 switch (fan_control_access_mode) {
6539 case TPACPI_FAN_WR_ACPI_SFAN:
6540 if (level >= 0 && level <= 7) {
6541 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
6542 return -EIO;
6543 } else
6544 return -EINVAL;
6545 break;
6546
6547 case TPACPI_FAN_WR_ACPI_FANS:
6548 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006549 if (!(level & TP_EC_FAN_AUTO) &&
6550 !(level & TP_EC_FAN_FULLSPEED) &&
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006551 ((level < 0) || (level > 7)))
6552 return -EINVAL;
6553
6554 /* safety net should the EC not support AUTO
6555 * or FULLSPEED mode bits and just ignore them */
6556 if (level & TP_EC_FAN_FULLSPEED)
6557 level |= 7; /* safety min speed 7 */
Roel Kluin547266e2008-02-05 00:24:56 +01006558 else if (level & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006559 level |= 4; /* safety min speed 4 */
6560
6561 if (!acpi_ec_write(fan_status_offset, level))
6562 return -EIO;
6563 else
6564 tp_features.fan_ctrl_status_undef = 0;
6565 break;
6566
6567 default:
6568 return -ENXIO;
6569 }
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00006570
6571 vdbg_printk(TPACPI_DBG_FAN,
6572 "fan control: set fan control register to 0x%02x\n", level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006573 return 0;
6574}
6575
6576static int fan_set_level_safe(int level)
6577{
6578 int rc;
6579
6580 if (!fan_control_allowed)
6581 return -EPERM;
6582
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006583 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006584 return -ERESTARTSYS;
6585
6586 if (level == TPACPI_FAN_LAST_LEVEL)
6587 level = fan_control_desired_level;
6588
6589 rc = fan_set_level(level);
6590 if (!rc)
6591 fan_update_desired_level(level);
6592
6593 mutex_unlock(&fan_mutex);
6594 return rc;
6595}
6596
6597static int fan_set_enable(void)
6598{
6599 u8 s;
6600 int rc;
6601
6602 if (!fan_control_allowed)
6603 return -EPERM;
6604
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006605 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006606 return -ERESTARTSYS;
6607
6608 switch (fan_control_access_mode) {
6609 case TPACPI_FAN_WR_ACPI_FANS:
6610 case TPACPI_FAN_WR_TPEC:
6611 rc = fan_get_status(&s);
6612 if (rc < 0)
6613 break;
6614
6615 /* Don't go out of emergency fan mode */
6616 if (s != 7) {
6617 s &= 0x07;
6618 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
6619 }
6620
6621 if (!acpi_ec_write(fan_status_offset, s))
6622 rc = -EIO;
6623 else {
6624 tp_features.fan_ctrl_status_undef = 0;
6625 rc = 0;
6626 }
6627 break;
6628
6629 case TPACPI_FAN_WR_ACPI_SFAN:
6630 rc = fan_get_status(&s);
6631 if (rc < 0)
6632 break;
6633
6634 s &= 0x07;
6635
6636 /* Set fan to at least level 4 */
6637 s |= 4;
6638
6639 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006640 rc = -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006641 else
6642 rc = 0;
6643 break;
6644
6645 default:
6646 rc = -ENXIO;
6647 }
6648
6649 mutex_unlock(&fan_mutex);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00006650
6651 if (!rc)
6652 vdbg_printk(TPACPI_DBG_FAN,
6653 "fan control: set fan control register to 0x%02x\n",
6654 s);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006655 return rc;
6656}
6657
6658static int fan_set_disable(void)
6659{
6660 int rc;
6661
6662 if (!fan_control_allowed)
6663 return -EPERM;
6664
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006665 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006666 return -ERESTARTSYS;
6667
6668 rc = 0;
6669 switch (fan_control_access_mode) {
6670 case TPACPI_FAN_WR_ACPI_FANS:
6671 case TPACPI_FAN_WR_TPEC:
6672 if (!acpi_ec_write(fan_status_offset, 0x00))
6673 rc = -EIO;
6674 else {
6675 fan_control_desired_level = 0;
6676 tp_features.fan_ctrl_status_undef = 0;
6677 }
6678 break;
6679
6680 case TPACPI_FAN_WR_ACPI_SFAN:
6681 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
6682 rc = -EIO;
6683 else
6684 fan_control_desired_level = 0;
6685 break;
6686
6687 default:
6688 rc = -ENXIO;
6689 }
6690
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00006691 if (!rc)
6692 vdbg_printk(TPACPI_DBG_FAN,
6693 "fan control: set fan control register to 0\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006694
6695 mutex_unlock(&fan_mutex);
6696 return rc;
6697}
6698
6699static int fan_set_speed(int speed)
6700{
6701 int rc;
6702
6703 if (!fan_control_allowed)
6704 return -EPERM;
6705
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006706 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006707 return -ERESTARTSYS;
6708
6709 rc = 0;
6710 switch (fan_control_access_mode) {
6711 case TPACPI_FAN_WR_ACPI_FANS:
6712 if (speed >= 0 && speed <= 65535) {
6713 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
6714 speed, speed, speed))
6715 rc = -EIO;
6716 } else
6717 rc = -EINVAL;
6718 break;
6719
6720 default:
6721 rc = -ENXIO;
6722 }
6723
6724 mutex_unlock(&fan_mutex);
6725 return rc;
6726}
6727
6728static void fan_watchdog_reset(void)
6729{
6730 static int fan_watchdog_active;
6731
6732 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
6733 return;
6734
6735 if (fan_watchdog_active)
6736 cancel_delayed_work(&fan_watchdog_task);
6737
6738 if (fan_watchdog_maxinterval > 0 &&
6739 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
6740 fan_watchdog_active = 1;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03006741 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006742 msecs_to_jiffies(fan_watchdog_maxinterval
6743 * 1000))) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006744 printk(TPACPI_ERR
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03006745 "failed to queue the fan watchdog, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006746 "watchdog will not trigger\n");
6747 }
6748 } else
6749 fan_watchdog_active = 0;
6750}
6751
6752static void fan_watchdog_fire(struct work_struct *ignored)
6753{
6754 int rc;
6755
6756 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
6757 return;
6758
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006759 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006760 rc = fan_set_enable();
6761 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006762 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006763 "will try again later...\n", -rc);
6764 /* reschedule for later */
6765 fan_watchdog_reset();
6766 }
6767}
6768
6769/*
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006770 * SYSFS fan layout: hwmon compatible (device)
6771 *
6772 * pwm*_enable:
6773 * 0: "disengaged" mode
6774 * 1: manual mode
6775 * 2: native EC "auto" mode (recommended, hardware default)
6776 *
6777 * pwm*: set speed in manual mode, ignored otherwise.
6778 * 0 is level 0; 255 is level 7. Intermediate points done with linear
6779 * interpolation.
6780 *
6781 * fan*_input: tachometer reading, RPM
6782 *
6783 *
6784 * SYSFS fan layout: extensions
6785 *
6786 * fan_watchdog (driver):
6787 * fan watchdog interval in seconds, 0 disables (default), max 120
6788 */
6789
6790/* sysfs fan pwm1_enable ----------------------------------------------- */
6791static ssize_t fan_pwm1_enable_show(struct device *dev,
6792 struct device_attribute *attr,
6793 char *buf)
6794{
6795 int res, mode;
6796 u8 status;
6797
6798 res = fan_get_status_safe(&status);
6799 if (res)
6800 return res;
6801
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006802 if (status & TP_EC_FAN_FULLSPEED) {
6803 mode = 0;
6804 } else if (status & TP_EC_FAN_AUTO) {
6805 mode = 2;
6806 } else
6807 mode = 1;
6808
6809 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
6810}
6811
6812static ssize_t fan_pwm1_enable_store(struct device *dev,
6813 struct device_attribute *attr,
6814 const char *buf, size_t count)
6815{
6816 unsigned long t;
6817 int res, level;
6818
6819 if (parse_strtoul(buf, 2, &t))
6820 return -EINVAL;
6821
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00006822 tpacpi_disclose_usertask("hwmon pwm1_enable",
6823 "set fan mode to %lu\n", t);
6824
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006825 switch (t) {
6826 case 0:
6827 level = TP_EC_FAN_FULLSPEED;
6828 break;
6829 case 1:
6830 level = TPACPI_FAN_LAST_LEVEL;
6831 break;
6832 case 2:
6833 level = TP_EC_FAN_AUTO;
6834 break;
6835 case 3:
6836 /* reserved for software-controlled auto mode */
6837 return -ENOSYS;
6838 default:
6839 return -EINVAL;
6840 }
6841
6842 res = fan_set_level_safe(level);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03006843 if (res == -ENXIO)
6844 return -EINVAL;
6845 else if (res < 0)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006846 return res;
6847
6848 fan_watchdog_reset();
6849
6850 return count;
6851}
6852
6853static struct device_attribute dev_attr_fan_pwm1_enable =
6854 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
6855 fan_pwm1_enable_show, fan_pwm1_enable_store);
6856
6857/* sysfs fan pwm1 ------------------------------------------------------ */
6858static ssize_t fan_pwm1_show(struct device *dev,
6859 struct device_attribute *attr,
6860 char *buf)
6861{
6862 int res;
6863 u8 status;
6864
6865 res = fan_get_status_safe(&status);
6866 if (res)
6867 return res;
6868
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006869 if ((status &
6870 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
6871 status = fan_control_desired_level;
6872
6873 if (status > 7)
6874 status = 7;
6875
6876 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
6877}
6878
6879static ssize_t fan_pwm1_store(struct device *dev,
6880 struct device_attribute *attr,
6881 const char *buf, size_t count)
6882{
6883 unsigned long s;
6884 int rc;
6885 u8 status, newlevel;
6886
6887 if (parse_strtoul(buf, 255, &s))
6888 return -EINVAL;
6889
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00006890 tpacpi_disclose_usertask("hwmon pwm1",
6891 "set fan speed to %lu\n", s);
6892
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006893 /* scale down from 0-255 to 0-7 */
6894 newlevel = (s >> 5) & 0x07;
6895
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006896 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02006897 return -ERESTARTSYS;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006898
6899 rc = fan_get_status(&status);
6900 if (!rc && (status &
6901 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
6902 rc = fan_set_level(newlevel);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03006903 if (rc == -ENXIO)
6904 rc = -EINVAL;
6905 else if (!rc) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006906 fan_update_desired_level(newlevel);
Henrique de Moraes Holschuhca4ac2f2007-04-27 22:00:11 -03006907 fan_watchdog_reset();
6908 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006909 }
6910
6911 mutex_unlock(&fan_mutex);
6912 return (rc)? rc : count;
6913}
6914
6915static struct device_attribute dev_attr_fan_pwm1 =
6916 __ATTR(pwm1, S_IWUSR | S_IRUGO,
6917 fan_pwm1_show, fan_pwm1_store);
6918
6919/* sysfs fan fan1_input ------------------------------------------------ */
6920static ssize_t fan_fan1_input_show(struct device *dev,
6921 struct device_attribute *attr,
6922 char *buf)
6923{
6924 int res;
6925 unsigned int speed;
6926
6927 res = fan_get_speed(&speed);
6928 if (res < 0)
6929 return res;
6930
6931 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
6932}
6933
6934static struct device_attribute dev_attr_fan_fan1_input =
6935 __ATTR(fan1_input, S_IRUGO,
6936 fan_fan1_input_show, NULL);
6937
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03006938/* sysfs fan fan2_input ------------------------------------------------ */
6939static ssize_t fan_fan2_input_show(struct device *dev,
6940 struct device_attribute *attr,
6941 char *buf)
6942{
6943 int res;
6944 unsigned int speed;
6945
6946 res = fan2_get_speed(&speed);
6947 if (res < 0)
6948 return res;
6949
6950 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
6951}
6952
6953static struct device_attribute dev_attr_fan_fan2_input =
6954 __ATTR(fan2_input, S_IRUGO,
6955 fan_fan2_input_show, NULL);
6956
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006957/* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006958static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
6959 char *buf)
6960{
6961 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
6962}
6963
6964static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
6965 const char *buf, size_t count)
6966{
6967 unsigned long t;
6968
6969 if (parse_strtoul(buf, 120, &t))
6970 return -EINVAL;
6971
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03006972 if (!fan_control_allowed)
6973 return -EPERM;
6974
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006975 fan_watchdog_maxinterval = t;
6976 fan_watchdog_reset();
6977
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00006978 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
6979
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006980 return count;
6981}
6982
6983static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
6984 fan_fan_watchdog_show, fan_fan_watchdog_store);
6985
6986/* --------------------------------------------------------------------- */
6987static struct attribute *fan_attributes[] = {
6988 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
6989 &dev_attr_fan_fan1_input.attr,
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03006990 NULL, /* for fan2_input */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006991 NULL
6992};
6993
6994static const struct attribute_group fan_attr_group = {
6995 .attrs = fan_attributes,
6996};
6997
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03006998#define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
6999#define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007000
7001#define TPACPI_FAN_QI(__id1, __id2, __quirks) \
7002 { .vendor = PCI_VENDOR_ID_IBM, \
7003 .bios = TPACPI_MATCH_ANY, \
7004 .ec = TPID(__id1, __id2), \
7005 .quirks = __quirks }
7006
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007007#define TPACPI_FAN_QL(__id1, __id2, __quirks) \
7008 { .vendor = PCI_VENDOR_ID_LENOVO, \
7009 .bios = TPACPI_MATCH_ANY, \
7010 .ec = TPID(__id1, __id2), \
7011 .quirks = __quirks }
7012
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007013static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
7014 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
7015 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
7016 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
7017 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007018 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007019};
7020
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007021#undef TPACPI_FAN_QL
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007022#undef TPACPI_FAN_QI
7023
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007024static int __init fan_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007025{
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007026 int rc;
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007027 unsigned long quirks;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007028
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007029 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7030 "initializing fan subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007031
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03007032 mutex_init(&fan_mutex);
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007033 fan_status_access_mode = TPACPI_FAN_NONE;
7034 fan_control_access_mode = TPACPI_FAN_WR_NONE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007035 fan_control_commands = 0;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007036 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03007037 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007038 tp_features.second_fan = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007039 fan_control_desired_level = 7;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007040
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007041 TPACPI_ACPIHANDLE_INIT(fans);
7042 TPACPI_ACPIHANDLE_INIT(gfan);
7043 TPACPI_ACPIHANDLE_INIT(sfan);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007044
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007045 quirks = tpacpi_check_quirks(fan_quirk_table,
7046 ARRAY_SIZE(fan_quirk_table));
7047
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007048 if (gfan_handle) {
7049 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007050 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007051 } else {
7052 /* all other ThinkPads: note that even old-style
7053 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02007054 if (likely(acpi_ec_read(fan_status_offset,
7055 &fan_control_initial_status))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007056 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007057 if (quirks & TPACPI_FAN_Q1)
7058 fan_quirk1_setup();
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007059 if (quirks & TPACPI_FAN_2FAN) {
7060 tp_features.second_fan = 1;
7061 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7062 "secondary fan support enabled\n");
7063 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007064 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007065 printk(TPACPI_ERR
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007066 "ThinkPad ACPI EC access misbehaving, "
7067 "fan status and control unavailable\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007068 return 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007069 }
7070 }
7071
7072 if (sfan_handle) {
7073 /* 570, 770x-JL */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007074 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02007075 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007076 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007077 } else {
7078 if (!gfan_handle) {
7079 /* gfan without sfan means no fan control */
7080 /* all other models implement TP EC 0x2f control */
7081
7082 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02007083 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007084 fan_control_access_mode =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007085 TPACPI_FAN_WR_ACPI_FANS;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007086 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007087 TPACPI_FAN_CMD_SPEED |
7088 TPACPI_FAN_CMD_LEVEL |
7089 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007090 } else {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007091 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02007092 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007093 TPACPI_FAN_CMD_LEVEL |
7094 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007095 }
7096 }
7097 }
7098
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007099 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7100 "fan is %s, modes %d, %d\n",
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007101 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
7102 fan_control_access_mode != TPACPI_FAN_WR_NONE),
7103 fan_status_access_mode, fan_control_access_mode);
7104
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03007105 /* fan control master switch */
7106 if (!fan_control_allowed) {
7107 fan_control_access_mode = TPACPI_FAN_WR_NONE;
7108 fan_control_commands = 0;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007109 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03007110 "fan control features disabled by parameter\n");
7111 }
7112
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007113 /* update fan_control_desired_level */
7114 if (fan_status_access_mode != TPACPI_FAN_NONE)
7115 fan_get_status_safe(NULL);
7116
7117 if (fan_status_access_mode != TPACPI_FAN_NONE ||
7118 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007119 if (tp_features.second_fan) {
7120 /* attach second fan tachometer */
7121 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
7122 &dev_attr_fan_fan2_input.attr;
7123 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007124 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007125 &fan_attr_group);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007126 if (rc < 0)
7127 return rc;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03007128
7129 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
7130 &driver_attr_fan_watchdog);
7131 if (rc < 0) {
7132 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
7133 &fan_attr_group);
7134 return rc;
7135 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007136 return 0;
7137 } else
7138 return 1;
7139}
7140
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007141static void fan_exit(void)
7142{
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007143 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007144 "cancelling any pending fan watchdog tasks\n");
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007145
7146 /* FIXME: can we really do this unconditionally? */
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007147 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007148 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
7149 &driver_attr_fan_watchdog);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007150
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007151 cancel_delayed_work(&fan_watchdog_task);
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03007152 flush_workqueue(tpacpi_wq);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007153}
7154
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007155static void fan_suspend(pm_message_t state)
7156{
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007157 int rc;
7158
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007159 if (!fan_control_allowed)
7160 return;
7161
7162 /* Store fan status in cache */
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007163 fan_control_resume_level = 0;
7164 rc = fan_get_status_safe(&fan_control_resume_level);
7165 if (rc < 0)
7166 printk(TPACPI_NOTICE
7167 "failed to read fan level for later "
7168 "restore during resume: %d\n", rc);
7169
7170 /* if it is undefined, don't attempt to restore it.
7171 * KEEP THIS LAST */
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007172 if (tp_features.fan_ctrl_status_undef)
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007173 fan_control_resume_level = 0;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007174}
7175
7176static void fan_resume(void)
7177{
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007178 u8 current_level = 7;
7179 bool do_set = false;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007180 int rc;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007181
7182 /* DSDT *always* updates status on resume */
7183 tp_features.fan_ctrl_status_undef = 0;
7184
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007185 if (!fan_control_allowed ||
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007186 !fan_control_resume_level ||
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007187 (fan_get_status_safe(&current_level) < 0))
7188 return;
7189
7190 switch (fan_control_access_mode) {
7191 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007192 /* never decrease fan level */
7193 do_set = (fan_control_resume_level > current_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007194 break;
7195 case TPACPI_FAN_WR_ACPI_FANS:
7196 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007197 /* never decrease fan level, scale is:
7198 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
7199 *
7200 * We expect the firmware to set either 7 or AUTO, but we
7201 * handle FULLSPEED out of paranoia.
7202 *
7203 * So, we can safely only restore FULLSPEED or 7, anything
7204 * else could slow the fan. Restoring AUTO is useless, at
7205 * best that's exactly what the DSDT already set (it is the
7206 * slower it uses).
7207 *
7208 * Always keep in mind that the DSDT *will* have set the
7209 * fans to what the vendor supposes is the best level. We
7210 * muck with it only to speed the fan up.
7211 */
7212 if (fan_control_resume_level != 7 &&
7213 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
7214 return;
7215 else
7216 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
7217 (current_level != fan_control_resume_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007218 break;
7219 default:
7220 return;
7221 }
7222 if (do_set) {
7223 printk(TPACPI_NOTICE
7224 "restoring fan level to 0x%02x\n",
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007225 fan_control_resume_level);
7226 rc = fan_set_level_safe(fan_control_resume_level);
7227 if (rc < 0)
7228 printk(TPACPI_NOTICE
7229 "failed to restore fan level: %d\n", rc);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007230 }
7231}
7232
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007233static int fan_read(char *p)
7234{
7235 int len = 0;
7236 int rc;
7237 u8 status;
7238 unsigned int speed = 0;
7239
7240 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007241 case TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007242 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007243 rc = fan_get_status_safe(&status);
7244 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007245 return rc;
7246
7247 len += sprintf(p + len, "status:\t\t%s\n"
7248 "level:\t\t%d\n",
7249 (status != 0) ? "enabled" : "disabled", status);
7250 break;
7251
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007252 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007253 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007254 rc = fan_get_status_safe(&status);
7255 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007256 return rc;
7257
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007258 len += sprintf(p + len, "status:\t\t%s\n",
7259 (status != 0) ? "enabled" : "disabled");
7260
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007261 rc = fan_get_speed(&speed);
7262 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007263 return rc;
7264
7265 len += sprintf(p + len, "speed:\t\t%d\n", speed);
7266
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007267 if (status & TP_EC_FAN_FULLSPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007268 /* Disengaged mode takes precedence */
7269 len += sprintf(p + len, "level:\t\tdisengaged\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007270 else if (status & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007271 len += sprintf(p + len, "level:\t\tauto\n");
7272 else
7273 len += sprintf(p + len, "level:\t\t%d\n", status);
7274 break;
7275
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007276 case TPACPI_FAN_NONE:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007277 default:
7278 len += sprintf(p + len, "status:\t\tnot supported\n");
7279 }
7280
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007281 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007282 len += sprintf(p + len, "commands:\tlevel <level>");
7283
7284 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007285 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007286 len += sprintf(p + len, " (<level> is 0-7)\n");
7287 break;
7288
7289 default:
7290 len += sprintf(p + len, " (<level> is 0-7, "
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007291 "auto, disengaged, full-speed)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007292 break;
7293 }
7294 }
7295
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007296 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007297 len += sprintf(p + len, "commands:\tenable, disable\n"
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007298 "commands:\twatchdog <timeout> (<timeout> "
7299 "is 0 (off), 1-120 (seconds))\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007300
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007301 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007302 len += sprintf(p + len, "commands:\tspeed <speed>"
7303 " (<speed> is 0-65535)\n");
7304
7305 return len;
7306}
7307
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007308static int fan_write_cmd_level(const char *cmd, int *rc)
7309{
7310 int level;
7311
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02007312 if (strlencmp(cmd, "level auto") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007313 level = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007314 else if ((strlencmp(cmd, "level disengaged") == 0) |
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007315 (strlencmp(cmd, "level full-speed") == 0))
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007316 level = TP_EC_FAN_FULLSPEED;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02007317 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007318 return 0;
7319
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007320 *rc = fan_set_level_safe(level);
7321 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007322 printk(TPACPI_ERR "level command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007323 "access mode %d", fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007324 else if (!*rc)
7325 tpacpi_disclose_usertask("procfs fan",
7326 "set level to %d\n", level);
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007327
7328 return 1;
7329}
7330
7331static int fan_write_cmd_enable(const char *cmd, int *rc)
7332{
7333 if (strlencmp(cmd, "enable") != 0)
7334 return 0;
7335
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007336 *rc = fan_set_enable();
7337 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007338 printk(TPACPI_ERR "enable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007339 "access mode %d", fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007340 else if (!*rc)
7341 tpacpi_disclose_usertask("procfs fan", "enable\n");
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007342
7343 return 1;
7344}
7345
7346static int fan_write_cmd_disable(const char *cmd, int *rc)
7347{
7348 if (strlencmp(cmd, "disable") != 0)
7349 return 0;
7350
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007351 *rc = fan_set_disable();
7352 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007353 printk(TPACPI_ERR "disable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007354 "access mode %d", fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007355 else if (!*rc)
7356 tpacpi_disclose_usertask("procfs fan", "disable\n");
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007357
7358 return 1;
7359}
7360
7361static int fan_write_cmd_speed(const char *cmd, int *rc)
7362{
7363 int speed;
7364
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02007365 /* TODO:
7366 * Support speed <low> <medium> <high> ? */
7367
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007368 if (sscanf(cmd, "speed %d", &speed) != 1)
7369 return 0;
7370
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007371 *rc = fan_set_speed(speed);
7372 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007373 printk(TPACPI_ERR "speed command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007374 "access mode %d", fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007375 else if (!*rc)
7376 tpacpi_disclose_usertask("procfs fan",
7377 "set speed to %d\n", speed);
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007378
7379 return 1;
7380}
7381
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007382static int fan_write_cmd_watchdog(const char *cmd, int *rc)
7383{
7384 int interval;
7385
7386 if (sscanf(cmd, "watchdog %d", &interval) != 1)
7387 return 0;
7388
7389 if (interval < 0 || interval > 120)
7390 *rc = -EINVAL;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007391 else {
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007392 fan_watchdog_maxinterval = interval;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007393 tpacpi_disclose_usertask("procfs fan",
7394 "set watchdog timer to %d\n",
7395 interval);
7396 }
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007397
7398 return 1;
7399}
7400
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007401static int fan_write(char *buf)
7402{
7403 char *cmd;
7404 int rc = 0;
7405
7406 while (!rc && (cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007407 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007408 fan_write_cmd_level(cmd, &rc)) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007409 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007410 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007411 fan_write_cmd_disable(cmd, &rc) ||
7412 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007413 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007414 fan_write_cmd_speed(cmd, &rc))
7415 )
7416 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007417 else if (!rc)
7418 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007419 }
7420
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007421 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007422}
7423
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007424static struct ibm_struct fan_driver_data = {
7425 .name = "fan",
7426 .read = fan_read,
7427 .write = fan_write,
7428 .exit = fan_exit,
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007429 .suspend = fan_suspend,
7430 .resume = fan_resume,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007431};
7432
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007433/****************************************************************************
7434 ****************************************************************************
7435 *
7436 * Infrastructure
7437 *
7438 ****************************************************************************
7439 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07007440
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007441/* sysfs name ---------------------------------------------------------- */
7442static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
7443 struct device_attribute *attr,
7444 char *buf)
7445{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007446 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007447}
7448
7449static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
7450 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
7451
7452/* --------------------------------------------------------------------- */
7453
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007454/* /proc support */
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -03007455static struct proc_dir_entry *proc_dir;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007456
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007457/*
7458 * Module and infrastructure proble, init and exit handling
7459 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007460
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007461static int force_load;
7462
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007463#ifdef CONFIG_THINKPAD_ACPI_DEBUG
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007464static const char * __init str_supported(int is_supported)
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007465{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007466 static char text_unsupported[] __initdata = "not supported";
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007467
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007468 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007469}
7470#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
7471
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007472static void ibm_exit(struct ibm_struct *ibm)
7473{
7474 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
7475
7476 list_del_init(&ibm->all_drivers);
7477
7478 if (ibm->flags.acpi_notify_installed) {
7479 dbg_printk(TPACPI_DBG_EXIT,
7480 "%s: acpi_remove_notify_handler\n", ibm->name);
7481 BUG_ON(!ibm->acpi);
7482 acpi_remove_notify_handler(*ibm->acpi->handle,
7483 ibm->acpi->type,
7484 dispatch_acpi_notify);
7485 ibm->flags.acpi_notify_installed = 0;
7486 ibm->flags.acpi_notify_installed = 0;
7487 }
7488
7489 if (ibm->flags.proc_created) {
7490 dbg_printk(TPACPI_DBG_EXIT,
7491 "%s: remove_proc_entry\n", ibm->name);
7492 remove_proc_entry(ibm->name, proc_dir);
7493 ibm->flags.proc_created = 0;
7494 }
7495
7496 if (ibm->flags.acpi_driver_registered) {
7497 dbg_printk(TPACPI_DBG_EXIT,
7498 "%s: acpi_bus_unregister_driver\n", ibm->name);
7499 BUG_ON(!ibm->acpi);
7500 acpi_bus_unregister_driver(ibm->acpi->driver);
7501 kfree(ibm->acpi->driver);
7502 ibm->acpi->driver = NULL;
7503 ibm->flags.acpi_driver_registered = 0;
7504 }
7505
7506 if (ibm->flags.init_called && ibm->exit) {
7507 ibm->exit();
7508 ibm->flags.init_called = 0;
7509 }
7510
7511 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
7512}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007513
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007514static int __init ibm_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007515{
7516 int ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007517 struct ibm_struct *ibm = iibm->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007518 struct proc_dir_entry *entry;
7519
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007520 BUG_ON(ibm == NULL);
7521
7522 INIT_LIST_HEAD(&ibm->all_drivers);
7523
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03007524 if (ibm->flags.experimental && !experimental)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007525 return 0;
7526
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007527 dbg_printk(TPACPI_DBG_INIT,
7528 "probing for %s\n", ibm->name);
7529
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007530 if (iibm->init) {
7531 ret = iibm->init(iibm);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007532 if (ret > 0)
7533 return 0; /* probe failed */
7534 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007535 return ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007536
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03007537 ibm->flags.init_called = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007538 }
7539
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03007540 if (ibm->acpi) {
7541 if (ibm->acpi->hid) {
7542 ret = register_tpacpi_subdriver(ibm);
7543 if (ret)
7544 goto err_out;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007545 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03007546
7547 if (ibm->acpi->notify) {
7548 ret = setup_acpi_notify(ibm);
7549 if (ret == -ENODEV) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007550 printk(TPACPI_NOTICE "disabling subdriver %s\n",
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03007551 ibm->name);
7552 ret = 0;
7553 goto err_out;
7554 }
7555 if (ret < 0)
7556 goto err_out;
7557 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007558 }
7559
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007560 dbg_printk(TPACPI_DBG_INIT,
7561 "%s installed\n", ibm->name);
7562
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007563 if (ibm->read) {
7564 entry = create_proc_entry(ibm->name,
7565 S_IFREG | S_IRUGO | S_IWUSR,
7566 proc_dir);
7567 if (!entry) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007568 printk(TPACPI_ERR "unable to create proc entry %s\n",
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007569 ibm->name);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007570 ret = -ENODEV;
7571 goto err_out;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007572 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007573 entry->data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03007574 entry->read_proc = &dispatch_procfs_read;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007575 if (ibm->write)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03007576 entry->write_proc = &dispatch_procfs_write;
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03007577 ibm->flags.proc_created = 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007578 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007579
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007580 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
7581
Linus Torvalds1da177e2005-04-16 15:20:36 -07007582 return 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007583
7584err_out:
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007585 dbg_printk(TPACPI_DBG_INIT,
7586 "%s: at error exit path with result %d\n",
7587 ibm->name, ret);
7588
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007589 ibm_exit(ibm);
7590 return (ret < 0)? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007591}
7592
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007593/* Probing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007594
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03007595static bool __pure __init tpacpi_is_fw_digit(const char c)
7596{
7597 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
7598}
7599
7600/* Most models: xxyTkkWW (#.##c); Ancient 570/600 and -SL lacks (#.##c) */
7601static bool __pure __init tpacpi_is_valid_fw_id(const char* const s,
7602 const char t)
7603{
7604 return s && strlen(s) >= 8 &&
7605 tpacpi_is_fw_digit(s[0]) &&
7606 tpacpi_is_fw_digit(s[1]) &&
7607 s[2] == t && s[3] == 'T' &&
7608 tpacpi_is_fw_digit(s[4]) &&
7609 tpacpi_is_fw_digit(s[5]) &&
7610 s[6] == 'W' && s[7] == 'W';
7611}
7612
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007613/* returns 0 - probe ok, or < 0 - probe error.
7614 * Probe ok doesn't mean thinkpad found.
7615 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
7616static int __must_check __init get_thinkpad_model_data(
7617 struct thinkpad_id_data *tp)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02007618{
Jeff Garzik18552562007-10-03 15:15:40 -04007619 const struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02007620 char ec_fw_string[18];
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007621 char const *s;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02007622
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007623 if (!tp)
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007624 return -EINVAL;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007625
7626 memset(tp, 0, sizeof(*tp));
7627
7628 if (dmi_name_in_vendors("IBM"))
7629 tp->vendor = PCI_VENDOR_ID_IBM;
7630 else if (dmi_name_in_vendors("LENOVO"))
7631 tp->vendor = PCI_VENDOR_ID_LENOVO;
7632 else
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007633 return 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007634
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007635 s = dmi_get_system_info(DMI_BIOS_VERSION);
7636 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
7637 if (s && !tp->bios_version_str)
7638 return -ENOMEM;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03007639
7640 /* Really ancient ThinkPad 240X will fail this, which is fine */
7641 if (!tpacpi_is_valid_fw_id(tp->bios_version_str, 'E'))
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007642 return 0;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03007643
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007644 tp->bios_model = tp->bios_version_str[0]
7645 | (tp->bios_version_str[1] << 8);
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03007646 tp->bios_release = (tp->bios_version_str[4] << 8)
7647 | tp->bios_version_str[5];
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007648
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02007649 /*
7650 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
7651 * X32 or newer, all Z series; Some models must have an
7652 * up-to-date BIOS or they will not be detected.
7653 *
7654 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
7655 */
7656 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02007657 if (sscanf(dev->name,
7658 "IBM ThinkPad Embedded Controller -[%17c",
7659 ec_fw_string) == 1) {
7660 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
7661 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007662
7663 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007664 if (!tp->ec_version_str)
7665 return -ENOMEM;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03007666
7667 if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) {
7668 tp->ec_model = ec_fw_string[0]
7669 | (ec_fw_string[1] << 8);
7670 tp->ec_release = (ec_fw_string[4] << 8)
7671 | ec_fw_string[5];
7672 } else {
7673 printk(TPACPI_NOTICE
7674 "ThinkPad firmware release %s "
7675 "doesn't match the known patterns\n",
7676 ec_fw_string);
7677 printk(TPACPI_NOTICE
7678 "please report this to %s\n",
7679 TPACPI_MAIL);
7680 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007681 break;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02007682 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02007683 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007684
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007685 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
7686 if (s && !strnicmp(s, "ThinkPad", 8)) {
7687 tp->model_str = kstrdup(s, GFP_KERNEL);
7688 if (!tp->model_str)
7689 return -ENOMEM;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007690 }
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03007691
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007692 s = dmi_get_system_info(DMI_PRODUCT_NAME);
7693 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
7694 if (s && !tp->nummodel_str)
7695 return -ENOMEM;
7696
7697 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007698}
7699
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007700static int __init probe_for_thinkpad(void)
7701{
7702 int is_thinkpad;
7703
7704 if (acpi_disabled)
7705 return -ENODEV;
7706
7707 /*
7708 * Non-ancient models have better DMI tagging, but very old models
7709 * don't.
7710 */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007711 is_thinkpad = (thinkpad_id.model_str != NULL);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007712
7713 /* ec is required because many other handles are relative to it */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007714 TPACPI_ACPIHANDLE_INIT(ec);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007715 if (!ec_handle) {
7716 if (is_thinkpad)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007717 printk(TPACPI_ERR
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007718 "Not yet supported ThinkPad detected!\n");
7719 return -ENODEV;
7720 }
7721
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03007722 /*
7723 * Risks a regression on very old machines, but reduces potential
7724 * false positives a damn great deal
7725 */
7726 if (!is_thinkpad)
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007727 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03007728
7729 if (!is_thinkpad && !force_load)
7730 return -ENODEV;
7731
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007732 return 0;
7733}
7734
7735
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007736/* Module init, exit, parameters */
7737
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007738static struct ibm_init_struct ibms_init[] __initdata = {
7739 {
7740 .init = thinkpad_acpi_driver_init,
7741 .data = &thinkpad_acpi_driver_data,
7742 },
7743 {
7744 .init = hotkey_init,
7745 .data = &hotkey_driver_data,
7746 },
7747 {
7748 .init = bluetooth_init,
7749 .data = &bluetooth_driver_data,
7750 },
7751 {
7752 .init = wan_init,
7753 .data = &wan_driver_data,
7754 },
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02007755 {
7756 .init = uwb_init,
7757 .data = &uwb_driver_data,
7758 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02007759#ifdef CONFIG_THINKPAD_ACPI_VIDEO
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007760 {
7761 .init = video_init,
7762 .data = &video_driver_data,
7763 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02007764#endif
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007765 {
7766 .init = light_init,
7767 .data = &light_driver_data,
7768 },
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007769 {
7770 .init = cmos_init,
7771 .data = &cmos_driver_data,
7772 },
7773 {
7774 .init = led_init,
7775 .data = &led_driver_data,
7776 },
7777 {
7778 .init = beep_init,
7779 .data = &beep_driver_data,
7780 },
7781 {
7782 .init = thermal_init,
7783 .data = &thermal_driver_data,
7784 },
7785 {
7786 .data = &ecdump_driver_data,
7787 },
7788 {
7789 .init = brightness_init,
7790 .data = &brightness_driver_data,
7791 },
7792 {
7793 .data = &volume_driver_data,
7794 },
7795 {
7796 .init = fan_init,
7797 .data = &fan_driver_data,
7798 },
7799};
7800
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007801static int __init set_ibm_param(const char *val, struct kernel_param *kp)
7802{
7803 unsigned int i;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007804 struct ibm_struct *ibm;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007805
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02007806 if (!kp || !kp->name || !val)
7807 return -EINVAL;
7808
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007809 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
7810 ibm = ibms_init[i].data;
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02007811 WARN_ON(ibm == NULL);
7812
7813 if (!ibm || !ibm->name)
7814 continue;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007815
7816 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
7817 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007818 return -ENOSPC;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007819 strcpy(ibms_init[i].param, val);
7820 strcat(ibms_init[i].param, ",");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007821 return 0;
7822 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007823 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007824
7825 return -EINVAL;
7826}
7827
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007828module_param(experimental, int, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007829MODULE_PARM_DESC(experimental,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007830 "Enables experimental features when non-zero");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007831
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03007832module_param_named(debug, dbg_level, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007833MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03007834
Henrique de Moraes Holschuh86cc9442007-07-18 23:45:41 -03007835module_param(force_load, bool, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007836MODULE_PARM_DESC(force_load,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007837 "Attempts to load the driver even on a "
7838 "mis-identified ThinkPad when true");
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03007839
Henrique de Moraes Holschuh86cc9442007-07-18 23:45:41 -03007840module_param_named(fan_control, fan_control_allowed, bool, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007841MODULE_PARM_DESC(fan_control,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007842 "Enables setting fan parameters features when true");
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03007843
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00007844module_param_named(brightness_mode, brightness_mode, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007845MODULE_PARM_DESC(brightness_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007846 "Selects brightness control strategy: "
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00007847 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03007848
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02007849module_param(brightness_enable, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007850MODULE_PARM_DESC(brightness_enable,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007851 "Enables backlight control when 1, disables when 0");
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02007852
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03007853module_param(hotkey_report_mode, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007854MODULE_PARM_DESC(hotkey_report_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007855 "used for backwards compatibility with userspace, "
7856 "see documentation");
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03007857
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007858#define TPACPI_PARAM(feature) \
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007859 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
Henrique de Moraes Holschuhcbb14842008-02-16 02:17:50 -02007860 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007861 "at module load, see documentation")
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007862
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007863TPACPI_PARAM(hotkey);
7864TPACPI_PARAM(bluetooth);
7865TPACPI_PARAM(video);
7866TPACPI_PARAM(light);
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007867TPACPI_PARAM(cmos);
7868TPACPI_PARAM(led);
7869TPACPI_PARAM(beep);
7870TPACPI_PARAM(ecdump);
7871TPACPI_PARAM(brightness);
7872TPACPI_PARAM(volume);
7873TPACPI_PARAM(fan);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007874
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02007875#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
7876module_param(dbg_wlswemul, uint, 0);
7877MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
7878module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
7879MODULE_PARM_DESC(wlsw_state,
7880 "Initial state of the emulated WLSW switch");
7881
7882module_param(dbg_bluetoothemul, uint, 0);
7883MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
7884module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
7885MODULE_PARM_DESC(bluetooth_state,
7886 "Initial state of the emulated bluetooth switch");
7887
7888module_param(dbg_wwanemul, uint, 0);
7889MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
7890module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
7891MODULE_PARM_DESC(wwan_state,
7892 "Initial state of the emulated WWAN switch");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02007893
7894module_param(dbg_uwbemul, uint, 0);
7895MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
7896module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
7897MODULE_PARM_DESC(uwb_state,
7898 "Initial state of the emulated UWB switch");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02007899#endif
7900
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007901static void thinkpad_acpi_module_exit(void)
7902{
7903 struct ibm_struct *ibm, *itmp;
7904
7905 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
7906
7907 list_for_each_entry_safe_reverse(ibm, itmp,
7908 &tpacpi_all_drivers,
7909 all_drivers) {
7910 ibm_exit(ibm);
7911 }
7912
7913 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
7914
7915 if (tpacpi_inputdev) {
7916 if (tp_features.input_device_registered)
7917 input_unregister_device(tpacpi_inputdev);
7918 else
7919 input_free_device(tpacpi_inputdev);
7920 }
7921
7922 if (tpacpi_hwmon)
7923 hwmon_device_unregister(tpacpi_hwmon);
7924
7925 if (tp_features.sensors_pdev_attrs_registered)
7926 device_remove_file(&tpacpi_sensors_pdev->dev,
7927 &dev_attr_thinkpad_acpi_pdev_name);
7928 if (tpacpi_sensors_pdev)
7929 platform_device_unregister(tpacpi_sensors_pdev);
7930 if (tpacpi_pdev)
7931 platform_device_unregister(tpacpi_pdev);
7932
7933 if (tp_features.sensors_pdrv_attrs_registered)
7934 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
7935 if (tp_features.platform_drv_attrs_registered)
7936 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
7937
7938 if (tp_features.sensors_pdrv_registered)
7939 platform_driver_unregister(&tpacpi_hwmon_pdriver);
7940
7941 if (tp_features.platform_drv_registered)
7942 platform_driver_unregister(&tpacpi_pdriver);
7943
7944 if (proc_dir)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007945 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007946
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03007947 if (tpacpi_wq)
7948 destroy_workqueue(tpacpi_wq);
7949
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007950 kfree(thinkpad_id.bios_version_str);
7951 kfree(thinkpad_id.ec_version_str);
7952 kfree(thinkpad_id.model_str);
7953}
7954
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007955
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03007956static int __init thinkpad_acpi_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007957{
7958 int ret, i;
7959
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03007960 tpacpi_lifecycle = TPACPI_LIFE_INIT;
7961
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03007962 /* Parameter checking */
7963 if (hotkey_report_mode > 2)
7964 return -EINVAL;
7965
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007966 /* Driver-level probe */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007967
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007968 ret = get_thinkpad_model_data(&thinkpad_id);
7969 if (ret) {
7970 printk(TPACPI_ERR
7971 "unable to get DMI data: %d\n", ret);
7972 thinkpad_acpi_module_exit();
7973 return ret;
7974 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007975 ret = probe_for_thinkpad();
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007976 if (ret) {
7977 thinkpad_acpi_module_exit();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007978 return ret;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007979 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007980
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007981 /* Driver initialization */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007982
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007983 TPACPI_ACPIHANDLE_INIT(ecrd);
7984 TPACPI_ACPIHANDLE_INIT(ecwr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007985
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03007986 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
7987 if (!tpacpi_wq) {
7988 thinkpad_acpi_module_exit();
7989 return -ENOMEM;
7990 }
7991
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007992 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007993 if (!proc_dir) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007994 printk(TPACPI_ERR
7995 "unable to create proc dir " TPACPI_PROC_DIR);
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03007996 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007997 return -ENODEV;
7998 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007999
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008000 ret = platform_driver_register(&tpacpi_pdriver);
8001 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008002 printk(TPACPI_ERR
8003 "unable to register main platform driver\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008004 thinkpad_acpi_module_exit();
8005 return ret;
8006 }
Henrique de Moraes Holschuhac363932007-07-27 17:04:40 -03008007 tp_features.platform_drv_registered = 1;
8008
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008009 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
8010 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008011 printk(TPACPI_ERR
8012 "unable to register hwmon platform driver\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008013 thinkpad_acpi_module_exit();
8014 return ret;
8015 }
8016 tp_features.sensors_pdrv_registered = 1;
8017
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03008018 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03008019 if (!ret) {
8020 tp_features.platform_drv_attrs_registered = 1;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008021 ret = tpacpi_create_driver_attributes(
8022 &tpacpi_hwmon_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03008023 }
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03008024 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008025 printk(TPACPI_ERR
8026 "unable to create sysfs driver attributes\n");
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03008027 thinkpad_acpi_module_exit();
8028 return ret;
8029 }
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03008030 tp_features.sensors_pdrv_attrs_registered = 1;
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03008031
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008032
8033 /* Device initialization */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008034 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008035 NULL, 0);
8036 if (IS_ERR(tpacpi_pdev)) {
8037 ret = PTR_ERR(tpacpi_pdev);
8038 tpacpi_pdev = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008039 printk(TPACPI_ERR "unable to register platform device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008040 thinkpad_acpi_module_exit();
8041 return ret;
8042 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008043 tpacpi_sensors_pdev = platform_device_register_simple(
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008044 TPACPI_HWMON_DRVR_NAME,
8045 -1, NULL, 0);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008046 if (IS_ERR(tpacpi_sensors_pdev)) {
8047 ret = PTR_ERR(tpacpi_sensors_pdev);
8048 tpacpi_sensors_pdev = NULL;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008049 printk(TPACPI_ERR
8050 "unable to register hwmon platform device\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008051 thinkpad_acpi_module_exit();
8052 return ret;
8053 }
8054 ret = device_create_file(&tpacpi_sensors_pdev->dev,
8055 &dev_attr_thinkpad_acpi_pdev_name);
8056 if (ret) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008057 printk(TPACPI_ERR
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008058 "unable to create sysfs hwmon device attributes\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008059 thinkpad_acpi_module_exit();
8060 return ret;
8061 }
8062 tp_features.sensors_pdev_attrs_registered = 1;
8063 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008064 if (IS_ERR(tpacpi_hwmon)) {
8065 ret = PTR_ERR(tpacpi_hwmon);
8066 tpacpi_hwmon = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008067 printk(TPACPI_ERR "unable to register hwmon device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008068 thinkpad_acpi_module_exit();
8069 return ret;
8070 }
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -03008071 mutex_init(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03008072 tpacpi_inputdev = input_allocate_device();
8073 if (!tpacpi_inputdev) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008074 printk(TPACPI_ERR "unable to allocate input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03008075 thinkpad_acpi_module_exit();
8076 return -ENOMEM;
8077 } else {
8078 /* Prepare input device, but don't register */
8079 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008080 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03008081 tpacpi_inputdev->id.bustype = BUS_HOST;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03008082 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
8083 thinkpad_id.vendor :
8084 PCI_VENDOR_ID_IBM;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03008085 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
8086 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
8087 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008088 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
8089 ret = ibm_init(&ibms_init[i]);
8090 if (ret >= 0 && *ibms_init[i].param)
8091 ret = ibms_init[i].data->write(ibms_init[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008092 if (ret < 0) {
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03008093 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008094 return ret;
8095 }
8096 }
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03008097 ret = input_register_device(tpacpi_inputdev);
8098 if (ret < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008099 printk(TPACPI_ERR "unable to register input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03008100 thinkpad_acpi_module_exit();
8101 return ret;
8102 } else {
8103 tp_features.input_device_registered = 1;
8104 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008105
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03008106 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008107 return 0;
8108}
8109
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03008110MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
8111
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008112/*
Henrique de Moraes Holschuh922fe092009-04-14 02:44:13 +00008113 * This will autoload the driver in almost every ThinkPad
8114 * in widespread use.
8115 *
8116 * Only _VERY_ old models, like the 240, 240x and 570 lack
8117 * the HKEY event interface.
8118 */
8119MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
8120
8121/*
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008122 * DMI matching for module autoloading
8123 *
8124 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
8125 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
8126 *
8127 * Only models listed in thinkwiki will be supported, so add yours
8128 * if it is not there yet.
8129 */
8130#define IBM_BIOS_MODULE_ALIAS(__type) \
Mathieu Chouquet-Stringerb36a50f2009-03-14 16:35:26 +01008131 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008132
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008133/* Ancient thinkpad BIOSes have to be identified by
8134 * BIOS type or model number, and there are far less
8135 * BIOS types than model numbers... */
Henrique de Moraes Holschuh922fe092009-04-14 02:44:13 +00008136IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008137
Henrique de Moraes Holschuhf68f53a2009-04-14 02:44:12 +00008138MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
8139MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008140MODULE_DESCRIPTION(TPACPI_DESC);
8141MODULE_VERSION(TPACPI_VERSION);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008142MODULE_LICENSE("GPL");
8143
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03008144module_init(thinkpad_acpi_module_init);
8145module_exit(thinkpad_acpi_module_exit);