blob: d2a0ef83fbb580380ea8279b709513dd3c028a70 [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 Holschuh2586d562009-04-04 04:25:48 +000025#define TPACPI_SYSFS_VERSION 0x020300
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 Holschuh0e74dc22008-07-21 09:15:51 -0300169/* rfkill switches */
170enum {
171 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
172 TPACPI_RFK_WWAN_SW_ID,
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -0200173 TPACPI_RFK_UWB_SW_ID,
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -0300174};
175
Henrique de Moraes Holschuh7ff8d622009-04-04 04:25:46 +0000176/* printk headers */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200177#define TPACPI_LOG TPACPI_FILE ": "
Henrique de Moraes Holschuh7ff8d622009-04-04 04:25:46 +0000178#define TPACPI_EMERG KERN_EMERG TPACPI_LOG
179#define TPACPI_ALERT KERN_ALERT TPACPI_LOG
180#define TPACPI_CRIT KERN_CRIT TPACPI_LOG
181#define TPACPI_ERR KERN_ERR TPACPI_LOG
182#define TPACPI_WARN KERN_WARNING TPACPI_LOG
183#define TPACPI_NOTICE KERN_NOTICE TPACPI_LOG
184#define TPACPI_INFO KERN_INFO TPACPI_LOG
185#define TPACPI_DEBUG KERN_DEBUG TPACPI_LOG
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200186
Henrique de Moraes Holschuh7ff8d622009-04-04 04:25:46 +0000187/* Debugging printk groups */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200188#define TPACPI_DBG_ALL 0xffff
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +0000189#define TPACPI_DBG_DISCLOSETASK 0x8000
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200190#define TPACPI_DBG_INIT 0x0001
191#define TPACPI_DBG_EXIT 0x0002
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +0000192#define TPACPI_DBG_RFKILL 0x0004
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +0000193#define TPACPI_DBG_HKEY 0x0008
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +0000194#define TPACPI_DBG_FAN 0x0010
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +0000195#define TPACPI_DBG_BRGHT 0x0020
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200196
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200197#define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
198#define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
199#define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200200
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200201
202/****************************************************************************
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200203 * Driver-wide structs and misc. variables
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200204 */
205
206struct ibm_struct;
207
208struct tp_acpi_drv_struct {
209 const struct acpi_device_id *hid;
210 struct acpi_driver *driver;
211
212 void (*notify) (struct ibm_struct *, u32);
213 acpi_handle *handle;
214 u32 type;
215 struct acpi_device *device;
216};
217
218struct ibm_struct {
219 char *name;
220
221 int (*read) (char *);
222 int (*write) (char *);
223 void (*exit) (void);
224 void (*resume) (void);
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200225 void (*suspend) (pm_message_t state);
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200226 void (*shutdown) (void);
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200227
228 struct list_head all_drivers;
229
230 struct tp_acpi_drv_struct *acpi;
231
232 struct {
233 u8 acpi_driver_registered:1;
234 u8 acpi_notify_installed:1;
235 u8 proc_created:1;
236 u8 init_called:1;
237 u8 experimental:1;
238 } flags;
239};
240
241struct ibm_init_struct {
242 char param[32];
243
244 int (*init) (struct ibm_init_struct *);
245 struct ibm_struct *data;
246};
247
248static struct {
249#ifdef CONFIG_THINKPAD_ACPI_BAY
250 u32 bay_status:1;
251 u32 bay_eject:1;
252 u32 bay_status2:1;
253 u32 bay_eject2:1;
254#endif
255 u32 bluetooth:1;
256 u32 hotkey:1;
257 u32 hotkey_mask:1;
258 u32 hotkey_wlsw:1;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -0200259 u32 hotkey_tablet:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200260 u32 light:1;
261 u32 light_status:1;
262 u32 bright_16levels:1;
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -0300263 u32 bright_acpimode:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200264 u32 wan:1;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -0200265 u32 uwb:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200266 u32 fan_ctrl_status_undef:1;
267 u32 input_device_registered:1;
268 u32 platform_drv_registered:1;
269 u32 platform_drv_attrs_registered:1;
270 u32 sensors_pdrv_registered:1;
271 u32 sensors_pdrv_attrs_registered:1;
272 u32 sensors_pdev_attrs_registered:1;
273 u32 hotkey_poll_active:1;
274} tp_features;
275
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -0300276static struct {
277 u16 hotkey_mask_ff:1;
278} tp_warned;
279
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200280struct thinkpad_id_data {
281 unsigned int vendor; /* ThinkPad vendor:
282 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
283
284 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
285 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
286
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -0300287 u16 bios_model; /* 1Y = 0x5931, 0 = unknown */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200288 u16 ec_model;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -0300289 u16 bios_release; /* 1ZETK1WW = 0x314b, 0 = unknown */
290 u16 ec_release;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200291
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -0300292 char *model_str; /* ThinkPad T43 */
293 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200294};
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200295static struct thinkpad_id_data thinkpad_id;
296
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300297static enum {
298 TPACPI_LIFE_INIT = 0,
299 TPACPI_LIFE_RUNNING,
300 TPACPI_LIFE_EXITING,
301} tpacpi_lifecycle;
302
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200303static int experimental;
304static u32 dbg_level;
305
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -0300306static struct workqueue_struct *tpacpi_wq;
307
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +0000308enum led_status_t {
309 TPACPI_LED_OFF = 0,
310 TPACPI_LED_ON,
311 TPACPI_LED_BLINK,
312};
313
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -0300314/* Special LED class that can defer work */
315struct tpacpi_led_classdev {
316 struct led_classdev led_classdev;
317 struct work_struct work;
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +0000318 enum led_status_t new_state;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -0300319 unsigned int led;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -0300320};
321
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200322#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
323static int dbg_wlswemul;
324static int tpacpi_wlsw_emulstate;
325static int dbg_bluetoothemul;
326static int tpacpi_bluetooth_emulstate;
327static int dbg_wwanemul;
328static int tpacpi_wwan_emulstate;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -0200329static int dbg_uwbemul;
330static int tpacpi_uwb_emulstate;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200331#endif
332
333
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000334/*************************************************************************
335 * Debugging helpers
336 */
337
338#define dbg_printk(a_dbg_level, format, arg...) \
339 do { if (dbg_level & (a_dbg_level)) \
340 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
341 } while (0)
342
343#ifdef CONFIG_THINKPAD_ACPI_DEBUG
344#define vdbg_printk dbg_printk
345static const char *str_supported(int is_supported);
346#else
347#define vdbg_printk(a_dbg_level, format, arg...) \
348 do { } while (0)
349#endif
350
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +0000351static void tpacpi_log_usertask(const char * const what)
352{
353 printk(TPACPI_DEBUG "%s: access by process with PID %d\n",
354 what, task_tgid_vnr(current));
355}
356
357#define tpacpi_disclose_usertask(what, format, arg...) \
358 do { \
359 if (unlikely( \
360 (dbg_level & TPACPI_DBG_DISCLOSETASK) && \
361 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
362 printk(TPACPI_DEBUG "%s: PID %d: " format, \
363 what, task_tgid_vnr(current), ## arg); \
364 } \
365 } while (0)
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000366
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300367/****************************************************************************
368 ****************************************************************************
369 *
370 * ACPI Helpers and device model
371 *
372 ****************************************************************************
373 ****************************************************************************/
374
375/*************************************************************************
376 * ACPI basic handles
377 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -0300379static acpi_handle root_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200381#define TPACPI_HANDLE(object, parent, paths...) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 static acpi_handle object##_handle; \
383 static acpi_handle *object##_parent = &parent##_handle; \
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400384 static char *object##_path; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 static char *object##_paths[] = { paths }
386
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200387TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400388 "\\_SB.PCI.ISA.EC", /* 570 */
389 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
390 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
391 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
392 "\\_SB.PCI0.ICH3.EC0", /* R31 */
393 "\\_SB.PCI0.LPC.EC", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300394 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200396TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
397TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200399TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
400 /* T4x, X31, X40 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400401 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
402 "\\CMS", /* R40, R40e */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300403 ); /* all others */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400404
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200405TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400406 "^HKEY", /* R30, R31 */
407 "HKEY", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300408 ); /* 570 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400409
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -0200410TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
411 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
412 "\\_SB.PCI0.VID0", /* 770e */
413 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
414 "\\_SB.PCI0.AGP.VID", /* all others */
415 ); /* R30, R31 */
416
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400417
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300418/*************************************************************************
419 * ACPI helpers
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -0200420 */
421
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422static int acpi_evalf(acpi_handle handle,
423 void *res, char *method, char *fmt, ...)
424{
425 char *fmt0 = fmt;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400426 struct acpi_object_list params;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200427 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400428 struct acpi_buffer result, *resultp;
429 union acpi_object out_obj;
430 acpi_status status;
431 va_list ap;
432 char res_type;
433 int success;
434 int quiet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
436 if (!*fmt) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200437 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 return 0;
439 }
440
441 if (*fmt == 'q') {
442 quiet = 1;
443 fmt++;
444 } else
445 quiet = 0;
446
447 res_type = *(fmt++);
448
449 params.count = 0;
450 params.pointer = &in_objs[0];
451
452 va_start(ap, fmt);
453 while (*fmt) {
454 char c = *(fmt++);
455 switch (c) {
456 case 'd': /* int */
457 in_objs[params.count].integer.value = va_arg(ap, int);
458 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
459 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400460 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 default:
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200462 printk(TPACPI_ERR "acpi_evalf() called "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 "with invalid format character '%c'\n", c);
464 return 0;
465 }
466 }
467 va_end(ap);
468
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400469 if (res_type != 'v') {
470 result.length = sizeof(out_obj);
471 result.pointer = &out_obj;
472 resultp = &result;
473 } else
474 resultp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400476 status = acpi_evaluate_object(handle, method, &params, resultp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
478 switch (res_type) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400479 case 'd': /* int */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 if (res)
481 *(int *)res = out_obj.integer.value;
482 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
483 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400484 case 'v': /* void */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 success = status == AE_OK;
486 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400487 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 default:
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200489 printk(TPACPI_ERR "acpi_evalf() called "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 "with invalid format character '%c'\n", res_type);
491 return 0;
492 }
493
494 if (!success && !quiet)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200495 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 method, fmt0, status);
497
498 return success;
499}
500
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200501static int acpi_ec_read(int i, u8 *p)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300502{
503 int v;
504
505 if (ecrd_handle) {
506 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
507 return 0;
508 *p = v;
509 } else {
510 if (ec_read(i, p) < 0)
511 return 0;
512 }
513
514 return 1;
515}
516
517static int acpi_ec_write(int i, u8 v)
518{
519 if (ecwr_handle) {
520 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
521 return 0;
522 } else {
523 if (ec_write(i, v) < 0)
524 return 0;
525 }
526
527 return 1;
528}
529
Henrique de Moraes Holschuh013c40e2008-01-08 13:02:54 -0200530#if defined(CONFIG_THINKPAD_ACPI_DOCK) || defined(CONFIG_THINKPAD_ACPI_BAY)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300531static int _sta(acpi_handle handle)
532{
533 int status;
534
535 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
536 status = 0;
537
538 return status;
539}
Henrique de Moraes Holschuh013c40e2008-01-08 13:02:54 -0200540#endif
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300541
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -0300542static int issue_thinkpad_cmos_command(int cmos_cmd)
543{
544 if (!cmos_handle)
545 return -ENXIO;
546
547 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
548 return -EIO;
549
550 return 0;
551}
552
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300553/*************************************************************************
554 * ACPI device model
555 */
556
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200557#define TPACPI_ACPIHANDLE_INIT(object) \
558 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200559 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
560
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300561static void drv_acpi_handle_init(char *name,
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300562 acpi_handle *handle, acpi_handle parent,
563 char **paths, int num_paths, char **path)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300564{
565 int i;
566 acpi_status status;
567
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300568 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
569 name);
570
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300571 for (i = 0; i < num_paths; i++) {
572 status = acpi_get_handle(parent, paths[i], handle);
573 if (ACPI_SUCCESS(status)) {
574 *path = paths[i];
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300575 dbg_printk(TPACPI_DBG_INIT,
576 "Found ACPI handle %s for %s\n",
577 *path, name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300578 return;
579 }
580 }
581
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300582 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
583 name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300584 *handle = NULL;
585}
586
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300587static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300588{
589 struct ibm_struct *ibm = data;
590
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300591 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
592 return;
593
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300594 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300595 return;
596
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300597 ibm->acpi->notify(ibm, event);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300598}
599
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300600static int __init setup_acpi_notify(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300601{
602 acpi_status status;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300603 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300604
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300605 BUG_ON(!ibm->acpi);
606
607 if (!*ibm->acpi->handle)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300608 return 0;
609
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300610 vdbg_printk(TPACPI_DBG_INIT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300611 "setting up ACPI notify for %s\n", ibm->name);
612
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300613 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
614 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200615 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300616 ibm->name, rc);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300617 return -ENODEV;
618 }
619
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700620 ibm->acpi->device->driver_data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300621 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200622 TPACPI_ACPI_EVENT_PREFIX,
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -0300623 ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300624
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300625 status = acpi_install_notify_handler(*ibm->acpi->handle,
626 ibm->acpi->type, dispatch_acpi_notify, ibm);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300627 if (ACPI_FAILURE(status)) {
628 if (status == AE_ALREADY_EXISTS) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200629 printk(TPACPI_NOTICE
630 "another device driver is already "
631 "handling %s events\n", ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300632 } else {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200633 printk(TPACPI_ERR
634 "acpi_install_notify_handler(%s) failed: %d\n",
635 ibm->name, status);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300636 }
637 return -ENODEV;
638 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300639 ibm->flags.acpi_notify_installed = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300640 return 0;
641}
642
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300643static int __init tpacpi_device_add(struct acpi_device *device)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300644{
645 return 0;
646}
647
Henrique de Moraes Holschuh67001212007-04-21 11:08:25 -0300648static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300649{
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300650 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300651
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300652 dbg_printk(TPACPI_DBG_INIT,
653 "registering %s as an ACPI driver\n", ibm->name);
654
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300655 BUG_ON(!ibm->acpi);
656
657 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
658 if (!ibm->acpi->driver) {
Mariusz Kozlowski4f778b92008-10-18 14:23:53 -0300659 printk(TPACPI_ERR
660 "failed to allocate memory for ibm->acpi->driver\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300661 return -ENOMEM;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300662 }
663
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200664 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300665 ibm->acpi->driver->ids = ibm->acpi->hid;
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200666
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300667 ibm->acpi->driver->ops.add = &tpacpi_device_add;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300668
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300669 rc = acpi_bus_register_driver(ibm->acpi->driver);
670 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200671 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200672 ibm->name, rc);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300673 kfree(ibm->acpi->driver);
674 ibm->acpi->driver = NULL;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300675 } else if (!rc)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300676 ibm->flags.acpi_driver_registered = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300677
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300678 return rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300679}
680
681
682/****************************************************************************
683 ****************************************************************************
684 *
685 * Procfs Helpers
686 *
687 ****************************************************************************
688 ****************************************************************************/
689
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300690static int dispatch_procfs_read(char *page, char **start, off_t off,
691 int count, int *eof, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300692{
693 struct ibm_struct *ibm = data;
694 int len;
695
696 if (!ibm || !ibm->read)
697 return -EINVAL;
698
699 len = ibm->read(page);
700 if (len < 0)
701 return len;
702
703 if (len <= off + count)
704 *eof = 1;
705 *start = page + off;
706 len -= off;
707 if (len > count)
708 len = count;
709 if (len < 0)
710 len = 0;
711
712 return len;
713}
714
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300715static int dispatch_procfs_write(struct file *file,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200716 const char __user *userbuf,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300717 unsigned long count, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300718{
719 struct ibm_struct *ibm = data;
720 char *kernbuf;
721 int ret;
722
723 if (!ibm || !ibm->write)
724 return -EINVAL;
725
726 kernbuf = kmalloc(count + 2, GFP_KERNEL);
727 if (!kernbuf)
728 return -ENOMEM;
729
730 if (copy_from_user(kernbuf, userbuf, count)) {
731 kfree(kernbuf);
732 return -EFAULT;
733 }
734
735 kernbuf[count] = 0;
736 strcat(kernbuf, ",");
737 ret = ibm->write(kernbuf);
738 if (ret == 0)
739 ret = count;
740
741 kfree(kernbuf);
742
743 return ret;
744}
745
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746static char *next_cmd(char **cmds)
747{
748 char *start = *cmds;
749 char *end;
750
751 while ((end = strchr(start, ',')) && end == start)
752 start = end + 1;
753
754 if (!end)
755 return NULL;
756
757 *end = 0;
758 *cmds = end + 1;
759 return start;
760}
761
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300762
763/****************************************************************************
764 ****************************************************************************
765 *
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300766 * Device model: input, hwmon and platform
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300767 *
768 ****************************************************************************
769 ****************************************************************************/
770
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -0300771static struct platform_device *tpacpi_pdev;
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300772static struct platform_device *tpacpi_sensors_pdev;
Tony Jones1beeffe2007-08-20 13:46:20 -0700773static struct device *tpacpi_hwmon;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300774static struct input_dev *tpacpi_inputdev;
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -0300775static struct mutex tpacpi_inputdev_send_mutex;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200776static LIST_HEAD(tpacpi_all_drivers);
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300777
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200778static int tpacpi_suspend_handler(struct platform_device *pdev,
779 pm_message_t state)
780{
781 struct ibm_struct *ibm, *itmp;
782
783 list_for_each_entry_safe(ibm, itmp,
784 &tpacpi_all_drivers,
785 all_drivers) {
786 if (ibm->suspend)
787 (ibm->suspend)(state);
788 }
789
790 return 0;
791}
792
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300793static int tpacpi_resume_handler(struct platform_device *pdev)
794{
795 struct ibm_struct *ibm, *itmp;
796
797 list_for_each_entry_safe(ibm, itmp,
798 &tpacpi_all_drivers,
799 all_drivers) {
800 if (ibm->resume)
801 (ibm->resume)();
802 }
803
804 return 0;
805}
806
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200807static void tpacpi_shutdown_handler(struct platform_device *pdev)
808{
809 struct ibm_struct *ibm, *itmp;
810
811 list_for_each_entry_safe(ibm, itmp,
812 &tpacpi_all_drivers,
813 all_drivers) {
814 if (ibm->shutdown)
815 (ibm->shutdown)();
816 }
817}
818
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300819static struct platform_driver tpacpi_pdriver = {
820 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200821 .name = TPACPI_DRVR_NAME,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300822 .owner = THIS_MODULE,
823 },
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200824 .suspend = tpacpi_suspend_handler,
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300825 .resume = tpacpi_resume_handler,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200826 .shutdown = tpacpi_shutdown_handler,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300827};
828
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300829static struct platform_driver tpacpi_hwmon_pdriver = {
830 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200831 .name = TPACPI_HWMON_DRVR_NAME,
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300832 .owner = THIS_MODULE,
833 },
834};
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300835
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -0300836/*************************************************************************
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300837 * sysfs support helpers
838 */
839
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200840struct attribute_set {
841 unsigned int members, max_members;
842 struct attribute_group group;
843};
844
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300845struct attribute_set_obj {
846 struct attribute_set s;
847 struct attribute *a;
848} __attribute__((packed));
849
850static struct attribute_set *create_attr_set(unsigned int max_members,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200851 const char *name)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300852{
853 struct attribute_set_obj *sobj;
854
855 if (max_members == 0)
856 return NULL;
857
858 /* Allocates space for implicit NULL at the end too */
859 sobj = kzalloc(sizeof(struct attribute_set_obj) +
860 max_members * sizeof(struct attribute *),
861 GFP_KERNEL);
862 if (!sobj)
863 return NULL;
864 sobj->s.max_members = max_members;
865 sobj->s.group.attrs = &sobj->a;
866 sobj->s.group.name = name;
867
868 return &sobj->s;
869}
870
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200871#define destroy_attr_set(_set) \
872 kfree(_set);
873
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300874/* not multi-threaded safe, use it in a single thread per set */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200875static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300876{
877 if (!s || !attr)
878 return -EINVAL;
879
880 if (s->members >= s->max_members)
881 return -ENOMEM;
882
883 s->group.attrs[s->members] = attr;
884 s->members++;
885
886 return 0;
887}
888
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200889static int add_many_to_attr_set(struct attribute_set *s,
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300890 struct attribute **attr,
891 unsigned int count)
892{
893 int i, res;
894
895 for (i = 0; i < count; i++) {
896 res = add_to_attr_set(s, attr[i]);
897 if (res)
898 return res;
899 }
900
901 return 0;
902}
903
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200904static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300905{
906 sysfs_remove_group(kobj, &s->group);
907 destroy_attr_set(s);
908}
909
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200910#define register_attr_set_with_sysfs(_attr_set, _kobj) \
911 sysfs_create_group(_kobj, &_attr_set->group)
912
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300913static int parse_strtoul(const char *buf,
914 unsigned long max, unsigned long *value)
915{
916 char *endp;
917
Henrique de Moraes Holschuh32afbf02007-10-08 10:12:56 -0300918 while (*buf && isspace(*buf))
919 buf++;
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300920 *value = simple_strtoul(buf, &endp, 0);
921 while (*endp && isspace(*endp))
922 endp++;
923 if (*endp || *value > max)
924 return -EINVAL;
925
926 return 0;
927}
928
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -0300929static void tpacpi_disable_brightness_delay(void)
930{
931 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
932 printk(TPACPI_NOTICE
933 "ACPI backlight control delay disabled\n");
934}
935
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -0300936static int __init tpacpi_query_bcl_levels(acpi_handle handle)
937{
938 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
939 union acpi_object *obj;
940 int rc;
941
942 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
943 obj = (union acpi_object *)buffer.pointer;
944 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
945 printk(TPACPI_ERR "Unknown _BCL data, "
946 "please report this to %s\n", TPACPI_MAIL);
947 rc = 0;
948 } else {
949 rc = obj->package.count;
950 }
951 } else {
952 return 0;
953 }
954
955 kfree(buffer.pointer);
956 return rc;
957}
958
959static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
960 u32 lvl, void *context, void **rv)
961{
962 char name[ACPI_PATH_SEGMENT_LENGTH];
963 struct acpi_buffer buffer = { sizeof(name), &name };
964
965 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
966 !strncmp("_BCL", name, sizeof(name) - 1)) {
967 BUG_ON(!rv || !*rv);
968 **(int **)rv = tpacpi_query_bcl_levels(handle);
969 return AE_CTRL_TERMINATE;
970 } else {
971 return AE_OK;
972 }
973}
974
975/*
976 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
977 */
978static int __init tpacpi_check_std_acpi_brightness_support(void)
979{
980 int status;
981 int bcl_levels = 0;
982 void *bcl_ptr = &bcl_levels;
983
984 if (!vid_handle) {
985 TPACPI_ACPIHANDLE_INIT(vid);
986 }
987 if (!vid_handle)
988 return 0;
989
990 /*
991 * Search for a _BCL method, and execute it. This is safe on all
992 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
993 * BIOS in ACPI backlight control mode. We do NOT have to care
994 * about calling the _BCL method in an enabled video device, any
995 * will do for our purposes.
996 */
997
998 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
999 tpacpi_acpi_walk_find_bcl, NULL,
1000 &bcl_ptr);
1001
1002 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
1003 tp_features.bright_acpimode = 1;
1004 return (bcl_levels - 2);
1005 }
1006
1007 return 0;
1008}
1009
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03001010static int __init tpacpi_new_rfkill(const unsigned int id,
1011 struct rfkill **rfk,
1012 const enum rfkill_type rfktype,
1013 const char *name,
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001014 const bool set_default,
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03001015 int (*toggle_radio)(void *, enum rfkill_state),
1016 int (*get_state)(void *, enum rfkill_state *))
1017{
1018 int res;
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02001019 enum rfkill_state initial_state = RFKILL_STATE_SOFT_BLOCKED;
1020
1021 res = get_state(NULL, &initial_state);
1022 if (res < 0) {
1023 printk(TPACPI_ERR
1024 "failed to read initial state for %s, error %d; "
1025 "will turn radio off\n", name, res);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001026 } else if (set_default) {
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02001027 /* try to set the initial state as the default for the rfkill
1028 * type, since we ask the firmware to preserve it across S5 in
1029 * NVRAM */
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00001030 if (rfkill_set_default(rfktype,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02001031 (initial_state == RFKILL_STATE_UNBLOCKED) ?
1032 RFKILL_STATE_UNBLOCKED :
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00001033 RFKILL_STATE_SOFT_BLOCKED) == -EPERM)
1034 vdbg_printk(TPACPI_DBG_RFKILL,
1035 "Default state for %s cannot be changed\n",
1036 name);
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02001037 }
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03001038
1039 *rfk = rfkill_allocate(&tpacpi_pdev->dev, rfktype);
1040 if (!*rfk) {
1041 printk(TPACPI_ERR
1042 "failed to allocate memory for rfkill class\n");
1043 return -ENOMEM;
1044 }
1045
1046 (*rfk)->name = name;
1047 (*rfk)->get_state = get_state;
1048 (*rfk)->toggle_radio = toggle_radio;
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02001049 (*rfk)->state = initial_state;
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03001050
1051 res = rfkill_register(*rfk);
1052 if (res < 0) {
1053 printk(TPACPI_ERR
1054 "failed to register %s rfkill switch: %d\n",
1055 name, res);
1056 rfkill_free(*rfk);
1057 *rfk = NULL;
1058 return res;
1059 }
1060
1061 return 0;
1062}
1063
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00001064static void printk_deprecated_attribute(const char * const what,
1065 const char * const details)
1066{
1067 tpacpi_log_usertask("deprecated sysfs attribute");
1068 printk(TPACPI_WARN "WARNING: sysfs attribute %s is deprecated and "
1069 "will be removed. %s\n",
1070 what, details);
1071}
1072
1073static void printk_deprecated_rfkill_attribute(const char * const what)
1074{
1075 printk_deprecated_attribute(what,
1076 "Please switch to generic rfkill before year 2010");
1077}
1078
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001079/*************************************************************************
1080 * thinkpad-acpi driver attributes
1081 */
1082
1083/* interface_version --------------------------------------------------- */
1084static ssize_t tpacpi_driver_interface_version_show(
1085 struct device_driver *drv,
1086 char *buf)
1087{
1088 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1089}
1090
1091static DRIVER_ATTR(interface_version, S_IRUGO,
1092 tpacpi_driver_interface_version_show, NULL);
1093
1094/* debug_level --------------------------------------------------------- */
1095static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1096 char *buf)
1097{
1098 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1099}
1100
1101static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1102 const char *buf, size_t count)
1103{
1104 unsigned long t;
1105
1106 if (parse_strtoul(buf, 0xffff, &t))
1107 return -EINVAL;
1108
1109 dbg_level = t;
1110
1111 return count;
1112}
1113
1114static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1115 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1116
1117/* version ------------------------------------------------------------- */
1118static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1119 char *buf)
1120{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001121 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1122 TPACPI_DESC, TPACPI_VERSION);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001123}
1124
1125static DRIVER_ATTR(version, S_IRUGO,
1126 tpacpi_driver_version_show, NULL);
1127
1128/* --------------------------------------------------------------------- */
1129
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001130#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1131
1132static void tpacpi_send_radiosw_update(void);
1133
1134/* wlsw_emulstate ------------------------------------------------------ */
1135static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1136 char *buf)
1137{
1138 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1139}
1140
1141static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1142 const char *buf, size_t count)
1143{
1144 unsigned long t;
1145
1146 if (parse_strtoul(buf, 1, &t))
1147 return -EINVAL;
1148
1149 if (tpacpi_wlsw_emulstate != t) {
1150 tpacpi_wlsw_emulstate = !!t;
1151 tpacpi_send_radiosw_update();
1152 } else
1153 tpacpi_wlsw_emulstate = !!t;
1154
1155 return count;
1156}
1157
1158static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1159 tpacpi_driver_wlsw_emulstate_show,
1160 tpacpi_driver_wlsw_emulstate_store);
1161
1162/* bluetooth_emulstate ------------------------------------------------- */
1163static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1164 struct device_driver *drv,
1165 char *buf)
1166{
1167 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1168}
1169
1170static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1171 struct device_driver *drv,
1172 const char *buf, size_t count)
1173{
1174 unsigned long t;
1175
1176 if (parse_strtoul(buf, 1, &t))
1177 return -EINVAL;
1178
1179 tpacpi_bluetooth_emulstate = !!t;
1180
1181 return count;
1182}
1183
1184static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1185 tpacpi_driver_bluetooth_emulstate_show,
1186 tpacpi_driver_bluetooth_emulstate_store);
1187
1188/* wwan_emulstate ------------------------------------------------- */
1189static ssize_t tpacpi_driver_wwan_emulstate_show(
1190 struct device_driver *drv,
1191 char *buf)
1192{
1193 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1194}
1195
1196static ssize_t tpacpi_driver_wwan_emulstate_store(
1197 struct device_driver *drv,
1198 const char *buf, size_t count)
1199{
1200 unsigned long t;
1201
1202 if (parse_strtoul(buf, 1, &t))
1203 return -EINVAL;
1204
1205 tpacpi_wwan_emulstate = !!t;
1206
1207 return count;
1208}
1209
1210static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1211 tpacpi_driver_wwan_emulstate_show,
1212 tpacpi_driver_wwan_emulstate_store);
1213
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001214/* uwb_emulstate ------------------------------------------------- */
1215static ssize_t tpacpi_driver_uwb_emulstate_show(
1216 struct device_driver *drv,
1217 char *buf)
1218{
1219 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1220}
1221
1222static ssize_t tpacpi_driver_uwb_emulstate_store(
1223 struct device_driver *drv,
1224 const char *buf, size_t count)
1225{
1226 unsigned long t;
1227
1228 if (parse_strtoul(buf, 1, &t))
1229 return -EINVAL;
1230
1231 tpacpi_uwb_emulstate = !!t;
1232
1233 return count;
1234}
1235
1236static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
1237 tpacpi_driver_uwb_emulstate_show,
1238 tpacpi_driver_uwb_emulstate_store);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001239#endif
1240
1241/* --------------------------------------------------------------------- */
1242
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001243static struct driver_attribute *tpacpi_driver_attributes[] = {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001244 &driver_attr_debug_level, &driver_attr_version,
1245 &driver_attr_interface_version,
1246};
1247
1248static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1249{
1250 int i, res;
1251
1252 i = 0;
1253 res = 0;
1254 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1255 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1256 i++;
1257 }
1258
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001259#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1260 if (!res && dbg_wlswemul)
1261 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1262 if (!res && dbg_bluetoothemul)
1263 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1264 if (!res && dbg_wwanemul)
1265 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001266 if (!res && dbg_uwbemul)
1267 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001268#endif
1269
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001270 return res;
1271}
1272
1273static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1274{
1275 int i;
1276
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001277 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001278 driver_remove_file(drv, tpacpi_driver_attributes[i]);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001279
1280#ifdef THINKPAD_ACPI_DEBUGFACILITIES
1281 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1282 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1283 driver_remove_file(drv, &driver_attr_wwan_emulstate);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001284 driver_remove_file(drv, &driver_attr_uwb_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001285#endif
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001286}
1287
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03001288/****************************************************************************
1289 ****************************************************************************
1290 *
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001291 * Subdrivers
1292 *
1293 ****************************************************************************
1294 ****************************************************************************/
1295
1296/*************************************************************************
Henrique de Moraes Holschuh142cfc92007-04-21 11:08:26 -03001297 * thinkpad-acpi init subdriver
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001298 */
1299
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001300static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001302 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
1303 printk(TPACPI_INFO "%s\n", TPACPI_URL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001305 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03001306 (thinkpad_id.bios_version_str) ?
1307 thinkpad_id.bios_version_str : "unknown",
1308 (thinkpad_id.ec_version_str) ?
1309 thinkpad_id.ec_version_str : "unknown");
1310
1311 if (thinkpad_id.vendor && thinkpad_id.model_str)
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03001312 printk(TPACPI_INFO "%s %s, model %s\n",
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03001313 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
1314 "IBM" : ((thinkpad_id.vendor ==
1315 PCI_VENDOR_ID_LENOVO) ?
1316 "Lenovo" : "Unknown vendor"),
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03001317 thinkpad_id.model_str,
1318 (thinkpad_id.nummodel_str) ?
1319 thinkpad_id.nummodel_str : "unknown");
Henrique de Moraes Holschuh3945ac32007-02-06 19:13:44 -02001320
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 return 0;
1322}
1323
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -03001324static int thinkpad_acpi_driver_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325{
1326 int len = 0;
1327
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001328 len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
1329 len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
1331 return len;
1332}
1333
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001334static struct ibm_struct thinkpad_acpi_driver_data = {
1335 .name = "driver",
1336 .read = thinkpad_acpi_driver_read,
1337};
1338
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001339/*************************************************************************
1340 * Hotkey subdriver
1341 */
1342
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001343enum { /* hot key scan codes (derived from ACPI DSDT) */
1344 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1345 TP_ACPI_HOTKEYSCAN_FNF2,
1346 TP_ACPI_HOTKEYSCAN_FNF3,
1347 TP_ACPI_HOTKEYSCAN_FNF4,
1348 TP_ACPI_HOTKEYSCAN_FNF5,
1349 TP_ACPI_HOTKEYSCAN_FNF6,
1350 TP_ACPI_HOTKEYSCAN_FNF7,
1351 TP_ACPI_HOTKEYSCAN_FNF8,
1352 TP_ACPI_HOTKEYSCAN_FNF9,
1353 TP_ACPI_HOTKEYSCAN_FNF10,
1354 TP_ACPI_HOTKEYSCAN_FNF11,
1355 TP_ACPI_HOTKEYSCAN_FNF12,
1356 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1357 TP_ACPI_HOTKEYSCAN_FNINSERT,
1358 TP_ACPI_HOTKEYSCAN_FNDELETE,
1359 TP_ACPI_HOTKEYSCAN_FNHOME,
1360 TP_ACPI_HOTKEYSCAN_FNEND,
1361 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1362 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1363 TP_ACPI_HOTKEYSCAN_FNSPACE,
1364 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1365 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1366 TP_ACPI_HOTKEYSCAN_MUTE,
1367 TP_ACPI_HOTKEYSCAN_THINKPAD,
1368};
1369
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001370enum { /* Keys available through NVRAM polling */
1371 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1372 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1373};
1374
1375enum { /* Positions of some of the keys in hotkey masks */
1376 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1377 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1378 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1379 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1380 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1381 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1382 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1383 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1384 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1385 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1386 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1387};
1388
1389enum { /* NVRAM to ACPI HKEY group map */
1390 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1391 TP_ACPI_HKEY_ZOOM_MASK |
1392 TP_ACPI_HKEY_DISPSWTCH_MASK |
1393 TP_ACPI_HKEY_HIBERNATE_MASK,
1394 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1395 TP_ACPI_HKEY_BRGHTDWN_MASK,
1396 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1397 TP_ACPI_HKEY_VOLDWN_MASK |
1398 TP_ACPI_HKEY_MUTE_MASK,
1399};
1400
1401#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1402struct tp_nvram_state {
1403 u16 thinkpad_toggle:1;
1404 u16 zoom_toggle:1;
1405 u16 display_toggle:1;
1406 u16 thinklight_toggle:1;
1407 u16 hibernate_toggle:1;
1408 u16 displayexp_toggle:1;
1409 u16 display_state:1;
1410 u16 brightness_toggle:1;
1411 u16 volume_toggle:1;
1412 u16 mute:1;
1413
1414 u8 brightness_level;
1415 u8 volume_level;
1416};
1417
1418static struct task_struct *tpacpi_hotkey_task;
1419static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1420static int hotkey_poll_freq = 10; /* Hz */
1421static struct mutex hotkey_thread_mutex;
1422static struct mutex hotkey_thread_data_mutex;
1423static unsigned int hotkey_config_change;
1424
1425#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1426
1427#define hotkey_source_mask 0U
1428
1429#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1430
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001431static struct mutex hotkey_mutex;
1432
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02001433static enum { /* Reasons for waking up */
1434 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1435 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1436 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1437} hotkey_wakeup_reason;
1438
1439static int hotkey_autosleep_ack;
1440
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001441static u32 hotkey_orig_mask;
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001442static u32 hotkey_all_mask;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001443static u32 hotkey_reserved_mask;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001444static u32 hotkey_mask;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001445
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001446static unsigned int hotkey_report_mode;
1447
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001448static u16 *hotkey_keycode_map;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001449
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03001450static struct attribute_set *hotkey_dev_attributes;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001451
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001452#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1453#define HOTKEY_CONFIG_CRITICAL_START \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001454 do { \
1455 mutex_lock(&hotkey_thread_data_mutex); \
1456 hotkey_config_change++; \
1457 } while (0);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001458#define HOTKEY_CONFIG_CRITICAL_END \
1459 mutex_unlock(&hotkey_thread_data_mutex);
1460#else
1461#define HOTKEY_CONFIG_CRITICAL_START
1462#define HOTKEY_CONFIG_CRITICAL_END
1463#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1464
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001465/* HKEY.MHKG() return bits */
1466#define TP_HOTKEY_TABLET_MASK (1 << 3)
1467
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001468static int hotkey_get_wlsw(int *status)
1469{
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001470#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1471 if (dbg_wlswemul) {
1472 *status = !!tpacpi_wlsw_emulstate;
1473 return 0;
1474 }
1475#endif
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001476 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
1477 return -EIO;
1478 return 0;
1479}
1480
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001481static int hotkey_get_tablet_mode(int *status)
1482{
1483 int s;
1484
1485 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
1486 return -EIO;
1487
Henrique de Moraes Holschuhcee47f52008-03-04 14:29:21 -08001488 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
1489 return 0;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001490}
1491
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001492/*
1493 * Call with hotkey_mutex held
1494 */
1495static int hotkey_mask_get(void)
1496{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001497 u32 m = 0;
1498
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001499 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001500 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001501 return -EIO;
1502 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001503 hotkey_mask = m | (hotkey_source_mask & hotkey_mask);
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001504
1505 return 0;
1506}
1507
1508/*
1509 * Call with hotkey_mutex held
1510 */
1511static int hotkey_mask_set(u32 mask)
1512{
1513 int i;
1514 int rc = 0;
1515
1516 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -03001517 if (!tp_warned.hotkey_mask_ff &&
1518 (mask == 0xffff || mask == 0xffffff ||
1519 mask == 0xffffffff)) {
1520 tp_warned.hotkey_mask_ff = 1;
1521 printk(TPACPI_NOTICE
1522 "setting the hotkey mask to 0x%08x is likely "
1523 "not the best way to go about it\n", mask);
1524 printk(TPACPI_NOTICE
1525 "please consider using the driver defaults, "
1526 "and refer to up-to-date thinkpad-acpi "
1527 "documentation\n");
1528 }
1529
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001530 HOTKEY_CONFIG_CRITICAL_START
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001531 for (i = 0; i < 32; i++) {
1532 u32 m = 1 << i;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001533 /* enable in firmware mask only keys not in NVRAM
1534 * mode, but enable the key in the cached hotkey_mask
1535 * regardless of mode, or the key will end up
1536 * disabled by hotkey_mask_get() */
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001537 if (!acpi_evalf(hkey_handle,
1538 NULL, "MHKM", "vdd", i + 1,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001539 !!((mask & ~hotkey_source_mask) & m))) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001540 rc = -EIO;
1541 break;
1542 } else {
1543 hotkey_mask = (hotkey_mask & ~m) | (mask & m);
1544 }
1545 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001546 HOTKEY_CONFIG_CRITICAL_END
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001547
1548 /* hotkey_mask_get must be called unconditionally below */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001549 if (!hotkey_mask_get() && !rc &&
1550 (hotkey_mask & ~hotkey_source_mask) !=
1551 (mask & ~hotkey_source_mask)) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001552 printk(TPACPI_NOTICE
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001553 "requested hot key mask 0x%08x, but "
1554 "firmware forced it to 0x%08x\n",
1555 mask, hotkey_mask);
1556 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001557 } else {
1558#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1559 HOTKEY_CONFIG_CRITICAL_START
1560 hotkey_mask = mask & hotkey_source_mask;
1561 HOTKEY_CONFIG_CRITICAL_END
1562 hotkey_mask_get();
1563 if (hotkey_mask != mask) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001564 printk(TPACPI_NOTICE
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001565 "requested hot key mask 0x%08x, "
1566 "forced to 0x%08x (NVRAM poll mask is "
1567 "0x%08x): no firmware mask support\n",
1568 mask, hotkey_mask, hotkey_source_mask);
1569 }
1570#else
1571 hotkey_mask_get();
1572 rc = -ENXIO;
1573#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001574 }
1575
1576 return rc;
1577}
1578
1579static int hotkey_status_get(int *status)
1580{
1581 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1582 return -EIO;
1583
1584 return 0;
1585}
1586
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00001587static int hotkey_status_set(bool enable)
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001588{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00001589 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001590 return -EIO;
1591
1592 return 0;
1593}
1594
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001595static void tpacpi_input_send_tabletsw(void)
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02001596{
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001597 int state;
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02001598
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001599 if (tp_features.hotkey_tablet &&
1600 !hotkey_get_tablet_mode(&state)) {
1601 mutex_lock(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02001602
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001603 input_report_switch(tpacpi_inputdev,
1604 SW_TABLET_MODE, !!state);
1605 input_sync(tpacpi_inputdev);
1606
1607 mutex_unlock(&tpacpi_inputdev_send_mutex);
1608 }
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02001609}
1610
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02001611static void tpacpi_input_send_key(unsigned int scancode)
1612{
1613 unsigned int keycode;
1614
1615 keycode = hotkey_keycode_map[scancode];
1616
1617 if (keycode != KEY_RESERVED) {
1618 mutex_lock(&tpacpi_inputdev_send_mutex);
1619
1620 input_report_key(tpacpi_inputdev, keycode, 1);
1621 if (keycode == KEY_UNKNOWN)
1622 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1623 scancode);
1624 input_sync(tpacpi_inputdev);
1625
1626 input_report_key(tpacpi_inputdev, keycode, 0);
1627 if (keycode == KEY_UNKNOWN)
1628 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1629 scancode);
1630 input_sync(tpacpi_inputdev);
1631
1632 mutex_unlock(&tpacpi_inputdev_send_mutex);
1633 }
1634}
1635
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001636#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1637static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
1638
1639static void tpacpi_hotkey_send_key(unsigned int scancode)
1640{
1641 tpacpi_input_send_key(scancode);
1642 if (hotkey_report_mode < 2) {
1643 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
1644 0x80, 0x1001 + scancode);
1645 }
1646}
1647
1648static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
1649{
1650 u8 d;
1651
1652 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
1653 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
1654 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
1655 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
1656 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
1657 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
1658 }
1659 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
1660 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
1661 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
1662 }
1663 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
1664 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
1665 n->displayexp_toggle =
1666 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
1667 }
1668 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
1669 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
1670 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
1671 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
1672 n->brightness_toggle =
1673 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
1674 }
1675 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
1676 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
1677 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
1678 >> TP_NVRAM_POS_LEVEL_VOLUME;
1679 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
1680 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
1681 }
1682}
1683
1684#define TPACPI_COMPARE_KEY(__scancode, __member) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001685 do { \
1686 if ((mask & (1 << __scancode)) && \
1687 oldn->__member != newn->__member) \
1688 tpacpi_hotkey_send_key(__scancode); \
1689 } while (0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001690
1691#define TPACPI_MAY_SEND_KEY(__scancode) \
1692 do { if (mask & (1 << __scancode)) \
1693 tpacpi_hotkey_send_key(__scancode); } while (0)
1694
1695static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001696 struct tp_nvram_state *newn,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001697 u32 mask)
1698{
1699 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
1700 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
1701 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
1702 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
1703
1704 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
1705
1706 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
1707
1708 /* handle volume */
1709 if (oldn->volume_toggle != newn->volume_toggle) {
1710 if (oldn->mute != newn->mute) {
1711 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1712 }
1713 if (oldn->volume_level > newn->volume_level) {
1714 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1715 } else if (oldn->volume_level < newn->volume_level) {
1716 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1717 } else if (oldn->mute == newn->mute) {
1718 /* repeated key presses that didn't change state */
1719 if (newn->mute) {
1720 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1721 } else if (newn->volume_level != 0) {
1722 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1723 } else {
1724 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1725 }
1726 }
1727 }
1728
1729 /* handle brightness */
1730 if (oldn->brightness_toggle != newn->brightness_toggle) {
1731 if (oldn->brightness_level < newn->brightness_level) {
1732 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1733 } else if (oldn->brightness_level > newn->brightness_level) {
1734 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1735 } else {
1736 /* repeated key presses that didn't change state */
1737 if (newn->brightness_level != 0) {
1738 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1739 } else {
1740 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1741 }
1742 }
1743 }
1744}
1745
1746#undef TPACPI_COMPARE_KEY
1747#undef TPACPI_MAY_SEND_KEY
1748
1749static int hotkey_kthread(void *data)
1750{
1751 struct tp_nvram_state s[2];
1752 u32 mask;
1753 unsigned int si, so;
1754 unsigned long t;
1755 unsigned int change_detector, must_reset;
1756
1757 mutex_lock(&hotkey_thread_mutex);
1758
1759 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
1760 goto exit;
1761
1762 set_freezable();
1763
1764 so = 0;
1765 si = 1;
1766 t = 0;
1767
1768 /* Initial state for compares */
1769 mutex_lock(&hotkey_thread_data_mutex);
1770 change_detector = hotkey_config_change;
1771 mask = hotkey_source_mask & hotkey_mask;
1772 mutex_unlock(&hotkey_thread_data_mutex);
1773 hotkey_read_nvram(&s[so], mask);
1774
1775 while (!kthread_should_stop() && hotkey_poll_freq) {
1776 if (t == 0)
1777 t = 1000/hotkey_poll_freq;
1778 t = msleep_interruptible(t);
1779 if (unlikely(kthread_should_stop()))
1780 break;
1781 must_reset = try_to_freeze();
1782 if (t > 0 && !must_reset)
1783 continue;
1784
1785 mutex_lock(&hotkey_thread_data_mutex);
1786 if (must_reset || hotkey_config_change != change_detector) {
1787 /* forget old state on thaw or config change */
1788 si = so;
1789 t = 0;
1790 change_detector = hotkey_config_change;
1791 }
1792 mask = hotkey_source_mask & hotkey_mask;
1793 mutex_unlock(&hotkey_thread_data_mutex);
1794
1795 if (likely(mask)) {
1796 hotkey_read_nvram(&s[si], mask);
1797 if (likely(si != so)) {
1798 hotkey_compare_and_issue_event(&s[so], &s[si],
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001799 mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001800 }
1801 }
1802
1803 so = si;
1804 si ^= 1;
1805 }
1806
1807exit:
1808 mutex_unlock(&hotkey_thread_mutex);
1809 return 0;
1810}
1811
1812static void hotkey_poll_stop_sync(void)
1813{
1814 if (tpacpi_hotkey_task) {
1815 if (frozen(tpacpi_hotkey_task) ||
1816 freezing(tpacpi_hotkey_task))
1817 thaw_process(tpacpi_hotkey_task);
1818
1819 kthread_stop(tpacpi_hotkey_task);
1820 tpacpi_hotkey_task = NULL;
1821 mutex_lock(&hotkey_thread_mutex);
1822 /* at this point, the thread did exit */
1823 mutex_unlock(&hotkey_thread_mutex);
1824 }
1825}
1826
1827/* call with hotkey_mutex held */
1828static void hotkey_poll_setup(int may_warn)
1829{
1830 if ((hotkey_source_mask & hotkey_mask) != 0 &&
1831 hotkey_poll_freq > 0 &&
1832 (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
1833 if (!tpacpi_hotkey_task) {
1834 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03001835 NULL, TPACPI_NVRAM_KTHREAD_NAME);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001836 if (IS_ERR(tpacpi_hotkey_task)) {
1837 tpacpi_hotkey_task = NULL;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001838 printk(TPACPI_ERR
1839 "could not create kernel thread "
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001840 "for hotkey polling\n");
1841 }
1842 }
1843 } else {
1844 hotkey_poll_stop_sync();
1845 if (may_warn &&
1846 hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001847 printk(TPACPI_NOTICE
1848 "hot keys 0x%08x require polling, "
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001849 "which is currently disabled\n",
1850 hotkey_source_mask);
1851 }
1852 }
1853}
1854
1855static void hotkey_poll_setup_safe(int may_warn)
1856{
1857 mutex_lock(&hotkey_mutex);
1858 hotkey_poll_setup(may_warn);
1859 mutex_unlock(&hotkey_mutex);
1860}
1861
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02001862#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1863
1864static void hotkey_poll_setup_safe(int __unused)
1865{
1866}
1867
1868#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1869
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001870static int hotkey_inputdev_open(struct input_dev *dev)
1871{
1872 switch (tpacpi_lifecycle) {
1873 case TPACPI_LIFE_INIT:
1874 /*
1875 * hotkey_init will call hotkey_poll_setup_safe
1876 * at the appropriate moment
1877 */
1878 return 0;
1879 case TPACPI_LIFE_EXITING:
1880 return -EBUSY;
1881 case TPACPI_LIFE_RUNNING:
1882 hotkey_poll_setup_safe(0);
1883 return 0;
1884 }
1885
1886 /* Should only happen if tpacpi_lifecycle is corrupt */
1887 BUG();
1888 return -EBUSY;
1889}
1890
1891static void hotkey_inputdev_close(struct input_dev *dev)
1892{
1893 /* disable hotkey polling when possible */
1894 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
1895 hotkey_poll_setup_safe(0);
1896}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001897
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001898/* sysfs hotkey enable ------------------------------------------------- */
1899static ssize_t hotkey_enable_show(struct device *dev,
1900 struct device_attribute *attr,
1901 char *buf)
1902{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001903 int res, status;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001904
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00001905 printk_deprecated_attribute("hotkey_enable",
1906 "Hotkey reporting is always enabled");
1907
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001908 res = hotkey_status_get(&status);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001909 if (res)
1910 return res;
1911
1912 return snprintf(buf, PAGE_SIZE, "%d\n", status);
1913}
1914
1915static ssize_t hotkey_enable_store(struct device *dev,
1916 struct device_attribute *attr,
1917 const char *buf, size_t count)
1918{
1919 unsigned long t;
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00001920
1921 printk_deprecated_attribute("hotkey_enable",
1922 "Hotkeys can be disabled through hotkey_mask");
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001923
1924 if (parse_strtoul(buf, 1, &t))
1925 return -EINVAL;
1926
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00001927 if (t == 0)
1928 return -EPERM;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001929
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00001930 return count;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001931}
1932
1933static struct device_attribute dev_attr_hotkey_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001934 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001935 hotkey_enable_show, hotkey_enable_store);
1936
1937/* sysfs hotkey mask --------------------------------------------------- */
1938static ssize_t hotkey_mask_show(struct device *dev,
1939 struct device_attribute *attr,
1940 char *buf)
1941{
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001942 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001943
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02001944 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001945 return -ERESTARTSYS;
1946 res = hotkey_mask_get();
1947 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001948
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001949 return (res)?
1950 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001951}
1952
1953static ssize_t hotkey_mask_store(struct device *dev,
1954 struct device_attribute *attr,
1955 const char *buf, size_t count)
1956{
1957 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001958 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001959
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001960 if (parse_strtoul(buf, 0xffffffffUL, &t))
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001961 return -EINVAL;
1962
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02001963 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001964 return -ERESTARTSYS;
1965
1966 res = hotkey_mask_set(t);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001967
1968#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1969 hotkey_poll_setup(1);
1970#endif
1971
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001972 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001973
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00001974 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
1975
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001976 return (res) ? res : count;
1977}
1978
1979static struct device_attribute dev_attr_hotkey_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001980 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001981 hotkey_mask_show, hotkey_mask_store);
1982
1983/* sysfs hotkey bios_enabled ------------------------------------------- */
1984static ssize_t hotkey_bios_enabled_show(struct device *dev,
1985 struct device_attribute *attr,
1986 char *buf)
1987{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00001988 return sprintf(buf, "0\n");
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001989}
1990
1991static struct device_attribute dev_attr_hotkey_bios_enabled =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001992 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001993
1994/* sysfs hotkey bios_mask ---------------------------------------------- */
1995static ssize_t hotkey_bios_mask_show(struct device *dev,
1996 struct device_attribute *attr,
1997 char *buf)
1998{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001999 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002000}
2001
2002static struct device_attribute dev_attr_hotkey_bios_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002003 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002004
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002005/* sysfs hotkey all_mask ----------------------------------------------- */
2006static ssize_t hotkey_all_mask_show(struct device *dev,
2007 struct device_attribute *attr,
2008 char *buf)
2009{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002010 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2011 hotkey_all_mask | hotkey_source_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002012}
2013
2014static struct device_attribute dev_attr_hotkey_all_mask =
2015 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
2016
2017/* sysfs hotkey recommended_mask --------------------------------------- */
2018static ssize_t hotkey_recommended_mask_show(struct device *dev,
2019 struct device_attribute *attr,
2020 char *buf)
2021{
2022 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002023 (hotkey_all_mask | hotkey_source_mask)
2024 & ~hotkey_reserved_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002025}
2026
2027static struct device_attribute dev_attr_hotkey_recommended_mask =
2028 __ATTR(hotkey_recommended_mask, S_IRUGO,
2029 hotkey_recommended_mask_show, NULL);
2030
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002031#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2032
2033/* sysfs hotkey hotkey_source_mask ------------------------------------- */
2034static ssize_t hotkey_source_mask_show(struct device *dev,
2035 struct device_attribute *attr,
2036 char *buf)
2037{
2038 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2039}
2040
2041static ssize_t hotkey_source_mask_store(struct device *dev,
2042 struct device_attribute *attr,
2043 const char *buf, size_t count)
2044{
2045 unsigned long t;
2046
2047 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2048 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2049 return -EINVAL;
2050
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002051 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002052 return -ERESTARTSYS;
2053
2054 HOTKEY_CONFIG_CRITICAL_START
2055 hotkey_source_mask = t;
2056 HOTKEY_CONFIG_CRITICAL_END
2057
2058 hotkey_poll_setup(1);
2059
2060 mutex_unlock(&hotkey_mutex);
2061
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002062 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2063
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002064 return count;
2065}
2066
2067static struct device_attribute dev_attr_hotkey_source_mask =
2068 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2069 hotkey_source_mask_show, hotkey_source_mask_store);
2070
2071/* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2072static ssize_t hotkey_poll_freq_show(struct device *dev,
2073 struct device_attribute *attr,
2074 char *buf)
2075{
2076 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2077}
2078
2079static ssize_t hotkey_poll_freq_store(struct device *dev,
2080 struct device_attribute *attr,
2081 const char *buf, size_t count)
2082{
2083 unsigned long t;
2084
2085 if (parse_strtoul(buf, 25, &t))
2086 return -EINVAL;
2087
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002088 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002089 return -ERESTARTSYS;
2090
2091 hotkey_poll_freq = t;
2092
2093 hotkey_poll_setup(1);
2094 mutex_unlock(&hotkey_mutex);
2095
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002096 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2097
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002098 return count;
2099}
2100
2101static struct device_attribute dev_attr_hotkey_poll_freq =
2102 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2103 hotkey_poll_freq_show, hotkey_poll_freq_store);
2104
2105#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2106
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002107/* sysfs hotkey radio_sw (pollable) ------------------------------------ */
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002108static ssize_t hotkey_radio_sw_show(struct device *dev,
2109 struct device_attribute *attr,
2110 char *buf)
2111{
2112 int res, s;
2113 res = hotkey_get_wlsw(&s);
2114 if (res < 0)
2115 return res;
2116
2117 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2118}
2119
2120static struct device_attribute dev_attr_hotkey_radio_sw =
2121 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2122
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002123static void hotkey_radio_sw_notify_change(void)
2124{
2125 if (tp_features.hotkey_wlsw)
2126 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2127 "hotkey_radio_sw");
2128}
2129
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002130/* sysfs hotkey tablet mode (pollable) --------------------------------- */
2131static ssize_t hotkey_tablet_mode_show(struct device *dev,
2132 struct device_attribute *attr,
2133 char *buf)
2134{
2135 int res, s;
2136 res = hotkey_get_tablet_mode(&s);
2137 if (res < 0)
2138 return res;
2139
2140 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2141}
2142
2143static struct device_attribute dev_attr_hotkey_tablet_mode =
2144 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2145
2146static void hotkey_tablet_mode_notify_change(void)
2147{
2148 if (tp_features.hotkey_tablet)
2149 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2150 "hotkey_tablet_mode");
2151}
2152
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002153/* sysfs hotkey report_mode -------------------------------------------- */
2154static ssize_t hotkey_report_mode_show(struct device *dev,
2155 struct device_attribute *attr,
2156 char *buf)
2157{
2158 return snprintf(buf, PAGE_SIZE, "%d\n",
2159 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2160}
2161
2162static struct device_attribute dev_attr_hotkey_report_mode =
2163 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2164
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002165/* sysfs wakeup reason (pollable) -------------------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002166static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2167 struct device_attribute *attr,
2168 char *buf)
2169{
2170 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2171}
2172
2173static struct device_attribute dev_attr_hotkey_wakeup_reason =
2174 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2175
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02002176static void hotkey_wakeup_reason_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002177{
2178 if (tp_features.hotkey_mask)
2179 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2180 "wakeup_reason");
2181}
2182
2183/* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002184static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2185 struct device_attribute *attr,
2186 char *buf)
2187{
2188 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2189}
2190
2191static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2192 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2193 hotkey_wakeup_hotunplug_complete_show, NULL);
2194
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02002195static void hotkey_wakeup_hotunplug_complete_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002196{
2197 if (tp_features.hotkey_mask)
2198 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2199 "wakeup_hotunplug_complete");
2200}
2201
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002202/* --------------------------------------------------------------------- */
2203
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002204static struct attribute *hotkey_attributes[] __initdata = {
2205 &dev_attr_hotkey_enable.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002206 &dev_attr_hotkey_bios_enabled.attr,
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002207 &dev_attr_hotkey_report_mode.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002208#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2209 &dev_attr_hotkey_mask.attr,
2210 &dev_attr_hotkey_all_mask.attr,
2211 &dev_attr_hotkey_recommended_mask.attr,
2212 &dev_attr_hotkey_source_mask.attr,
2213 &dev_attr_hotkey_poll_freq.attr,
2214#endif
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002215};
2216
2217static struct attribute *hotkey_mask_attributes[] __initdata = {
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002218 &dev_attr_hotkey_bios_mask.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002219#ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2220 &dev_attr_hotkey_mask.attr,
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002221 &dev_attr_hotkey_all_mask.attr,
2222 &dev_attr_hotkey_recommended_mask.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002223#endif
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002224 &dev_attr_hotkey_wakeup_reason.attr,
2225 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002226};
2227
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002228static void bluetooth_update_rfk(void);
2229static void wan_update_rfk(void);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02002230static void uwb_update_rfk(void);
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002231static void tpacpi_send_radiosw_update(void)
2232{
2233 int wlsw;
2234
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002235 /* Sync these BEFORE sending any rfkill events */
2236 if (tp_features.bluetooth)
2237 bluetooth_update_rfk();
2238 if (tp_features.wan)
2239 wan_update_rfk();
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02002240 if (tp_features.uwb)
2241 uwb_update_rfk();
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002242
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002243 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
2244 mutex_lock(&tpacpi_inputdev_send_mutex);
2245
2246 input_report_switch(tpacpi_inputdev,
2247 SW_RFKILL_ALL, !!wlsw);
2248 input_sync(tpacpi_inputdev);
2249
2250 mutex_unlock(&tpacpi_inputdev_send_mutex);
2251 }
2252 hotkey_radio_sw_notify_change();
2253}
2254
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002255static void hotkey_exit(void)
2256{
2257#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2258 hotkey_poll_stop_sync();
2259#endif
2260
2261 if (hotkey_dev_attributes)
2262 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2263
2264 kfree(hotkey_keycode_map);
2265
2266 if (tp_features.hotkey) {
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002267 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002268 "restoring original hot key mask\n");
2269 /* no short-circuit boolean operator below! */
2270 if ((hotkey_mask_set(hotkey_orig_mask) |
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002271 hotkey_status_set(false)) != 0)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002272 printk(TPACPI_ERR
2273 "failed to restore hot key mask "
2274 "to BIOS defaults\n");
2275 }
2276}
2277
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002278static int __init hotkey_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002279{
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002280 /* Requirements for changing the default keymaps:
2281 *
2282 * 1. Many of the keys are mapped to KEY_RESERVED for very
2283 * good reasons. Do not change them unless you have deep
2284 * knowledge on the IBM and Lenovo ThinkPad firmware for
2285 * the various ThinkPad models. The driver behaves
2286 * differently for KEY_RESERVED: such keys have their
2287 * hot key mask *unset* in mask_recommended, and also
2288 * in the initial hot key mask programmed into the
2289 * firmware at driver load time, which means the firm-
2290 * ware may react very differently if you change them to
2291 * something else;
2292 *
2293 * 2. You must be subscribed to the linux-thinkpad and
2294 * ibm-acpi-devel mailing lists, and you should read the
2295 * list archives since 2007 if you want to change the
2296 * keymaps. This requirement exists so that you will
2297 * know the past history of problems with the thinkpad-
2298 * acpi driver keymaps, and also that you will be
2299 * listening to any bug reports;
2300 *
2301 * 3. Do not send thinkpad-acpi specific patches directly to
2302 * for merging, *ever*. Send them to the linux-acpi
2303 * mailinglist for comments. Merging is to be done only
2304 * through acpi-test and the ACPI maintainer.
2305 *
2306 * If the above is too much to ask, don't change the keymap.
2307 * Ask the thinkpad-acpi maintainer to do it, instead.
2308 */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002309 static u16 ibm_keycode_map[] __initdata = {
2310 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2311 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
2312 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2313 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002314
2315 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002316 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2317 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2318 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002319
2320 /* brightness: firmware always reacts to them, unless
2321 * X.org did some tricks in the radeon BIOS scratch
2322 * registers of *some* models */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02002323 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02002324 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002325
2326 /* Thinklight: firmware always react to it */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002327 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002328
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002329 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2330 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002331
2332 /* Volume: firmware always react to it and reprograms
2333 * the built-in *extra* mixer. Never map it to control
2334 * another mixer by default. */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02002335 KEY_RESERVED, /* 0x14: VOLUME UP */
2336 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2337 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002338
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002339 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002340
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002341 /* (assignments unknown, please report if found) */
2342 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2343 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2344 };
2345 static u16 lenovo_keycode_map[] __initdata = {
2346 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2347 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
2348 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2349 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002350
2351 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002352 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2353 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2354 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002355
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03002356 /* These either have to go through ACPI video, or
2357 * act like in the IBM ThinkPads, so don't ever
2358 * enable them by default */
Henrique de Moraes Holschuh56a185b2007-12-13 12:14:09 -02002359 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuh56a185b2007-12-13 12:14:09 -02002360 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002361
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002362 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002363
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002364 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2365 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002366
2367 /* Volume: z60/z61, T60 (BIOS version?): firmware always
2368 * react to it and reprograms the built-in *extra* mixer.
2369 * Never map it to control another mixer by default.
2370 *
2371 * T60?, T61, R60?, R61: firmware and EC tries to send
2372 * these over the regular keyboard, so these are no-ops,
2373 * but there are still weird bugs re. MUTE, so do not
2374 * change unless you get test reports from all Lenovo
2375 * models. May cause the BIOS to interfere with the
2376 * HDA mixer.
2377 */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02002378 KEY_RESERVED, /* 0x14: VOLUME UP */
2379 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2380 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002381
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002382 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002383
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002384 /* (assignments unknown, please report if found) */
2385 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2386 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2387 };
2388
2389#define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
2390#define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
2391#define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
2392
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002393 int res, i;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002394 int status;
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03002395 int hkeyv;
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03002396
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002397 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2398 "initializing hotkey subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002399
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002400 BUG_ON(!tpacpi_inputdev);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002401 BUG_ON(tpacpi_inputdev->open != NULL ||
2402 tpacpi_inputdev->close != NULL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002403
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002404 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002405 mutex_init(&hotkey_mutex);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002406
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002407#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2408 mutex_init(&hotkey_thread_mutex);
2409 mutex_init(&hotkey_thread_data_mutex);
2410#endif
2411
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002412 /* hotkey not supported on 570 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002413 tp_features.hotkey = hkey_handle != NULL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002414
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002415 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2416 "hotkeys are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002417 str_supported(tp_features.hotkey));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002418
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002419 if (!tp_features.hotkey)
2420 return 1;
2421
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03002422 tpacpi_disable_brightness_delay();
2423
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002424 hotkey_dev_attributes = create_attr_set(13, NULL);
2425 if (!hotkey_dev_attributes)
2426 return -ENOMEM;
2427 res = add_many_to_attr_set(hotkey_dev_attributes,
2428 hotkey_attributes,
2429 ARRAY_SIZE(hotkey_attributes));
2430 if (res)
2431 goto err_exit;
2432
2433 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2434 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
2435 for HKEY interface version 0x100 */
2436 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
2437 if ((hkeyv >> 8) != 1) {
2438 printk(TPACPI_ERR "unknown version of the "
2439 "HKEY interface: 0x%x\n", hkeyv);
2440 printk(TPACPI_ERR "please report this to %s\n",
2441 TPACPI_MAIL);
2442 } else {
2443 /*
2444 * MHKV 0x100 in A31, R40, R40e,
2445 * T4x, X31, and later
2446 */
2447 tp_features.hotkey_mask = 1;
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002448 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2449 "firmware HKEY interface version: 0x%x\n",
2450 hkeyv);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002451 }
2452 }
2453
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002454 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2455 "hotkey masks are %s\n",
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002456 str_supported(tp_features.hotkey_mask));
2457
2458 if (tp_features.hotkey_mask) {
2459 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
2460 "MHKA", "qd")) {
2461 printk(TPACPI_ERR
2462 "missing MHKA handler, "
2463 "please report this to %s\n",
2464 TPACPI_MAIL);
2465 /* FN+F12, FN+F4, FN+F3 */
2466 hotkey_all_mask = 0x080cU;
2467 }
2468 }
2469
2470 /* hotkey_source_mask *must* be zero for
2471 * the first hotkey_mask_get */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002472 if (tp_features.hotkey_mask) {
2473 res = hotkey_mask_get();
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002474 if (res)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002475 goto err_exit;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002476
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002477 hotkey_orig_mask = hotkey_mask;
2478 res = add_many_to_attr_set(
2479 hotkey_dev_attributes,
2480 hotkey_mask_attributes,
2481 ARRAY_SIZE(hotkey_mask_attributes));
2482 if (res)
2483 goto err_exit;
2484 }
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002485
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002486#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002487 if (tp_features.hotkey_mask) {
2488 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
2489 & ~hotkey_all_mask;
2490 } else {
2491 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK;
2492 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002493
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002494 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002495 "hotkey source mask 0x%08x, polling freq %d\n",
2496 hotkey_source_mask, hotkey_poll_freq);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002497#endif
2498
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02002499#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2500 if (dbg_wlswemul) {
2501 tp_features.hotkey_wlsw = 1;
2502 printk(TPACPI_INFO
2503 "radio switch emulation enabled\n");
2504 } else
2505#endif
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002506 /* Not all thinkpads have a hardware radio switch */
2507 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
2508 tp_features.hotkey_wlsw = 1;
2509 printk(TPACPI_INFO
2510 "radio switch found; radios are %s\n",
2511 enabled(status, 0));
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03002512 }
2513 if (tp_features.hotkey_wlsw)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002514 res = add_to_attr_set(hotkey_dev_attributes,
2515 &dev_attr_hotkey_radio_sw.attr);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002516
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002517 /* For X41t, X60t, X61t Tablets... */
2518 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
2519 tp_features.hotkey_tablet = 1;
2520 printk(TPACPI_INFO
2521 "possible tablet mode switch found; "
2522 "ThinkPad in %s mode\n",
2523 (status & TP_HOTKEY_TABLET_MASK)?
2524 "tablet" : "laptop");
2525 res = add_to_attr_set(hotkey_dev_attributes,
2526 &dev_attr_hotkey_tablet_mode.attr);
2527 }
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002528
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002529 if (!res)
2530 res = register_attr_set_with_sysfs(
2531 hotkey_dev_attributes,
2532 &tpacpi_pdev->dev.kobj);
2533 if (res)
2534 goto err_exit;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002535
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002536 /* Set up key map */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002537
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002538 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
2539 GFP_KERNEL);
2540 if (!hotkey_keycode_map) {
2541 printk(TPACPI_ERR
2542 "failed to allocate memory for key map\n");
2543 res = -ENOMEM;
2544 goto err_exit;
2545 }
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002546
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002547 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002548 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002549 "using Lenovo default hot key map\n");
2550 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
2551 TPACPI_HOTKEY_MAP_SIZE);
2552 } else {
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002553 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002554 "using IBM default hot key map\n");
2555 memcpy(hotkey_keycode_map, &ibm_keycode_map,
2556 TPACPI_HOTKEY_MAP_SIZE);
2557 }
2558
2559 set_bit(EV_KEY, tpacpi_inputdev->evbit);
2560 set_bit(EV_MSC, tpacpi_inputdev->evbit);
2561 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
2562 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
2563 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
2564 tpacpi_inputdev->keycode = hotkey_keycode_map;
2565 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
2566 if (hotkey_keycode_map[i] != KEY_RESERVED) {
2567 set_bit(hotkey_keycode_map[i],
2568 tpacpi_inputdev->keybit);
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002569 } else {
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002570 if (i < sizeof(hotkey_reserved_mask)*8)
2571 hotkey_reserved_mask |= 1 << i;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002572 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002573 }
2574
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002575 if (tp_features.hotkey_wlsw) {
2576 set_bit(EV_SW, tpacpi_inputdev->evbit);
2577 set_bit(SW_RFKILL_ALL, tpacpi_inputdev->swbit);
2578 }
2579 if (tp_features.hotkey_tablet) {
2580 set_bit(EV_SW, tpacpi_inputdev->evbit);
2581 set_bit(SW_TABLET_MODE, tpacpi_inputdev->swbit);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002582 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002583
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002584 /* Do not issue duplicate brightness change events to
2585 * userspace */
2586 if (!tp_features.bright_acpimode)
2587 /* update bright_acpimode... */
2588 tpacpi_check_std_acpi_brightness_support();
2589
2590 if (tp_features.bright_acpimode) {
2591 printk(TPACPI_INFO
2592 "This ThinkPad has standard ACPI backlight "
2593 "brightness control, supported by the ACPI "
2594 "video driver\n");
2595 printk(TPACPI_NOTICE
2596 "Disabling thinkpad-acpi brightness events "
2597 "by default...\n");
2598
2599 /* The hotkey_reserved_mask change below is not
2600 * necessary while the keys are at KEY_RESERVED in the
2601 * default map, but better safe than sorry, leave it
2602 * here as a marker of what we have to do, especially
2603 * when we finally become able to set this at runtime
2604 * on response to X.org requests */
2605 hotkey_reserved_mask |=
2606 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
2607 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002608 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002609
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002610 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2611 "enabling firmware HKEY event interface...\n");
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002612 res = hotkey_status_set(true);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002613 if (res) {
2614 hotkey_exit();
2615 return res;
2616 }
2617 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
2618 & ~hotkey_reserved_mask)
2619 | hotkey_orig_mask);
2620 if (res < 0 && res != -ENXIO) {
2621 hotkey_exit();
2622 return res;
2623 }
2624
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002625 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2626 "legacy ibm/hotkey event reporting over procfs %s\n",
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002627 (hotkey_report_mode < 2) ?
2628 "enabled" : "disabled");
2629
2630 tpacpi_inputdev->open = &hotkey_inputdev_open;
2631 tpacpi_inputdev->close = &hotkey_inputdev_close;
2632
2633 hotkey_poll_setup_safe(1);
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002634 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002635 tpacpi_input_send_tabletsw();
2636
2637 return 0;
2638
2639err_exit:
2640 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2641 hotkey_dev_attributes = NULL;
2642
2643 return (res < 0)? res : 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002644}
2645
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002646static bool hotkey_notify_hotkey(const u32 hkey,
2647 bool *send_acpi_ev,
2648 bool *ignore_acpi_ev)
2649{
2650 /* 0x1000-0x1FFF: key presses */
2651 unsigned int scancode = hkey & 0xfff;
2652 *send_acpi_ev = true;
2653 *ignore_acpi_ev = false;
2654
2655 if (scancode > 0 && scancode < 0x21) {
2656 scancode--;
2657 if (!(hotkey_source_mask & (1 << scancode))) {
2658 tpacpi_input_send_key(scancode);
2659 *send_acpi_ev = false;
2660 } else {
2661 *ignore_acpi_ev = true;
2662 }
2663 return true;
2664 }
2665 return false;
2666}
2667
2668static bool hotkey_notify_wakeup(const u32 hkey,
2669 bool *send_acpi_ev,
2670 bool *ignore_acpi_ev)
2671{
2672 /* 0x2000-0x2FFF: Wakeup reason */
2673 *send_acpi_ev = true;
2674 *ignore_acpi_ev = false;
2675
2676 switch (hkey) {
2677 case 0x2304: /* suspend, undock */
2678 case 0x2404: /* hibernation, undock */
2679 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
2680 *ignore_acpi_ev = true;
2681 break;
2682
2683 case 0x2305: /* suspend, bay eject */
2684 case 0x2405: /* hibernation, bay eject */
2685 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
2686 *ignore_acpi_ev = true;
2687 break;
2688
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02002689 case 0x2313: /* Battery on critical low level (S3) */
2690 case 0x2413: /* Battery on critical low level (S4) */
2691 printk(TPACPI_ALERT
2692 "EMERGENCY WAKEUP: battery almost empty\n");
2693 /* how to auto-heal: */
2694 /* 2313: woke up from S3, go to S4/S5 */
2695 /* 2413: woke up from S4, go to S5 */
2696 break;
2697
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002698 default:
2699 return false;
2700 }
2701
2702 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
2703 printk(TPACPI_INFO
2704 "woke up due to a hot-unplug "
2705 "request...\n");
2706 hotkey_wakeup_reason_notify_change();
2707 }
2708 return true;
2709}
2710
2711static bool hotkey_notify_usrevent(const u32 hkey,
2712 bool *send_acpi_ev,
2713 bool *ignore_acpi_ev)
2714{
2715 /* 0x5000-0x5FFF: human interface helpers */
2716 *send_acpi_ev = true;
2717 *ignore_acpi_ev = false;
2718
2719 switch (hkey) {
2720 case 0x5010: /* Lenovo new BIOS: brightness changed */
2721 case 0x500b: /* X61t: tablet pen inserted into bay */
2722 case 0x500c: /* X61t: tablet pen removed from bay */
2723 return true;
2724
2725 case 0x5009: /* X41t-X61t: swivel up (tablet mode) */
2726 case 0x500a: /* X41t-X61t: swivel down (normal mode) */
2727 tpacpi_input_send_tabletsw();
2728 hotkey_tablet_mode_notify_change();
2729 *send_acpi_ev = false;
2730 return true;
2731
2732 case 0x5001:
2733 case 0x5002:
2734 /* LID switch events. Do not propagate */
2735 *ignore_acpi_ev = true;
2736 return true;
2737
2738 default:
2739 return false;
2740 }
2741}
2742
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02002743static bool hotkey_notify_thermal(const u32 hkey,
2744 bool *send_acpi_ev,
2745 bool *ignore_acpi_ev)
2746{
2747 /* 0x6000-0x6FFF: thermal alarms */
2748 *send_acpi_ev = true;
2749 *ignore_acpi_ev = false;
2750
2751 switch (hkey) {
2752 case 0x6011:
2753 printk(TPACPI_CRIT
2754 "THERMAL ALARM: battery is too hot!\n");
2755 /* recommended action: warn user through gui */
2756 return true;
2757 case 0x6012:
2758 printk(TPACPI_ALERT
2759 "THERMAL EMERGENCY: battery is extremely hot!\n");
2760 /* recommended action: immediate sleep/hibernate */
2761 return true;
2762 case 0x6021:
2763 printk(TPACPI_CRIT
2764 "THERMAL ALARM: "
2765 "a sensor reports something is too hot!\n");
2766 /* recommended action: warn user through gui, that */
2767 /* some internal component is too hot */
2768 return true;
2769 case 0x6022:
2770 printk(TPACPI_ALERT
2771 "THERMAL EMERGENCY: "
2772 "a sensor reports something is extremely hot!\n");
2773 /* recommended action: immediate sleep/hibernate */
2774 return true;
Henrique de Moraes Holschuh54926ce2009-01-11 03:01:09 -02002775 case 0x6030:
2776 printk(TPACPI_INFO
2777 "EC reports that Thermal Table has changed\n");
2778 /* recommended action: do nothing, we don't have
2779 * Lenovo ATM information */
2780 return true;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02002781 default:
2782 printk(TPACPI_ALERT
2783 "THERMAL ALERT: unknown thermal alarm received\n");
2784 return false;
2785 }
2786}
2787
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002788static void hotkey_notify(struct ibm_struct *ibm, u32 event)
2789{
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002790 u32 hkey;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002791 bool send_acpi_ev;
2792 bool ignore_acpi_ev;
2793 bool known_ev;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002794
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002795 if (event != 0x80) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002796 printk(TPACPI_ERR
2797 "unknown HKEY notification event %d\n", event);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002798 /* forward it to userspace, maybe it knows how to handle it */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002799 acpi_bus_generate_netlink_event(
2800 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02002801 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002802 event, 0);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002803 return;
2804 }
2805
2806 while (1) {
2807 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002808 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002809 return;
2810 }
2811
2812 if (hkey == 0) {
2813 /* queue empty */
2814 return;
2815 }
2816
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002817 send_acpi_ev = true;
2818 ignore_acpi_ev = false;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002819
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002820 switch (hkey >> 12) {
2821 case 1:
2822 /* 0x1000-0x1FFF: key presses */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002823 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
2824 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002825 break;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002826 case 2:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002827 /* 0x2000-0x2FFF: Wakeup reason */
2828 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
2829 &ignore_acpi_ev);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002830 break;
2831 case 3:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002832 /* 0x3000-0x3FFF: bay-related wakeups */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002833 if (hkey == 0x3003) {
2834 hotkey_autosleep_ack = 1;
2835 printk(TPACPI_INFO
2836 "bay ejected\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002837 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002838 known_ev = true;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002839 } else {
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002840 known_ev = false;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002841 }
2842 break;
2843 case 4:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002844 /* 0x4000-0x4FFF: dock-related wakeups */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002845 if (hkey == 0x4003) {
2846 hotkey_autosleep_ack = 1;
2847 printk(TPACPI_INFO
2848 "undocked\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002849 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002850 known_ev = true;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002851 } else {
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002852 known_ev = false;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002853 }
2854 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002855 case 5:
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02002856 /* 0x5000-0x5FFF: human interface helpers */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002857 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
2858 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002859 break;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02002860 case 6:
2861 /* 0x6000-0x6FFF: thermal alarms */
2862 known_ev = hotkey_notify_thermal(hkey, &send_acpi_ev,
2863 &ignore_acpi_ev);
2864 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002865 case 7:
2866 /* 0x7000-0x7FFF: misc */
2867 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002868 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002869 send_acpi_ev = 0;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002870 known_ev = true;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002871 break;
2872 }
2873 /* fallthrough to default */
2874 default:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002875 known_ev = false;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002876 }
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002877 if (!known_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002878 printk(TPACPI_NOTICE
2879 "unhandled HKEY event 0x%04x\n", hkey);
Henrique de Moraes Holschuhcb429352009-01-11 03:01:07 -02002880 printk(TPACPI_NOTICE
2881 "please report the conditions when this "
2882 "event happened to %s\n", TPACPI_MAIL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002883 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002884
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002885 /* Legacy events */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002886 if (!ignore_acpi_ev &&
2887 (send_acpi_ev || hotkey_report_mode < 2)) {
2888 acpi_bus_generate_proc_event(ibm->acpi->device,
2889 event, hkey);
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002890 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002891
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002892 /* netlink events */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002893 if (!ignore_acpi_ev && send_acpi_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002894 acpi_bus_generate_netlink_event(
2895 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02002896 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002897 event, hkey);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002898 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002899 }
2900}
2901
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002902static void hotkey_suspend(pm_message_t state)
2903{
2904 /* Do these on suspend, we get the events on early resume! */
2905 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
2906 hotkey_autosleep_ack = 0;
2907}
2908
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002909static void hotkey_resume(void)
2910{
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03002911 tpacpi_disable_brightness_delay();
2912
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002913 if (hotkey_mask_get())
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002914 printk(TPACPI_ERR
2915 "error while trying to read hot key mask "
2916 "from firmware\n");
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002917 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002918 hotkey_tablet_mode_notify_change();
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002919 hotkey_wakeup_reason_notify_change();
2920 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002921 hotkey_poll_setup_safe(0);
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002922}
2923
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002924/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002925static int hotkey_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002927 int res, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 int len = 0;
2929
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002930 if (!tp_features.hotkey) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002931 len += sprintf(p + len, "status:\t\tnot supported\n");
2932 return len;
2933 }
2934
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002935 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02002936 return -ERESTARTSYS;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002937 res = hotkey_status_get(&status);
2938 if (!res)
2939 res = hotkey_mask_get();
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002940 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03002941 if (res)
2942 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943
2944 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002945 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002946 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 len += sprintf(p + len,
2948 "commands:\tenable, disable, reset, <mask>\n");
2949 } else {
2950 len += sprintf(p + len, "mask:\t\tnot supported\n");
2951 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
2952 }
2953
2954 return len;
2955}
2956
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00002957static void hotkey_enabledisable_warn(bool enable)
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002958{
2959 tpacpi_log_usertask("procfs hotkey enable/disable");
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00002960 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
2961 TPACPI_WARN
2962 "hotkey enable/disable functionality has been "
2963 "removed from the driver. Hotkeys are always "
2964 "enabled\n"))
2965 printk(TPACPI_ERR
2966 "Please remove the hotkey=enable module "
2967 "parameter, it is deprecated. Hotkeys are always "
2968 "enabled\n");
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002969}
2970
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002971static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002973 int res;
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002974 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002977 if (!tp_features.hotkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 return -ENODEV;
2979
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002980 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02002981 return -ERESTARTSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002982
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002983 mask = hotkey_mask;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002984
2985 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 while ((cmd = next_cmd(&buf))) {
2987 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00002988 hotkey_enabledisable_warn(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00002990 hotkey_enabledisable_warn(0);
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002991 res = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 } else if (strlencmp(cmd, "reset") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002993 mask = hotkey_orig_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
2995 /* mask set */
2996 } else if (sscanf(cmd, "%x", &mask) == 1) {
2997 /* mask set */
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002998 } else {
2999 res = -EINVAL;
3000 goto errexit;
3001 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 }
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003003
3004 if (!res)
3005 tpacpi_disclose_usertask("procfs hotkey",
3006 "set mask to 0x%08x\n", mask);
3007
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02003008 if (!res && mask != hotkey_mask)
3009 res = hotkey_mask_set(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003011errexit:
3012 mutex_unlock(&hotkey_mutex);
3013 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003014}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003016static const struct acpi_device_id ibm_htk_device_ids[] = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003017 {TPACPI_ACPI_HKEY_HID, 0},
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003018 {"", 0},
3019};
3020
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003021static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003022 .hid = ibm_htk_device_ids,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003023 .notify = hotkey_notify,
3024 .handle = &hkey_handle,
3025 .type = ACPI_DEVICE_NOTIFY,
3026};
3027
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003028static struct ibm_struct hotkey_driver_data = {
3029 .name = "hotkey",
3030 .read = hotkey_read,
3031 .write = hotkey_write,
3032 .exit = hotkey_exit,
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03003033 .resume = hotkey_resume,
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003034 .suspend = hotkey_suspend,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003035 .acpi = &ibm_hotkey_acpidriver,
3036};
3037
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003038/*************************************************************************
3039 * Bluetooth subdriver
3040 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003042enum {
3043 /* ACPI GBDC/SBDC bits */
3044 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
3045 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003046 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
3047 off / last state */
3048};
3049
3050enum {
3051 /* ACPI \BLTH commands */
3052 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
3053 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
3054 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
3055 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
3056 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003057};
3058
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003059#define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
3060
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003061static struct rfkill *tpacpi_bluetooth_rfkill;
3062
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003063static void bluetooth_suspend(pm_message_t state)
3064{
3065 /* Try to make sure radio will resume powered off */
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003066 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3067 TP_ACPI_BLTH_PWR_OFF_ON_RESUME))
3068 vdbg_printk(TPACPI_DBG_RFKILL,
3069 "bluetooth power down on resume request failed\n");
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003070}
3071
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003072static int bluetooth_get_radiosw(void)
3073{
3074 int status;
3075
3076 if (!tp_features.bluetooth)
3077 return -ENODEV;
3078
Henrique de Moraes Holschuh133ec3b2008-07-21 09:15:50 -03003079 /* WLSW overrides bluetooth in firmware/hardware, reflect that */
3080 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003081 return RFKILL_STATE_HARD_BLOCKED;
Henrique de Moraes Holschuh133ec3b2008-07-21 09:15:50 -03003082
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003083#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3084 if (dbg_bluetoothemul)
3085 return (tpacpi_bluetooth_emulstate) ?
3086 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3087#endif
3088
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003089 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
3090 return -EIO;
3091
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003092 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
3093 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003094}
3095
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003096static void bluetooth_update_rfk(void)
3097{
3098 int status;
3099
3100 if (!tpacpi_bluetooth_rfkill)
3101 return;
3102
3103 status = bluetooth_get_radiosw();
3104 if (status < 0)
3105 return;
3106 rfkill_force_state(tpacpi_bluetooth_rfkill, status);
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003107
3108 vdbg_printk(TPACPI_DBG_RFKILL,
3109 "forced rfkill state to %d\n",
3110 status);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003111}
3112
3113static int bluetooth_set_radiosw(int radio_on, int update_rfk)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003114{
3115 int status;
3116
3117 if (!tp_features.bluetooth)
3118 return -ENODEV;
3119
Henrique de Moraes Holschuh133ec3b2008-07-21 09:15:50 -03003120 /* WLSW overrides bluetooth in firmware/hardware, but there is no
3121 * reason to risk weird behaviour. */
3122 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3123 && radio_on)
3124 return -EPERM;
3125
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003126 vdbg_printk(TPACPI_DBG_RFKILL,
3127 "will %s bluetooth\n", radio_on ? "enable" : "disable");
3128
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003129#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3130 if (dbg_bluetoothemul) {
3131 tpacpi_bluetooth_emulstate = !!radio_on;
3132 if (update_rfk)
3133 bluetooth_update_rfk();
3134 return 0;
3135 }
3136#endif
3137
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003138 /* We make sure to keep TP_ACPI_BLUETOOTH_RESUMECTRL off */
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003139 if (radio_on)
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003140 status = TP_ACPI_BLUETOOTH_RADIOSSW;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003141 else
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003142 status = 0;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003143 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
3144 return -EIO;
3145
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003146 if (update_rfk)
3147 bluetooth_update_rfk();
3148
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003149 return 0;
3150}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003151
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003152/* sysfs bluetooth enable ---------------------------------------------- */
3153static ssize_t bluetooth_enable_show(struct device *dev,
3154 struct device_attribute *attr,
3155 char *buf)
3156{
3157 int status;
3158
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00003159 printk_deprecated_rfkill_attribute("bluetooth_enable");
3160
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003161 status = bluetooth_get_radiosw();
3162 if (status < 0)
3163 return status;
3164
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003165 return snprintf(buf, PAGE_SIZE, "%d\n",
3166 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003167}
3168
3169static ssize_t bluetooth_enable_store(struct device *dev,
3170 struct device_attribute *attr,
3171 const char *buf, size_t count)
3172{
3173 unsigned long t;
3174 int res;
3175
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00003176 printk_deprecated_rfkill_attribute("bluetooth_enable");
3177
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003178 if (parse_strtoul(buf, 1, &t))
3179 return -EINVAL;
3180
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003181 tpacpi_disclose_usertask("bluetooth_enable", "set to %ld\n", t);
3182
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003183 res = bluetooth_set_radiosw(t, 1);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003184
3185 return (res) ? res : count;
3186}
3187
3188static struct device_attribute dev_attr_bluetooth_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03003189 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003190 bluetooth_enable_show, bluetooth_enable_store);
3191
3192/* --------------------------------------------------------------------- */
3193
3194static struct attribute *bluetooth_attributes[] = {
3195 &dev_attr_bluetooth_enable.attr,
3196 NULL
3197};
3198
3199static const struct attribute_group bluetooth_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003200 .attrs = bluetooth_attributes,
3201};
3202
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003203static int tpacpi_bluetooth_rfk_get(void *data, enum rfkill_state *state)
3204{
3205 int bts = bluetooth_get_radiosw();
3206
3207 if (bts < 0)
3208 return bts;
3209
3210 *state = bts;
3211 return 0;
3212}
3213
3214static int tpacpi_bluetooth_rfk_set(void *data, enum rfkill_state state)
3215{
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003216 dbg_printk(TPACPI_DBG_RFKILL,
3217 "request to change radio state to %d\n", state);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003218 return bluetooth_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3219}
3220
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003221static void bluetooth_shutdown(void)
3222{
3223 /* Order firmware to save current state to NVRAM */
3224 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3225 TP_ACPI_BLTH_SAVE_STATE))
3226 printk(TPACPI_NOTICE
3227 "failed to save bluetooth state to NVRAM\n");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003228 else
3229 vdbg_printk(TPACPI_DBG_RFKILL,
3230 "bluestooth state saved to NVRAM\n");
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003231}
3232
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003233static void bluetooth_exit(void)
3234{
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003235 bluetooth_shutdown();
3236
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003237 if (tpacpi_bluetooth_rfkill)
3238 rfkill_unregister(tpacpi_bluetooth_rfkill);
3239
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003240 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3241 &bluetooth_attr_group);
3242}
3243
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003244static int __init bluetooth_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003246 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003247 int status = 0;
3248
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003249 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3250 "initializing bluetooth subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003251
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003252 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003253
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003254 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3255 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003256 tp_features.bluetooth = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003257 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003259 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3260 "bluetooth is %s, status 0x%02x\n",
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003261 str_supported(tp_features.bluetooth),
3262 status);
3263
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003264#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3265 if (dbg_bluetoothemul) {
3266 tp_features.bluetooth = 1;
3267 printk(TPACPI_INFO
3268 "bluetooth switch emulation enabled\n");
3269 } else
3270#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003271 if (tp_features.bluetooth &&
3272 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
3273 /* no bluetooth hardware present in system */
3274 tp_features.bluetooth = 0;
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003275 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003276 "bluetooth hardware not installed\n");
3277 }
3278
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003279 if (!tp_features.bluetooth)
3280 return 1;
3281
3282 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003283 &bluetooth_attr_group);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003284 if (res)
3285 return res;
3286
3287 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
3288 &tpacpi_bluetooth_rfkill,
3289 RFKILL_TYPE_BLUETOOTH,
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003290 TPACPI_RFK_BLUETOOTH_SW_NAME,
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02003291 true,
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003292 tpacpi_bluetooth_rfk_set,
3293 tpacpi_bluetooth_rfk_get);
3294 if (res) {
3295 bluetooth_exit();
3296 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003297 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003298
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003299 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300}
3301
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003302/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003303static int bluetooth_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304{
3305 int len = 0;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003306 int status = bluetooth_get_radiosw();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003308 if (!tp_features.bluetooth)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309 len += sprintf(p + len, "status:\t\tnot supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310 else {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003311 len += sprintf(p + len, "status:\t\t%s\n",
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003312 (status == RFKILL_STATE_UNBLOCKED) ?
3313 "enabled" : "disabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314 len += sprintf(p + len, "commands:\tenable, disable\n");
3315 }
3316
3317 return len;
3318}
3319
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003320static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322 char *cmd;
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003323 int state = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003325 if (!tp_features.bluetooth)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003326 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327
3328 while ((cmd = next_cmd(&buf))) {
3329 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003330 state = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003332 state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333 } else
3334 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335 }
3336
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003337 if (state != -1) {
3338 tpacpi_disclose_usertask("procfs bluetooth",
3339 "attempt to %s\n",
3340 state ? "enable" : "disable");
3341 bluetooth_set_radiosw(state, 1);
3342 }
3343
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 return 0;
3345}
3346
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003347static struct ibm_struct bluetooth_driver_data = {
3348 .name = "bluetooth",
3349 .read = bluetooth_read,
3350 .write = bluetooth_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003351 .exit = bluetooth_exit,
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003352 .suspend = bluetooth_suspend,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003353 .shutdown = bluetooth_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003354};
3355
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003356/*************************************************************************
3357 * Wan subdriver
3358 */
3359
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003360enum {
3361 /* ACPI GWAN/SWAN bits */
3362 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
3363 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003364 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
3365 off / last state */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003366};
3367
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003368#define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
3369
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003370static struct rfkill *tpacpi_wan_rfkill;
3371
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003372static void wan_suspend(pm_message_t state)
3373{
3374 /* Try to make sure radio will resume powered off */
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003375 if (!acpi_evalf(NULL, NULL, "\\WGSV", "qvd",
3376 TP_ACPI_WGSV_PWR_OFF_ON_RESUME))
3377 vdbg_printk(TPACPI_DBG_RFKILL,
3378 "WWAN power down on resume request failed\n");
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003379}
3380
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003381static int wan_get_radiosw(void)
3382{
3383 int status;
3384
3385 if (!tp_features.wan)
3386 return -ENODEV;
3387
Henrique de Moraes Holschuh133ec3b2008-07-21 09:15:50 -03003388 /* WLSW overrides WWAN in firmware/hardware, reflect that */
3389 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003390 return RFKILL_STATE_HARD_BLOCKED;
Henrique de Moraes Holschuh133ec3b2008-07-21 09:15:50 -03003391
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003392#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3393 if (dbg_wwanemul)
3394 return (tpacpi_wwan_emulstate) ?
3395 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3396#endif
3397
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003398 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
3399 return -EIO;
3400
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003401 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
3402 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003403}
3404
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003405static void wan_update_rfk(void)
3406{
3407 int status;
3408
3409 if (!tpacpi_wan_rfkill)
3410 return;
3411
3412 status = wan_get_radiosw();
3413 if (status < 0)
3414 return;
3415 rfkill_force_state(tpacpi_wan_rfkill, status);
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003416
3417 vdbg_printk(TPACPI_DBG_RFKILL,
3418 "forced rfkill state to %d\n",
3419 status);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003420}
3421
3422static int wan_set_radiosw(int radio_on, int update_rfk)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003423{
3424 int status;
3425
3426 if (!tp_features.wan)
3427 return -ENODEV;
3428
Henrique de Moraes Holschuh133ec3b2008-07-21 09:15:50 -03003429 /* WLSW overrides bluetooth in firmware/hardware, but there is no
3430 * reason to risk weird behaviour. */
3431 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3432 && radio_on)
3433 return -EPERM;
3434
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003435 vdbg_printk(TPACPI_DBG_RFKILL,
3436 "will %s WWAN\n", radio_on ? "enable" : "disable");
3437
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003438#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3439 if (dbg_wwanemul) {
3440 tpacpi_wwan_emulstate = !!radio_on;
3441 if (update_rfk)
3442 wan_update_rfk();
3443 return 0;
3444 }
3445#endif
3446
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003447 /* We make sure to keep TP_ACPI_WANCARD_RESUMECTRL off */
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003448 if (radio_on)
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003449 status = TP_ACPI_WANCARD_RADIOSSW;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003450 else
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003451 status = 0;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003452 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
3453 return -EIO;
3454
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003455 if (update_rfk)
3456 wan_update_rfk();
3457
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003458 return 0;
3459}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003460
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003461/* sysfs wan enable ---------------------------------------------------- */
3462static ssize_t wan_enable_show(struct device *dev,
3463 struct device_attribute *attr,
3464 char *buf)
3465{
3466 int status;
3467
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00003468 printk_deprecated_rfkill_attribute("wwan_enable");
3469
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003470 status = wan_get_radiosw();
3471 if (status < 0)
3472 return status;
3473
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003474 return snprintf(buf, PAGE_SIZE, "%d\n",
3475 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003476}
3477
3478static ssize_t wan_enable_store(struct device *dev,
3479 struct device_attribute *attr,
3480 const char *buf, size_t count)
3481{
3482 unsigned long t;
3483 int res;
3484
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00003485 printk_deprecated_rfkill_attribute("wwan_enable");
3486
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003487 if (parse_strtoul(buf, 1, &t))
3488 return -EINVAL;
3489
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003490 tpacpi_disclose_usertask("wwan_enable", "set to %ld\n", t);
3491
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003492 res = wan_set_radiosw(t, 1);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003493
3494 return (res) ? res : count;
3495}
3496
3497static struct device_attribute dev_attr_wan_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03003498 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003499 wan_enable_show, wan_enable_store);
3500
3501/* --------------------------------------------------------------------- */
3502
3503static struct attribute *wan_attributes[] = {
3504 &dev_attr_wan_enable.attr,
3505 NULL
3506};
3507
3508static const struct attribute_group wan_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003509 .attrs = wan_attributes,
3510};
3511
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003512static int tpacpi_wan_rfk_get(void *data, enum rfkill_state *state)
3513{
3514 int wans = wan_get_radiosw();
3515
3516 if (wans < 0)
3517 return wans;
3518
3519 *state = wans;
3520 return 0;
3521}
3522
3523static int tpacpi_wan_rfk_set(void *data, enum rfkill_state state)
3524{
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003525 dbg_printk(TPACPI_DBG_RFKILL,
3526 "request to change radio state to %d\n", state);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003527 return wan_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3528}
3529
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003530static void wan_shutdown(void)
3531{
3532 /* Order firmware to save current state to NVRAM */
3533 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
3534 TP_ACPI_WGSV_SAVE_STATE))
3535 printk(TPACPI_NOTICE
3536 "failed to save WWAN state to NVRAM\n");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003537 else
3538 vdbg_printk(TPACPI_DBG_RFKILL,
3539 "WWAN state saved to NVRAM\n");
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003540}
3541
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003542static void wan_exit(void)
3543{
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003544 wan_shutdown();
3545
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003546 if (tpacpi_wan_rfkill)
3547 rfkill_unregister(tpacpi_wan_rfkill);
3548
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003549 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3550 &wan_attr_group);
3551}
3552
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003553static int __init wan_init(struct ibm_init_struct *iibm)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003554{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003555 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003556 int status = 0;
3557
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003558 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3559 "initializing wan subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003560
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003561 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003562
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003563 tp_features.wan = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003564 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003565
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003566 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3567 "wan is %s, status 0x%02x\n",
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003568 str_supported(tp_features.wan),
3569 status);
3570
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003571#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3572 if (dbg_wwanemul) {
3573 tp_features.wan = 1;
3574 printk(TPACPI_INFO
3575 "wwan switch emulation enabled\n");
3576 } else
3577#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003578 if (tp_features.wan &&
3579 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
3580 /* no wan hardware present in system */
3581 tp_features.wan = 0;
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003582 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003583 "wan hardware not installed\n");
3584 }
3585
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003586 if (!tp_features.wan)
3587 return 1;
3588
3589 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003590 &wan_attr_group);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003591 if (res)
3592 return res;
3593
3594 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
3595 &tpacpi_wan_rfkill,
3596 RFKILL_TYPE_WWAN,
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003597 TPACPI_RFK_WWAN_SW_NAME,
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02003598 true,
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003599 tpacpi_wan_rfk_set,
3600 tpacpi_wan_rfk_get);
3601 if (res) {
3602 wan_exit();
3603 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003604 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003605
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003606 return 0;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003607}
3608
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003609/* procfs -------------------------------------------------------------- */
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003610static int wan_read(char *p)
3611{
3612 int len = 0;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003613 int status = wan_get_radiosw();
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003614
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003615 tpacpi_disclose_usertask("procfs wan", "read");
3616
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003617 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003618 len += sprintf(p + len, "status:\t\tnot supported\n");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003619 else {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003620 len += sprintf(p + len, "status:\t\t%s\n",
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003621 (status == RFKILL_STATE_UNBLOCKED) ?
3622 "enabled" : "disabled");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003623 len += sprintf(p + len, "commands:\tenable, disable\n");
3624 }
3625
3626 return len;
3627}
3628
3629static int wan_write(char *buf)
3630{
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003631 char *cmd;
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003632 int state = -1;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003633
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003634 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003635 return -ENODEV;
3636
3637 while ((cmd = next_cmd(&buf))) {
3638 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003639 state = 1;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003640 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003641 state = 0;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003642 } else
3643 return -EINVAL;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003644 }
3645
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003646 if (state != -1) {
3647 tpacpi_disclose_usertask("procfs wan",
3648 "attempt to %s\n",
3649 state ? "enable" : "disable");
3650 wan_set_radiosw(state, 1);
3651 }
3652
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003653 return 0;
3654}
3655
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003656static struct ibm_struct wan_driver_data = {
3657 .name = "wan",
3658 .read = wan_read,
3659 .write = wan_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003660 .exit = wan_exit,
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003661 .suspend = wan_suspend,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003662 .shutdown = wan_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003663};
3664
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003665/*************************************************************************
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02003666 * UWB subdriver
3667 */
3668
3669enum {
3670 /* ACPI GUWB/SUWB bits */
3671 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
3672 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
3673};
3674
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003675#define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
3676
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02003677static struct rfkill *tpacpi_uwb_rfkill;
3678
3679static int uwb_get_radiosw(void)
3680{
3681 int status;
3682
3683 if (!tp_features.uwb)
3684 return -ENODEV;
3685
3686 /* WLSW overrides UWB in firmware/hardware, reflect that */
3687 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
3688 return RFKILL_STATE_HARD_BLOCKED;
3689
3690#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3691 if (dbg_uwbemul)
3692 return (tpacpi_uwb_emulstate) ?
3693 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3694#endif
3695
3696 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
3697 return -EIO;
3698
3699 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
3700 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3701}
3702
3703static void uwb_update_rfk(void)
3704{
3705 int status;
3706
3707 if (!tpacpi_uwb_rfkill)
3708 return;
3709
3710 status = uwb_get_radiosw();
3711 if (status < 0)
3712 return;
3713 rfkill_force_state(tpacpi_uwb_rfkill, status);
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003714
3715 vdbg_printk(TPACPI_DBG_RFKILL,
3716 "forced rfkill state to %d\n",
3717 status);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02003718}
3719
3720static int uwb_set_radiosw(int radio_on, int update_rfk)
3721{
3722 int status;
3723
3724 if (!tp_features.uwb)
3725 return -ENODEV;
3726
3727 /* WLSW overrides UWB in firmware/hardware, but there is no
3728 * reason to risk weird behaviour. */
3729 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3730 && radio_on)
3731 return -EPERM;
3732
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003733 vdbg_printk(TPACPI_DBG_RFKILL,
3734 "will %s UWB\n", radio_on ? "enable" : "disable");
3735
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02003736#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3737 if (dbg_uwbemul) {
3738 tpacpi_uwb_emulstate = !!radio_on;
3739 if (update_rfk)
3740 uwb_update_rfk();
3741 return 0;
3742 }
3743#endif
3744
3745 status = (radio_on) ? TP_ACPI_UWB_RADIOSSW : 0;
3746 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
3747 return -EIO;
3748
3749 if (update_rfk)
3750 uwb_update_rfk();
3751
3752 return 0;
3753}
3754
3755/* --------------------------------------------------------------------- */
3756
3757static int tpacpi_uwb_rfk_get(void *data, enum rfkill_state *state)
3758{
3759 int uwbs = uwb_get_radiosw();
3760
3761 if (uwbs < 0)
3762 return uwbs;
3763
3764 *state = uwbs;
3765 return 0;
3766}
3767
3768static int tpacpi_uwb_rfk_set(void *data, enum rfkill_state state)
3769{
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003770 dbg_printk(TPACPI_DBG_RFKILL,
3771 "request to change radio state to %d\n", state);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02003772 return uwb_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3773}
3774
3775static void uwb_exit(void)
3776{
3777 if (tpacpi_uwb_rfkill)
3778 rfkill_unregister(tpacpi_uwb_rfkill);
3779}
3780
3781static int __init uwb_init(struct ibm_init_struct *iibm)
3782{
3783 int res;
3784 int status = 0;
3785
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003786 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3787 "initializing uwb subdriver\n");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02003788
3789 TPACPI_ACPIHANDLE_INIT(hkey);
3790
3791 tp_features.uwb = hkey_handle &&
3792 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
3793
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003794 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3795 "uwb is %s, status 0x%02x\n",
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02003796 str_supported(tp_features.uwb),
3797 status);
3798
3799#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3800 if (dbg_uwbemul) {
3801 tp_features.uwb = 1;
3802 printk(TPACPI_INFO
3803 "uwb switch emulation enabled\n");
3804 } else
3805#endif
3806 if (tp_features.uwb &&
3807 !(status & TP_ACPI_UWB_HWPRESENT)) {
3808 /* no uwb hardware present in system */
3809 tp_features.uwb = 0;
3810 dbg_printk(TPACPI_DBG_INIT,
3811 "uwb hardware not installed\n");
3812 }
3813
3814 if (!tp_features.uwb)
3815 return 1;
3816
3817 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
3818 &tpacpi_uwb_rfkill,
3819 RFKILL_TYPE_UWB,
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003820 TPACPI_RFK_UWB_SW_NAME,
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02003821 false,
3822 tpacpi_uwb_rfk_set,
3823 tpacpi_uwb_rfk_get);
3824
3825 return res;
3826}
3827
3828static struct ibm_struct uwb_driver_data = {
3829 .name = "uwb",
3830 .exit = uwb_exit,
3831 .flags.experimental = 1,
3832};
3833
3834/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003835 * Video subdriver
3836 */
3837
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02003838#ifdef CONFIG_THINKPAD_ACPI_VIDEO
3839
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003840enum video_access_mode {
3841 TPACPI_VIDEO_NONE = 0,
3842 TPACPI_VIDEO_570, /* 570 */
3843 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
3844 TPACPI_VIDEO_NEW, /* all others */
3845};
3846
3847enum { /* video status flags, based on VIDEO_570 */
3848 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
3849 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
3850 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
3851};
3852
3853enum { /* TPACPI_VIDEO_570 constants */
3854 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
3855 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
3856 * video_status_flags */
3857 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
3858 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
3859};
3860
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02003861static enum video_access_mode video_supported;
3862static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003863
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003864static int video_autosw_get(void);
3865static int video_autosw_set(int enable);
3866
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003867TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003868
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003869static int __init video_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003871 int ivga;
3872
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003873 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
3874
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003875 TPACPI_ACPIHANDLE_INIT(vid);
3876 TPACPI_ACPIHANDLE_INIT(vid2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003877
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003878 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
3879 /* G41, assume IVGA doesn't change */
3880 vid_handle = vid2_handle;
3881
3882 if (!vid_handle)
3883 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003884 video_supported = TPACPI_VIDEO_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003885 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
3886 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003887 video_supported = TPACPI_VIDEO_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003888 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
3889 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003890 video_supported = TPACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003891 else
3892 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003893 video_supported = TPACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003894
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003895 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
3896 str_supported(video_supported != TPACPI_VIDEO_NONE),
3897 video_supported);
3898
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003899 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900}
3901
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003902static void video_exit(void)
3903{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003904 dbg_printk(TPACPI_DBG_EXIT,
3905 "restoring original video autoswitch mode\n");
3906 if (video_autosw_set(video_orig_autosw))
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003907 printk(TPACPI_ERR "error while trying to restore original "
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003908 "video autoswitch mode\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003909}
3910
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003911static int video_outputsw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912{
3913 int status = 0;
3914 int i;
3915
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003916 switch (video_supported) {
3917 case TPACPI_VIDEO_570:
3918 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
3919 TP_ACPI_VIDEO_570_PHSCMD))
3920 return -EIO;
3921 status = i & TP_ACPI_VIDEO_570_PHSMASK;
3922 break;
3923 case TPACPI_VIDEO_770:
3924 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
3925 return -EIO;
3926 if (i)
3927 status |= TP_ACPI_VIDEO_S_LCD;
3928 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
3929 return -EIO;
3930 if (i)
3931 status |= TP_ACPI_VIDEO_S_CRT;
3932 break;
3933 case TPACPI_VIDEO_NEW:
3934 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
3935 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
3936 return -EIO;
3937 if (i)
3938 status |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003940 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
3941 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
3942 return -EIO;
3943 if (i)
3944 status |= TP_ACPI_VIDEO_S_LCD;
3945 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
3946 return -EIO;
3947 if (i)
3948 status |= TP_ACPI_VIDEO_S_DVI;
3949 break;
3950 default:
3951 return -ENOSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003952 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953
3954 return status;
3955}
3956
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003957static int video_outputsw_set(int status)
3958{
3959 int autosw;
3960 int res = 0;
3961
3962 switch (video_supported) {
3963 case TPACPI_VIDEO_570:
3964 res = acpi_evalf(NULL, NULL,
3965 "\\_SB.PHS2", "vdd",
3966 TP_ACPI_VIDEO_570_PHS2CMD,
3967 status | TP_ACPI_VIDEO_570_PHS2SET);
3968 break;
3969 case TPACPI_VIDEO_770:
3970 autosw = video_autosw_get();
3971 if (autosw < 0)
3972 return autosw;
3973
3974 res = video_autosw_set(1);
3975 if (res)
3976 return res;
3977 res = acpi_evalf(vid_handle, NULL,
3978 "ASWT", "vdd", status * 0x100, 0);
3979 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003980 printk(TPACPI_ERR
3981 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003982 return -EIO;
3983 }
3984 break;
3985 case TPACPI_VIDEO_NEW:
3986 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003987 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003988 break;
3989 default:
3990 return -ENOSYS;
3991 }
3992
3993 return (res)? 0 : -EIO;
3994}
3995
3996static int video_autosw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003998 int autosw = 0;
3999
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004000 switch (video_supported) {
4001 case TPACPI_VIDEO_570:
4002 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
4003 return -EIO;
4004 break;
4005 case TPACPI_VIDEO_770:
4006 case TPACPI_VIDEO_NEW:
4007 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4008 return -EIO;
4009 break;
4010 default:
4011 return -ENOSYS;
4012 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004013
4014 return autosw & 1;
4015}
4016
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004017static int video_autosw_set(int enable)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004018{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004019 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004020 return -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004021 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004022}
4023
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004024static int video_outputsw_cycle(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004025{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004026 int autosw = video_autosw_get();
4027 int res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004028
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004029 if (autosw < 0)
4030 return autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004031
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004032 switch (video_supported) {
4033 case TPACPI_VIDEO_570:
4034 res = video_autosw_set(1);
4035 if (res)
4036 return res;
4037 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4038 break;
4039 case TPACPI_VIDEO_770:
4040 case TPACPI_VIDEO_NEW:
4041 res = video_autosw_set(1);
4042 if (res)
4043 return res;
4044 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4045 break;
4046 default:
4047 return -ENOSYS;
4048 }
4049 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004050 printk(TPACPI_ERR
4051 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004052 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004053 }
4054
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004055 return (res)? 0 : -EIO;
4056}
4057
4058static int video_expand_toggle(void)
4059{
4060 switch (video_supported) {
4061 case TPACPI_VIDEO_570:
4062 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
4063 0 : -EIO;
4064 case TPACPI_VIDEO_770:
4065 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
4066 0 : -EIO;
4067 case TPACPI_VIDEO_NEW:
4068 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
4069 0 : -EIO;
4070 default:
4071 return -ENOSYS;
4072 }
4073 /* not reached */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004074}
4075
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004076static int video_read(char *p)
4077{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004078 int status, autosw;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004079 int len = 0;
4080
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004081 if (video_supported == TPACPI_VIDEO_NONE) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004082 len += sprintf(p + len, "status:\t\tnot supported\n");
4083 return len;
4084 }
4085
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004086 status = video_outputsw_get();
4087 if (status < 0)
4088 return status;
4089
4090 autosw = video_autosw_get();
4091 if (autosw < 0)
4092 return autosw;
4093
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004094 len += sprintf(p + len, "status:\t\tsupported\n");
4095 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
4096 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004097 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004098 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
4099 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
4100 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
4101 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004102 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004103 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
4104 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
4105 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
4106
4107 return len;
4108}
4109
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004110static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111{
4112 char *cmd;
4113 int enable, disable, status;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004114 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004116 if (video_supported == TPACPI_VIDEO_NONE)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004117 return -ENODEV;
4118
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004119 enable = 0;
4120 disable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121
4122 while ((cmd = next_cmd(&buf))) {
4123 if (strlencmp(cmd, "lcd_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004124 enable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125 } else if (strlencmp(cmd, "lcd_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004126 disable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127 } else if (strlencmp(cmd, "crt_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004128 enable |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129 } else if (strlencmp(cmd, "crt_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004130 disable |= TP_ACPI_VIDEO_S_CRT;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004131 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004132 strlencmp(cmd, "dvi_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004133 enable |= TP_ACPI_VIDEO_S_DVI;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004134 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004135 strlencmp(cmd, "dvi_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004136 disable |= TP_ACPI_VIDEO_S_DVI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137 } else if (strlencmp(cmd, "auto_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004138 res = video_autosw_set(1);
4139 if (res)
4140 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141 } else if (strlencmp(cmd, "auto_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004142 res = video_autosw_set(0);
4143 if (res)
4144 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145 } else if (strlencmp(cmd, "video_switch") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004146 res = video_outputsw_cycle();
4147 if (res)
4148 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004150 res = video_expand_toggle();
4151 if (res)
4152 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153 } else
4154 return -EINVAL;
4155 }
4156
4157 if (enable || disable) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004158 status = video_outputsw_get();
4159 if (status < 0)
4160 return status;
4161 res = video_outputsw_set((status & ~disable) | enable);
4162 if (res)
4163 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164 }
4165
4166 return 0;
4167}
4168
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004169static struct ibm_struct video_driver_data = {
4170 .name = "video",
4171 .read = video_read,
4172 .write = video_write,
4173 .exit = video_exit,
4174};
4175
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02004176#endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4177
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004178/*************************************************************************
4179 * Light (thinklight) subdriver
4180 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004182TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4183TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004184
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004185static int light_get_status(void)
4186{
4187 int status = 0;
4188
4189 if (tp_features.light_status) {
4190 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4191 return -EIO;
4192 return (!!status);
4193 }
4194
4195 return -ENXIO;
4196}
4197
4198static int light_set_status(int status)
4199{
4200 int rc;
4201
4202 if (tp_features.light) {
4203 if (cmos_handle) {
4204 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4205 (status)?
4206 TP_CMOS_THINKLIGHT_ON :
4207 TP_CMOS_THINKLIGHT_OFF);
4208 } else {
4209 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4210 (status)? 1 : 0);
4211 }
4212 return (rc)? 0 : -EIO;
4213 }
4214
4215 return -ENXIO;
4216}
4217
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004218static void light_set_status_worker(struct work_struct *work)
4219{
4220 struct tpacpi_led_classdev *data =
4221 container_of(work, struct tpacpi_led_classdev, work);
4222
4223 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00004224 light_set_status((data->new_state != TPACPI_LED_OFF));
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004225}
4226
4227static void light_sysfs_set(struct led_classdev *led_cdev,
4228 enum led_brightness brightness)
4229{
4230 struct tpacpi_led_classdev *data =
4231 container_of(led_cdev,
4232 struct tpacpi_led_classdev,
4233 led_classdev);
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00004234 data->new_state = (brightness != LED_OFF) ?
4235 TPACPI_LED_ON : TPACPI_LED_OFF;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004236 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004237}
4238
4239static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4240{
4241 return (light_get_status() == 1)? LED_FULL : LED_OFF;
4242}
4243
4244static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4245 .led_classdev = {
4246 .name = "tpacpi::thinklight",
4247 .brightness_set = &light_sysfs_set,
4248 .brightness_get = &light_sysfs_get,
4249 }
4250};
4251
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004252static int __init light_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253{
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004254 int rc;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004255
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004256 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4257
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004258 TPACPI_ACPIHANDLE_INIT(ledb);
4259 TPACPI_ACPIHANDLE_INIT(lght);
4260 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004261 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004262
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004263 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004264 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004265
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004266 if (tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004267 /* light status not supported on
4268 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004269 tp_features.light_status =
4270 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004272 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4273 str_supported(tp_features.light),
4274 str_supported(tp_features.light_status));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004275
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004276 if (!tp_features.light)
4277 return 1;
4278
4279 rc = led_classdev_register(&tpacpi_pdev->dev,
4280 &tpacpi_led_thinklight.led_classdev);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004281
4282 if (rc < 0) {
4283 tp_features.light = 0;
4284 tp_features.light_status = 0;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004285 } else {
4286 rc = 0;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004287 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004288
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004289 return rc;
4290}
4291
4292static void light_exit(void)
4293{
4294 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
4295 if (work_pending(&tpacpi_led_thinklight.work))
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004296 flush_workqueue(tpacpi_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297}
4298
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004299static int light_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300{
4301 int len = 0;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004302 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004304 if (!tp_features.light) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004305 len += sprintf(p + len, "status:\t\tnot supported\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004306 } else if (!tp_features.light_status) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004307 len += sprintf(p + len, "status:\t\tunknown\n");
4308 len += sprintf(p + len, "commands:\ton, off\n");
4309 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004310 status = light_get_status();
4311 if (status < 0)
4312 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004314 len += sprintf(p + len, "commands:\ton, off\n");
4315 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316
4317 return len;
4318}
4319
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004320static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004323 int newstatus = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004324
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004325 if (!tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004326 return -ENODEV;
4327
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328 while ((cmd = next_cmd(&buf))) {
4329 if (strlencmp(cmd, "on") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004330 newstatus = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331 } else if (strlencmp(cmd, "off") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004332 newstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333 } else
4334 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335 }
4336
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004337 return light_set_status(newstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338}
4339
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004340static struct ibm_struct light_driver_data = {
4341 .name = "light",
4342 .read = light_read,
4343 .write = light_write,
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004344 .exit = light_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004345};
4346
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004347/*************************************************************************
4348 * Dock subdriver
4349 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03004351#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004352
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004353static void dock_notify(struct ibm_struct *ibm, u32 event);
4354static int dock_read(char *p);
4355static int dock_write(char *buf);
4356
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004357TPACPI_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004358 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
4359 "\\_SB.PCI0.PCI1.DOCK", /* all others */
4360 "\\_SB.PCI.ISA.SLCE", /* 570 */
4361 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
4362
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004363/* don't list other alternatives as we install a notify handler on the 570 */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004364TPACPI_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004365
Thomas Renninger1ba90e32007-07-23 14:44:41 +02004366static const struct acpi_device_id ibm_pci_device_ids[] = {
4367 {PCI_ROOT_HID_STRING, 0},
4368 {"", 0},
4369};
4370
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03004371static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
4372 {
4373 .notify = dock_notify,
4374 .handle = &dock_handle,
4375 .type = ACPI_SYSTEM_NOTIFY,
4376 },
4377 {
Henrique de Moraes Holschuh996fba02007-07-18 23:45:40 -03004378 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
4379 * We just use it to get notifications of dock hotplug
4380 * in very old thinkpads */
Thomas Renninger1ba90e32007-07-23 14:44:41 +02004381 .hid = ibm_pci_device_ids,
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03004382 .notify = dock_notify,
4383 .handle = &pci_handle,
4384 .type = ACPI_SYSTEM_NOTIFY,
4385 },
4386};
4387
4388static struct ibm_struct dock_driver_data[2] = {
4389 {
4390 .name = "dock",
4391 .read = dock_read,
4392 .write = dock_write,
4393 .acpi = &ibm_dock_acpidriver[0],
4394 },
4395 {
4396 .name = "dock",
4397 .acpi = &ibm_dock_acpidriver[1],
4398 },
4399};
4400
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401#define dock_docked() (_sta(dock_handle) & 1)
4402
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004403static int __init dock_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004404{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004405 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
4406
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004407 TPACPI_ACPIHANDLE_INIT(dock);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004408
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004409 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
4410 str_supported(dock_handle != NULL));
4411
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004412 return (dock_handle)? 0 : 1;
4413}
4414
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03004415static int __init dock_init2(struct ibm_init_struct *iibm)
4416{
4417 int dock2_needed;
4418
4419 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
4420
4421 if (dock_driver_data[0].flags.acpi_driver_registered &&
4422 dock_driver_data[0].flags.acpi_notify_installed) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004423 TPACPI_ACPIHANDLE_INIT(pci);
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03004424 dock2_needed = (pci_handle != NULL);
4425 vdbg_printk(TPACPI_DBG_INIT,
4426 "dock PCI handler for the TP 570 is %s\n",
4427 str_supported(dock2_needed));
4428 } else {
4429 vdbg_printk(TPACPI_DBG_INIT,
4430 "dock subdriver part 2 not required\n");
4431 dock2_needed = 0;
4432 }
4433
4434 return (dock2_needed)? 0 : 1;
4435}
4436
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004437static void dock_notify(struct ibm_struct *ibm, u32 event)
4438{
4439 int docked = dock_docked();
Thomas Renninger1ba90e32007-07-23 14:44:41 +02004440 int pci = ibm->acpi->hid && ibm->acpi->device &&
4441 acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
Zhang Rui962ce8c2007-08-23 01:24:31 +08004442 int data;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004443
4444 if (event == 1 && !pci) /* 570 */
Zhang Rui962ce8c2007-08-23 01:24:31 +08004445 data = 1; /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004446 else if (event == 1 && pci) /* 570 */
Zhang Rui962ce8c2007-08-23 01:24:31 +08004447 data = 3; /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004448 else if (event == 3 && docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08004449 data = 1; /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004450 else if (event == 3 && !docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08004451 data = 2; /* undock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004452 else if (event == 0 && docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08004453 data = 3; /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004454 else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004455 printk(TPACPI_ERR "unknown dock event %d, status %d\n",
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004456 event, _sta(dock_handle));
Zhang Rui962ce8c2007-08-23 01:24:31 +08004457 data = 0; /* unknown */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004458 }
Len Brown14e04fb32007-08-23 15:20:26 -04004459 acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
Zhang Rui962ce8c2007-08-23 01:24:31 +08004460 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02004461 dev_name(&ibm->acpi->device->dev),
Zhang Rui962ce8c2007-08-23 01:24:31 +08004462 event, data);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004463}
4464
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004465static int dock_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466{
4467 int len = 0;
4468 int docked = dock_docked();
4469
4470 if (!dock_handle)
4471 len += sprintf(p + len, "status:\t\tnot supported\n");
4472 else if (!docked)
4473 len += sprintf(p + len, "status:\t\tundocked\n");
4474 else {
4475 len += sprintf(p + len, "status:\t\tdocked\n");
4476 len += sprintf(p + len, "commands:\tdock, undock\n");
4477 }
4478
4479 return len;
4480}
4481
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004482static int dock_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483{
4484 char *cmd;
4485
4486 if (!dock_docked())
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004487 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488
4489 while ((cmd = next_cmd(&buf))) {
4490 if (strlencmp(cmd, "undock") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004491 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
4492 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493 return -EIO;
4494 } else if (strlencmp(cmd, "dock") == 0) {
4495 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
4496 return -EIO;
4497 } else
4498 return -EINVAL;
4499 }
4500
4501 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004502}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03004504#endif /* CONFIG_THINKPAD_ACPI_DOCK */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004506/*************************************************************************
4507 * Bay subdriver
4508 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004509
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03004510#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004511
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004512TPACPI_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004513 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
4514 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
4515 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
4516 ); /* A21e, R30, R31 */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004517TPACPI_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004518 "_EJ0", /* all others */
4519 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004520TPACPI_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004521 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
4522 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004523TPACPI_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004524 "_EJ0", /* 770x */
4525 ); /* all others */
4526
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004527static int __init bay_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004529 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
4530
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004531 TPACPI_ACPIHANDLE_INIT(bay);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004532 if (bay_handle)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004533 TPACPI_ACPIHANDLE_INIT(bay_ej);
4534 TPACPI_ACPIHANDLE_INIT(bay2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004535 if (bay2_handle)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004536 TPACPI_ACPIHANDLE_INIT(bay2_ej);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004537
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004538 tp_features.bay_status = bay_handle &&
4539 acpi_evalf(bay_handle, NULL, "_STA", "qv");
4540 tp_features.bay_status2 = bay2_handle &&
4541 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004542
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004543 tp_features.bay_eject = bay_handle && bay_ej_handle &&
4544 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
4545 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
4546 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004548 vdbg_printk(TPACPI_DBG_INIT,
4549 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004550 str_supported(tp_features.bay_status),
4551 str_supported(tp_features.bay_eject),
4552 str_supported(tp_features.bay_status2),
4553 str_supported(tp_features.bay_eject2));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004554
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004555 return (tp_features.bay_status || tp_features.bay_eject ||
4556 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557}
4558
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004559static void bay_notify(struct ibm_struct *ibm, u32 event)
4560{
Len Brown14e04fb32007-08-23 15:20:26 -04004561 acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
Zhang Rui962ce8c2007-08-23 01:24:31 +08004562 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02004563 dev_name(&ibm->acpi->device->dev),
Zhang Rui962ce8c2007-08-23 01:24:31 +08004564 event, 0);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004565}
4566
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004567#define bay_occupied(b) (_sta(b##_handle) & 1)
4568
4569static int bay_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570{
4571 int len = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004572 int occupied = bay_occupied(bay);
4573 int occupied2 = bay_occupied(bay2);
4574 int eject, eject2;
4575
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004576 len += sprintf(p + len, "status:\t\t%s\n",
4577 tp_features.bay_status ?
4578 (occupied ? "occupied" : "unoccupied") :
4579 "not supported");
4580 if (tp_features.bay_status2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004581 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
4582 "occupied" : "unoccupied");
4583
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004584 eject = tp_features.bay_eject && occupied;
4585 eject2 = tp_features.bay_eject2 && occupied2;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004586
4587 if (eject && eject2)
4588 len += sprintf(p + len, "commands:\teject, eject2\n");
4589 else if (eject)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590 len += sprintf(p + len, "commands:\teject\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004591 else if (eject2)
4592 len += sprintf(p + len, "commands:\teject2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593
4594 return len;
4595}
4596
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004597static int bay_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598{
4599 char *cmd;
4600
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004601 if (!tp_features.bay_eject && !tp_features.bay_eject2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004602 return -ENODEV;
4603
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004605 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004606 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
4607 return -EIO;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004608 } else if (tp_features.bay_eject2 &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004609 strlencmp(cmd, "eject2") == 0) {
4610 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611 return -EIO;
4612 } else
4613 return -EINVAL;
4614 }
4615
4616 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004617}
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004618
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004619static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
4620 .notify = bay_notify,
4621 .handle = &bay_handle,
4622 .type = ACPI_SYSTEM_NOTIFY,
4623};
4624
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004625static struct ibm_struct bay_driver_data = {
4626 .name = "bay",
4627 .read = bay_read,
4628 .write = bay_write,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004629 .acpi = &ibm_bay_acpidriver,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004630};
4631
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03004632#endif /* CONFIG_THINKPAD_ACPI_BAY */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004634/*************************************************************************
4635 * CMOS subdriver
4636 */
4637
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004638/* sysfs cmos_command -------------------------------------------------- */
4639static ssize_t cmos_command_store(struct device *dev,
4640 struct device_attribute *attr,
4641 const char *buf, size_t count)
4642{
4643 unsigned long cmos_cmd;
4644 int res;
4645
4646 if (parse_strtoul(buf, 21, &cmos_cmd))
4647 return -EINVAL;
4648
4649 res = issue_thinkpad_cmos_command(cmos_cmd);
4650 return (res)? res : count;
4651}
4652
4653static struct device_attribute dev_attr_cmos_command =
4654 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4655
4656/* --------------------------------------------------------------------- */
4657
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004658static int __init cmos_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004659{
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004660 int res;
4661
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004662 vdbg_printk(TPACPI_DBG_INIT,
4663 "initializing cmos commands subdriver\n");
4664
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004665 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004666
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004667 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4668 str_supported(cmos_handle != NULL));
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004669
4670 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4671 if (res)
4672 return res;
4673
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004674 return (cmos_handle)? 0 : 1;
4675}
4676
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004677static void cmos_exit(void)
4678{
4679 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4680}
4681
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004682static int cmos_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683{
4684 int len = 0;
4685
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004686 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4687 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688 if (!cmos_handle)
4689 len += sprintf(p + len, "status:\t\tnot supported\n");
4690 else {
4691 len += sprintf(p + len, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004692 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004693 }
4694
4695 return len;
4696}
4697
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004698static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004699{
4700 char *cmd;
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004701 int cmos_cmd, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004702
4703 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004704 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
4705 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706 /* cmos_cmd set */
4707 } else
4708 return -EINVAL;
4709
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004710 res = issue_thinkpad_cmos_command(cmos_cmd);
4711 if (res)
4712 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004713 }
4714
4715 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004716}
4717
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004718static struct ibm_struct cmos_driver_data = {
4719 .name = "cmos",
4720 .read = cmos_read,
4721 .write = cmos_write,
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004722 .exit = cmos_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004723};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004724
4725/*************************************************************************
4726 * LED subdriver
4727 */
4728
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004729enum led_access_mode {
4730 TPACPI_LED_NONE = 0,
4731 TPACPI_LED_570, /* 570 */
4732 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4733 TPACPI_LED_NEW, /* all others */
4734};
4735
4736enum { /* For TPACPI_LED_OLD */
4737 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
4738 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
4739 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
4740};
4741
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02004742static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004743
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004744TPACPI_HANDLE(led, ec, "SLED", /* 570 */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004745 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
4746 /* T20-22, X20-21 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004747 "LED", /* all others */
4748 ); /* R30, R31 */
4749
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004750#define TPACPI_LED_NUMLEDS 8
4751static struct tpacpi_led_classdev *tpacpi_leds;
4752static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
Harvey Harrisone3aa51f2008-05-29 17:51:57 -07004753static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004754 /* there's a limit of 19 chars + NULL before 2.6.26 */
4755 "tpacpi::power",
4756 "tpacpi:orange:batt",
4757 "tpacpi:green:batt",
4758 "tpacpi::dock_active",
4759 "tpacpi::bay_active",
4760 "tpacpi::dock_batt",
4761 "tpacpi::unknown_led",
4762 "tpacpi::standby",
4763};
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004764#define TPACPI_SAFE_LEDS 0x0081U
4765
4766static inline bool tpacpi_is_led_restricted(const unsigned int led)
4767{
4768#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
4769 return false;
4770#else
4771 return (TPACPI_SAFE_LEDS & (1 << led)) == 0;
4772#endif
4773}
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004774
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004775static int led_get_status(const unsigned int led)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004776{
4777 int status;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004778 enum led_status_t led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004779
4780 switch (led_supported) {
4781 case TPACPI_LED_570:
4782 if (!acpi_evalf(ec_handle,
4783 &status, "GLED", "dd", 1 << led))
4784 return -EIO;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004785 led_s = (status == 0)?
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004786 TPACPI_LED_OFF :
4787 ((status == 1)?
4788 TPACPI_LED_ON :
4789 TPACPI_LED_BLINK);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004790 tpacpi_led_state_cache[led] = led_s;
4791 return led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004792 default:
4793 return -ENXIO;
4794 }
4795
4796 /* not reached */
4797}
4798
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004799static int led_set_status(const unsigned int led,
4800 const enum led_status_t ledstatus)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004801{
4802 /* off, on, blink. Index is led_status_t */
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004803 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
4804 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004805
4806 int rc = 0;
4807
4808 switch (led_supported) {
4809 case TPACPI_LED_570:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004810 /* 570 */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004811 if (unlikely(led > 7))
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004812 return -EINVAL;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004813 if (unlikely(tpacpi_is_led_restricted(led)))
4814 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004815 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4816 (1 << led), led_sled_arg1[ledstatus]))
4817 rc = -EIO;
4818 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004819 case TPACPI_LED_OLD:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004820 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004821 if (unlikely(led > 7))
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004822 return -EINVAL;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004823 if (unlikely(tpacpi_is_led_restricted(led)))
4824 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004825 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
4826 if (rc >= 0)
4827 rc = ec_write(TPACPI_LED_EC_HLBL,
4828 (ledstatus == TPACPI_LED_BLINK) << led);
4829 if (rc >= 0)
4830 rc = ec_write(TPACPI_LED_EC_HLCL,
4831 (ledstatus != TPACPI_LED_OFF) << led);
4832 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004833 case TPACPI_LED_NEW:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004834 /* all others */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004835 if (unlikely(led >= TPACPI_LED_NUMLEDS))
4836 return -EINVAL;
4837 if (unlikely(tpacpi_is_led_restricted(led)))
4838 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004839 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4840 led, led_led_arg1[ledstatus]))
4841 rc = -EIO;
4842 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004843 default:
4844 rc = -ENXIO;
4845 }
4846
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004847 if (!rc)
4848 tpacpi_led_state_cache[led] = ledstatus;
4849
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004850 return rc;
4851}
4852
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004853static void led_set_status_worker(struct work_struct *work)
4854{
4855 struct tpacpi_led_classdev *data =
4856 container_of(work, struct tpacpi_led_classdev, work);
4857
4858 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00004859 led_set_status(data->led, data->new_state);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004860}
4861
4862static void led_sysfs_set(struct led_classdev *led_cdev,
4863 enum led_brightness brightness)
4864{
4865 struct tpacpi_led_classdev *data = container_of(led_cdev,
4866 struct tpacpi_led_classdev, led_classdev);
4867
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00004868 if (brightness == LED_OFF)
4869 data->new_state = TPACPI_LED_OFF;
4870 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
4871 data->new_state = TPACPI_LED_ON;
4872 else
4873 data->new_state = TPACPI_LED_BLINK;
4874
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004875 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004876}
4877
4878static int led_sysfs_blink_set(struct led_classdev *led_cdev,
4879 unsigned long *delay_on, unsigned long *delay_off)
4880{
4881 struct tpacpi_led_classdev *data = container_of(led_cdev,
4882 struct tpacpi_led_classdev, led_classdev);
4883
4884 /* Can we choose the flash rate? */
4885 if (*delay_on == 0 && *delay_off == 0) {
4886 /* yes. set them to the hardware blink rate (1 Hz) */
4887 *delay_on = 500; /* ms */
4888 *delay_off = 500; /* ms */
4889 } else if ((*delay_on != 500) || (*delay_off != 500))
4890 return -EINVAL;
4891
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00004892 data->new_state = TPACPI_LED_BLINK;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004893 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004894
4895 return 0;
4896}
4897
4898static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
4899{
4900 int rc;
4901
4902 struct tpacpi_led_classdev *data = container_of(led_cdev,
4903 struct tpacpi_led_classdev, led_classdev);
4904
4905 rc = led_get_status(data->led);
4906
4907 if (rc == TPACPI_LED_OFF || rc < 0)
4908 rc = LED_OFF; /* no error handling in led class :( */
4909 else
4910 rc = LED_FULL;
4911
4912 return rc;
4913}
4914
4915static void led_exit(void)
4916{
4917 unsigned int i;
4918
4919 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4920 if (tpacpi_leds[i].led_classdev.name)
4921 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
4922 }
4923
4924 kfree(tpacpi_leds);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004925}
4926
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004927static int __init tpacpi_init_led(unsigned int led)
4928{
4929 int rc;
4930
4931 tpacpi_leds[led].led = led;
4932
4933 tpacpi_leds[led].led_classdev.brightness_set = &led_sysfs_set;
4934 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
4935 if (led_supported == TPACPI_LED_570)
4936 tpacpi_leds[led].led_classdev.brightness_get =
4937 &led_sysfs_get;
4938
4939 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
4940
4941 INIT_WORK(&tpacpi_leds[led].work, led_set_status_worker);
4942
4943 rc = led_classdev_register(&tpacpi_pdev->dev,
4944 &tpacpi_leds[led].led_classdev);
4945 if (rc < 0)
4946 tpacpi_leds[led].led_classdev.name = NULL;
4947
4948 return rc;
4949}
4950
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004951static int __init led_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004952{
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004953 unsigned int i;
4954 int rc;
4955
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004956 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
4957
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004958 TPACPI_ACPIHANDLE_INIT(led);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004959
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004960 if (!led_handle)
4961 /* led not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004962 led_supported = TPACPI_LED_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004963 else if (strlencmp(led_path, "SLED") == 0)
4964 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004965 led_supported = TPACPI_LED_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004966 else if (strlencmp(led_path, "SYSL") == 0)
4967 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004968 led_supported = TPACPI_LED_OLD;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004969 else
4970 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004971 led_supported = TPACPI_LED_NEW;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004972
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004973 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
4974 str_supported(led_supported), led_supported);
4975
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004976 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
4977 GFP_KERNEL);
4978 if (!tpacpi_leds) {
4979 printk(TPACPI_ERR "Out of memory for LED data\n");
4980 return -ENOMEM;
4981 }
4982
4983 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004984 if (!tpacpi_is_led_restricted(i)) {
4985 rc = tpacpi_init_led(i);
4986 if (rc < 0) {
4987 led_exit();
4988 return rc;
4989 }
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004990 }
4991 }
4992
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004993#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
4994 if (led_supported != TPACPI_LED_NONE)
4995 printk(TPACPI_NOTICE
4996 "warning: userspace override of important "
4997 "firmware LEDs is enabled\n");
4998#endif
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004999 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005000}
5001
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005002#define str_led_status(s) \
5003 ((s) == TPACPI_LED_OFF ? "off" : \
5004 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005005
5006static int led_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005007{
5008 int len = 0;
5009
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005010 if (!led_supported) {
5011 len += sprintf(p + len, "status:\t\tnot supported\n");
5012 return len;
5013 }
5014 len += sprintf(p + len, "status:\t\tsupported\n");
5015
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005016 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005017 /* 570 */
5018 int i, status;
5019 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005020 status = led_get_status(i);
5021 if (status < 0)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005022 return -EIO;
5023 len += sprintf(p + len, "%d:\t\t%s\n",
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005024 i, str_led_status(status));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005025 }
5026 }
5027
Linus Torvalds1da177e2005-04-16 15:20:36 -07005028 len += sprintf(p + len, "commands:\t"
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005029 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005030
5031 return len;
5032}
5033
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005034static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005035{
5036 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005037 int led, rc;
5038 enum led_status_t s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005039
5040 if (!led_supported)
5041 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005042
5043 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005044 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005045 return -EINVAL;
5046
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005047 if (strstr(cmd, "off")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005048 s = TPACPI_LED_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005049 } else if (strstr(cmd, "on")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005050 s = TPACPI_LED_ON;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005051 } else if (strstr(cmd, "blink")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005052 s = TPACPI_LED_BLINK;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005053 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005054 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005055 }
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005056
5057 rc = led_set_status(led, s);
5058 if (rc < 0)
5059 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005060 }
5061
5062 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005063}
5064
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005065static struct ibm_struct led_driver_data = {
5066 .name = "led",
5067 .read = led_read,
5068 .write = led_write,
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005069 .exit = led_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005070};
5071
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005072/*************************************************************************
5073 * Beep subdriver
5074 */
5075
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005076TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005077
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005078static int __init beep_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005079{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005080 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
5081
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005082 TPACPI_ACPIHANDLE_INIT(beep);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005083
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005084 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
5085 str_supported(beep_handle != NULL));
5086
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005087 return (beep_handle)? 0 : 1;
5088}
5089
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005090static int beep_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005091{
5092 int len = 0;
5093
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005094 if (!beep_handle)
5095 len += sprintf(p + len, "status:\t\tnot supported\n");
5096 else {
5097 len += sprintf(p + len, "status:\t\tsupported\n");
5098 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
5099 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005100
5101 return len;
5102}
5103
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005104static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005105{
5106 char *cmd;
5107 int beep_cmd;
5108
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005109 if (!beep_handle)
5110 return -ENODEV;
5111
Linus Torvalds1da177e2005-04-16 15:20:36 -07005112 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005113 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
5114 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005115 /* beep_cmd set */
5116 } else
5117 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005118 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005119 return -EIO;
5120 }
5121
5122 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005123}
5124
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005125static struct ibm_struct beep_driver_data = {
5126 .name = "beep",
5127 .read = beep_read,
5128 .write = beep_write,
5129};
5130
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005131/*************************************************************************
5132 * Thermal subdriver
5133 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005134
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005135enum thermal_access_mode {
5136 TPACPI_THERMAL_NONE = 0, /* No thermal support */
5137 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
5138 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
5139 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
5140 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
5141};
5142
5143enum { /* TPACPI_THERMAL_TPEC_* */
5144 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
5145 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
5146 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
5147};
5148
5149#define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5150struct ibm_thermal_sensors_struct {
5151 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
5152};
5153
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005154static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005155
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005156/* idx is zero-based */
5157static int thermal_get_sensor(int idx, s32 *value)
5158{
5159 int t;
5160 s8 tmp;
5161 char tmpi[5];
5162
5163 t = TP_EC_THERMAL_TMP0;
5164
5165 switch (thermal_read_mode) {
5166#if TPACPI_MAX_THERMAL_SENSORS >= 16
5167 case TPACPI_THERMAL_TPEC_16:
5168 if (idx >= 8 && idx <= 15) {
5169 t = TP_EC_THERMAL_TMP8;
5170 idx -= 8;
5171 }
5172 /* fallthrough */
5173#endif
5174 case TPACPI_THERMAL_TPEC_8:
5175 if (idx <= 7) {
5176 if (!acpi_ec_read(t + idx, &tmp))
5177 return -EIO;
5178 *value = tmp * 1000;
5179 return 0;
5180 }
5181 break;
5182
5183 case TPACPI_THERMAL_ACPI_UPDT:
5184 if (idx <= 7) {
5185 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5186 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
5187 return -EIO;
5188 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5189 return -EIO;
5190 *value = (t - 2732) * 100;
5191 return 0;
5192 }
5193 break;
5194
5195 case TPACPI_THERMAL_ACPI_TMP07:
5196 if (idx <= 7) {
5197 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5198 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5199 return -EIO;
5200 if (t > 127 || t < -127)
5201 t = TP_EC_THERMAL_TMP_NA;
5202 *value = t * 1000;
5203 return 0;
5204 }
5205 break;
5206
5207 case TPACPI_THERMAL_NONE:
5208 default:
5209 return -ENOSYS;
5210 }
5211
5212 return -EINVAL;
5213}
5214
5215static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5216{
5217 int res, i;
5218 int n;
5219
5220 n = 8;
5221 i = 0;
5222
5223 if (!s)
5224 return -EINVAL;
5225
5226 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5227 n = 16;
5228
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005229 for (i = 0 ; i < n; i++) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005230 res = thermal_get_sensor(i, &s->temp[i]);
5231 if (res)
5232 return res;
5233 }
5234
5235 return n;
5236}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005237
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005238/* sysfs temp##_input -------------------------------------------------- */
5239
5240static ssize_t thermal_temp_input_show(struct device *dev,
5241 struct device_attribute *attr,
5242 char *buf)
5243{
5244 struct sensor_device_attribute *sensor_attr =
5245 to_sensor_dev_attr(attr);
5246 int idx = sensor_attr->index;
5247 s32 value;
5248 int res;
5249
5250 res = thermal_get_sensor(idx, &value);
5251 if (res)
5252 return res;
5253 if (value == TP_EC_THERMAL_TMP_NA * 1000)
5254 return -ENXIO;
5255
5256 return snprintf(buf, PAGE_SIZE, "%d\n", value);
5257}
5258
5259#define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005260 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5261 thermal_temp_input_show, NULL, _idxB)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005262
5263static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5264 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5265 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5266 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5267 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5268 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5269 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5270 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5271 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5272 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5273 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5274 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5275 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5276 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5277 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5278 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5279 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5280};
5281
5282#define THERMAL_ATTRS(X) \
5283 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5284
5285static struct attribute *thermal_temp_input_attr[] = {
5286 THERMAL_ATTRS(8),
5287 THERMAL_ATTRS(9),
5288 THERMAL_ATTRS(10),
5289 THERMAL_ATTRS(11),
5290 THERMAL_ATTRS(12),
5291 THERMAL_ATTRS(13),
5292 THERMAL_ATTRS(14),
5293 THERMAL_ATTRS(15),
5294 THERMAL_ATTRS(0),
5295 THERMAL_ATTRS(1),
5296 THERMAL_ATTRS(2),
5297 THERMAL_ATTRS(3),
5298 THERMAL_ATTRS(4),
5299 THERMAL_ATTRS(5),
5300 THERMAL_ATTRS(6),
5301 THERMAL_ATTRS(7),
5302 NULL
5303};
5304
5305static const struct attribute_group thermal_temp_input16_group = {
5306 .attrs = thermal_temp_input_attr
5307};
5308
5309static const struct attribute_group thermal_temp_input8_group = {
5310 .attrs = &thermal_temp_input_attr[8]
5311};
5312
5313#undef THERMAL_SENSOR_ATTR_TEMP
5314#undef THERMAL_ATTRS
5315
5316/* --------------------------------------------------------------------- */
5317
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005318static int __init thermal_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005319{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005320 u8 t, ta1, ta2;
5321 int i;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005322 int acpi_tmp7;
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005323 int res;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005324
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005325 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5326
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005327 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005328
Henrique de Moraes Holschuh3d6f99c2007-07-18 23:45:46 -03005329 if (thinkpad_id.ec_model) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005330 /*
5331 * Direct EC access mode: sensors at registers
5332 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5333 * non-implemented, thermal sensors return 0x80 when
5334 * not available
5335 */
5336
5337 ta1 = ta2 = 0;
5338 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03005339 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005340 ta1 |= t;
5341 } else {
5342 ta1 = 0;
5343 break;
5344 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03005345 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005346 ta2 |= t;
5347 } else {
5348 ta1 = 0;
5349 break;
5350 }
5351 }
5352 if (ta1 == 0) {
5353 /* This is sheer paranoia, but we handle it anyway */
5354 if (acpi_tmp7) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005355 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005356 "ThinkPad ACPI EC access misbehaving, "
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005357 "falling back to ACPI TMPx access "
5358 "mode\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005359 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005360 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005361 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005362 "ThinkPad ACPI EC access misbehaving, "
5363 "disabling thermal sensors access\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005364 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005365 }
5366 } else {
5367 thermal_read_mode =
5368 (ta2 != 0) ?
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005369 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005370 }
5371 } else if (acpi_tmp7) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005372 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
5373 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005374 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005375 } else {
5376 /* Standard ACPI TMPx access, max 8 sensors */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005377 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005378 }
5379 } else {
5380 /* temperatures not supported on 570, G4x, R30, R31, R32 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005381 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005382 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005383
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005384 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5385 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5386 thermal_read_mode);
5387
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005388 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005389 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005390 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005391 &thermal_temp_input16_group);
5392 if (res)
5393 return res;
5394 break;
5395 case TPACPI_THERMAL_TPEC_8:
5396 case TPACPI_THERMAL_ACPI_TMP07:
5397 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005398 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005399 &thermal_temp_input8_group);
5400 if (res)
5401 return res;
5402 break;
5403 case TPACPI_THERMAL_NONE:
5404 default:
5405 return 1;
5406 }
5407
5408 return 0;
5409}
5410
5411static void thermal_exit(void)
5412{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005413 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005414 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005415 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005416 &thermal_temp_input16_group);
5417 break;
5418 case TPACPI_THERMAL_TPEC_8:
5419 case TPACPI_THERMAL_ACPI_TMP07:
5420 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005421 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005422 &thermal_temp_input16_group);
5423 break;
5424 case TPACPI_THERMAL_NONE:
5425 default:
5426 break;
5427 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005428}
5429
5430static int thermal_read(char *p)
5431{
5432 int len = 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005433 int n, i;
5434 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005435
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005436 n = thermal_get_sensors(&t);
5437 if (unlikely(n < 0))
5438 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005439
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005440 len += sprintf(p + len, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005441
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005442 if (n > 0) {
5443 for (i = 0; i < (n - 1); i++)
5444 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
5445 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
5446 } else
5447 len += sprintf(p + len, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005448
5449 return len;
5450}
5451
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005452static struct ibm_struct thermal_driver_data = {
5453 .name = "thermal",
5454 .read = thermal_read,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005455 .exit = thermal_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005456};
5457
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005458/*************************************************************************
5459 * EC Dump subdriver
5460 */
5461
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005462static u8 ecdump_regs[256];
5463
5464static int ecdump_read(char *p)
5465{
5466 int len = 0;
5467 int i, j;
5468 u8 v;
5469
5470 len += sprintf(p + len, "EC "
5471 " +00 +01 +02 +03 +04 +05 +06 +07"
5472 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
5473 for (i = 0; i < 256; i += 16) {
5474 len += sprintf(p + len, "EC 0x%02x:", i);
5475 for (j = 0; j < 16; j++) {
5476 if (!acpi_ec_read(i + j, &v))
5477 break;
5478 if (v != ecdump_regs[i + j])
5479 len += sprintf(p + len, " *%02x", v);
5480 else
5481 len += sprintf(p + len, " %02x", v);
5482 ecdump_regs[i + j] = v;
5483 }
5484 len += sprintf(p + len, "\n");
5485 if (j != 16)
5486 break;
5487 }
5488
5489 /* These are way too dangerous to advertise openly... */
5490#if 0
5491 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
5492 " (<offset> is 00-ff, <value> is 00-ff)\n");
5493 len += sprintf(p + len, "commands:\t0x<offset> <value> "
5494 " (<offset> is 00-ff, <value> is 0-255)\n");
5495#endif
5496 return len;
5497}
5498
5499static int ecdump_write(char *buf)
5500{
5501 char *cmd;
5502 int i, v;
5503
5504 while ((cmd = next_cmd(&buf))) {
5505 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
5506 /* i and v set */
5507 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
5508 /* i and v set */
5509 } else
5510 return -EINVAL;
5511 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
5512 if (!acpi_ec_write(i, v))
5513 return -EIO;
5514 } else
5515 return -EINVAL;
5516 }
5517
5518 return 0;
5519}
5520
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005521static struct ibm_struct ecdump_driver_data = {
5522 .name = "ecdump",
5523 .read = ecdump_read,
5524 .write = ecdump_write,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03005525 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005526};
5527
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005528/*************************************************************************
5529 * Backlight/brightness subdriver
5530 */
Holger Macht8acb0252006-10-20 14:30:28 -07005531
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005532#define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5533
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005534/*
5535 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
5536 * CMOS NVRAM byte 0x5E, bits 0-3.
5537 *
5538 * EC HBRV (0x31) has the following layout
5539 * Bit 7: unknown function
5540 * Bit 6: unknown function
5541 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5542 * Bit 4: must be set to zero to avoid problems
5543 * Bit 3-0: backlight brightness level
5544 *
5545 * brightness_get_raw returns status data in the HBRV layout
5546 */
5547
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005548enum {
5549 TP_EC_BACKLIGHT = 0x31,
5550
5551 /* TP_EC_BACKLIGHT bitmasks */
5552 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5553 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5554 TP_EC_BACKLIGHT_MAPSW = 0x20,
5555};
5556
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005557enum tpacpi_brightness_access_mode {
5558 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
5559 TPACPI_BRGHT_MODE_EC, /* EC control */
5560 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
5561 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
5562 TPACPI_BRGHT_MODE_MAX
5563};
5564
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03005565static struct backlight_device *ibm_backlight_device;
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005566
5567static enum tpacpi_brightness_access_mode brightness_mode =
5568 TPACPI_BRGHT_MODE_MAX;
5569
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005570static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5571
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005572static struct mutex brightness_mutex;
5573
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005574/* NVRAM brightness access,
5575 * call with brightness_mutex held! */
5576static unsigned int tpacpi_brightness_nvram_get(void)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005577{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005578 u8 lnvram;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005579
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005580 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5581 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5582 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
5583 lnvram &= (tp_features.bright_16levels) ? 0x0f : 0x07;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005584
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005585 return lnvram;
5586}
5587
5588static void tpacpi_brightness_checkpoint_nvram(void)
5589{
5590 u8 lec = 0;
5591 u8 b_nvram;
5592
5593 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
5594 return;
5595
5596 vdbg_printk(TPACPI_DBG_BRGHT,
5597 "trying to checkpoint backlight level to NVRAM...\n");
5598
5599 if (mutex_lock_killable(&brightness_mutex) < 0)
5600 return;
5601
5602 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5603 goto unlock;
5604 lec &= TP_EC_BACKLIGHT_LVLMSK;
5605 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
5606
5607 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5608 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
5609 /* NVRAM needs update */
5610 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
5611 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
5612 b_nvram |= lec;
5613 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
5614 dbg_printk(TPACPI_DBG_BRGHT,
5615 "updated NVRAM backlight level to %u (0x%02x)\n",
5616 (unsigned int) lec, (unsigned int) b_nvram);
5617 } else
5618 vdbg_printk(TPACPI_DBG_BRGHT,
5619 "NVRAM backlight level already is %u (0x%02x)\n",
5620 (unsigned int) lec, (unsigned int) b_nvram);
5621
5622unlock:
5623 mutex_unlock(&brightness_mutex);
5624}
5625
5626
5627/* call with brightness_mutex held! */
5628static int tpacpi_brightness_get_raw(int *status)
5629{
5630 u8 lec = 0;
5631
5632 switch (brightness_mode) {
5633 case TPACPI_BRGHT_MODE_UCMS_STEP:
5634 *status = tpacpi_brightness_nvram_get();
5635 return 0;
5636 case TPACPI_BRGHT_MODE_EC:
5637 case TPACPI_BRGHT_MODE_ECNVRAM:
5638 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
Henrique de Moraes Holschuh2d5e94d2008-04-26 01:02:20 -03005639 return -EIO;
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005640 *status = lec;
5641 return 0;
5642 default:
5643 return -ENXIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005644 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005645}
5646
5647/* call with brightness_mutex held! */
5648/* do NOT call with illegal backlight level value */
5649static int tpacpi_brightness_set_ec(unsigned int value)
5650{
5651 u8 lec = 0;
5652
5653 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5654 return -EIO;
5655
5656 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
5657 (lec & TP_EC_BACKLIGHT_CMDMSK) |
5658 (value & TP_EC_BACKLIGHT_LVLMSK))))
5659 return -EIO;
5660
5661 return 0;
5662}
5663
5664/* call with brightness_mutex held! */
5665static int tpacpi_brightness_set_ucmsstep(unsigned int value)
5666{
5667 int cmos_cmd, inc;
5668 unsigned int current_value, i;
5669
5670 current_value = tpacpi_brightness_nvram_get();
5671
5672 if (value == current_value)
5673 return 0;
5674
5675 cmos_cmd = (value > current_value) ?
5676 TP_CMOS_BRIGHTNESS_UP :
5677 TP_CMOS_BRIGHTNESS_DOWN;
5678 inc = (value > current_value) ? 1 : -1;
5679
5680 for (i = current_value; i != value; i += inc)
5681 if (issue_thinkpad_cmos_command(cmos_cmd))
5682 return -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005683
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005684 return 0;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005685}
5686
5687/* May return EINTR which can always be mapped to ERESTARTSYS */
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005688static int brightness_set(unsigned int value)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005689{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005690 int res;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005691
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005692 if (value > ((tp_features.bright_16levels)? 15 : 7) ||
5693 value < 0)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005694 return -EINVAL;
5695
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005696 vdbg_printk(TPACPI_DBG_BRGHT,
5697 "set backlight level to %d\n", value);
5698
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02005699 res = mutex_lock_killable(&brightness_mutex);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005700 if (res < 0)
5701 return res;
5702
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005703 switch (brightness_mode) {
5704 case TPACPI_BRGHT_MODE_EC:
5705 case TPACPI_BRGHT_MODE_ECNVRAM:
5706 res = tpacpi_brightness_set_ec(value);
5707 break;
5708 case TPACPI_BRGHT_MODE_UCMS_STEP:
5709 res = tpacpi_brightness_set_ucmsstep(value);
5710 break;
5711 default:
5712 res = -ENXIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005713 }
5714
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005715 mutex_unlock(&brightness_mutex);
5716 return res;
5717}
5718
5719/* sysfs backlight class ----------------------------------------------- */
5720
5721static int brightness_update_status(struct backlight_device *bd)
5722{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005723 unsigned int level =
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005724 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
5725 bd->props.power == FB_BLANK_UNBLANK) ?
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005726 bd->props.brightness : 0;
5727
5728 dbg_printk(TPACPI_DBG_BRGHT,
5729 "backlight: attempt to set level to %d\n",
5730 level);
5731
5732 /* it is the backlight class's job (caller) to handle
5733 * EINTR and other errors properly */
5734 return brightness_set(level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005735}
Holger Macht8acb0252006-10-20 14:30:28 -07005736
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005737static int brightness_get(struct backlight_device *bd)
5738{
5739 int status, res;
5740
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005741 res = mutex_lock_killable(&brightness_mutex);
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005742 if (res < 0)
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005743 return 0;
5744
5745 res = tpacpi_brightness_get_raw(&status);
5746
5747 mutex_unlock(&brightness_mutex);
5748
5749 if (res < 0)
5750 return 0;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005751
5752 return status & TP_EC_BACKLIGHT_LVLMSK;
5753}
5754
Richard Purdie599a52d2007-02-10 23:07:48 +00005755static struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005756 .get_brightness = brightness_get,
5757 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005758};
5759
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005760/* --------------------------------------------------------------------- */
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03005761
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005762static int __init brightness_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005763{
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02005764 int b;
5765
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005766 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
5767
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03005768 mutex_init(&brightness_mutex);
5769
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03005770 /*
5771 * We always attempt to detect acpi support, so as to switch
5772 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
5773 * going to publish a backlight interface
5774 */
5775 b = tpacpi_check_std_acpi_brightness_support();
5776 if (b > 0) {
Thomas Renninger2dba1b52008-08-01 17:38:03 +02005777
5778 if (acpi_video_backlight_support()) {
5779 if (brightness_enable > 1) {
5780 printk(TPACPI_NOTICE
5781 "Standard ACPI backlight interface "
5782 "available, not loading native one.\n");
5783 return 1;
5784 } else if (brightness_enable == 1) {
5785 printk(TPACPI_NOTICE
5786 "Backlight control force enabled, even if standard "
5787 "ACPI backlight interface is available\n");
5788 }
5789 } else {
5790 if (brightness_enable > 1) {
5791 printk(TPACPI_NOTICE
5792 "Standard ACPI backlight interface not "
5793 "available, thinkpad_acpi native "
5794 "brightness control enabled\n");
5795 }
Henrique de Moraes Holschuhe11e2112007-10-30 17:46:22 -02005796 }
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02005797 }
5798
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03005799 if (!brightness_enable) {
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005800 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03005801 "brightness support disabled by "
5802 "module parameter\n");
5803 return 1;
5804 }
5805
5806 if (b > 16) {
5807 printk(TPACPI_ERR
5808 "Unsupported brightness interface, "
5809 "please contact %s\n", TPACPI_MAIL);
5810 return 1;
5811 }
5812 if (b == 16)
5813 tp_features.bright_16levels = 1;
5814
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005815 /*
5816 * Check for module parameter bogosity, note that we
5817 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
5818 * able to detect "unspecified"
5819 */
5820 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03005821 return -EINVAL;
5822
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005823 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
5824 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
5825 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
5826 if (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) {
5827 /*
5828 * IBM models that define HBRV probably have
5829 * EC-based backlight level control
5830 */
5831 if (acpi_evalf(ec_handle, NULL, "HBRV", "qd"))
5832 /* T40-T43, R50-R52, R50e, R51e, X31-X41 */
5833 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
5834 else
5835 /* all other IBM ThinkPads */
5836 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
5837 } else
5838 /* All Lenovo ThinkPads */
5839 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
5840
5841 dbg_printk(TPACPI_DBG_BRGHT,
5842 "selected brightness_mode=%d\n",
5843 brightness_mode);
5844 }
5845
5846 if (tpacpi_brightness_get_raw(&b) < 0)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03005847 return 1;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02005848
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02005849 if (tp_features.bright_16levels)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005850 printk(TPACPI_INFO
5851 "detected a 16-level brightness capable ThinkPad\n");
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02005852
Henrique de Moraes Holschuh7d5a0152007-04-24 11:48:20 -03005853 ibm_backlight_device = backlight_device_register(
5854 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
5855 &ibm_backlight_data);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005856 if (IS_ERR(ibm_backlight_device)) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005857 printk(TPACPI_ERR "Could not register backlight device\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005858 return PTR_ERR(ibm_backlight_device);
5859 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005860 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
5861 "brightness is supported\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005862
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02005863 ibm_backlight_device->props.max_brightness =
5864 (tp_features.bright_16levels)? 15 : 7;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005865 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02005866 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00005867
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005868 return 0;
5869}
5870
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005871static void brightness_suspend(pm_message_t state)
5872{
5873 tpacpi_brightness_checkpoint_nvram();
5874}
5875
5876static void brightness_shutdown(void)
5877{
5878 tpacpi_brightness_checkpoint_nvram();
5879}
5880
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005881static void brightness_exit(void)
5882{
5883 if (ibm_backlight_device) {
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005884 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005885 "calling backlight_device_unregister()\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005886 backlight_device_unregister(ibm_backlight_device);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005887 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005888
5889 tpacpi_brightness_checkpoint_nvram();
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005890}
5891
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005892static int brightness_read(char *p)
5893{
5894 int len = 0;
5895 int level;
5896
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005897 level = brightness_get(NULL);
5898 if (level < 0) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005899 len += sprintf(p + len, "level:\t\tunreadable\n");
5900 } else {
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02005901 len += sprintf(p + len, "level:\t\t%d\n", level);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005902 len += sprintf(p + len, "commands:\tup, down\n");
5903 len += sprintf(p + len, "commands:\tlevel <level>"
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02005904 " (<level> is 0-%d)\n",
5905 (tp_features.bright_16levels) ? 15 : 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005906 }
5907
5908 return len;
5909}
5910
5911static int brightness_write(char *buf)
5912{
5913 int level;
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02005914 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005915 char *cmd;
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02005916 int max_level = (tp_features.bright_16levels) ? 15 : 7;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005917
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02005918 level = brightness_get(NULL);
5919 if (level < 0)
5920 return level;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005921
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02005922 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005923 if (strlencmp(cmd, "up") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02005924 if (level < max_level)
5925 level++;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005926 } else if (strlencmp(cmd, "down") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02005927 if (level > 0)
5928 level--;
5929 } else if (sscanf(cmd, "level %d", &level) == 1 &&
5930 level >= 0 && level <= max_level) {
5931 /* new level set */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005932 } else
5933 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005934 }
5935
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005936 tpacpi_disclose_usertask("procfs brightness",
5937 "set level to %d\n", level);
5938
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02005939 /*
5940 * Now we know what the final level should be, so we try to set it.
5941 * Doing it this way makes the syscall restartable in case of EINTR
5942 */
5943 rc = brightness_set(level);
5944 return (rc == -EINTR)? ERESTARTSYS : rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005945}
5946
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005947static struct ibm_struct brightness_driver_data = {
5948 .name = "brightness",
5949 .read = brightness_read,
5950 .write = brightness_write,
5951 .exit = brightness_exit,
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005952 .suspend = brightness_suspend,
5953 .shutdown = brightness_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005954};
5955
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005956/*************************************************************************
5957 * Volume subdriver
5958 */
5959
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005960static int volume_offset = 0x30;
5961
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005962static int volume_read(char *p)
5963{
5964 int len = 0;
5965 u8 level;
5966
5967 if (!acpi_ec_read(volume_offset, &level)) {
5968 len += sprintf(p + len, "level:\t\tunreadable\n");
5969 } else {
5970 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
5971 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
5972 len += sprintf(p + len, "commands:\tup, down, mute\n");
5973 len += sprintf(p + len, "commands:\tlevel <level>"
5974 " (<level> is 0-15)\n");
5975 }
5976
5977 return len;
5978}
5979
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005980static int volume_write(char *buf)
5981{
5982 int cmos_cmd, inc, i;
5983 u8 level, mute;
5984 int new_level, new_mute;
5985 char *cmd;
5986
5987 while ((cmd = next_cmd(&buf))) {
5988 if (!acpi_ec_read(volume_offset, &level))
5989 return -EIO;
5990 new_mute = mute = level & 0x40;
5991 new_level = level = level & 0xf;
5992
5993 if (strlencmp(cmd, "up") == 0) {
5994 if (mute)
5995 new_mute = 0;
5996 else
5997 new_level = level == 15 ? 15 : level + 1;
5998 } else if (strlencmp(cmd, "down") == 0) {
5999 if (mute)
6000 new_mute = 0;
6001 else
6002 new_level = level == 0 ? 0 : level - 1;
6003 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
6004 new_level >= 0 && new_level <= 15) {
6005 /* new_level set */
6006 } else if (strlencmp(cmd, "mute") == 0) {
6007 new_mute = 0x40;
6008 } else
6009 return -EINVAL;
6010
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006011 if (new_level != level) {
6012 /* mute doesn't change */
6013
6014 cmos_cmd = (new_level > level) ?
6015 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006016 inc = new_level > level ? 1 : -1;
6017
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03006018 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006019 !acpi_ec_write(volume_offset, level)))
6020 return -EIO;
6021
6022 for (i = level; i != new_level; i += inc)
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03006023 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006024 !acpi_ec_write(volume_offset, i + inc))
6025 return -EIO;
6026
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006027 if (mute &&
6028 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
6029 !acpi_ec_write(volume_offset, new_level + mute))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006030 return -EIO;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006031 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006032 }
6033
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006034 if (new_mute != mute) {
6035 /* level doesn't change */
6036
6037 cmos_cmd = (new_mute) ?
6038 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006039
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03006040 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006041 !acpi_ec_write(volume_offset, level + new_mute))
6042 return -EIO;
6043 }
6044 }
6045
6046 return 0;
6047}
6048
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006049static struct ibm_struct volume_driver_data = {
6050 .name = "volume",
6051 .read = volume_read,
6052 .write = volume_write,
6053};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006054
6055/*************************************************************************
6056 * Fan subdriver
6057 */
6058
6059/*
6060 * FAN ACCESS MODES
6061 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006062 * TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006063 * ACPI GFAN method: returns fan level
6064 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006065 * see TPACPI_FAN_WR_ACPI_SFAN
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03006066 * EC 0x2f (HFSP) not available if GFAN exists
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006067 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006068 * TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006069 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
6070 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03006071 * EC 0x2f (HFSP) might be available *for reading*, but do not use
6072 * it for writing.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006073 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006074 * TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03006075 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
6076 * Supported on almost all ThinkPads
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006077 *
6078 * Fan speed changes of any sort (including those caused by the
6079 * disengaged mode) are usually done slowly by the firmware as the
6080 * maximum ammount of fan duty cycle change per second seems to be
6081 * limited.
6082 *
6083 * Reading is not available if GFAN exists.
6084 * Writing is not available if SFAN exists.
6085 *
6086 * Bits
6087 * 7 automatic mode engaged;
6088 * (default operation mode of the ThinkPad)
6089 * fan level is ignored in this mode.
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03006090 * 6 full speed mode (takes precedence over bit 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006091 * not available on all thinkpads. May disable
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03006092 * the tachometer while the fan controller ramps up
6093 * the speed (which can take up to a few *minutes*).
6094 * Speeds up fan to 100% duty-cycle, which is far above
6095 * the standard RPM levels. It is not impossible that
6096 * it could cause hardware damage.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006097 * 5-3 unused in some models. Extra bits for fan level
6098 * in others, but still useless as all values above
6099 * 7 map to the same speed as level 7 in these models.
6100 * 2-0 fan level (0..7 usually)
6101 * 0x00 = stop
6102 * 0x07 = max (set when temperatures critical)
6103 * Some ThinkPads may have other levels, see
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006104 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006105 *
6106 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
6107 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
6108 * does so, its initial value is meaningless (0x07).
6109 *
6110 * For firmware bugs, refer to:
6111 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
6112 *
6113 * ----
6114 *
6115 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
6116 * Main fan tachometer reading (in RPM)
6117 *
6118 * This register is present on all ThinkPads with a new-style EC, and
6119 * it is known not to be present on the A21m/e, and T22, as there is
6120 * something else in offset 0x84 according to the ACPI DSDT. Other
6121 * ThinkPads from this same time period (and earlier) probably lack the
6122 * tachometer as well.
6123 *
Nick Andrew877d0312009-01-26 11:06:57 +01006124 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006125 * was never fixed by IBM to report the EC firmware version string
6126 * probably support the tachometer (like the early X models), so
6127 * detecting it is quite hard. We need more data to know for sure.
6128 *
6129 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
6130 * might result.
6131 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03006132 * FIRMWARE BUG: may go stale while the EC is switching to full speed
6133 * mode.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006134 *
6135 * For firmware bugs, refer to:
6136 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
6137 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006138 * TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006139 * ThinkPad X31, X40, X41. Not available in the X60.
6140 *
6141 * FANS ACPI handle: takes three arguments: low speed, medium speed,
6142 * high speed. ACPI DSDT seems to map these three speeds to levels
6143 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
6144 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
6145 *
6146 * The speeds are stored on handles
6147 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
6148 *
6149 * There are three default speed sets, acessible as handles:
6150 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
6151 *
6152 * ACPI DSDT switches which set is in use depending on various
6153 * factors.
6154 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006155 * TPACPI_FAN_WR_TPEC is also available and should be used to
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006156 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
6157 * but the ACPI tables just mention level 7.
6158 */
6159
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006160enum { /* Fan control constants */
6161 fan_status_offset = 0x2f, /* EC register 0x2f */
6162 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
6163 * 0x84 must be read before 0x85 */
6164
6165 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
6166 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
6167
6168 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
6169};
6170
6171enum fan_status_access_mode {
6172 TPACPI_FAN_NONE = 0, /* No fan status or control */
6173 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
6174 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
6175};
6176
6177enum fan_control_access_mode {
6178 TPACPI_FAN_WR_NONE = 0, /* No fan control */
6179 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
6180 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
6181 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
6182};
6183
6184enum fan_control_commands {
6185 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
6186 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
6187 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
6188 * and also watchdog cmd */
6189};
6190
6191static int fan_control_allowed;
6192
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006193static enum fan_status_access_mode fan_status_access_mode;
6194static enum fan_control_access_mode fan_control_access_mode;
6195static enum fan_control_commands fan_control_commands;
6196
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02006197static u8 fan_control_initial_status;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006198static u8 fan_control_desired_level;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006199static u8 fan_control_resume_level;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02006200static int fan_watchdog_maxinterval;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006201
6202static struct mutex fan_mutex;
6203
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006204static void fan_watchdog_fire(struct work_struct *ignored);
Len Brown25c68a32006-12-08 04:43:41 -05006205static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02006206
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006207TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
6208TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006209 "\\FSPD", /* 600e/x, 770e, 770x */
6210 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006211TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006212 "JFNS", /* 770x-JL */
6213 ); /* all others */
6214
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006215/*
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006216 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
6217 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
6218 * be in auto mode (0x80).
6219 *
6220 * This is corrected by any write to HFSP either by the driver, or
6221 * by the firmware.
6222 *
6223 * We assume 0x07 really means auto mode while this quirk is active,
6224 * as this is far more likely than the ThinkPad being in level 7,
6225 * which is only used by the firmware during thermal emergencies.
6226 */
6227
6228static void fan_quirk1_detect(void)
6229{
6230 /* In some ThinkPads, neither the EC nor the ACPI
6231 * DSDT initialize the HFSP register, and it ends up
6232 * being initially set to 0x07 when it *could* be
6233 * either 0x07 or 0x80.
6234 *
6235 * Enable for TP-1Y (T43), TP-78 (R51e),
6236 * TP-76 (R52), TP-70 (T43, R52), which are known
6237 * to be buggy. */
6238 if (fan_control_initial_status == 0x07) {
6239 switch (thinkpad_id.ec_model) {
6240 case 0x5931: /* TP-1Y */
6241 case 0x3837: /* TP-78 */
6242 case 0x3637: /* TP-76 */
6243 case 0x3037: /* TP-70 */
6244 printk(TPACPI_NOTICE
6245 "fan_init: initial fan status is unknown, "
6246 "assuming it is in auto mode\n");
6247 tp_features.fan_ctrl_status_undef = 1;
6248 ;;
6249 }
6250 }
6251}
6252
6253static void fan_quirk1_handle(u8 *fan_status)
6254{
6255 if (unlikely(tp_features.fan_ctrl_status_undef)) {
6256 if (*fan_status != fan_control_initial_status) {
6257 /* something changed the HFSP regisnter since
6258 * driver init time, so it is not undefined
6259 * anymore */
6260 tp_features.fan_ctrl_status_undef = 0;
6261 } else {
6262 /* Return most likely status. In fact, it
6263 * might be the only possible status */
6264 *fan_status = TP_EC_FAN_AUTO;
6265 }
6266 }
6267}
6268
6269/*
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006270 * Call with fan_mutex held
6271 */
6272static void fan_update_desired_level(u8 status)
6273{
6274 if ((status &
6275 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
6276 if (status > 7)
6277 fan_control_desired_level = 7;
6278 else
6279 fan_control_desired_level = status;
6280 }
6281}
6282
6283static int fan_get_status(u8 *status)
6284{
6285 u8 s;
6286
6287 /* TODO:
6288 * Add TPACPI_FAN_RD_ACPI_FANS ? */
6289
6290 switch (fan_status_access_mode) {
6291 case TPACPI_FAN_RD_ACPI_GFAN:
6292 /* 570, 600e/x, 770e, 770x */
6293
6294 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
6295 return -EIO;
6296
6297 if (likely(status))
6298 *status = s & 0x07;
6299
6300 break;
6301
6302 case TPACPI_FAN_RD_TPEC:
6303 /* all except 570, 600e/x, 770e, 770x */
6304 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
6305 return -EIO;
6306
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006307 if (likely(status)) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006308 *status = s;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006309 fan_quirk1_handle(status);
6310 }
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006311
6312 break;
6313
6314 default:
6315 return -ENXIO;
6316 }
6317
6318 return 0;
6319}
6320
6321static int fan_get_status_safe(u8 *status)
6322{
6323 int rc;
6324 u8 s;
6325
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006326 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006327 return -ERESTARTSYS;
6328 rc = fan_get_status(&s);
6329 if (!rc)
6330 fan_update_desired_level(s);
6331 mutex_unlock(&fan_mutex);
6332
6333 if (status)
6334 *status = s;
6335
6336 return rc;
6337}
6338
6339static int fan_get_speed(unsigned int *speed)
6340{
6341 u8 hi, lo;
6342
6343 switch (fan_status_access_mode) {
6344 case TPACPI_FAN_RD_TPEC:
6345 /* all except 570, 600e/x, 770e, 770x */
6346 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
6347 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
6348 return -EIO;
6349
6350 if (likely(speed))
6351 *speed = (hi << 8) | lo;
6352
6353 break;
6354
6355 default:
6356 return -ENXIO;
6357 }
6358
6359 return 0;
6360}
6361
6362static int fan_set_level(int level)
6363{
6364 if (!fan_control_allowed)
6365 return -EPERM;
6366
6367 switch (fan_control_access_mode) {
6368 case TPACPI_FAN_WR_ACPI_SFAN:
6369 if (level >= 0 && level <= 7) {
6370 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
6371 return -EIO;
6372 } else
6373 return -EINVAL;
6374 break;
6375
6376 case TPACPI_FAN_WR_ACPI_FANS:
6377 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006378 if (!(level & TP_EC_FAN_AUTO) &&
6379 !(level & TP_EC_FAN_FULLSPEED) &&
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006380 ((level < 0) || (level > 7)))
6381 return -EINVAL;
6382
6383 /* safety net should the EC not support AUTO
6384 * or FULLSPEED mode bits and just ignore them */
6385 if (level & TP_EC_FAN_FULLSPEED)
6386 level |= 7; /* safety min speed 7 */
Roel Kluin547266e2008-02-05 00:24:56 +01006387 else if (level & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006388 level |= 4; /* safety min speed 4 */
6389
6390 if (!acpi_ec_write(fan_status_offset, level))
6391 return -EIO;
6392 else
6393 tp_features.fan_ctrl_status_undef = 0;
6394 break;
6395
6396 default:
6397 return -ENXIO;
6398 }
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00006399
6400 vdbg_printk(TPACPI_DBG_FAN,
6401 "fan control: set fan control register to 0x%02x\n", level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006402 return 0;
6403}
6404
6405static int fan_set_level_safe(int level)
6406{
6407 int rc;
6408
6409 if (!fan_control_allowed)
6410 return -EPERM;
6411
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006412 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006413 return -ERESTARTSYS;
6414
6415 if (level == TPACPI_FAN_LAST_LEVEL)
6416 level = fan_control_desired_level;
6417
6418 rc = fan_set_level(level);
6419 if (!rc)
6420 fan_update_desired_level(level);
6421
6422 mutex_unlock(&fan_mutex);
6423 return rc;
6424}
6425
6426static int fan_set_enable(void)
6427{
6428 u8 s;
6429 int rc;
6430
6431 if (!fan_control_allowed)
6432 return -EPERM;
6433
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006434 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006435 return -ERESTARTSYS;
6436
6437 switch (fan_control_access_mode) {
6438 case TPACPI_FAN_WR_ACPI_FANS:
6439 case TPACPI_FAN_WR_TPEC:
6440 rc = fan_get_status(&s);
6441 if (rc < 0)
6442 break;
6443
6444 /* Don't go out of emergency fan mode */
6445 if (s != 7) {
6446 s &= 0x07;
6447 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
6448 }
6449
6450 if (!acpi_ec_write(fan_status_offset, s))
6451 rc = -EIO;
6452 else {
6453 tp_features.fan_ctrl_status_undef = 0;
6454 rc = 0;
6455 }
6456 break;
6457
6458 case TPACPI_FAN_WR_ACPI_SFAN:
6459 rc = fan_get_status(&s);
6460 if (rc < 0)
6461 break;
6462
6463 s &= 0x07;
6464
6465 /* Set fan to at least level 4 */
6466 s |= 4;
6467
6468 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006469 rc = -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006470 else
6471 rc = 0;
6472 break;
6473
6474 default:
6475 rc = -ENXIO;
6476 }
6477
6478 mutex_unlock(&fan_mutex);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00006479
6480 if (!rc)
6481 vdbg_printk(TPACPI_DBG_FAN,
6482 "fan control: set fan control register to 0x%02x\n",
6483 s);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006484 return rc;
6485}
6486
6487static int fan_set_disable(void)
6488{
6489 int rc;
6490
6491 if (!fan_control_allowed)
6492 return -EPERM;
6493
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006494 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006495 return -ERESTARTSYS;
6496
6497 rc = 0;
6498 switch (fan_control_access_mode) {
6499 case TPACPI_FAN_WR_ACPI_FANS:
6500 case TPACPI_FAN_WR_TPEC:
6501 if (!acpi_ec_write(fan_status_offset, 0x00))
6502 rc = -EIO;
6503 else {
6504 fan_control_desired_level = 0;
6505 tp_features.fan_ctrl_status_undef = 0;
6506 }
6507 break;
6508
6509 case TPACPI_FAN_WR_ACPI_SFAN:
6510 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
6511 rc = -EIO;
6512 else
6513 fan_control_desired_level = 0;
6514 break;
6515
6516 default:
6517 rc = -ENXIO;
6518 }
6519
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00006520 if (!rc)
6521 vdbg_printk(TPACPI_DBG_FAN,
6522 "fan control: set fan control register to 0\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006523
6524 mutex_unlock(&fan_mutex);
6525 return rc;
6526}
6527
6528static int fan_set_speed(int speed)
6529{
6530 int rc;
6531
6532 if (!fan_control_allowed)
6533 return -EPERM;
6534
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006535 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006536 return -ERESTARTSYS;
6537
6538 rc = 0;
6539 switch (fan_control_access_mode) {
6540 case TPACPI_FAN_WR_ACPI_FANS:
6541 if (speed >= 0 && speed <= 65535) {
6542 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
6543 speed, speed, speed))
6544 rc = -EIO;
6545 } else
6546 rc = -EINVAL;
6547 break;
6548
6549 default:
6550 rc = -ENXIO;
6551 }
6552
6553 mutex_unlock(&fan_mutex);
6554 return rc;
6555}
6556
6557static void fan_watchdog_reset(void)
6558{
6559 static int fan_watchdog_active;
6560
6561 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
6562 return;
6563
6564 if (fan_watchdog_active)
6565 cancel_delayed_work(&fan_watchdog_task);
6566
6567 if (fan_watchdog_maxinterval > 0 &&
6568 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
6569 fan_watchdog_active = 1;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03006570 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006571 msecs_to_jiffies(fan_watchdog_maxinterval
6572 * 1000))) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006573 printk(TPACPI_ERR
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03006574 "failed to queue the fan watchdog, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006575 "watchdog will not trigger\n");
6576 }
6577 } else
6578 fan_watchdog_active = 0;
6579}
6580
6581static void fan_watchdog_fire(struct work_struct *ignored)
6582{
6583 int rc;
6584
6585 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
6586 return;
6587
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006588 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006589 rc = fan_set_enable();
6590 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006591 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006592 "will try again later...\n", -rc);
6593 /* reschedule for later */
6594 fan_watchdog_reset();
6595 }
6596}
6597
6598/*
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006599 * SYSFS fan layout: hwmon compatible (device)
6600 *
6601 * pwm*_enable:
6602 * 0: "disengaged" mode
6603 * 1: manual mode
6604 * 2: native EC "auto" mode (recommended, hardware default)
6605 *
6606 * pwm*: set speed in manual mode, ignored otherwise.
6607 * 0 is level 0; 255 is level 7. Intermediate points done with linear
6608 * interpolation.
6609 *
6610 * fan*_input: tachometer reading, RPM
6611 *
6612 *
6613 * SYSFS fan layout: extensions
6614 *
6615 * fan_watchdog (driver):
6616 * fan watchdog interval in seconds, 0 disables (default), max 120
6617 */
6618
6619/* sysfs fan pwm1_enable ----------------------------------------------- */
6620static ssize_t fan_pwm1_enable_show(struct device *dev,
6621 struct device_attribute *attr,
6622 char *buf)
6623{
6624 int res, mode;
6625 u8 status;
6626
6627 res = fan_get_status_safe(&status);
6628 if (res)
6629 return res;
6630
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006631 if (status & TP_EC_FAN_FULLSPEED) {
6632 mode = 0;
6633 } else if (status & TP_EC_FAN_AUTO) {
6634 mode = 2;
6635 } else
6636 mode = 1;
6637
6638 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
6639}
6640
6641static ssize_t fan_pwm1_enable_store(struct device *dev,
6642 struct device_attribute *attr,
6643 const char *buf, size_t count)
6644{
6645 unsigned long t;
6646 int res, level;
6647
6648 if (parse_strtoul(buf, 2, &t))
6649 return -EINVAL;
6650
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00006651 tpacpi_disclose_usertask("hwmon pwm1_enable",
6652 "set fan mode to %lu\n", t);
6653
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006654 switch (t) {
6655 case 0:
6656 level = TP_EC_FAN_FULLSPEED;
6657 break;
6658 case 1:
6659 level = TPACPI_FAN_LAST_LEVEL;
6660 break;
6661 case 2:
6662 level = TP_EC_FAN_AUTO;
6663 break;
6664 case 3:
6665 /* reserved for software-controlled auto mode */
6666 return -ENOSYS;
6667 default:
6668 return -EINVAL;
6669 }
6670
6671 res = fan_set_level_safe(level);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03006672 if (res == -ENXIO)
6673 return -EINVAL;
6674 else if (res < 0)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006675 return res;
6676
6677 fan_watchdog_reset();
6678
6679 return count;
6680}
6681
6682static struct device_attribute dev_attr_fan_pwm1_enable =
6683 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
6684 fan_pwm1_enable_show, fan_pwm1_enable_store);
6685
6686/* sysfs fan pwm1 ------------------------------------------------------ */
6687static ssize_t fan_pwm1_show(struct device *dev,
6688 struct device_attribute *attr,
6689 char *buf)
6690{
6691 int res;
6692 u8 status;
6693
6694 res = fan_get_status_safe(&status);
6695 if (res)
6696 return res;
6697
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006698 if ((status &
6699 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
6700 status = fan_control_desired_level;
6701
6702 if (status > 7)
6703 status = 7;
6704
6705 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
6706}
6707
6708static ssize_t fan_pwm1_store(struct device *dev,
6709 struct device_attribute *attr,
6710 const char *buf, size_t count)
6711{
6712 unsigned long s;
6713 int rc;
6714 u8 status, newlevel;
6715
6716 if (parse_strtoul(buf, 255, &s))
6717 return -EINVAL;
6718
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00006719 tpacpi_disclose_usertask("hwmon pwm1",
6720 "set fan speed to %lu\n", s);
6721
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006722 /* scale down from 0-255 to 0-7 */
6723 newlevel = (s >> 5) & 0x07;
6724
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006725 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02006726 return -ERESTARTSYS;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006727
6728 rc = fan_get_status(&status);
6729 if (!rc && (status &
6730 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
6731 rc = fan_set_level(newlevel);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03006732 if (rc == -ENXIO)
6733 rc = -EINVAL;
6734 else if (!rc) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006735 fan_update_desired_level(newlevel);
Henrique de Moraes Holschuhca4ac2f2007-04-27 22:00:11 -03006736 fan_watchdog_reset();
6737 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006738 }
6739
6740 mutex_unlock(&fan_mutex);
6741 return (rc)? rc : count;
6742}
6743
6744static struct device_attribute dev_attr_fan_pwm1 =
6745 __ATTR(pwm1, S_IWUSR | S_IRUGO,
6746 fan_pwm1_show, fan_pwm1_store);
6747
6748/* sysfs fan fan1_input ------------------------------------------------ */
6749static ssize_t fan_fan1_input_show(struct device *dev,
6750 struct device_attribute *attr,
6751 char *buf)
6752{
6753 int res;
6754 unsigned int speed;
6755
6756 res = fan_get_speed(&speed);
6757 if (res < 0)
6758 return res;
6759
6760 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
6761}
6762
6763static struct device_attribute dev_attr_fan_fan1_input =
6764 __ATTR(fan1_input, S_IRUGO,
6765 fan_fan1_input_show, NULL);
6766
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006767/* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006768static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
6769 char *buf)
6770{
6771 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
6772}
6773
6774static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
6775 const char *buf, size_t count)
6776{
6777 unsigned long t;
6778
6779 if (parse_strtoul(buf, 120, &t))
6780 return -EINVAL;
6781
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03006782 if (!fan_control_allowed)
6783 return -EPERM;
6784
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006785 fan_watchdog_maxinterval = t;
6786 fan_watchdog_reset();
6787
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00006788 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
6789
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006790 return count;
6791}
6792
6793static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
6794 fan_fan_watchdog_show, fan_fan_watchdog_store);
6795
6796/* --------------------------------------------------------------------- */
6797static struct attribute *fan_attributes[] = {
6798 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
6799 &dev_attr_fan_fan1_input.attr,
6800 NULL
6801};
6802
6803static const struct attribute_group fan_attr_group = {
6804 .attrs = fan_attributes,
6805};
6806
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006807static int __init fan_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006808{
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006809 int rc;
6810
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00006811 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
6812 "initializing fan subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006813
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03006814 mutex_init(&fan_mutex);
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006815 fan_status_access_mode = TPACPI_FAN_NONE;
6816 fan_control_access_mode = TPACPI_FAN_WR_NONE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006817 fan_control_commands = 0;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02006818 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03006819 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006820 fan_control_desired_level = 7;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006821
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006822 TPACPI_ACPIHANDLE_INIT(fans);
6823 TPACPI_ACPIHANDLE_INIT(gfan);
6824 TPACPI_ACPIHANDLE_INIT(sfan);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006825
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006826 if (gfan_handle) {
6827 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006828 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006829 } else {
6830 /* all other ThinkPads: note that even old-style
6831 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02006832 if (likely(acpi_ec_read(fan_status_offset,
6833 &fan_control_initial_status))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006834 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006835 fan_quirk1_detect();
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006836 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006837 printk(TPACPI_ERR
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006838 "ThinkPad ACPI EC access misbehaving, "
6839 "fan status and control unavailable\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006840 return 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006841 }
6842 }
6843
6844 if (sfan_handle) {
6845 /* 570, 770x-JL */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006846 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02006847 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006848 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006849 } else {
6850 if (!gfan_handle) {
6851 /* gfan without sfan means no fan control */
6852 /* all other models implement TP EC 0x2f control */
6853
6854 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02006855 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006856 fan_control_access_mode =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006857 TPACPI_FAN_WR_ACPI_FANS;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006858 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006859 TPACPI_FAN_CMD_SPEED |
6860 TPACPI_FAN_CMD_LEVEL |
6861 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006862 } else {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006863 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02006864 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006865 TPACPI_FAN_CMD_LEVEL |
6866 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006867 }
6868 }
6869 }
6870
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00006871 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
6872 "fan is %s, modes %d, %d\n",
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006873 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
6874 fan_control_access_mode != TPACPI_FAN_WR_NONE),
6875 fan_status_access_mode, fan_control_access_mode);
6876
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03006877 /* fan control master switch */
6878 if (!fan_control_allowed) {
6879 fan_control_access_mode = TPACPI_FAN_WR_NONE;
6880 fan_control_commands = 0;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00006881 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03006882 "fan control features disabled by parameter\n");
6883 }
6884
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006885 /* update fan_control_desired_level */
6886 if (fan_status_access_mode != TPACPI_FAN_NONE)
6887 fan_get_status_safe(NULL);
6888
6889 if (fan_status_access_mode != TPACPI_FAN_NONE ||
6890 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006891 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006892 &fan_attr_group);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006893 if (rc < 0)
6894 return rc;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03006895
6896 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
6897 &driver_attr_fan_watchdog);
6898 if (rc < 0) {
6899 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
6900 &fan_attr_group);
6901 return rc;
6902 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006903 return 0;
6904 } else
6905 return 1;
6906}
6907
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006908static void fan_exit(void)
6909{
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00006910 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006911 "cancelling any pending fan watchdog tasks\n");
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006912
6913 /* FIXME: can we really do this unconditionally? */
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006914 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006915 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
6916 &driver_attr_fan_watchdog);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006917
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006918 cancel_delayed_work(&fan_watchdog_task);
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03006919 flush_workqueue(tpacpi_wq);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006920}
6921
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006922static void fan_suspend(pm_message_t state)
6923{
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006924 int rc;
6925
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006926 if (!fan_control_allowed)
6927 return;
6928
6929 /* Store fan status in cache */
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006930 fan_control_resume_level = 0;
6931 rc = fan_get_status_safe(&fan_control_resume_level);
6932 if (rc < 0)
6933 printk(TPACPI_NOTICE
6934 "failed to read fan level for later "
6935 "restore during resume: %d\n", rc);
6936
6937 /* if it is undefined, don't attempt to restore it.
6938 * KEEP THIS LAST */
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006939 if (tp_features.fan_ctrl_status_undef)
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006940 fan_control_resume_level = 0;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006941}
6942
6943static void fan_resume(void)
6944{
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006945 u8 current_level = 7;
6946 bool do_set = false;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006947 int rc;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006948
6949 /* DSDT *always* updates status on resume */
6950 tp_features.fan_ctrl_status_undef = 0;
6951
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006952 if (!fan_control_allowed ||
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006953 !fan_control_resume_level ||
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006954 (fan_get_status_safe(&current_level) < 0))
6955 return;
6956
6957 switch (fan_control_access_mode) {
6958 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006959 /* never decrease fan level */
6960 do_set = (fan_control_resume_level > current_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006961 break;
6962 case TPACPI_FAN_WR_ACPI_FANS:
6963 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006964 /* never decrease fan level, scale is:
6965 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
6966 *
6967 * We expect the firmware to set either 7 or AUTO, but we
6968 * handle FULLSPEED out of paranoia.
6969 *
6970 * So, we can safely only restore FULLSPEED or 7, anything
6971 * else could slow the fan. Restoring AUTO is useless, at
6972 * best that's exactly what the DSDT already set (it is the
6973 * slower it uses).
6974 *
6975 * Always keep in mind that the DSDT *will* have set the
6976 * fans to what the vendor supposes is the best level. We
6977 * muck with it only to speed the fan up.
6978 */
6979 if (fan_control_resume_level != 7 &&
6980 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
6981 return;
6982 else
6983 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
6984 (current_level != fan_control_resume_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006985 break;
6986 default:
6987 return;
6988 }
6989 if (do_set) {
6990 printk(TPACPI_NOTICE
6991 "restoring fan level to 0x%02x\n",
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006992 fan_control_resume_level);
6993 rc = fan_set_level_safe(fan_control_resume_level);
6994 if (rc < 0)
6995 printk(TPACPI_NOTICE
6996 "failed to restore fan level: %d\n", rc);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006997 }
6998}
6999
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007000static int fan_read(char *p)
7001{
7002 int len = 0;
7003 int rc;
7004 u8 status;
7005 unsigned int speed = 0;
7006
7007 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007008 case TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007009 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007010 rc = fan_get_status_safe(&status);
7011 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007012 return rc;
7013
7014 len += sprintf(p + len, "status:\t\t%s\n"
7015 "level:\t\t%d\n",
7016 (status != 0) ? "enabled" : "disabled", status);
7017 break;
7018
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007019 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007020 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007021 rc = fan_get_status_safe(&status);
7022 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007023 return rc;
7024
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007025 len += sprintf(p + len, "status:\t\t%s\n",
7026 (status != 0) ? "enabled" : "disabled");
7027
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007028 rc = fan_get_speed(&speed);
7029 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007030 return rc;
7031
7032 len += sprintf(p + len, "speed:\t\t%d\n", speed);
7033
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007034 if (status & TP_EC_FAN_FULLSPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007035 /* Disengaged mode takes precedence */
7036 len += sprintf(p + len, "level:\t\tdisengaged\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007037 else if (status & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007038 len += sprintf(p + len, "level:\t\tauto\n");
7039 else
7040 len += sprintf(p + len, "level:\t\t%d\n", status);
7041 break;
7042
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007043 case TPACPI_FAN_NONE:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007044 default:
7045 len += sprintf(p + len, "status:\t\tnot supported\n");
7046 }
7047
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007048 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007049 len += sprintf(p + len, "commands:\tlevel <level>");
7050
7051 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007052 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007053 len += sprintf(p + len, " (<level> is 0-7)\n");
7054 break;
7055
7056 default:
7057 len += sprintf(p + len, " (<level> is 0-7, "
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007058 "auto, disengaged, full-speed)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007059 break;
7060 }
7061 }
7062
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007063 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007064 len += sprintf(p + len, "commands:\tenable, disable\n"
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007065 "commands:\twatchdog <timeout> (<timeout> "
7066 "is 0 (off), 1-120 (seconds))\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007067
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007068 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007069 len += sprintf(p + len, "commands:\tspeed <speed>"
7070 " (<speed> is 0-65535)\n");
7071
7072 return len;
7073}
7074
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007075static int fan_write_cmd_level(const char *cmd, int *rc)
7076{
7077 int level;
7078
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02007079 if (strlencmp(cmd, "level auto") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007080 level = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007081 else if ((strlencmp(cmd, "level disengaged") == 0) |
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007082 (strlencmp(cmd, "level full-speed") == 0))
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007083 level = TP_EC_FAN_FULLSPEED;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02007084 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007085 return 0;
7086
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007087 *rc = fan_set_level_safe(level);
7088 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007089 printk(TPACPI_ERR "level command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007090 "access mode %d", fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007091 else if (!*rc)
7092 tpacpi_disclose_usertask("procfs fan",
7093 "set level to %d\n", level);
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007094
7095 return 1;
7096}
7097
7098static int fan_write_cmd_enable(const char *cmd, int *rc)
7099{
7100 if (strlencmp(cmd, "enable") != 0)
7101 return 0;
7102
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007103 *rc = fan_set_enable();
7104 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007105 printk(TPACPI_ERR "enable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007106 "access mode %d", fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007107 else if (!*rc)
7108 tpacpi_disclose_usertask("procfs fan", "enable\n");
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007109
7110 return 1;
7111}
7112
7113static int fan_write_cmd_disable(const char *cmd, int *rc)
7114{
7115 if (strlencmp(cmd, "disable") != 0)
7116 return 0;
7117
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007118 *rc = fan_set_disable();
7119 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007120 printk(TPACPI_ERR "disable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007121 "access mode %d", fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007122 else if (!*rc)
7123 tpacpi_disclose_usertask("procfs fan", "disable\n");
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007124
7125 return 1;
7126}
7127
7128static int fan_write_cmd_speed(const char *cmd, int *rc)
7129{
7130 int speed;
7131
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02007132 /* TODO:
7133 * Support speed <low> <medium> <high> ? */
7134
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007135 if (sscanf(cmd, "speed %d", &speed) != 1)
7136 return 0;
7137
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007138 *rc = fan_set_speed(speed);
7139 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007140 printk(TPACPI_ERR "speed command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007141 "access mode %d", fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007142 else if (!*rc)
7143 tpacpi_disclose_usertask("procfs fan",
7144 "set speed to %d\n", speed);
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007145
7146 return 1;
7147}
7148
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007149static int fan_write_cmd_watchdog(const char *cmd, int *rc)
7150{
7151 int interval;
7152
7153 if (sscanf(cmd, "watchdog %d", &interval) != 1)
7154 return 0;
7155
7156 if (interval < 0 || interval > 120)
7157 *rc = -EINVAL;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007158 else {
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007159 fan_watchdog_maxinterval = interval;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007160 tpacpi_disclose_usertask("procfs fan",
7161 "set watchdog timer to %d\n",
7162 interval);
7163 }
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007164
7165 return 1;
7166}
7167
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007168static int fan_write(char *buf)
7169{
7170 char *cmd;
7171 int rc = 0;
7172
7173 while (!rc && (cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007174 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007175 fan_write_cmd_level(cmd, &rc)) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007176 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007177 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007178 fan_write_cmd_disable(cmd, &rc) ||
7179 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007180 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007181 fan_write_cmd_speed(cmd, &rc))
7182 )
7183 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007184 else if (!rc)
7185 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007186 }
7187
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007188 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007189}
7190
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007191static struct ibm_struct fan_driver_data = {
7192 .name = "fan",
7193 .read = fan_read,
7194 .write = fan_write,
7195 .exit = fan_exit,
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007196 .suspend = fan_suspend,
7197 .resume = fan_resume,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007198};
7199
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007200/****************************************************************************
7201 ****************************************************************************
7202 *
7203 * Infrastructure
7204 *
7205 ****************************************************************************
7206 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07007207
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007208/* sysfs name ---------------------------------------------------------- */
7209static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
7210 struct device_attribute *attr,
7211 char *buf)
7212{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007213 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007214}
7215
7216static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
7217 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
7218
7219/* --------------------------------------------------------------------- */
7220
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007221/* /proc support */
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03007222static struct proc_dir_entry *proc_dir;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007223
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007224/*
7225 * Module and infrastructure proble, init and exit handling
7226 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007227
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007228static int force_load;
7229
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007230#ifdef CONFIG_THINKPAD_ACPI_DEBUG
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007231static const char * __init str_supported(int is_supported)
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007232{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007233 static char text_unsupported[] __initdata = "not supported";
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007234
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007235 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007236}
7237#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
7238
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007239static void ibm_exit(struct ibm_struct *ibm)
7240{
7241 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
7242
7243 list_del_init(&ibm->all_drivers);
7244
7245 if (ibm->flags.acpi_notify_installed) {
7246 dbg_printk(TPACPI_DBG_EXIT,
7247 "%s: acpi_remove_notify_handler\n", ibm->name);
7248 BUG_ON(!ibm->acpi);
7249 acpi_remove_notify_handler(*ibm->acpi->handle,
7250 ibm->acpi->type,
7251 dispatch_acpi_notify);
7252 ibm->flags.acpi_notify_installed = 0;
7253 ibm->flags.acpi_notify_installed = 0;
7254 }
7255
7256 if (ibm->flags.proc_created) {
7257 dbg_printk(TPACPI_DBG_EXIT,
7258 "%s: remove_proc_entry\n", ibm->name);
7259 remove_proc_entry(ibm->name, proc_dir);
7260 ibm->flags.proc_created = 0;
7261 }
7262
7263 if (ibm->flags.acpi_driver_registered) {
7264 dbg_printk(TPACPI_DBG_EXIT,
7265 "%s: acpi_bus_unregister_driver\n", ibm->name);
7266 BUG_ON(!ibm->acpi);
7267 acpi_bus_unregister_driver(ibm->acpi->driver);
7268 kfree(ibm->acpi->driver);
7269 ibm->acpi->driver = NULL;
7270 ibm->flags.acpi_driver_registered = 0;
7271 }
7272
7273 if (ibm->flags.init_called && ibm->exit) {
7274 ibm->exit();
7275 ibm->flags.init_called = 0;
7276 }
7277
7278 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
7279}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007280
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007281static int __init ibm_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007282{
7283 int ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007284 struct ibm_struct *ibm = iibm->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007285 struct proc_dir_entry *entry;
7286
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007287 BUG_ON(ibm == NULL);
7288
7289 INIT_LIST_HEAD(&ibm->all_drivers);
7290
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03007291 if (ibm->flags.experimental && !experimental)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007292 return 0;
7293
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007294 dbg_printk(TPACPI_DBG_INIT,
7295 "probing for %s\n", ibm->name);
7296
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007297 if (iibm->init) {
7298 ret = iibm->init(iibm);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007299 if (ret > 0)
7300 return 0; /* probe failed */
7301 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007302 return ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007303
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03007304 ibm->flags.init_called = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007305 }
7306
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03007307 if (ibm->acpi) {
7308 if (ibm->acpi->hid) {
7309 ret = register_tpacpi_subdriver(ibm);
7310 if (ret)
7311 goto err_out;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007312 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03007313
7314 if (ibm->acpi->notify) {
7315 ret = setup_acpi_notify(ibm);
7316 if (ret == -ENODEV) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007317 printk(TPACPI_NOTICE "disabling subdriver %s\n",
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03007318 ibm->name);
7319 ret = 0;
7320 goto err_out;
7321 }
7322 if (ret < 0)
7323 goto err_out;
7324 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007325 }
7326
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007327 dbg_printk(TPACPI_DBG_INIT,
7328 "%s installed\n", ibm->name);
7329
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007330 if (ibm->read) {
7331 entry = create_proc_entry(ibm->name,
7332 S_IFREG | S_IRUGO | S_IWUSR,
7333 proc_dir);
7334 if (!entry) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007335 printk(TPACPI_ERR "unable to create proc entry %s\n",
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007336 ibm->name);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007337 ret = -ENODEV;
7338 goto err_out;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007339 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007340 entry->data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03007341 entry->read_proc = &dispatch_procfs_read;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007342 if (ibm->write)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03007343 entry->write_proc = &dispatch_procfs_write;
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03007344 ibm->flags.proc_created = 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007345 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007346
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007347 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
7348
Linus Torvalds1da177e2005-04-16 15:20:36 -07007349 return 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007350
7351err_out:
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007352 dbg_printk(TPACPI_DBG_INIT,
7353 "%s: at error exit path with result %d\n",
7354 ibm->name, ret);
7355
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007356 ibm_exit(ibm);
7357 return (ret < 0)? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007358}
7359
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007360/* Probing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007361
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03007362static bool __pure __init tpacpi_is_fw_digit(const char c)
7363{
7364 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
7365}
7366
7367/* Most models: xxyTkkWW (#.##c); Ancient 570/600 and -SL lacks (#.##c) */
7368static bool __pure __init tpacpi_is_valid_fw_id(const char* const s,
7369 const char t)
7370{
7371 return s && strlen(s) >= 8 &&
7372 tpacpi_is_fw_digit(s[0]) &&
7373 tpacpi_is_fw_digit(s[1]) &&
7374 s[2] == t && s[3] == 'T' &&
7375 tpacpi_is_fw_digit(s[4]) &&
7376 tpacpi_is_fw_digit(s[5]) &&
7377 s[6] == 'W' && s[7] == 'W';
7378}
7379
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007380/* returns 0 - probe ok, or < 0 - probe error.
7381 * Probe ok doesn't mean thinkpad found.
7382 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
7383static int __must_check __init get_thinkpad_model_data(
7384 struct thinkpad_id_data *tp)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02007385{
Jeff Garzik18552562007-10-03 15:15:40 -04007386 const struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02007387 char ec_fw_string[18];
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007388 char const *s;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02007389
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007390 if (!tp)
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007391 return -EINVAL;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007392
7393 memset(tp, 0, sizeof(*tp));
7394
7395 if (dmi_name_in_vendors("IBM"))
7396 tp->vendor = PCI_VENDOR_ID_IBM;
7397 else if (dmi_name_in_vendors("LENOVO"))
7398 tp->vendor = PCI_VENDOR_ID_LENOVO;
7399 else
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007400 return 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007401
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007402 s = dmi_get_system_info(DMI_BIOS_VERSION);
7403 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
7404 if (s && !tp->bios_version_str)
7405 return -ENOMEM;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03007406
7407 /* Really ancient ThinkPad 240X will fail this, which is fine */
7408 if (!tpacpi_is_valid_fw_id(tp->bios_version_str, 'E'))
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007409 return 0;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03007410
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007411 tp->bios_model = tp->bios_version_str[0]
7412 | (tp->bios_version_str[1] << 8);
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03007413 tp->bios_release = (tp->bios_version_str[4] << 8)
7414 | tp->bios_version_str[5];
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007415
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02007416 /*
7417 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
7418 * X32 or newer, all Z series; Some models must have an
7419 * up-to-date BIOS or they will not be detected.
7420 *
7421 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
7422 */
7423 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02007424 if (sscanf(dev->name,
7425 "IBM ThinkPad Embedded Controller -[%17c",
7426 ec_fw_string) == 1) {
7427 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
7428 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007429
7430 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007431 if (!tp->ec_version_str)
7432 return -ENOMEM;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03007433
7434 if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) {
7435 tp->ec_model = ec_fw_string[0]
7436 | (ec_fw_string[1] << 8);
7437 tp->ec_release = (ec_fw_string[4] << 8)
7438 | ec_fw_string[5];
7439 } else {
7440 printk(TPACPI_NOTICE
7441 "ThinkPad firmware release %s "
7442 "doesn't match the known patterns\n",
7443 ec_fw_string);
7444 printk(TPACPI_NOTICE
7445 "please report this to %s\n",
7446 TPACPI_MAIL);
7447 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007448 break;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02007449 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02007450 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007451
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007452 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
7453 if (s && !strnicmp(s, "ThinkPad", 8)) {
7454 tp->model_str = kstrdup(s, GFP_KERNEL);
7455 if (!tp->model_str)
7456 return -ENOMEM;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007457 }
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03007458
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007459 s = dmi_get_system_info(DMI_PRODUCT_NAME);
7460 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
7461 if (s && !tp->nummodel_str)
7462 return -ENOMEM;
7463
7464 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007465}
7466
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007467static int __init probe_for_thinkpad(void)
7468{
7469 int is_thinkpad;
7470
7471 if (acpi_disabled)
7472 return -ENODEV;
7473
7474 /*
7475 * Non-ancient models have better DMI tagging, but very old models
7476 * don't.
7477 */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007478 is_thinkpad = (thinkpad_id.model_str != NULL);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007479
7480 /* ec is required because many other handles are relative to it */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007481 TPACPI_ACPIHANDLE_INIT(ec);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007482 if (!ec_handle) {
7483 if (is_thinkpad)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007484 printk(TPACPI_ERR
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007485 "Not yet supported ThinkPad detected!\n");
7486 return -ENODEV;
7487 }
7488
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03007489 /*
7490 * Risks a regression on very old machines, but reduces potential
7491 * false positives a damn great deal
7492 */
7493 if (!is_thinkpad)
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007494 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03007495
7496 if (!is_thinkpad && !force_load)
7497 return -ENODEV;
7498
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007499 return 0;
7500}
7501
7502
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007503/* Module init, exit, parameters */
7504
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007505static struct ibm_init_struct ibms_init[] __initdata = {
7506 {
7507 .init = thinkpad_acpi_driver_init,
7508 .data = &thinkpad_acpi_driver_data,
7509 },
7510 {
7511 .init = hotkey_init,
7512 .data = &hotkey_driver_data,
7513 },
7514 {
7515 .init = bluetooth_init,
7516 .data = &bluetooth_driver_data,
7517 },
7518 {
7519 .init = wan_init,
7520 .data = &wan_driver_data,
7521 },
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02007522 {
7523 .init = uwb_init,
7524 .data = &uwb_driver_data,
7525 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02007526#ifdef CONFIG_THINKPAD_ACPI_VIDEO
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007527 {
7528 .init = video_init,
7529 .data = &video_driver_data,
7530 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02007531#endif
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007532 {
7533 .init = light_init,
7534 .data = &light_driver_data,
7535 },
7536#ifdef CONFIG_THINKPAD_ACPI_DOCK
7537 {
7538 .init = dock_init,
7539 .data = &dock_driver_data[0],
7540 },
7541 {
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03007542 .init = dock_init2,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007543 .data = &dock_driver_data[1],
7544 },
7545#endif
7546#ifdef CONFIG_THINKPAD_ACPI_BAY
7547 {
7548 .init = bay_init,
7549 .data = &bay_driver_data,
7550 },
7551#endif
7552 {
7553 .init = cmos_init,
7554 .data = &cmos_driver_data,
7555 },
7556 {
7557 .init = led_init,
7558 .data = &led_driver_data,
7559 },
7560 {
7561 .init = beep_init,
7562 .data = &beep_driver_data,
7563 },
7564 {
7565 .init = thermal_init,
7566 .data = &thermal_driver_data,
7567 },
7568 {
7569 .data = &ecdump_driver_data,
7570 },
7571 {
7572 .init = brightness_init,
7573 .data = &brightness_driver_data,
7574 },
7575 {
7576 .data = &volume_driver_data,
7577 },
7578 {
7579 .init = fan_init,
7580 .data = &fan_driver_data,
7581 },
7582};
7583
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007584static int __init set_ibm_param(const char *val, struct kernel_param *kp)
7585{
7586 unsigned int i;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007587 struct ibm_struct *ibm;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007588
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02007589 if (!kp || !kp->name || !val)
7590 return -EINVAL;
7591
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007592 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
7593 ibm = ibms_init[i].data;
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02007594 WARN_ON(ibm == NULL);
7595
7596 if (!ibm || !ibm->name)
7597 continue;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007598
7599 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
7600 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007601 return -ENOSPC;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007602 strcpy(ibms_init[i].param, val);
7603 strcat(ibms_init[i].param, ",");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007604 return 0;
7605 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007606 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007607
7608 return -EINVAL;
7609}
7610
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007611module_param(experimental, int, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007612MODULE_PARM_DESC(experimental,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007613 "Enables experimental features when non-zero");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007614
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03007615module_param_named(debug, dbg_level, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007616MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03007617
Henrique de Moraes Holschuh86cc9442007-07-18 23:45:41 -03007618module_param(force_load, bool, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007619MODULE_PARM_DESC(force_load,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007620 "Attempts to load the driver even on a "
7621 "mis-identified ThinkPad when true");
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03007622
Henrique de Moraes Holschuh86cc9442007-07-18 23:45:41 -03007623module_param_named(fan_control, fan_control_allowed, bool, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007624MODULE_PARM_DESC(fan_control,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007625 "Enables setting fan parameters features when true");
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03007626
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00007627module_param_named(brightness_mode, brightness_mode, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007628MODULE_PARM_DESC(brightness_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007629 "Selects brightness control strategy: "
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00007630 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03007631
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02007632module_param(brightness_enable, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007633MODULE_PARM_DESC(brightness_enable,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007634 "Enables backlight control when 1, disables when 0");
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02007635
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03007636module_param(hotkey_report_mode, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007637MODULE_PARM_DESC(hotkey_report_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007638 "used for backwards compatibility with userspace, "
7639 "see documentation");
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03007640
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007641#define TPACPI_PARAM(feature) \
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007642 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
Henrique de Moraes Holschuhcbb14842008-02-16 02:17:50 -02007643 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007644 "at module load, see documentation")
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007645
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007646TPACPI_PARAM(hotkey);
7647TPACPI_PARAM(bluetooth);
7648TPACPI_PARAM(video);
7649TPACPI_PARAM(light);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03007650#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007651TPACPI_PARAM(dock);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007652#endif
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03007653#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007654TPACPI_PARAM(bay);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03007655#endif /* CONFIG_THINKPAD_ACPI_BAY */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007656TPACPI_PARAM(cmos);
7657TPACPI_PARAM(led);
7658TPACPI_PARAM(beep);
7659TPACPI_PARAM(ecdump);
7660TPACPI_PARAM(brightness);
7661TPACPI_PARAM(volume);
7662TPACPI_PARAM(fan);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007663
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02007664#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
7665module_param(dbg_wlswemul, uint, 0);
7666MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
7667module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
7668MODULE_PARM_DESC(wlsw_state,
7669 "Initial state of the emulated WLSW switch");
7670
7671module_param(dbg_bluetoothemul, uint, 0);
7672MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
7673module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
7674MODULE_PARM_DESC(bluetooth_state,
7675 "Initial state of the emulated bluetooth switch");
7676
7677module_param(dbg_wwanemul, uint, 0);
7678MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
7679module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
7680MODULE_PARM_DESC(wwan_state,
7681 "Initial state of the emulated WWAN switch");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02007682
7683module_param(dbg_uwbemul, uint, 0);
7684MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
7685module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
7686MODULE_PARM_DESC(uwb_state,
7687 "Initial state of the emulated UWB switch");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02007688#endif
7689
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007690static void thinkpad_acpi_module_exit(void)
7691{
7692 struct ibm_struct *ibm, *itmp;
7693
7694 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
7695
7696 list_for_each_entry_safe_reverse(ibm, itmp,
7697 &tpacpi_all_drivers,
7698 all_drivers) {
7699 ibm_exit(ibm);
7700 }
7701
7702 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
7703
7704 if (tpacpi_inputdev) {
7705 if (tp_features.input_device_registered)
7706 input_unregister_device(tpacpi_inputdev);
7707 else
7708 input_free_device(tpacpi_inputdev);
7709 }
7710
7711 if (tpacpi_hwmon)
7712 hwmon_device_unregister(tpacpi_hwmon);
7713
7714 if (tp_features.sensors_pdev_attrs_registered)
7715 device_remove_file(&tpacpi_sensors_pdev->dev,
7716 &dev_attr_thinkpad_acpi_pdev_name);
7717 if (tpacpi_sensors_pdev)
7718 platform_device_unregister(tpacpi_sensors_pdev);
7719 if (tpacpi_pdev)
7720 platform_device_unregister(tpacpi_pdev);
7721
7722 if (tp_features.sensors_pdrv_attrs_registered)
7723 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
7724 if (tp_features.platform_drv_attrs_registered)
7725 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
7726
7727 if (tp_features.sensors_pdrv_registered)
7728 platform_driver_unregister(&tpacpi_hwmon_pdriver);
7729
7730 if (tp_features.platform_drv_registered)
7731 platform_driver_unregister(&tpacpi_pdriver);
7732
7733 if (proc_dir)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007734 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007735
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03007736 if (tpacpi_wq)
7737 destroy_workqueue(tpacpi_wq);
7738
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007739 kfree(thinkpad_id.bios_version_str);
7740 kfree(thinkpad_id.ec_version_str);
7741 kfree(thinkpad_id.model_str);
7742}
7743
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007744
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03007745static int __init thinkpad_acpi_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007746{
7747 int ret, i;
7748
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03007749 tpacpi_lifecycle = TPACPI_LIFE_INIT;
7750
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03007751 /* Parameter checking */
7752 if (hotkey_report_mode > 2)
7753 return -EINVAL;
7754
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007755 /* Driver-level probe */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007756
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007757 ret = get_thinkpad_model_data(&thinkpad_id);
7758 if (ret) {
7759 printk(TPACPI_ERR
7760 "unable to get DMI data: %d\n", ret);
7761 thinkpad_acpi_module_exit();
7762 return ret;
7763 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007764 ret = probe_for_thinkpad();
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007765 if (ret) {
7766 thinkpad_acpi_module_exit();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007767 return ret;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007768 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007769
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007770 /* Driver initialization */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007771
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007772 TPACPI_ACPIHANDLE_INIT(ecrd);
7773 TPACPI_ACPIHANDLE_INIT(ecwr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007774
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03007775 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
7776 if (!tpacpi_wq) {
7777 thinkpad_acpi_module_exit();
7778 return -ENOMEM;
7779 }
7780
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007781 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007782 if (!proc_dir) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007783 printk(TPACPI_ERR
7784 "unable to create proc dir " TPACPI_PROC_DIR);
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03007785 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007786 return -ENODEV;
7787 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007788
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007789 ret = platform_driver_register(&tpacpi_pdriver);
7790 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007791 printk(TPACPI_ERR
7792 "unable to register main platform driver\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007793 thinkpad_acpi_module_exit();
7794 return ret;
7795 }
Henrique de Moraes Holschuhac363932007-07-27 17:04:40 -03007796 tp_features.platform_drv_registered = 1;
7797
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007798 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
7799 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007800 printk(TPACPI_ERR
7801 "unable to register hwmon platform driver\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007802 thinkpad_acpi_module_exit();
7803 return ret;
7804 }
7805 tp_features.sensors_pdrv_registered = 1;
7806
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03007807 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03007808 if (!ret) {
7809 tp_features.platform_drv_attrs_registered = 1;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007810 ret = tpacpi_create_driver_attributes(
7811 &tpacpi_hwmon_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03007812 }
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03007813 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007814 printk(TPACPI_ERR
7815 "unable to create sysfs driver attributes\n");
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03007816 thinkpad_acpi_module_exit();
7817 return ret;
7818 }
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03007819 tp_features.sensors_pdrv_attrs_registered = 1;
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03007820
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007821
7822 /* Device initialization */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007823 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007824 NULL, 0);
7825 if (IS_ERR(tpacpi_pdev)) {
7826 ret = PTR_ERR(tpacpi_pdev);
7827 tpacpi_pdev = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007828 printk(TPACPI_ERR "unable to register platform device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007829 thinkpad_acpi_module_exit();
7830 return ret;
7831 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007832 tpacpi_sensors_pdev = platform_device_register_simple(
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007833 TPACPI_HWMON_DRVR_NAME,
7834 -1, NULL, 0);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007835 if (IS_ERR(tpacpi_sensors_pdev)) {
7836 ret = PTR_ERR(tpacpi_sensors_pdev);
7837 tpacpi_sensors_pdev = NULL;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007838 printk(TPACPI_ERR
7839 "unable to register hwmon platform device\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007840 thinkpad_acpi_module_exit();
7841 return ret;
7842 }
7843 ret = device_create_file(&tpacpi_sensors_pdev->dev,
7844 &dev_attr_thinkpad_acpi_pdev_name);
7845 if (ret) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007846 printk(TPACPI_ERR
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007847 "unable to create sysfs hwmon device attributes\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007848 thinkpad_acpi_module_exit();
7849 return ret;
7850 }
7851 tp_features.sensors_pdev_attrs_registered = 1;
7852 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007853 if (IS_ERR(tpacpi_hwmon)) {
7854 ret = PTR_ERR(tpacpi_hwmon);
7855 tpacpi_hwmon = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007856 printk(TPACPI_ERR "unable to register hwmon device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007857 thinkpad_acpi_module_exit();
7858 return ret;
7859 }
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -03007860 mutex_init(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03007861 tpacpi_inputdev = input_allocate_device();
7862 if (!tpacpi_inputdev) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007863 printk(TPACPI_ERR "unable to allocate input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03007864 thinkpad_acpi_module_exit();
7865 return -ENOMEM;
7866 } else {
7867 /* Prepare input device, but don't register */
7868 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007869 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03007870 tpacpi_inputdev->id.bustype = BUS_HOST;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03007871 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
7872 thinkpad_id.vendor :
7873 PCI_VENDOR_ID_IBM;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03007874 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
7875 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
7876 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007877 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
7878 ret = ibm_init(&ibms_init[i]);
7879 if (ret >= 0 && *ibms_init[i].param)
7880 ret = ibms_init[i].data->write(ibms_init[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007881 if (ret < 0) {
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03007882 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007883 return ret;
7884 }
7885 }
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03007886 ret = input_register_device(tpacpi_inputdev);
7887 if (ret < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007888 printk(TPACPI_ERR "unable to register input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03007889 thinkpad_acpi_module_exit();
7890 return ret;
7891 } else {
7892 tp_features.input_device_registered = 1;
7893 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007894
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03007895 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007896 return 0;
7897}
7898
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03007899MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
7900
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007901/*
Henrique de Moraes Holschuh922fe092009-04-14 02:44:13 +00007902 * This will autoload the driver in almost every ThinkPad
7903 * in widespread use.
7904 *
7905 * Only _VERY_ old models, like the 240, 240x and 570 lack
7906 * the HKEY event interface.
7907 */
7908MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
7909
7910/*
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007911 * DMI matching for module autoloading
7912 *
7913 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
7914 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
7915 *
7916 * Only models listed in thinkwiki will be supported, so add yours
7917 * if it is not there yet.
7918 */
7919#define IBM_BIOS_MODULE_ALIAS(__type) \
Mathieu Chouquet-Stringerb36a50f2009-03-14 16:35:26 +01007920 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007921
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007922/* Ancient thinkpad BIOSes have to be identified by
7923 * BIOS type or model number, and there are far less
7924 * BIOS types than model numbers... */
Henrique de Moraes Holschuh922fe092009-04-14 02:44:13 +00007925IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007926
Henrique de Moraes Holschuhf68f53a2009-04-14 02:44:12 +00007927MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
7928MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007929MODULE_DESCRIPTION(TPACPI_DESC);
7930MODULE_VERSION(TPACPI_VERSION);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007931MODULE_LICENSE("GPL");
7932
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03007933module_init(thinkpad_acpi_module_init);
7934module_exit(thinkpad_acpi_module_exit);